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.

178 lines
4.6 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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. "fmt"
  7. "strconv"
  8. "strings"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. )
  16. func NewUser(ctx *middleware.Context) {
  17. ctx.Data["Title"] = "New Account"
  18. ctx.Data["PageIsUsers"] = true
  19. auths, err := models.GetAuths()
  20. if err != nil {
  21. ctx.Handle(500, "admin.user.NewUser", err)
  22. return
  23. }
  24. ctx.Data["LoginSources"] = auths
  25. ctx.HTML(200, "admin/users/new")
  26. }
  27. func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
  28. ctx.Data["Title"] = "New Account"
  29. ctx.Data["PageIsUsers"] = true
  30. if ctx.HasError() {
  31. ctx.HTML(200, "admin/users/new")
  32. return
  33. }
  34. if form.Password != form.RetypePasswd {
  35. ctx.Data["Err_Password"] = true
  36. ctx.Data["Err_RetypePasswd"] = true
  37. ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
  38. return
  39. }
  40. u := &models.User{
  41. Name: form.UserName,
  42. Email: form.Email,
  43. Passwd: form.Password,
  44. IsActive: true,
  45. LoginType: models.LT_PLAIN,
  46. }
  47. if len(form.LoginType) > 0 {
  48. fields := strings.Split(form.LoginType, "-")
  49. u.LoginType, _ = strconv.Atoi(fields[0])
  50. u.LoginSource, _ = strconv.ParseInt(fields[1], 10, 64)
  51. u.LoginName = form.LoginName
  52. fmt.Println(u.LoginType, u.LoginSource, u.LoginName)
  53. }
  54. var err error
  55. if u, err = models.RegisterUser(u); err != nil {
  56. switch err {
  57. case models.ErrUserAlreadyExist:
  58. ctx.RenderWithErr("Username has been already taken", "admin/users/new", &form)
  59. case models.ErrEmailAlreadyUsed:
  60. ctx.RenderWithErr("E-mail address has been already used", "admin/users/new", &form)
  61. case models.ErrUserNameIllegal:
  62. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), "admin/users/new", &form)
  63. default:
  64. ctx.Handle(500, "admin.user.NewUser", err)
  65. }
  66. return
  67. }
  68. log.Trace("%s User created by admin(%s): %s", ctx.Req.RequestURI,
  69. ctx.User.LowerName, strings.ToLower(form.UserName))
  70. ctx.Redirect("/admin/users")
  71. }
  72. func EditUser(ctx *middleware.Context, params martini.Params) {
  73. ctx.Data["Title"] = "Edit Account"
  74. ctx.Data["PageIsUsers"] = true
  75. uid, err := base.StrTo(params["userid"]).Int()
  76. if err != nil {
  77. ctx.Handle(404, "admin.user.EditUser", err)
  78. return
  79. }
  80. u, err := models.GetUserById(int64(uid))
  81. if err != nil {
  82. ctx.Handle(500, "admin.user.EditUser", err)
  83. return
  84. }
  85. ctx.Data["User"] = u
  86. auths, err := models.GetAuths()
  87. if err != nil {
  88. ctx.Handle(500, "admin.user.NewUser", err)
  89. return
  90. }
  91. ctx.Data["LoginSources"] = auths
  92. ctx.HTML(200, "admin/users/edit")
  93. }
  94. func EditUserPost(ctx *middleware.Context, params martini.Params, form auth.AdminEditUserForm) {
  95. ctx.Data["Title"] = "Edit Account"
  96. ctx.Data["PageIsUsers"] = true
  97. uid, err := base.StrTo(params["userid"]).Int()
  98. if err != nil {
  99. ctx.Handle(404, "admin.user.EditUser", err)
  100. return
  101. }
  102. u, err := models.GetUserById(int64(uid))
  103. if err != nil {
  104. ctx.Handle(500, "admin.user.EditUser", err)
  105. return
  106. }
  107. u.Email = form.Email
  108. u.Website = form.Website
  109. u.Location = form.Location
  110. u.Avatar = base.EncodeMd5(form.Avatar)
  111. u.AvatarEmail = form.Avatar
  112. u.IsActive = form.Active
  113. u.IsAdmin = form.Admin
  114. if err := models.UpdateUser(u); err != nil {
  115. ctx.Handle(500, "admin.user.EditUser", err)
  116. return
  117. }
  118. log.Trace("%s User profile updated by admin(%s): %s", ctx.Req.RequestURI,
  119. ctx.User.LowerName, ctx.User.LowerName)
  120. ctx.Data["User"] = u
  121. ctx.Flash.Success("Account profile has been successfully updated.")
  122. ctx.Redirect("/admin/users/" + params["userid"])
  123. }
  124. func DeleteUser(ctx *middleware.Context, params martini.Params) {
  125. ctx.Data["Title"] = "Delete Account"
  126. ctx.Data["PageIsUsers"] = true
  127. //log.Info("delete")
  128. uid, err := base.StrTo(params["userid"]).Int()
  129. if err != nil {
  130. ctx.Handle(404, "admin.user.EditUser", err)
  131. return
  132. }
  133. u, err := models.GetUserById(int64(uid))
  134. if err != nil {
  135. ctx.Handle(500, "admin.user.EditUser", err)
  136. return
  137. }
  138. if err = models.DeleteUser(u); err != nil {
  139. switch err {
  140. case models.ErrUserOwnRepos:
  141. ctx.Flash.Error("This account still has ownership of repository, owner has to delete or transfer them first.")
  142. ctx.Redirect("/admin/users/" + params["userid"])
  143. default:
  144. ctx.Handle(500, "admin.user.DeleteUser", err)
  145. }
  146. return
  147. }
  148. log.Trace("%s User deleted by admin(%s): %s", ctx.Req.RequestURI,
  149. ctx.User.LowerName, ctx.User.LowerName)
  150. ctx.Redirect("/admin/users")
  151. }