You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

users.go 6.8 kB

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