users.go 6.4 KB

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