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.

228 lines
5.4 KiB

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