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.

265 lines
7.1 KiB

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