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

225 satır
6.5KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package setting
  6. import (
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "strings"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "github.com/unknwon/com"
  18. "github.com/unknwon/i18n"
  19. )
  20. const (
  21. tplSettingsProfile base.TplName = "user/settings/profile"
  22. tplSettingsOrganization base.TplName = "user/settings/organization"
  23. tplSettingsRepositories base.TplName = "user/settings/repos"
  24. )
  25. // Profile render user's profile page
  26. func Profile(ctx *context.Context) {
  27. ctx.Data["Title"] = ctx.Tr("settings")
  28. ctx.Data["PageIsSettingsProfile"] = true
  29. ctx.HTML(200, tplSettingsProfile)
  30. }
  31. func handleUsernameChange(ctx *context.Context, newName string) {
  32. // Non-local users are not allowed to change their username.
  33. if len(newName) == 0 || !ctx.User.IsLocal() {
  34. return
  35. }
  36. // Check if user name has been changed
  37. if ctx.User.LowerName != strings.ToLower(newName) {
  38. if err := models.ChangeUserName(ctx.User, newName); err != nil {
  39. switch {
  40. case models.IsErrUserAlreadyExist(err):
  41. ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
  42. ctx.Redirect(setting.AppSubURL + "/user/settings")
  43. case models.IsErrEmailAlreadyUsed(err):
  44. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  45. ctx.Redirect(setting.AppSubURL + "/user/settings")
  46. case models.IsErrNameReserved(err):
  47. ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName))
  48. ctx.Redirect(setting.AppSubURL + "/user/settings")
  49. case models.IsErrNamePatternNotAllowed(err):
  50. ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName))
  51. ctx.Redirect(setting.AppSubURL + "/user/settings")
  52. default:
  53. ctx.ServerError("ChangeUserName", err)
  54. }
  55. return
  56. }
  57. log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
  58. }
  59. // In case it's just a case change
  60. ctx.User.Name = newName
  61. ctx.User.LowerName = strings.ToLower(newName)
  62. }
  63. // ProfilePost response for change user's profile
  64. func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
  65. ctx.Data["Title"] = ctx.Tr("settings")
  66. ctx.Data["PageIsSettingsProfile"] = true
  67. if ctx.HasError() {
  68. ctx.HTML(200, tplSettingsProfile)
  69. return
  70. }
  71. handleUsernameChange(ctx, form.Name)
  72. if ctx.Written() {
  73. return
  74. }
  75. ctx.User.FullName = form.FullName
  76. ctx.User.Email = form.Email
  77. ctx.User.KeepEmailPrivate = form.KeepEmailPrivate
  78. ctx.User.Website = form.Website
  79. ctx.User.Location = form.Location
  80. ctx.User.Language = form.Language
  81. ctx.User.Description = form.Description
  82. if err := models.UpdateUserSetting(ctx.User); err != nil {
  83. if _, ok := err.(models.ErrEmailAlreadyUsed); ok {
  84. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  85. ctx.Redirect(setting.AppSubURL + "/user/settings")
  86. return
  87. }
  88. ctx.ServerError("UpdateUser", err)
  89. return
  90. }
  91. // Update the language to the one we just set
  92. ctx.SetCookie("lang", ctx.User.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
  93. log.Trace("User settings updated: %s", ctx.User.Name)
  94. ctx.Flash.Success(i18n.Tr(ctx.User.Language, "settings.update_profile_success"))
  95. ctx.Redirect(setting.AppSubURL + "/user/settings")
  96. }
  97. // UpdateAvatarSetting update user's avatar
  98. // FIXME: limit size.
  99. func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *models.User) error {
  100. ctxUser.UseCustomAvatar = form.Source == auth.AvatarLocal
  101. if len(form.Gravatar) > 0 {
  102. ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
  103. ctxUser.AvatarEmail = form.Gravatar
  104. }
  105. if form.Avatar != nil && form.Avatar.Filename != "" {
  106. fr, err := form.Avatar.Open()
  107. if err != nil {
  108. return fmt.Errorf("Avatar.Open: %v", err)
  109. }
  110. defer fr.Close()
  111. if form.Avatar.Size > setting.AvatarMaxFileSize {
  112. return errors.New(ctx.Tr("settings.uploaded_avatar_is_too_big"))
  113. }
  114. data, err := ioutil.ReadAll(fr)
  115. if err != nil {
  116. return fmt.Errorf("ioutil.ReadAll: %v", err)
  117. }
  118. if !base.IsImageFile(data) {
  119. return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
  120. }
  121. if err = ctxUser.UploadAvatar(data); err != nil {
  122. return fmt.Errorf("UploadAvatar: %v", err)
  123. }
  124. } else if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
  125. // No avatar is uploaded but setting has been changed to enable,
  126. // generate a random one when needed.
  127. if err := ctxUser.GenerateRandomAvatar(); err != nil {
  128. log.Error("GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
  129. }
  130. }
  131. if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil {
  132. return fmt.Errorf("UpdateUser: %v", err)
  133. }
  134. return nil
  135. }
  136. // AvatarPost response for change user's avatar request
  137. func AvatarPost(ctx *context.Context, form auth.AvatarForm) {
  138. if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
  139. ctx.Flash.Error(err.Error())
  140. } else {
  141. ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
  142. }
  143. ctx.Redirect(setting.AppSubURL + "/user/settings")
  144. }
  145. // DeleteAvatar render delete avatar page
  146. func DeleteAvatar(ctx *context.Context) {
  147. if err := ctx.User.DeleteAvatar(); err != nil {
  148. ctx.Flash.Error(err.Error())
  149. }
  150. ctx.Redirect(setting.AppSubURL + "/user/settings")
  151. }
  152. // Organization render all the organization of the user
  153. func Organization(ctx *context.Context) {
  154. ctx.Data["Title"] = ctx.Tr("settings")
  155. ctx.Data["PageIsSettingsOrganization"] = true
  156. orgs, err := models.GetOrgsByUserID(ctx.User.ID, ctx.IsSigned)
  157. if err != nil {
  158. ctx.ServerError("GetOrgsByUserID", err)
  159. return
  160. }
  161. ctx.Data["Orgs"] = orgs
  162. ctx.HTML(200, tplSettingsOrganization)
  163. }
  164. // Repos display a list of all repositories of the user
  165. func Repos(ctx *context.Context) {
  166. ctx.Data["Title"] = ctx.Tr("settings")
  167. ctx.Data["PageIsSettingsRepos"] = true
  168. ctxUser := ctx.User
  169. var err error
  170. if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
  171. ctx.ServerError("GetRepositories", err)
  172. return
  173. }
  174. repos := ctxUser.Repos
  175. for i := range repos {
  176. if repos[i].IsFork {
  177. err := repos[i].GetBaseRepo()
  178. if err != nil {
  179. ctx.ServerError("GetBaseRepo", err)
  180. return
  181. }
  182. err = repos[i].BaseRepo.GetOwner()
  183. if err != nil {
  184. ctx.ServerError("GetOwner", err)
  185. return
  186. }
  187. }
  188. }
  189. ctx.Data["Owner"] = ctxUser
  190. ctx.Data["Repos"] = repos
  191. ctx.HTML(200, tplSettingsRepositories)
  192. }
上海开阖软件有限公司 沪ICP备12045867号-1