本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

275 lines
7.4KB

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