users.go 7.0 KB

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