users.go 6.7 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. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/auth"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/context"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/setting"
  15. "github.com/gogits/gogs/routers"
  16. )
  17. const (
  18. USERS base.TplName = "admin/user/list"
  19. USER_NEW base.TplName = "admin/user/new"
  20. USER_EDIT base.TplName = "admin/user/edit"
  21. )
  22. func Users(ctx *context.Context) {
  23. ctx.Data["Title"] = ctx.Tr("admin.users")
  24. ctx.Data["PageIsAdmin"] = true
  25. ctx.Data["PageIsAdminUsers"] = true
  26. routers.RenderUserSearch(ctx, &routers.UserSearchOptions{
  27. Type: models.USER_TYPE_INDIVIDUAL,
  28. Counter: models.CountUsers,
  29. Ranger: models.Users,
  30. PageSize: setting.AdminUserPagingNum,
  31. OrderBy: "id ASC",
  32. TplName: USERS,
  33. })
  34. }
  35. func NewUser(ctx *context.Context) {
  36. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  37. ctx.Data["PageIsAdmin"] = true
  38. ctx.Data["PageIsAdminUsers"] = true
  39. ctx.Data["login_type"] = "0-0"
  40. sources, err := models.LoginSources()
  41. if err != nil {
  42. ctx.Handle(500, "LoginSources", err)
  43. return
  44. }
  45. ctx.Data["Sources"] = sources
  46. ctx.Data["CanSendEmail"] = setting.MailService != nil
  47. ctx.HTML(200, USER_NEW)
  48. }
  49. func NewUserPost(ctx *context.Context, form auth.AdminCrateUserForm) {
  50. ctx.Data["Title"] = ctx.Tr("admin.users.new_account")
  51. ctx.Data["PageIsAdmin"] = true
  52. ctx.Data["PageIsAdminUsers"] = true
  53. sources, err := models.LoginSources()
  54. if err != nil {
  55. ctx.Handle(500, "LoginSources", err)
  56. return
  57. }
  58. ctx.Data["Sources"] = sources
  59. ctx.Data["CanSendEmail"] = setting.MailService != nil
  60. if ctx.HasError() {
  61. ctx.HTML(200, USER_NEW)
  62. return
  63. }
  64. u := &models.User{
  65. Name: form.UserName,
  66. Email: form.Email,
  67. Passwd: form.Password,
  68. IsActive: true,
  69. LoginType: models.LOGIN_PLAIN,
  70. }
  71. if len(form.LoginType) > 0 {
  72. fields := strings.Split(form.LoginType, "-")
  73. if len(fields) == 2 {
  74. u.LoginType = models.LoginType(com.StrTo(fields[0]).MustInt())
  75. u.LoginSource = com.StrTo(fields[1]).MustInt64()
  76. u.LoginName = form.LoginName
  77. }
  78. }
  79. if err := models.CreateUser(u); err != nil {
  80. switch {
  81. case models.IsErrUserAlreadyExist(err):
  82. ctx.Data["Err_UserName"] = true
  83. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), USER_NEW, &form)
  84. case models.IsErrEmailAlreadyUsed(err):
  85. ctx.Data["Err_Email"] = true
  86. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &form)
  87. case models.IsErrNameReserved(err):
  88. ctx.Data["Err_UserName"] = true
  89. ctx.RenderWithErr(ctx.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &form)
  90. case models.IsErrNamePatternNotAllowed(err):
  91. ctx.Data["Err_UserName"] = true
  92. ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &form)
  93. default:
  94. ctx.Handle(500, "CreateUser", err)
  95. }
  96. return
  97. }
  98. log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name)
  99. // Send e-mail notification.
  100. if form.SendNotify && setting.MailService != nil {
  101. mailer.SendRegisterNotifyMail(ctx.Context, u)
  102. }
  103. ctx.Flash.Success(ctx.Tr("admin.users.new_success", u.Name))
  104. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + com.ToStr(u.Id))
  105. }
  106. func prepareUserInfo(ctx *context.Context) *models.User {
  107. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  108. if err != nil {
  109. ctx.Handle(500, "GetUserByID", err)
  110. return nil
  111. }
  112. ctx.Data["User"] = u
  113. if u.LoginSource > 0 {
  114. ctx.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
  115. if err != nil {
  116. ctx.Handle(500, "GetLoginSourceByID", err)
  117. return nil
  118. }
  119. } else {
  120. ctx.Data["LoginSource"] = &models.LoginSource{}
  121. }
  122. sources, err := models.LoginSources()
  123. if err != nil {
  124. ctx.Handle(500, "LoginSources", err)
  125. return nil
  126. }
  127. ctx.Data["Sources"] = sources
  128. return u
  129. }
  130. func EditUser(ctx *context.Context) {
  131. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  132. ctx.Data["PageIsAdmin"] = true
  133. ctx.Data["PageIsAdminUsers"] = true
  134. prepareUserInfo(ctx)
  135. if ctx.Written() {
  136. return
  137. }
  138. ctx.HTML(200, USER_EDIT)
  139. }
  140. func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
  141. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  142. ctx.Data["PageIsAdmin"] = true
  143. ctx.Data["PageIsAdminUsers"] = true
  144. u := prepareUserInfo(ctx)
  145. if ctx.Written() {
  146. return
  147. }
  148. if ctx.HasError() {
  149. ctx.HTML(200, USER_EDIT)
  150. return
  151. }
  152. fields := strings.Split(form.LoginType, "-")
  153. if len(fields) == 2 {
  154. loginType := models.LoginType(com.StrTo(fields[0]).MustInt())
  155. loginSource := com.StrTo(fields[1]).MustInt64()
  156. if u.LoginSource != loginSource {
  157. u.LoginSource = loginSource
  158. u.LoginType = loginType
  159. }
  160. }
  161. if len(form.Password) > 0 {
  162. u.Passwd = form.Password
  163. u.Salt = models.GetUserSalt()
  164. u.EncodePasswd()
  165. }
  166. u.LoginName = form.LoginName
  167. u.FullName = form.FullName
  168. u.Email = form.Email
  169. u.Website = form.Website
  170. u.Location = form.Location
  171. u.MaxRepoCreation = form.MaxRepoCreation
  172. u.IsActive = form.Active
  173. u.IsAdmin = form.Admin
  174. u.AllowGitHook = form.AllowGitHook
  175. u.AllowImportLocal = form.AllowImportLocal
  176. if err := models.UpdateUser(u); err != nil {
  177. if models.IsErrEmailAlreadyUsed(err) {
  178. ctx.Data["Err_Email"] = true
  179. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form)
  180. } else {
  181. ctx.Handle(500, "UpdateUser", err)
  182. }
  183. return
  184. }
  185. log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name)
  186. ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success"))
  187. ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"))
  188. }
  189. func DeleteUser(ctx *context.Context) {
  190. u, err := models.GetUserByID(ctx.ParamsInt64(":userid"))
  191. if err != nil {
  192. ctx.Handle(500, "GetUserByID", err)
  193. return
  194. }
  195. if err = models.DeleteUser(u); err != nil {
  196. switch {
  197. case models.IsErrUserOwnRepos(err):
  198. ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo"))
  199. ctx.JSON(200, map[string]interface{}{
  200. "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
  201. })
  202. case models.IsErrUserHasOrgs(err):
  203. ctx.Flash.Error(ctx.Tr("admin.users.still_has_org"))
  204. ctx.JSON(200, map[string]interface{}{
  205. "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"),
  206. })
  207. default:
  208. ctx.Handle(500, "DeleteUser", err)
  209. }
  210. return
  211. }
  212. log.Trace("Account deleted by admin (%s): %s", ctx.User.Name, u.Name)
  213. ctx.Flash.Success(ctx.Tr("admin.users.deletion_success"))
  214. ctx.JSON(200, map[string]interface{}{
  215. "redirect": setting.AppSubUrl + "/admin/users",
  216. })
  217. }