users.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. log "unknwon.dev/clog/v2"
  9. "gogs.io/gogs/internal/conf"
  10. "gogs.io/gogs/internal/context"
  11. "gogs.io/gogs/internal/db"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/form"
  14. "gogs.io/gogs/internal/route"
  15. "gogs.io/gogs/internal/userutil"
  16. )
  17. const (
  18. USERS = "admin/user/list"
  19. USER_NEW = "admin/user/new"
  20. USER_EDIT = "admin/user/edit"
  21. )
  22. func Users(c *context.Context) {
  23. c.Data["Title"] = c.Tr("admin.users")
  24. c.Data["PageIsAdmin"] = true
  25. c.Data["PageIsAdminUsers"] = true
  26. route.RenderUserSearch(c, &route.UserSearchOptions{
  27. Type: db.UserTypeIndividual,
  28. Counter: db.CountUsers,
  29. Ranger: db.ListUsers,
  30. PageSize: conf.UI.Admin.UserPagingNum,
  31. OrderBy: "id ASC",
  32. TplName: USERS,
  33. })
  34. }
  35. func NewUser(c *context.Context) {
  36. c.Data["Title"] = c.Tr("admin.users.new_account")
  37. c.Data["PageIsAdmin"] = true
  38. c.Data["PageIsAdminUsers"] = true
  39. c.Data["login_type"] = "0-0"
  40. sources, err := db.LoginSources.List(c.Req.Context(), db.ListLoginSourceOptions{})
  41. if err != nil {
  42. c.Error(err, "list login sources")
  43. return
  44. }
  45. c.Data["Sources"] = sources
  46. c.Data["CanSendEmail"] = conf.Email.Enabled
  47. c.Success(USER_NEW)
  48. }
  49. func NewUserPost(c *context.Context, f form.AdminCrateUser) {
  50. c.Data["Title"] = c.Tr("admin.users.new_account")
  51. c.Data["PageIsAdmin"] = true
  52. c.Data["PageIsAdminUsers"] = true
  53. sources, err := db.LoginSources.List(c.Req.Context(), db.ListLoginSourceOptions{})
  54. if err != nil {
  55. c.Error(err, "list login sources")
  56. return
  57. }
  58. c.Data["Sources"] = sources
  59. c.Data["CanSendEmail"] = conf.Email.Enabled
  60. if c.HasError() {
  61. c.Success(USER_NEW)
  62. return
  63. }
  64. u := &db.User{
  65. Name: f.UserName,
  66. Email: f.Email,
  67. Password: f.Password,
  68. IsActive: true,
  69. }
  70. if len(f.LoginType) > 0 {
  71. fields := strings.Split(f.LoginType, "-")
  72. if len(fields) == 2 {
  73. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  74. u.LoginName = f.LoginName
  75. }
  76. }
  77. if err := db.CreateUser(u); 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 created by admin (%s): %s", c.User.Name, u.Name)
  94. // Send email notification.
  95. if f.SendNotify && conf.Email.Enabled {
  96. email.SendRegisterNotifyMail(c.Context, db.NewMailerUser(u))
  97. }
  98. c.Flash.Success(c.Tr("admin.users.new_success", u.Name))
  99. c.Redirect(conf.Server.Subpath + "/admin/users/" + com.ToStr(u.ID))
  100. }
  101. func prepareUserInfo(c *context.Context) *db.User {
  102. u, err := db.GetUserByID(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.GetUserByID(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. }