users.go 6.6 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(c *context.Context) {
  22. c.Data["Title"] = c.Tr("admin.users")
  23. c.Data["PageIsAdmin"] = true
  24. c.Data["PageIsAdminUsers"] = true
  25. routers.RenderUserSearch(c, &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(c *context.Context) {
  35. c.Data["Title"] = c.Tr("admin.users.new_account")
  36. c.Data["PageIsAdmin"] = true
  37. c.Data["PageIsAdminUsers"] = true
  38. c.Data["login_type"] = "0-0"
  39. sources, err := models.LoginSources()
  40. if err != nil {
  41. c.Handle(500, "LoginSources", err)
  42. return
  43. }
  44. c.Data["Sources"] = sources
  45. c.Data["CanSendEmail"] = setting.MailService != nil
  46. c.HTML(200, USER_NEW)
  47. }
  48. func NewUserPost(c *context.Context, f form.AdminCrateUser) {
  49. c.Data["Title"] = c.Tr("admin.users.new_account")
  50. c.Data["PageIsAdmin"] = true
  51. c.Data["PageIsAdminUsers"] = true
  52. sources, err := models.LoginSources()
  53. if err != nil {
  54. c.Handle(500, "LoginSources", err)
  55. return
  56. }
  57. c.Data["Sources"] = sources
  58. c.Data["CanSendEmail"] = setting.MailService != nil
  59. if c.HasError() {
  60. c.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. c.Data["Err_UserName"] = true
  82. c.RenderWithErr(c.Tr("form.username_been_taken"), USER_NEW, &f)
  83. case models.IsErrEmailAlreadyUsed(err):
  84. c.Data["Err_Email"] = true
  85. c.RenderWithErr(c.Tr("form.email_been_used"), USER_NEW, &f)
  86. case models.IsErrNameReserved(err):
  87. c.Data["Err_UserName"] = true
  88. c.RenderWithErr(c.Tr("user.form.name_reserved", err.(models.ErrNameReserved).Name), USER_NEW, &f)
  89. case models.IsErrNamePatternNotAllowed(err):
  90. c.Data["Err_UserName"] = true
  91. c.RenderWithErr(c.Tr("user.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), USER_NEW, &f)
  92. default:
  93. c.Handle(500, "CreateUser", err)
  94. }
  95. return
  96. }
  97. log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name)
  98. // Send email notification.
  99. if f.SendNotify && setting.MailService != nil {
  100. mailer.SendRegisterNotifyMail(c.Context, models.NewMailerUser(u))
  101. }
  102. c.Flash.Success(c.Tr("admin.users.new_success", u.Name))
  103. c.Redirect(setting.AppSubURL + "/admin/users/" + com.ToStr(u.ID))
  104. }
  105. func prepareUserInfo(c *context.Context) *models.User {
  106. u, err := models.GetUserByID(c.ParamsInt64(":userid"))
  107. if err != nil {
  108. c.Handle(500, "GetUserByID", err)
  109. return nil
  110. }
  111. c.Data["User"] = u
  112. if u.LoginSource > 0 {
  113. c.Data["LoginSource"], err = models.GetLoginSourceByID(u.LoginSource)
  114. if err != nil {
  115. c.Handle(500, "GetLoginSourceByID", err)
  116. return nil
  117. }
  118. } else {
  119. c.Data["LoginSource"] = &models.LoginSource{}
  120. }
  121. sources, err := models.LoginSources()
  122. if err != nil {
  123. c.Handle(500, "LoginSources", err)
  124. return nil
  125. }
  126. c.Data["Sources"] = sources
  127. return u
  128. }
  129. func EditUser(c *context.Context) {
  130. c.Data["Title"] = c.Tr("admin.users.edit_account")
  131. c.Data["PageIsAdmin"] = true
  132. c.Data["PageIsAdminUsers"] = true
  133. c.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
  134. prepareUserInfo(c)
  135. if c.Written() {
  136. return
  137. }
  138. c.HTML(200, USER_EDIT)
  139. }
  140. func EditUserPost(c *context.Context, f form.AdminEditUser) {
  141. c.Data["Title"] = c.Tr("admin.users.edit_account")
  142. c.Data["PageIsAdmin"] = true
  143. c.Data["PageIsAdminUsers"] = true
  144. c.Data["EnableLocalPathMigration"] = setting.Repository.EnableLocalPathMigration
  145. u := prepareUserInfo(c)
  146. if c.Written() {
  147. return
  148. }
  149. if c.HasError() {
  150. c.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. c.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. c.Data["Err_Email"] = true
  185. c.RenderWithErr(c.Tr("form.email_been_used"), USER_EDIT, &f)
  186. } else {
  187. c.Handle(500, "UpdateUser", err)
  188. }
  189. return
  190. }
  191. log.Trace("Account profile updated by admin (%s): %s", c.User.Name, u.Name)
  192. c.Flash.Success(c.Tr("admin.users.update_profile_success"))
  193. c.Redirect(setting.AppSubURL + "/admin/users/" + c.Params(":userid"))
  194. }
  195. func DeleteUser(c *context.Context) {
  196. u, err := models.GetUserByID(c.ParamsInt64(":userid"))
  197. if err != nil {
  198. c.Handle(500, "GetUserByID", err)
  199. return
  200. }
  201. if err = models.DeleteUser(u); err != nil {
  202. switch {
  203. case models.IsErrUserOwnRepos(err):
  204. c.Flash.Error(c.Tr("admin.users.still_own_repo"))
  205. c.JSON(200, map[string]interface{}{
  206. "redirect": setting.AppSubURL + "/admin/users/" + c.Params(":userid"),
  207. })
  208. case models.IsErrUserHasOrgs(err):
  209. c.Flash.Error(c.Tr("admin.users.still_has_org"))
  210. c.JSON(200, map[string]interface{}{
  211. "redirect": setting.AppSubURL + "/admin/users/" + c.Params(":userid"),
  212. })
  213. default:
  214. c.Handle(500, "DeleteUser", err)
  215. }
  216. return
  217. }
  218. log.Trace("Account deleted by admin (%s): %s", c.User.Name, u.Name)
  219. c.Flash.Success(c.Tr("admin.users.deletion_success"))
  220. c.JSON(200, map[string]interface{}{
  221. "redirect": setting.AppSubURL + "/admin/users",
  222. })
  223. }