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.

232 lines
5.8 KiB

10 years ago
10 years ago
  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 org
  5. import (
  6. "github.com/go-martini/martini"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/auth"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/log"
  11. "github.com/gogits/gogs/modules/middleware"
  12. "github.com/gogits/gogs/routers/user"
  13. )
  14. const (
  15. HOME base.TplName = "org/home"
  16. NEW base.TplName = "org/new"
  17. SETTINGS base.TplName = "org/settings"
  18. )
  19. func Home(ctx *middleware.Context, params martini.Params) {
  20. ctx.Data["Title"] = "Organization " + params["org"]
  21. org, err := models.GetUserByName(params["org"])
  22. if err != nil {
  23. if err == models.ErrUserNotExist {
  24. ctx.Handle(404, "org.Home(GetUserByName)", err)
  25. } else {
  26. ctx.Handle(500, "org.Home(GetUserByName)", err)
  27. }
  28. return
  29. }
  30. ctx.Data["Org"] = org
  31. ctx.Data["Repos"], err = models.GetRepositories(org.Id,
  32. ctx.IsSigned && org.IsOrgMember(ctx.User.Id))
  33. if err != nil {
  34. ctx.Handle(500, "org.Home(GetRepositories)", err)
  35. return
  36. }
  37. if err = org.GetMembers(); err != nil {
  38. ctx.Handle(500, "org.Home(GetMembers)", err)
  39. return
  40. }
  41. ctx.Data["Members"] = org.Members
  42. if err = org.GetTeams(); err != nil {
  43. ctx.Handle(500, "org.Home(GetTeams)", err)
  44. return
  45. }
  46. ctx.Data["Teams"] = org.Teams
  47. ctx.HTML(200, HOME)
  48. }
  49. func New(ctx *middleware.Context) {
  50. ctx.Data["Title"] = "Create An Organization"
  51. ctx.HTML(200, NEW)
  52. }
  53. func NewPost(ctx *middleware.Context, form auth.CreateOrgForm) {
  54. ctx.Data["Title"] = "Create An Organization"
  55. if ctx.HasError() {
  56. ctx.HTML(200, NEW)
  57. return
  58. }
  59. org := &models.User{
  60. Name: form.OrgName,
  61. Email: form.Email,
  62. IsActive: true, // NOTE: may need to set false when require e-mail confirmation.
  63. Type: models.ORGANIZATION,
  64. }
  65. var err error
  66. if org, err = models.CreateOrganization(org, ctx.User); err != nil {
  67. switch err {
  68. case models.ErrUserAlreadyExist:
  69. ctx.Data["Err_OrgName"] = true
  70. ctx.RenderWithErr("Organization name has been already taken", NEW, &form)
  71. case models.ErrEmailAlreadyUsed:
  72. ctx.Data["Err_Email"] = true
  73. ctx.RenderWithErr("E-mail address has been already used", NEW, &form)
  74. case models.ErrUserNameIllegal:
  75. ctx.Data["Err_OrgName"] = true
  76. ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), NEW, &form)
  77. default:
  78. ctx.Handle(500, "org.NewPost(CreateUser)", err)
  79. }
  80. return
  81. }
  82. log.Trace("%s Organization created: %s", ctx.Req.RequestURI, org.Name)
  83. ctx.Redirect("/org/" + form.OrgName + "/dashboard")
  84. }
  85. func Dashboard(ctx *middleware.Context, params martini.Params) {
  86. ctx.Data["Title"] = "Dashboard"
  87. ctx.Data["PageIsUserDashboard"] = true
  88. ctx.Data["PageIsOrgDashboard"] = true
  89. org, err := models.GetUserByName(params["org"])
  90. if err != nil {
  91. if err == models.ErrUserNotExist {
  92. ctx.Handle(404, "org.Dashboard(GetUserByName)", err)
  93. } else {
  94. ctx.Handle(500, "org.Dashboard(GetUserByName)", err)
  95. }
  96. return
  97. }
  98. if err := ctx.User.GetOrganizations(); err != nil {
  99. ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
  100. return
  101. }
  102. ctx.Data["Orgs"] = ctx.User.Orgs
  103. ctx.Data["ContextUser"] = org
  104. ctx.Data["MyRepos"], err = models.GetRepositories(org.Id, true)
  105. if err != nil {
  106. ctx.Handle(500, "org.Dashboard(GetRepositories)", err)
  107. return
  108. }
  109. actions, err := models.GetFeeds(org.Id, 0, false)
  110. if err != nil {
  111. ctx.Handle(500, "org.Dashboard(GetFeeds)", err)
  112. return
  113. }
  114. ctx.Data["Feeds"] = actions
  115. ctx.HTML(200, user.DASHBOARD)
  116. }
  117. func Settings(ctx *middleware.Context, params martini.Params) {
  118. ctx.Data["Title"] = "Settings"
  119. org, err := models.GetUserByName(params["org"])
  120. if err != nil {
  121. if err == models.ErrUserNotExist {
  122. ctx.Handle(404, "org.Settings(GetUserByName)", err)
  123. } else {
  124. ctx.Handle(500, "org.Settings(GetUserByName)", err)
  125. }
  126. return
  127. }
  128. ctx.Data["Org"] = org
  129. ctx.HTML(200, SETTINGS)
  130. }
  131. func SettingsPost(ctx *middleware.Context, params martini.Params, form auth.OrgSettingForm) {
  132. ctx.Data["Title"] = "Settings"
  133. org, err := models.GetUserByName(params["org"])
  134. if err != nil {
  135. if err == models.ErrUserNotExist {
  136. ctx.Handle(404, "org.SettingsPost(GetUserByName)", err)
  137. } else {
  138. ctx.Handle(500, "org.SettingsPost(GetUserByName)", err)
  139. }
  140. return
  141. }
  142. ctx.Data["Org"] = org
  143. if ctx.HasError() {
  144. ctx.HTML(200, SETTINGS)
  145. return
  146. }
  147. org.FullName = form.DisplayName
  148. org.Email = form.Email
  149. org.Description = form.Description
  150. org.Website = form.Website
  151. org.Location = form.Location
  152. if err = models.UpdateUser(org); err != nil {
  153. ctx.Handle(500, "org.SettingsPost(UpdateUser)", err)
  154. return
  155. }
  156. log.Trace("%s Organization setting updated: %s", ctx.Req.RequestURI, org.LowerName)
  157. ctx.Flash.Success("Organization profile has been successfully updated.")
  158. ctx.Redirect("/org/" + org.Name + "/settings")
  159. }
  160. func DeletePost(ctx *middleware.Context, params martini.Params) {
  161. ctx.Data["Title"] = "Settings"
  162. org, err := models.GetUserByName(params["org"])
  163. if err != nil {
  164. if err == models.ErrUserNotExist {
  165. ctx.Handle(404, "org.DeletePost(GetUserByName)", err)
  166. } else {
  167. ctx.Handle(500, "org.DeletePost(GetUserByName)", err)
  168. }
  169. return
  170. }
  171. ctx.Data["Org"] = org
  172. if !org.IsOrgOwner(ctx.User.Id) {
  173. ctx.Error(403)
  174. return
  175. }
  176. tmpUser := models.User{
  177. Passwd: ctx.Query("password"),
  178. Salt: ctx.User.Salt,
  179. }
  180. tmpUser.EncodePasswd()
  181. if tmpUser.Passwd != ctx.User.Passwd {
  182. ctx.Flash.Error("Password is not correct. Make sure you are owner of this account.")
  183. } else {
  184. if err := models.DeleteOrganization(org); err != nil {
  185. switch err {
  186. case models.ErrUserOwnRepos:
  187. ctx.Flash.Error("This organization still have ownership of repository, you have to delete or transfer them first.")
  188. default:
  189. ctx.Handle(500, "org.DeletePost(DeleteOrganization)", err)
  190. return
  191. }
  192. } else {
  193. ctx.Redirect("/")
  194. return
  195. }
  196. }
  197. ctx.Redirect("/org/" + org.Name + "/settings")
  198. }