You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
6.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package admin
  5. import (
  6. "math"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/middleware"
  14. "github.com/gogits/gogs/modules/setting"
  15. )
  16. const (
  17. USERS base.TplName = "admin/user/list"
  18. USER_NEW base.TplName = "admin/user/new"
  19. USER_EDIT base.TplName = "admin/user/edit"
  20. )
  21. func pagination(ctx *middleware.Context, count int64, pageNum int) int {
  22. p := ctx.QueryInt("p")
  23. if p < 1 {
  24. p = 1
  25. }
  26. curCount := int64((p-1)*pageNum + pageNum)
  27. if curCount >= count {
  28. p = int(math.Ceil(float64(count) / float64(pageNum)))
  29. } else {
  30. ctx.Data["NextPageNum"] = p + 1
  31. }
  32. if p > 1 {
  33. ctx.Data["LastPageNum"] = p - 1
  34. }
  35. return p
  36. }
  37. func Users(ctx *middleware.Context) {
  38. ctx.Data["Title"] = ctx.Tr("admin.users")
  39. ctx.Data["PageIsAdmin"] = true
  40. ctx.Data["PageIsAdminUsers"] = true
  41. pageNum := 50
  42. p := pagination(ctx, models.CountUsers(), pageNum)
  43. users, err := models.GetUsers(pageNum, (p-1)*pageNum)
  44. if err != nil {
  45. ctx.Handle(500, "GetUsers", err)
  46. return
  47. }
  48. ctx.Data["Users"] = users
  49. ctx.HTML(200, USERS)
  50. }
  51. func NewUser(ctx *middleware.Context) {
  52. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  53. ctx.Data["PageIsAdmin"] = true
  54. ctx.Data["PageIsAdminUsers"] = true
  55. auths, err := models.GetAuths()
  56. if err != nil {
  57. ctx.Handle(500, "GetAuths", err)
  58. return
  59. }
  60. ctx.Data["LoginSources"] = auths
  61. ctx.HTML(200, USER_NEW)
  62. }
  63. func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
  64. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  65. ctx.Data["PageIsAdmin"] = true
  66. ctx.Data["PageIsAdminUsers"] = true
  67. if ctx.HasError() {
  68. ctx.HTML(200, USER_NEW)
  69. return
  70. }
  71. if form.Password != form.Retype {
  72. ctx.Data["Err_Password"] = true
  73. ctx.RenderWithErr(ctx.Tr("form.password_not_match"), USER_NEW, &form)
  74. return
  75. }
  76. u := &models.User{
  77. Name: form.UserName,
  78. Email: form.Email,
  79. Passwd: form.Password,
  80. IsActive: true,
  81. LoginType: models.PLAIN,
  82. }
  83. if len(form.LoginType) > 0 {
  84. // NOTE: need rewrite.
  85. fields := strings.Split(form.LoginType, "-")
  86. tp, _ := com.StrTo(fields[0]).Int()
  87. u.LoginType = models.LoginType(tp)
  88. u.LoginSource, _ = com.StrTo(fields[1]).Int64()
  89. u.LoginName = form.LoginName
  90. }
  91. if err := models.CreateUser(u); err != nil {
  92. switch {
  93. case models.IsErrUserAlreadyExist(err):
  94. ctx.Data["Err_UserName"] = true
  95. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form)
  96. case models.IsErrEmailAlreadyUsed(err):
  97. ctx.Data["Err_Email"] = true
  98. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form)
  99. case models.IsErrNameReserved(err):
  100. ctx.Data["Err_UserName"] = true
  101. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &form)
  102. case models.IsErrNamePatternNotAllowed(err):
  103. ctx.Data["Err_UserName"] = true
  104. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &form)
  105. default:
  106. ctx.Handle(500, "CreateUser", err)
  107. }
  108. return
  109. }
  110. log.Trace("Account created by admin(%s): %s", ctx.User.Name, u.Name)
  111. ctx.Redirect(setting.AppSubUrl + "/admin/users")
  112. }
  113. func EditUser(ctx *middleware.Context) {
  114. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  115. ctx.Data["PageIsAdmin"] = true
  116. ctx.Data["PageIsAdminUsers"] = true
  117. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  118. if uid == 0 {
  119. ctx.Handle(404, "EditUser", nil)
  120. return
  121. }
  122. u, err := models.GetUserByID(uid)
  123. if err != nil {
  124. ctx.Handle(500, "GetUserByID", err)
  125. return
  126. }
  127. ctx.Data["User"] = u
  128. auths, err := models.GetAuths()
  129. if err != nil {
  130. ctx.Handle(500, "GetAuths", err)
  131. return
  132. }
  133. ctx.Data["LoginSources"] = auths
  134. ctx.HTML(200, USER_EDIT)
  135. }
  136. func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) {
  137. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  138. ctx.Data["PageIsAdmin"] = true
  139. ctx.Data["PageIsAdminUsers"] = true
  140. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  141. if uid == 0 {
  142. ctx.Handle(404, "EditUser", nil)
  143. return
  144. }
  145. u, err := models.GetUserByID(uid)
  146. if err != nil {
  147. ctx.Handle(500, "GetUserById", err)
  148. return
  149. }
  150. ctx.Data["User"] = u
  151. if ctx.HasError() {
  152. ctx.HTML(200, USER_EDIT)
  153. return
  154. }
  155. // FIXME: need password length check
  156. if len(form.Password) > 0 {
  157. u.Passwd = form.Password
  158. u.Salt = models.GetUserSalt()
  159. u.EncodePasswd()
  160. }
  161. u.FullName = form.FullName
  162. u.Email = form.Email
  163. u.Website = form.Website
  164. u.Location = form.Location
  165. if len(form.Avatar) == 0 {
  166. form.Avatar = form.Email
  167. }
  168. u.Avatar = base.EncodeMd5(form.Avatar)
  169. u.AvatarEmail = form.Avatar
  170. u.IsActive = form.Active
  171. u.IsAdmin = form.Admin
  172. u.AllowGitHook = form.AllowGitHook
  173. if err := models.UpdateUser(u); err != nil {
  174. if models.IsErrEmailAlreadyUsed(err) {
  175. ctx.Data["Err_Email"] = true
  176. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form)
  177. } else {
  178. ctx.Handle(500, "UpdateUser", err)
  179. }
  180. return
  181. }
  182. log.Trace("Account profile updated by admin(%s): %s", ctx.User.Name, u.Name)
  183. ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
  184. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  185. }
  186. func DeleteUser(ctx *middleware.Context) {
  187. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  188. if uid == 0 {
  189. ctx.Handle(404, "DeleteUser", nil)
  190. return
  191. }
  192. u, err := models.GetUserByID(uid)
  193. if err != nil {
  194. ctx.Handle(500, "GetUserByID", err)
  195. return
  196. }
  197. if err = models.DeleteUser(u); err != nil {
  198. switch {
  199. case models.IsErrUserOwnRepos(err):
  200. ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
  201. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  202. case models.IsErrUserHasOrgs(err):
  203. ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
  204. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  205. default:
  206. ctx.Handle(500, "DeleteUser", err)
  207. }
  208. return
  209. }
  210. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  211. ctx.Redirect(setting.AppSubUrl + "/admin/users")
  212. }