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.

262 lines
6.9 KiB

8 years ago
8 years ago
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. "strings"
  7. "github.com/Unknwon/com"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/routers"
  15. )
  16. const (
  17. tplUsers base.TplName = "admin/user/list"
  18. tplUserNew base.TplName = "admin/user/new"
  19. tplUserEdit base.TplName = "admin/user/edit"
  20. )
  21. // Users show all the users
  22. func Users(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("admin.users")
  24. ctx.Data["PageIsAdmin"] = true
  25. ctx.Data["PageIsAdminUsers"] = true
  26. routers.RenderUserSearch(ctx, &routers.UserSearchOptions{
  27. Type: models.UserTypeIndividual,
  28. Counter: models.CountUsers,
  29. Ranger: models.Users,
  30. PageSize: setting.UI.Admin.UserPagingNum,
  31. OrderBy: "id ASC",
  32. TplName: tplUsers,
  33. })
  34. }
  35. // NewUser render adding a new user page
  36. func NewUser(ctx *context.Context) {
  37. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  38. ctx.Data["PageIsAdmin"] = true
  39. ctx.Data["PageIsAdminUsers"] = true
  40. ctx.Data["login_type"] = "0-0"
  41. sources, err := models.LoginSources()
  42. if err != nil {
  43. ctx.Handle(500, "LoginSources", err)
  44. return
  45. }
  46. ctx.Data["Sources"] = sources
  47. ctx.Data["CanSendEmail"] = setting.MailService != nil
  48. ctx.HTML(200, tplUserNew)
  49. }
  50. // NewUserPost response for adding a new user
  51. func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) {
  52. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  53. ctx.Data["PageIsAdmin"] = true
  54. ctx.Data["PageIsAdminUsers"] = true
  55. sources, err := models.LoginSources()
  56. if err != nil {
  57. ctx.Handle(500, "LoginSources", err)
  58. return
  59. }
  60. ctx.Data["Sources"] = sources
  61. ctx.Data["CanSendEmail"] = setting.MailService != nil
  62. if ctx.HasError() {
  63. ctx.HTML(200, tplUserNew)
  64. return
  65. }
  66. u := &models.User{
  67. Name: form.UserName,
  68. Email: form.Email,
  69. Passwd: form.Password,
  70. IsActive: true,
  71. LoginType: models.LoginPlain,
  72. }
  73. if len(form.LoginType) > 0 {
  74. fields := strings.Split(form.LoginType, "-")
  75. if len(fields) == 2 {
  76. u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt())
  77. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  78. u.LoginName = form.LoginName
  79. }
  80. }
  81. if err := models.CreateUser(u); err != nil {
  82. switch {
  83. case models.IsErrUserAlreadyExist(err):
  84. ctx.Data["Err_UserName"] = true
  85. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplUserNew, &form)
  86. case models.IsErrEmailAlreadyUsed(err):
  87. ctx.Data["Err_Email"] = true
  88. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserNew, &form)
  89. case models.IsErrNameReserved(err):
  90. ctx.Data["Err_UserName"] = true
  91. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplUserNew, &form)
  92. case models.IsErrNamePatternNotAllowed(err):
  93. ctx.Data["Err_UserName"] = true
  94. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form)
  95. default:
  96. ctx.Handle(500, "CreateUser", err)
  97. }
  98. return
  99. }
  100. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  101. // Send email notification.
  102. if form.SendNotify && setting.MailService != nil {
  103. models.SendRegisterNotifyMail(ctx.Context, u)
  104. }
  105. ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
  106. ctx.Redirect(setting.AppSubURL + "/admin/users/" + com.ToStr(u.ID))
  107. }
  108. func prepareUserInfo(ctx *context.Context) *models.User {
  109. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  110. if err != nil {
  111. ctx.Handle(500, "GetUserByID", err)
  112. return nil
  113. }
  114. ctx.Data["User"] = u
  115. if u.LoginSource > 0 {
  116. ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
  117. if err != nil {
  118. ctx.Handle(500, "GetLoginSourceByID", err)
  119. return nil
  120. }
  121. } else {
  122. ctx.Data["LoginSource"] = &models.LoginSource{}
  123. }
  124. sources, err := models.LoginSources()
  125. if err != nil {
  126. ctx.Handle(500, "LoginSources", err)
  127. return nil
  128. }
  129. ctx.Data["Sources"] = sources
  130. return u
  131. }
  132. // EditUser show editting user page
  133. func EditUser(ctx *context.Context) {
  134. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  135. ctx.Data["PageIsAdmin"] = true
  136. ctx.Data["PageIsAdminUsers"] = true
  137. prepareUserInfo(ctx)
  138. if ctx.Written() {
  139. return
  140. }
  141. ctx.HTML(200, tplUserEdit)
  142. }
  143. // EditUserPost response for editting user
  144. func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
  145. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  146. ctx.Data["PageIsAdmin"] = true
  147. ctx.Data["PageIsAdminUsers"] = true
  148. u := prepareUserInfo(ctx)
  149. if ctx.Written() {
  150. return
  151. }
  152. if ctx.HasError() {
  153. ctx.HTML(200, tplUserEdit)
  154. return
  155. }
  156. fields := strings.Split(form.LoginType, "-")
  157. if len(fields) == 2 {
  158. loginType := models.LoginType(com.StrTo(fields[0]).MustInt())
  159. loginSource := com.StrTo(fields[1]).MustInt64()
  160. if u.LoginSource != loginSource {
  161. u.LoginSource = loginSource
  162. u.LoginType = loginType
  163. }
  164. }
  165. if len(form.Password) > 0 {
  166. u.Passwd = form.Password
  167. u.Salt = models.GetUserSalt()
  168. u.EncodePasswd()
  169. }
  170. u.LoginName = form.LoginName
  171. u.FullName = form.FullName
  172. u.Email = form.Email
  173. u.Website = form.Website
  174. u.Location = form.Location
  175. u.MaxRepoCreation = form.MaxRepoCreation
  176. u.IsActive = form.Active
  177. u.IsAdmin = form.Admin
  178. u.AllowGitHook = form.AllowGitHook
  179. u.AllowImportLocal = form.AllowImportLocal
  180. u.ProhibitLogin = form.ProhibitLogin
  181. if err := models.UpdateUser(u); err != nil {
  182. if models.IsErrEmailAlreadyUsed(err) {
  183. ctx.Data["Err_Email"] = true
  184. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form)
  185. } else {
  186. ctx.Handle(500, "UpdateUser", err)
  187. }
  188. return
  189. }
  190. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  191. ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
  192. ctx.Redirect(setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"))
  193. }
  194. // DeleteUser response for deleting a user
  195. func DeleteUser(ctx *context.Context) {
  196. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  197. if err != nil {
  198. ctx.Handle(500, "GetUserByID", err)
  199. return
  200. }
  201. if err = models.DeleteUser(u); err != nil {
  202. switch {
  203. case models.IsErrUserOwnRepos(err):
  204. ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
  205. ctx.JSON(200, map[string]interface{}{
  206. "redirect": setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"),
  207. })
  208. case models.IsErrUserHasOrgs(err):
  209. ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
  210. ctx.JSON(200, map[string]interface{}{
  211. "redirect": setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"),
  212. })
  213. default:
  214. ctx.Handle(500, "DeleteUser", err)
  215. }
  216. return
  217. }
  218. log.Trace("Account deleted by admin (%s): %s", ctx.User.Name, u.Name)
  219. ctx.Flash.Success(ctx.Tr("admin.users.deletion_success"))
  220. ctx.JSON(200, map[string]interface{}{
  221. "redirect": setting.AppSubURL + "/admin/users",
  222. })
  223. }