Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

users.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Handle(500, "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.Handle(500, "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. }
  70. if len(form.LoginType) > 0 {
  71. fields := strings.Split(form.LoginType, "-")
  72. if len(fields) == 2 {
  73. u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt())
  74. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  75. u.LoginName = form.LoginName
  76. }
  77. }
  78. if err := models.CreateUser(u); err != nil {
  79. switch {
  80. case models.IsErrUserAlreadyExist(err):
  81. ctx.Data["Err_UserName"] = true
  82. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplUserNew, &form)
  83. case models.IsErrEmailAlreadyUsed(err):
  84. ctx.Data["Err_Email"] = true
  85. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserNew, &form)
  86. case models.IsErrNameReserved(err):
  87. ctx.Data["Err_UserName"] = true
  88. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplUserNew, &form)
  89. case models.IsErrNamePatternNotAllowed(err):
  90. ctx.Data["Err_UserName"] = true
  91. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form)
  92. default:
  93. ctx.Handle(500, "CreateUser", err)
  94. }
  95. return
  96. }
  97. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  98. // Send email notification.
  99. if form.SendNotify && setting.MailService != nil {
  100. models.SendRegisterNotifyMail(ctx.Context, u)
  101. }
  102. ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
  103. ctx.Redirect(setting.AppSubURL + "/admin/users/" + com.ToStr(u.ID))
  104. }
  105. func prepareUserInfo(ctx *context.Context) *models.User {
  106. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  107. if err != nil {
  108. ctx.Handle(500, "GetUserByID", err)
  109. return nil
  110. }
  111. ctx.Data["User"] = u
  112. if u.LoginSource > 0 {
  113. ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
  114. if err != nil {
  115. ctx.Handle(500, "GetLoginSourceByID", err)
  116. return nil
  117. }
  118. } else {
  119. ctx.Data["LoginSource"] = &models.LoginSource{}
  120. }
  121. sources, err := models.LoginSources()
  122. if err != nil {
  123. ctx.Handle(500, "LoginSources", err)
  124. return nil
  125. }
  126. ctx.Data["Sources"] = sources
  127. return u
  128. }
  129. // EditUser show editting user page
  130. func EditUser(ctx *context.Context) {
  131. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  132. ctx.Data["PageIsAdmin"] = true
  133. ctx.Data["PageIsAdminUsers"] = true
  134. ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
  135. prepareUserInfo(ctx)
  136. if ctx.Written() {
  137. return
  138. }
  139. ctx.HTML(200, tplUserEdit)
  140. }
  141. // EditUserPost response for editting user
  142. func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
  143. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  144. ctx.Data["PageIsAdmin"] = true
  145. ctx.Data["PageIsAdminUsers"] = true
  146. u := prepareUserInfo(ctx)
  147. if ctx.Written() {
  148. return
  149. }
  150. if ctx.HasError() {
  151. ctx.HTML(200, tplUserEdit)
  152. return
  153. }
  154. fields := strings.Split(form.LoginType, "-")
  155. if len(fields) == 2 {
  156. loginType := models.LoginType(com.StrTo(fields[0]).MustInt())
  157. loginSource := com.StrTo(fields[1]).MustInt64()
  158. if u.LoginSource != loginSource {
  159. u.LoginSource = loginSource
  160. u.LoginType = loginType
  161. }
  162. }
  163. if len(form.Password) > 0 {
  164. u.Passwd = form.Password
  165. var err error
  166. if u.Salt, err = models.GetUserSalt(); err != nil {
  167. ctx.Handle(500, "UpdateUser", err)
  168. return
  169. }
  170. u.EncodePasswd()
  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.Handle(500, "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.Handle(500, "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.Handle(500, "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. }