users.go 6.3 KB

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