users.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/pkg/context"
  11. "github.com/gogits/gogs/pkg/form"
  12. "github.com/gogits/gogs/pkg/mailer"
  13. "github.com/gogits/gogs/pkg/setting"
  14. "github.com/gogits/gogs/routers"
  15. )
  16. const (
  17. USERS = "admin/user/list"
  18. USER_NEW = "admin/user/new"
  19. USER_EDIT = "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.UI.Admin.UserPagingNum,
  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, f form.AdminCrateUser) {
  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: f.UserName,
  65. Email: f.Email,
  66. Passwd: f.Password,
  67. IsActive: true,
  68. LoginType: models.LOGIN_PLAIN,
  69. }
  70. if len(f.LoginType) > 0 {
  71. fields := strings.Split(f.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 = f.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, &f)
  83. case models.IsErrEmailAlreadyUsed(err):
  84. ctx.Data["Err_Email"] = true
  85. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_NEW, &f)
  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, &f)
  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, &f)
  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 f.SendNotify && setting.MailService != nil {
  100. mailer.SendRegisterNotifyMail(ctx.Context, models.NewMailerUser(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. ctx.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
  134. prepareUserInfo(ctx)
  135. if ctx.Written() {
  136. return
  137. }
  138. ctx.HTML(200, USER_EDIT)
  139. }
  140. func EditUserPost(ctx *context.Context, f form.AdminEditUser) {
  141. ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
  142. ctx.Data["PageIsAdmin"] = true
  143. ctx.Data["PageIsAdminUsers"] = true
  144. ctx.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
  145. u := prepareUserInfo(ctx)
  146. if ctx.Written() {
  147. return
  148. }
  149. if ctx.HasError() {
  150. ctx.HTML(200, USER_EDIT)
  151. return
  152. }
  153. fields := strings.Split(f.LoginType, "-")
  154. if len(fields) == 2 {
  155. loginType := models.LoginType(com.StrTo(fields[0]).MustInt())
  156. loginSource := com.StrTo(fields[1]).MustInt64()
  157. if u.LoginSource != loginSource {
  158. u.LoginSource = loginSource
  159. u.LoginType = loginType
  160. }
  161. }
  162. if len(f.Password) > 0 {
  163. u.Passwd = f.Password
  164. var err error
  165. if u.Salt, err = models.GetUserSalt(); err != nil {
  166. ctx.Handle(500, "UpdateUser", err)
  167. return
  168. }
  169. u.EncodePasswd()
  170. }
  171. u.LoginName = f.LoginName
  172. u.FullName = f.FullName
  173. u.Email = f.Email
  174. u.Website = f.Website
  175. u.Location = f.Location
  176. u.MaxRepoCreation = f.MaxRepoCreation
  177. u.IsActive = f.Active
  178. u.IsAdmin = f.Admin
  179. u.AllowGitHook = f.AllowGitHook
  180. u.AllowImportLocal = f.AllowImportLocal
  181. u.ProhibitLogin = f.ProhibitLogin
  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, &f)
  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 *context.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. }