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.

107 lines
2.6 KiB

  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/codegangsta/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. if ctx.Req.Method == "GET" {
  17. ctx.HTML(200, "admin/users/new")
  18. return
  19. }
  20. if form.Password != form.RetypePasswd {
  21. ctx.Data["HasError"] = true
  22. ctx.Data["Err_Password"] = true
  23. ctx.Data["Err_RetypePasswd"] = true
  24. ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
  25. auth.AssignForm(form, ctx.Data)
  26. }
  27. if ctx.HasError() {
  28. ctx.HTML(200, "admin/users/new")
  29. return
  30. }
  31. u := &models.User{
  32. Name: form.UserName,
  33. Email: form.Email,
  34. Passwd: form.Password,
  35. IsActive: true,
  36. }
  37. var err error
  38. if u, err = models.RegisterUser(u); err != nil {
  39. switch err {
  40. case models.ErrUserAlreadyExist:
  41. ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form)
  42. case models.ErrEmailAlreadyUsed:
  43. ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form)
  44. case models.ErrUserNameIllegal:
  45. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form)
  46. default:
  47. ctx.Handle(200, "admin.user.NewUser", err)
  48. }
  49. return
  50. }
  51. log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI,
  52. ctx.User.LowerName, strings.ToLower(form.UserName))
  53. ctx.Redirect("/admin/users")
  54. }
  55. func EditUser(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
  56. ctx.Data["Title"] = "Edit Account"
  57. uid, err := base.StrTo(params["userid"]).Int()
  58. if err != nil {
  59. ctx.Handle(200, "admin.user.EditUser", err)
  60. return
  61. }
  62. u, err := models.GetUserById(int64(uid))
  63. if err != nil {
  64. ctx.Handle(200, "admin.user.EditUser", err)
  65. return
  66. }
  67. if ctx.Req.Method == "GET" {
  68. ctx.Data["User"] = u
  69. ctx.HTML(200, "admin/users/edit")
  70. return
  71. }
  72. u.Email = form.Email
  73. u.Website = form.Website
  74. u.Location = form.Location
  75. u.Avatar = base.EncodeMd5(form.Avatar)
  76. u.AvatarEmail = form.Avatar
  77. u.IsActive = form.Active == "on"
  78. u.IsAdmin = form.Admin == "on"
  79. if err := models.UpdateUser(u); err != nil {
  80. ctx.Handle(200, "admin.user.EditUser", err)
  81. return
  82. }
  83. ctx.Data["IsSuccess"] = true
  84. ctx.Data["User"] = u
  85. ctx.HTML(200, "admin/users/edit")
  86. log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
  87. ctx.User.LowerName, ctx.User.LowerName)
  88. }