users.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. "strconv"
  7. "strings"
  8. log "unknwon.dev/clog/v2"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/database"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/form"
  14. "gogs.io/gogs/internal/route"
  15. )
  16. const (
  17. USERS = "admin/user/list"
  18. USER_NEW = "admin/user/new"
  19. USER_EDIT = "admin/user/edit"
  20. )
  21. func Users(c *context.Context) {
  22. c.Data["Title"] = c.Tr("admin.users")
  23. c.Data["PageIsAdmin"] = true
  24. c.Data["PageIsAdminUsers"] = true
  25. route.RenderUserSearch(c, &route.UserSearchOptions{
  26. Type: database.UserTypeIndividual,
  27. Counter: database.Handle.Users().Count,
  28. Ranger: database.Handle.Users().List,
  29. PageSize: conf.UI.Admin.UserPagingNum,
  30. OrderBy: "id ASC",
  31. TplName: USERS,
  32. })
  33. }
  34. func NewUser(c *context.Context) {
  35. c.Data["Title"] = c.Tr("admin.users.new_account")
  36. c.Data["PageIsAdmin"] = true
  37. c.Data["PageIsAdminUsers"] = true
  38. c.Data["login_type"] = "0-0"
  39. sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
  40. if err != nil {
  41. c.Error(err, "list login sources")
  42. return
  43. }
  44. c.Data["Sources"] = sources
  45. c.Data["CanSendEmail"] = conf.Email.Enabled
  46. c.Success(USER_NEW)
  47. }
  48. func NewUserPost(c *context.Context, f form.AdminCrateUser) {
  49. c.Data["Title"] = c.Tr("admin.users.new_account")
  50. c.Data["PageIsAdmin"] = true
  51. c.Data["PageIsAdminUsers"] = true
  52. sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
  53. if err != nil {
  54. c.Error(err, "list login sources")
  55. return
  56. }
  57. c.Data["Sources"] = sources
  58. c.Data["CanSendEmail"] = conf.Email.Enabled
  59. if c.HasError() {
  60. c.Success(USER_NEW)
  61. return
  62. }
  63. createUserOpts := database.CreateUserOptions{
  64. Password: f.Password,
  65. Activated: true,
  66. }
  67. if len(f.LoginType) > 0 {
  68. fields := strings.Split(f.LoginType, "-")
  69. if len(fields) == 2 {
  70. createUserOpts.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
  71. createUserOpts.LoginName = f.LoginName
  72. }
  73. }
  74. user, err := database.Handle.Users().Create(c.Req.Context(), f.UserName, f.Email, createUserOpts)
  75. if err != nil {
  76. switch {
  77. case database.IsErrUserAlreadyExist(err):
  78. c.Data["Err_UserName"] = true
  79. c.RenderWithErr(c.Tr("form.username_been_taken"), USER_NEW, &f)
  80. case database.IsErrEmailAlreadyUsed(err):
  81. c.Data["Err_Email"] = true
  82. c.RenderWithErr(c.Tr("form.email_been_used"), USER_NEW, &f)
  83. case database.IsErrNameNotAllowed(err):
  84. c.Data["Err_UserName"] = true
  85. c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(database.ErrNameNotAllowed).Value()), USER_NEW, &f)
  86. default:
  87. c.Error(err, "create user")
  88. }
  89. return
  90. }
  91. log.Trace("Account %q created by admin %q", user.Name, c.User.Name)
  92. // Send email notification.
  93. if f.SendNotify && conf.Email.Enabled {
  94. email.SendRegisterNotifyMail(c.Context, database.NewMailerUser(user))
  95. }
  96. c.Flash.Success(c.Tr("admin.users.new_success", user.Name))
  97. c.Redirect(conf.Server.Subpath + "/admin/users/" + strconv.FormatInt(user.ID, 10))
  98. }
  99. func prepareUserInfo(c *context.Context) *database.User {
  100. u, err := database.Handle.Users().GetByID(c.Req.Context(), c.ParamsInt64(":userid"))
  101. if err != nil {
  102. c.Error(err, "get user by ID")
  103. return nil
  104. }
  105. c.Data["User"] = u
  106. if u.LoginSource > 0 {
  107. c.Data["LoginSource"], err = database.Handle.LoginSources().GetByID(c.Req.Context(), u.LoginSource)
  108. if err != nil {
  109. c.Error(err, "get login source by ID")
  110. return nil
  111. }
  112. } else {
  113. c.Data["LoginSource"] = &database.LoginSource{}
  114. }
  115. sources, err := database.Handle.LoginSources().List(c.Req.Context(), database.ListLoginSourceOptions{})
  116. if err != nil {
  117. c.Error(err, "list login sources")
  118. return nil
  119. }
  120. c.Data["Sources"] = sources
  121. return u
  122. }
  123. func EditUser(c *context.Context) {
  124. c.Data["Title"] = c.Tr("admin.users.edit_account")
  125. c.Data["PageIsAdmin"] = true
  126. c.Data["PageIsAdminUsers"] = true
  127. c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
  128. prepareUserInfo(c)
  129. if c.Written() {
  130. return
  131. }
  132. c.Success(USER_EDIT)
  133. }
  134. func EditUserPost(c *context.Context, f form.AdminEditUser) {
  135. c.Data["Title"] = c.Tr("admin.users.edit_account")
  136. c.Data["PageIsAdmin"] = true
  137. c.Data["PageIsAdminUsers"] = true
  138. c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
  139. u := prepareUserInfo(c)
  140. if c.Written() {
  141. return
  142. }
  143. if c.HasError() {
  144. c.Success(USER_EDIT)
  145. return
  146. }
  147. opts := database.UpdateUserOptions{
  148. LoginName: &f.LoginName,
  149. FullName: &f.FullName,
  150. Website: &f.Website,
  151. Location: &f.Location,
  152. MaxRepoCreation: &f.MaxRepoCreation,
  153. IsActivated: &f.Active,
  154. IsAdmin: &f.Admin,
  155. AllowGitHook: &f.AllowGitHook,
  156. AllowImportLocal: &f.AllowImportLocal,
  157. ProhibitLogin: &f.ProhibitLogin,
  158. }
  159. fields := strings.Split(f.LoginType, "-")
  160. if len(fields) == 2 {
  161. loginSource, _ := strconv.ParseInt(fields[1], 10, 64)
  162. if u.LoginSource != loginSource {
  163. opts.LoginSource = &loginSource
  164. }
  165. }
  166. if f.Password != "" {
  167. opts.Password = &f.Password
  168. }
  169. if u.Email != f.Email {
  170. opts.Email = &f.Email
  171. }
  172. err := database.Handle.Users().Update(c.Req.Context(), u.ID, opts)
  173. if err != nil {
  174. if database.IsErrEmailAlreadyUsed(err) {
  175. c.Data["Err_Email"] = true
  176. c.RenderWithErr(c.Tr("form.email_been_used"), USER_EDIT, &f)
  177. } else {
  178. c.Error(err, "update user")
  179. }
  180. return
  181. }
  182. log.Trace("Account updated by admin %q: %s", c.User.Name, u.Name)
  183. c.Flash.Success(c.Tr("admin.users.update_profile_success"))
  184. c.Redirect(conf.Server.Subpath + "/admin/users/" + c.Params(":userid"))
  185. }
  186. func DeleteUser(c *context.Context) {
  187. u, err := database.Handle.Users().GetByID(c.Req.Context(), c.ParamsInt64(":userid"))
  188. if err != nil {
  189. c.Error(err, "get user by ID")
  190. return
  191. }
  192. if err = database.Handle.Users().DeleteByID(c.Req.Context(), u.ID, false); err != nil {
  193. switch {
  194. case database.IsErrUserOwnRepos(err):
  195. c.Flash.Error(c.Tr("admin.users.still_own_repo"))
  196. c.JSONSuccess(map[string]any{
  197. "redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
  198. })
  199. case database.IsErrUserHasOrgs(err):
  200. c.Flash.Error(c.Tr("admin.users.still_has_org"))
  201. c.JSONSuccess(map[string]any{
  202. "redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
  203. })
  204. default:
  205. c.Error(err, "delete user")
  206. }
  207. return
  208. }
  209. log.Trace("Account deleted by admin (%s): %s", c.User.Name, u.Name)
  210. c.Flash.Success(c.Tr("admin.users.deletion_success"))
  211. c.JSONSuccess(map[string]any{
  212. "redirect": conf.Server.Subpath + "/admin/users",
  213. })
  214. }