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.

144 lines
3.6 KiB

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/go-martini/martini"
  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/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. func NewUser(ctx *middleware.Context, form auth.RegisterForm) {
  15. ctx.Data["Title"] = "New Account"
  16. ctx.Data["PageIsUsers"] = true
  17. if ctx.Req.Method == "GET" {
  18. ctx.HTML(200, "admin/users/new")
  19. return
  20. }
  21. if form.Password != form.RetypePasswd {
  22. ctx.Data["HasError"] = true
  23. ctx.Data["Err_Password"] = true
  24. ctx.Data["Err_RetypePasswd"] = true
  25. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  26. auth.AssignForm(form, ctx.Data)
  27. }
  28. if ctx.HasError() {
  29. ctx.HTML(200, "admin/users/new")
  30. return
  31. }
  32. u := &models.User{
  33. Name: form.UserName,
  34. Email: form.Email,
  35. Passwd: form.Password,
  36. IsActive: true,
  37. }
  38. var err error
  39. if u, err = models.RegisterUser(u); err != nil {
  40. switch err {
  41. case models.ErrUserAlreadyExist:
  42. ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form)
  43. case models.ErrEmailAlreadyUsed:
  44. ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form)
  45. case models.ErrUserNameIllegal:
  46. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form)
  47. default:
  48. ctx.Handle(200, "admin.user.NewUser", err)
  49. }
  50. return
  51. }
  52. log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI,
  53. ctx.User.LowerName, strings.ToLower(form.UserName))
  54. ctx.Redirect("/admin/users")
  55. }
  56. func EditUser(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
  57. ctx.Data["Title"] = "Edit Account"
  58. ctx.Data["PageIsUsers"] = true
  59. uid, err := base.StrTo(params["userid"]).Int()
  60. if err != nil {
  61. ctx.Handle(200, "admin.user.EditUser", err)
  62. return
  63. }
  64. u, err := models.GetUserById(int64(uid))
  65. if err != nil {
  66. ctx.Handle(200, "admin.user.EditUser", err)
  67. return
  68. }
  69. if ctx.Req.Method == "GET" {
  70. ctx.Data["User"] = u
  71. ctx.HTML(200, "admin/users/edit")
  72. return
  73. }
  74. u.Email = form.Email
  75. u.Website = form.Website
  76. u.Location = form.Location
  77. u.Avatar = base.EncodeMd5(form.Avatar)
  78. u.AvatarEmail = form.Avatar
  79. u.IsActive = form.Active == "on"
  80. u.IsAdmin = form.Admin == "on"
  81. if err := models.UpdateUser(u); err != nil {
  82. ctx.Handle(200, "admin.user.EditUser", err)
  83. return
  84. }
  85. ctx.Data["IsSuccess"] = true
  86. ctx.Data["User"] = u
  87. ctx.HTML(200, "admin/users/edit")
  88. log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
  89. ctx.User.LowerName, ctx.User.LowerName)
  90. }
  91. func DeleteUser(ctx *middleware.Context, params martini.Params) {
  92. ctx.Data["Title"] = "Edit Account"
  93. ctx.Data["PageIsUsers"] = true
  94. uid, err := base.StrTo(params["userid"]).Int()
  95. if err != nil {
  96. ctx.Handle(200, "admin.user.EditUser", err)
  97. return
  98. }
  99. u, err := models.GetUserById(int64(uid))
  100. if err != nil {
  101. ctx.Handle(200, "admin.user.EditUser", err)
  102. return
  103. }
  104. if err = models.DeleteUser(u); err != nil {
  105. ctx.Data["HasError"] = true
  106. switch err {
  107. case models.ErrUserOwnRepos:
  108. ctx.Data["ErrorMsg"] = "This account still has ownership of repository, owner has to delete or transfer them first."
  109. ctx.Data["User"] = u
  110. ctx.HTML(200, "admin/users/edit")
  111. default:
  112. ctx.Handle(200, "admin.user.DeleteUser", err)
  113. }
  114. return
  115. }
  116. log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
  117. ctx.User.LowerName, ctx.User.LowerName)
  118. ctx.Redirect("/admin/users")
  119. }