users.go 6.7 KB

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