users.go 6.1 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. "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. )
  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: db.UserIndividual,
  27. Counter: db.CountUsers,
  28. Ranger: db.ListUsers,
  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 := db.LoginSources.List(db.ListLoginSourceOpts{})
  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 := db.LoginSources.List(db.ListLoginSourceOpts{})
  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. u := &db.User{
  64. Name: f.UserName,
  65. Email: f.Email,
  66. Passwd: f.Password,
  67. IsActive: true,
  68. }
  69. if len(f.LoginType) > 0 {
  70. fields := strings.Split(f.LoginType, "-")
  71. if len(fields) == 2 {
  72. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  73. u.LoginName = f.LoginName
  74. }
  75. }
  76. if err := db.CreateUser(u); err != nil {
  77. switch {
  78. case db.IsErrUserAlreadyExist(err):
  79. c.Data["Err_UserName"] = true
  80. c.RenderWithErr(c.Tr("form.username_been_taken"), USER_NEW, &f)
  81. case db.IsErrEmailAlreadyUsed(err):
  82. c.Data["Err_Email"] = true
  83. c.RenderWithErr(c.Tr("form.email_been_used"), USER_NEW, &f)
  84. case db.IsErrNameNotAllowed(err):
  85. c.Data["Err_UserName"] = true
  86. c.RenderWithErr(c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()), USER_NEW, &f)
  87. default:
  88. c.Error(err, "create user")
  89. }
  90. return
  91. }
  92. log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name)
  93. // Send email notification.
  94. if f.SendNotify && conf.Email.Enabled {
  95. email.SendRegisterNotifyMail(c.Context, db.NewMailerUser(u))
  96. }
  97. c.Flash.Success(c.Tr("admin.users.new_success", u.Name))
  98. c.Redirect(conf.Server.Subpath + "/admin/users/" + com.ToStr(u.ID))
  99. }
  100. func prepareUserInfo(c *context.Context) *db.User {
  101. u, err := db.GetUserByID(c.ParamsInt64(":userid"))
  102. if err != nil {
  103. c.Error(err, "get user by ID")
  104. return nil
  105. }
  106. c.Data["User"] = u
  107. if u.LoginSource > 0 {
  108. c.Data["LoginSource"], err = db.LoginSources.GetByID(u.LoginSource)
  109. if err != nil {
  110. c.Error(err, "get login source by ID")
  111. return nil
  112. }
  113. } else {
  114. c.Data["LoginSource"] = &db.LoginSource{}
  115. }
  116. sources, err := db.LoginSources.List(db.ListLoginSourceOpts{})
  117. if err != nil {
  118. c.Error(err, "list login sources")
  119. return nil
  120. }
  121. c.Data["Sources"] = sources
  122. return u
  123. }
  124. func EditUser(c *context.Context) {
  125. c.Data["Title"] = c.Tr("admin.users.edit_account")
  126. c.Data["PageIsAdmin"] = true
  127. c.Data["PageIsAdminUsers"] = true
  128. c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
  129. prepareUserInfo(c)
  130. if c.Written() {
  131. return
  132. }
  133. c.Success(USER_EDIT)
  134. }
  135. func EditUserPost(c *context.Context, f form.AdminEditUser) {
  136. c.Data["Title"] = c.Tr("admin.users.edit_account")
  137. c.Data["PageIsAdmin"] = true
  138. c.Data["PageIsAdminUsers"] = true
  139. c.Data["EnableLocalPathMigration"] = conf.Repository.EnableLocalPathMigration
  140. u := prepareUserInfo(c)
  141. if c.Written() {
  142. return
  143. }
  144. if c.HasError() {
  145. c.Success(USER_EDIT)
  146. return
  147. }
  148. fields := strings.Split(f.LoginType, "-")
  149. if len(fields) == 2 {
  150. loginSource := com.StrTo(fields[1]).MustInt64()
  151. if u.LoginSource != loginSource {
  152. u.LoginSource = loginSource
  153. }
  154. }
  155. if len(f.Password) > 0 {
  156. u.Passwd = f.Password
  157. var err error
  158. if u.Salt, err = db.GetUserSalt(); err != nil {
  159. c.Error(err, "get user salt")
  160. return
  161. }
  162. u.EncodePassword()
  163. }
  164. u.LoginName = f.LoginName
  165. u.FullName = f.FullName
  166. u.Email = f.Email
  167. u.Website = f.Website
  168. u.Location = f.Location
  169. u.MaxRepoCreation = f.MaxRepoCreation
  170. u.IsActive = f.Active
  171. u.IsAdmin = f.Admin
  172. u.AllowGitHook = f.AllowGitHook
  173. u.AllowImportLocal = f.AllowImportLocal
  174. u.ProhibitLogin = f.ProhibitLogin
  175. if err := db.UpdateUser(u); err != nil {
  176. if db.IsErrEmailAlreadyUsed(err) {
  177. c.Data["Err_Email"] = true
  178. c.RenderWithErr(c.Tr("form.email_been_used"), USER_EDIT, &f)
  179. } else {
  180. c.Error(err, "update user")
  181. }
  182. return
  183. }
  184. log.Trace("Account profile updated by admin (%s): %s", c.User.Name, u.Name)
  185. c.Flash.Success(c.Tr("admin.users.update_profile_success"))
  186. c.Redirect(conf.Server.Subpath + "/admin/users/" + c.Params(":userid"))
  187. }
  188. func DeleteUser(c *context.Context) {
  189. u, err := db.GetUserByID(c.ParamsInt64(":userid"))
  190. if err != nil {
  191. c.Error(err, "get user by ID")
  192. return
  193. }
  194. if err = db.DeleteUser(u); err != nil {
  195. switch {
  196. case db.IsErrUserOwnRepos(err):
  197. c.Flash.Error(c.Tr("admin.users.still_own_repo"))
  198. c.JSONSuccess(map[string]interface{}{
  199. "redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
  200. })
  201. case db.IsErrUserHasOrgs(err):
  202. c.Flash.Error(c.Tr("admin.users.still_has_org"))
  203. c.JSONSuccess(map[string]interface{}{
  204. "redirect": conf.Server.Subpath + "/admin/users/" + c.Params(":userid"),
  205. })
  206. default:
  207. c.Error(err, "delete user")
  208. }
  209. return
  210. }
  211. log.Trace("Account deleted by admin (%s): %s", c.User.Name, u.Name)
  212. c.Flash.Success(c.Tr("admin.users.deletion_success"))
  213. c.JSONSuccess(map[string]interface{}{
  214. "redirect": conf.Server.Subpath + "/admin/users",
  215. })
  216. }