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 7.0 kB

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