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

users.go 7.0KB

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