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.

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