本站源代码
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.

180 lines
5.5KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 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 org
  6. import (
  7. "strings"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. userSetting "code.gitea.io/gitea/routers/user/setting"
  15. )
  16. const (
  17. // tplSettingsOptions template path for render settings
  18. tplSettingsOptions base.TplName = "org/settings/options"
  19. // tplSettingsDelete template path for render delete repository
  20. tplSettingsDelete base.TplName = "org/settings/delete"
  21. // tplSettingsHooks template path for render hook settings
  22. tplSettingsHooks base.TplName = "org/settings/hooks"
  23. )
  24. // Settings render the main settings page
  25. func Settings(ctx *context.Context) {
  26. ctx.Data["Title"] = ctx.Tr("org.settings")
  27. ctx.Data["PageIsSettingsOptions"] = true
  28. ctx.Data["CurrentVisibility"] = ctx.Org.Organization.Visibility
  29. ctx.Data["RepoAdminChangeTeamAccess"] = ctx.Org.Organization.RepoAdminChangeTeamAccess
  30. ctx.HTML(200, tplSettingsOptions)
  31. }
  32. // SettingsPost response for settings change submited
  33. func SettingsPost(ctx *context.Context, form auth.UpdateOrgSettingForm) {
  34. ctx.Data["Title"] = ctx.Tr("org.settings")
  35. ctx.Data["PageIsSettingsOptions"] = true
  36. ctx.Data["CurrentVisibility"] = ctx.Org.Organization.Visibility
  37. if ctx.HasError() {
  38. ctx.HTML(200, tplSettingsOptions)
  39. return
  40. }
  41. org := ctx.Org.Organization
  42. // Check if organization name has been changed.
  43. if org.LowerName != strings.ToLower(form.Name) {
  44. isExist, err := models.IsUserExist(org.ID, form.Name)
  45. if err != nil {
  46. ctx.ServerError("IsUserExist", err)
  47. return
  48. } else if isExist {
  49. ctx.Data["OrgName"] = true
  50. ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form)
  51. return
  52. } else if err = models.ChangeUserName(org, form.Name); err != nil {
  53. if err == models.ErrUserNameIllegal {
  54. ctx.Data["OrgName"] = true
  55. ctx.RenderWithErr(ctx.Tr("form.illegal_username"), tplSettingsOptions, &form)
  56. } else {
  57. ctx.ServerError("ChangeUserName", err)
  58. }
  59. return
  60. }
  61. // reset ctx.org.OrgLink with new name
  62. ctx.Org.OrgLink = setting.AppSubURL + "/org/" + form.Name
  63. log.Trace("Organization name changed: %s -> %s", org.Name, form.Name)
  64. }
  65. // In case it's just a case change.
  66. org.Name = form.Name
  67. org.LowerName = strings.ToLower(form.Name)
  68. if ctx.User.IsAdmin {
  69. org.MaxRepoCreation = form.MaxRepoCreation
  70. }
  71. org.FullName = form.FullName
  72. org.Description = form.Description
  73. org.Website = form.Website
  74. org.Location = form.Location
  75. org.Visibility = form.Visibility
  76. org.RepoAdminChangeTeamAccess = form.RepoAdminChangeTeamAccess
  77. if err := models.UpdateUser(org); err != nil {
  78. ctx.ServerError("UpdateUser", err)
  79. return
  80. }
  81. log.Trace("Organization setting updated: %s", org.Name)
  82. ctx.Flash.Success(ctx.Tr("org.settings.update_setting_success"))
  83. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  84. }
  85. // SettingsAvatar response for change avatar on settings page
  86. func SettingsAvatar(ctx *context.Context, form auth.AvatarForm) {
  87. form.Source = auth.AvatarLocal
  88. if err := userSetting.UpdateAvatarSetting(ctx, form, ctx.Org.Organization); err != nil {
  89. ctx.Flash.Error(err.Error())
  90. } else {
  91. ctx.Flash.Success(ctx.Tr("org.settings.update_avatar_success"))
  92. }
  93. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  94. }
  95. // SettingsDeleteAvatar response for delete avatar on setings page
  96. func SettingsDeleteAvatar(ctx *context.Context) {
  97. if err := ctx.Org.Organization.DeleteAvatar(); err != nil {
  98. ctx.Flash.Error(err.Error())
  99. }
  100. ctx.Redirect(ctx.Org.OrgLink + "/settings")
  101. }
  102. // SettingsDelete response for delete repository
  103. func SettingsDelete(ctx *context.Context) {
  104. ctx.Data["Title"] = ctx.Tr("org.settings")
  105. ctx.Data["PageIsSettingsDelete"] = true
  106. org := ctx.Org.Organization
  107. if ctx.Req.Method == "POST" {
  108. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  109. if models.IsErrUserNotExist(err) {
  110. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
  111. } else {
  112. ctx.ServerError("UserSignIn", err)
  113. }
  114. return
  115. }
  116. if err := models.DeleteOrganization(org); err != nil {
  117. if models.IsErrUserOwnRepos(err) {
  118. ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
  119. ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
  120. } else {
  121. ctx.ServerError("DeleteOrganization", err)
  122. }
  123. } else {
  124. log.Trace("Organization deleted: %s", org.Name)
  125. ctx.Redirect(setting.AppSubURL + "/")
  126. }
  127. return
  128. }
  129. ctx.HTML(200, tplSettingsDelete)
  130. }
  131. // Webhooks render webhook list page
  132. func Webhooks(ctx *context.Context) {
  133. ctx.Data["Title"] = ctx.Tr("org.settings")
  134. ctx.Data["PageIsSettingsHooks"] = true
  135. ctx.Data["BaseLink"] = ctx.Org.OrgLink + "/settings/hooks"
  136. ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
  137. ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
  138. if err != nil {
  139. ctx.ServerError("GetWebhooksByOrgId", err)
  140. return
  141. }
  142. ctx.Data["Webhooks"] = ws
  143. ctx.HTML(200, tplSettingsHooks)
  144. }
  145. // DeleteWebhook response for delete webhook
  146. func DeleteWebhook(ctx *context.Context) {
  147. if err := models.DeleteWebhookByOrgID(ctx.Org.Organization.ID, ctx.QueryInt64("id")); err != nil {
  148. ctx.Flash.Error("DeleteWebhookByOrgID: " + err.Error())
  149. } else {
  150. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  151. }
  152. ctx.JSON(200, map[string]interface{}{
  153. "redirect": ctx.Org.OrgLink + "/settings/hooks",
  154. })
  155. }
上海开阖软件有限公司 沪ICP备12045867号-1