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

users.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. TplName: tplUsers,
  32. })
  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.Handle(500, "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.Handle(500, "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. }
  72. if len(form.LoginType) > 0 {
  73. fields := strings.Split(form.LoginType, "-")
  74. if len(fields) == 2 {
  75. u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt())
  76. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  77. u.LoginName = form.LoginName
  78. }
  79. }
  80. if err := models.CreateUser(u); err != nil {
  81. switch {
  82. case models.IsErrUserAlreadyExist(err):
  83. ctx.Data["Err_UserName"] = true
  84. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplUserNew, &form)
  85. case models.IsErrEmailAlreadyUsed(err):
  86. ctx.Data["Err_Email"] = true
  87. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserNew, &form)
  88. case models.IsErrNameReserved(err):
  89. ctx.Data["Err_UserName"] = true
  90. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), tplUserNew, &form)
  91. case models.IsErrNamePatternNotAllowed(err):
  92. ctx.Data["Err_UserName"] = true
  93. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplUserNew, &form)
  94. default:
  95. ctx.Handle(500, "CreateUser", err)
  96. }
  97. return
  98. }
  99. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  100. // Send email notification.
  101. if form.SendNotify && setting.MailService != nil {
  102. models.SendRegisterNotifyMail(ctx.Context, u)
  103. }
  104. ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
  105. ctx.Redirect(setting.AppSubURL + "/admin/users/" + com.ToStr(u.ID))
  106. }
  107. func prepareUserInfo(ctx *context.Context) *models.User {
  108. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  109. if err != nil {
  110. ctx.Handle(500, "GetUserByID", err)
  111. return nil
  112. }
  113. ctx.Data["User"] = u
  114. if u.LoginSource > 0 {
  115. ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
  116. if err != nil {
  117. ctx.Handle(500, "GetLoginSourceByID", err)
  118. return nil
  119. }
  120. } else {
  121. ctx.Data["LoginSource"] = &models.LoginSource{}
  122. }
  123. sources, err := models.LoginSources()
  124. if err != nil {
  125. ctx.Handle(500, "LoginSources", err)
  126. return nil
  127. }
  128. ctx.Data["Sources"] = sources
  129. return u
  130. }
  131. // EditUser show editting user page
  132. func EditUser(ctx *context.Context) {
  133. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  134. ctx.Data["PageIsAdmin"] = true
  135. ctx.Data["PageIsAdminUsers"] = true
  136. ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
  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. var err error
  168. if u.Salt, err = models.GetUserSalt(); err != nil {
  169. ctx.Handle(500, "UpdateUser", err)
  170. return
  171. }
  172. u.EncodePasswd()
  173. }
  174. u.LoginName = form.LoginName
  175. u.FullName = form.FullName
  176. u.Email = form.Email
  177. u.Website = form.Website
  178. u.Location = form.Location
  179. u.MaxRepoCreation = form.MaxRepoCreation
  180. u.IsActive = form.Active
  181. u.IsAdmin = form.Admin
  182. u.AllowGitHook = form.AllowGitHook
  183. u.AllowImportLocal = form.AllowImportLocal
  184. u.AllowCreateOrganization = form.AllowCreateOrganization
  185. u.ProhibitLogin = form.ProhibitLogin
  186. if err := models.UpdateUser(u); err != nil {
  187. if models.IsErrEmailAlreadyUsed(err) {
  188. ctx.Data["Err_Email"] = true
  189. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplUserEdit, &form)
  190. } else {
  191. ctx.Handle(500, "UpdateUser", err)
  192. }
  193. return
  194. }
  195. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  196. ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
  197. ctx.Redirect(setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"))
  198. }
  199. // DeleteUser response for deleting a user
  200. func DeleteUser(ctx *context.Context) {
  201. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  202. if err != nil {
  203. ctx.Handle(500, "GetUserByID", err)
  204. return
  205. }
  206. if err = models.DeleteUser(u); err != nil {
  207. switch {
  208. case models.IsErrUserOwnRepos(err):
  209. ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
  210. ctx.JSON(200, map[string]interface{}{
  211. "redirect": setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"),
  212. })
  213. case models.IsErrUserHasOrgs(err):
  214. ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
  215. ctx.JSON(200, map[string]interface{}{
  216. "redirect": setting.AppSubURL + "/admin/users/" + ctx.Params(":userid"),
  217. })
  218. default:
  219. ctx.Handle(500, "DeleteUser", err)
  220. }
  221. return
  222. }
  223. log.Trace("Account deleted by admin (%s): %s", ctx.User.Name, u.Name)
  224. ctx.Flash.Success(ctx.Tr("admin.users.deletion_success"))
  225. ctx.JSON(200, map[string]interface{}{
  226. "redirect": setting.AppSubURL + "/admin/users",
  227. })
  228. }