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.

240 lines
5.9 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 err {
  93. case models.ErrUserAlreadyExist:
  94. ctx.Data["Err_UserName"] = true
  95. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form)
  96. case models.ErrEmailAlreadyUsed:
  97. ctx.Data["Err_Email"] = true
  98. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form)
  99. case models.ErrUserNameIllegal:
  100. ctx.Data["Err_UserName"] = true
  101. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), USER_NEW, &form)
  102. default:
  103. ctx.Handle(500, "CreateUser", err)
  104. }
  105. return
  106. }
  107. log.Trace("Account created by admin(%s): %s", ctx.User.Name, u.Name)
  108. ctx.Redirect(setting.AppSubUrl + "/admin/users")
  109. }
  110. func EditUser(ctx *middleware.Context) {
  111. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  112. ctx.Data["PageIsAdmin"] = true
  113. ctx.Data["PageIsAdminUsers"] = true
  114. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  115. if uid == 0 {
  116. ctx.Handle(404, "EditUser", nil)
  117. return
  118. }
  119. u, err := models.GetUserById(uid)
  120. if err != nil {
  121. ctx.Handle(500, "GetUserById", err)
  122. return
  123. }
  124. ctx.Data["User"] = u
  125. auths, err := models.GetAuths()
  126. if err != nil {
  127. ctx.Handle(500, "GetAuths", err)
  128. return
  129. }
  130. ctx.Data["LoginSources"] = auths
  131. ctx.HTML(200, USER_EDIT)
  132. }
  133. func EditUserPost(ctx *middleware.Context, form auth.AdminEditUserForm) {
  134. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  135. ctx.Data["PageIsAdmin"] = true
  136. ctx.Data["PageIsAdminUsers"] = true
  137. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  138. if uid == 0 {
  139. ctx.Handle(404, "EditUser", nil)
  140. return
  141. }
  142. u, err := models.GetUserById(uid)
  143. if err != nil {
  144. ctx.Handle(500, "GetUserById", err)
  145. return
  146. }
  147. if ctx.HasError() {
  148. ctx.HTML(200, USER_EDIT)
  149. return
  150. }
  151. // FIXME: need password length check
  152. if len(form.Passwd) > 0 {
  153. u.Passwd = form.Passwd
  154. u.Salt = models.GetUserSalt()
  155. u.EncodePasswd()
  156. }
  157. u.Email = form.Email
  158. u.Website = form.Website
  159. u.Location = form.Location
  160. if len(form.Avatar) == 0 {
  161. form.Avatar = form.Email
  162. }
  163. u.Avatar = base.EncodeMd5(form.Avatar)
  164. u.AvatarEmail = form.Avatar
  165. u.IsActive = form.Active
  166. u.IsAdmin = form.Admin
  167. u.AllowGitHook = form.AllowGitHook
  168. ctx.Data["User"] = u
  169. if err := models.UpdateUser(u); err != nil {
  170. if err == models.ErrEmailAlreadyUsed {
  171. ctx.Data["Err_Email"] = true
  172. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form)
  173. } else {
  174. ctx.Handle(500, "UpdateUser", err)
  175. }
  176. return
  177. }
  178. log.Trace("Account profile updated by admin(%s): %s", ctx.User.Name, u.Name)
  179. ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
  180. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  181. }
  182. func DeleteUser(ctx *middleware.Context) {
  183. uid := com.StrTo(ctx.Params(":userid")).MustInt64()
  184. if uid == 0 {
  185. ctx.Handle(404, "DeleteUser", nil)
  186. return
  187. }
  188. u, err := models.GetUserById(uid)
  189. if err != nil {
  190. ctx.Handle(500, "GetUserById", err)
  191. return
  192. }
  193. if err = models.DeleteUser(u); err != nil {
  194. switch err {
  195. case models.ErrUserOwnRepos:
  196. ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
  197. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  198. case models.ErrUserHasOrgs:
  199. ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
  200. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  201. default:
  202. ctx.Handle(500, "DeleteUser", err)
  203. }
  204. return
  205. }
  206. log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name)
  207. ctx.Redirect(setting.AppSubUrl + "/admin/users")
  208. }