You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.6 KiB

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