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.

265 lines
7.0 KiB

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