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.

org.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 models
  5. import (
  6. "strings"
  7. "github.com/gogits/gogs/modules/base"
  8. )
  9. // GetOwnerTeam returns owner team of organization.
  10. func (org *User) GetOwnerTeam() (*Team, error) {
  11. t := &Team{
  12. OrgId: org.Id,
  13. Name: OWNER_TEAM,
  14. }
  15. _, err := x.Get(t)
  16. return t, err
  17. }
  18. // CreateOrganization creates record of a new organization.
  19. func CreateOrganization(org, owner *User) (*User, error) {
  20. if !IsLegalName(org.Name) {
  21. return nil, ErrUserNameIllegal
  22. }
  23. isExist, err := IsUserExist(org.Name)
  24. if err != nil {
  25. return nil, err
  26. } else if isExist {
  27. return nil, ErrUserAlreadyExist
  28. }
  29. isExist, err = IsEmailUsed(org.Email)
  30. if err != nil {
  31. return nil, err
  32. } else if isExist {
  33. return nil, ErrEmailAlreadyUsed
  34. }
  35. org.LowerName = strings.ToLower(org.Name)
  36. org.FullName = org.Name
  37. org.Avatar = base.EncodeMd5(org.Email)
  38. org.AvatarEmail = org.Email
  39. // No password for organization.
  40. org.NumTeams = 1
  41. org.NumMembers = 1
  42. sess := x.NewSession()
  43. defer sess.Close()
  44. if err = sess.Begin(); err != nil {
  45. return nil, err
  46. }
  47. if _, err = sess.Insert(org); err != nil {
  48. sess.Rollback()
  49. return nil, err
  50. }
  51. // Create default owner team.
  52. t := &Team{
  53. OrgId: org.Id,
  54. Name: OWNER_TEAM,
  55. Authorize: ORG_ADMIN,
  56. NumMembers: 1,
  57. }
  58. if _, err = sess.Insert(t); err != nil {
  59. sess.Rollback()
  60. return nil, err
  61. }
  62. // Add initial creator to organization and owner team.
  63. ou := &OrgUser{
  64. Uid: owner.Id,
  65. OrgId: org.Id,
  66. IsOwner: true,
  67. NumTeam: 1,
  68. }
  69. if _, err = sess.Insert(ou); err != nil {
  70. sess.Rollback()
  71. return nil, err
  72. }
  73. tu := &TeamUser{
  74. Uid: owner.Id,
  75. OrgId: org.Id,
  76. TeamId: t.Id,
  77. }
  78. if _, err = sess.Insert(tu); err != nil {
  79. sess.Rollback()
  80. return nil, err
  81. }
  82. return org, sess.Commit()
  83. }
  84. // TODO: need some kind of mechanism to record failure.
  85. // DeleteOrganization completely and permanently deletes everything of organization.
  86. func DeleteOrganization(org *User) (err error) {
  87. if err := DeleteUser(org); err != nil {
  88. return err
  89. }
  90. sess := x.NewSession()
  91. defer sess.Close()
  92. if err = sess.Begin(); err != nil {
  93. return err
  94. }
  95. if _, err = sess.Delete(&Team{OrgId: org.Id}); err != nil {
  96. sess.Rollback()
  97. return err
  98. }
  99. if _, err = sess.Delete(&OrgUser{OrgId: org.Id}); err != nil {
  100. sess.Rollback()
  101. return err
  102. }
  103. if _, err = sess.Delete(&TeamUser{OrgId: org.Id}); err != nil {
  104. sess.Rollback()
  105. return err
  106. }
  107. return sess.Commit()
  108. }
  109. type AuthorizeType int
  110. const (
  111. ORG_READABLE AuthorizeType = iota + 1
  112. ORG_WRITABLE
  113. ORG_ADMIN
  114. )
  115. const OWNER_TEAM = "Owner"
  116. // Team represents a organization team.
  117. type Team struct {
  118. Id int64
  119. OrgId int64 `xorm:"INDEX"`
  120. Name string
  121. Description string
  122. Authorize AuthorizeType
  123. RepoIds string `xorm:"TEXT"`
  124. NumMembers int
  125. NumRepos int
  126. }
  127. // NewTeam creates a record of new team.
  128. func NewTeam(t *Team) error {
  129. _, err := x.Insert(t)
  130. return err
  131. }
  132. func UpdateTeam(t *Team) error {
  133. if len(t.Description) > 255 {
  134. t.Description = t.Description[:255]
  135. }
  136. _, err := x.Id(t.Id).AllCols().Update(t)
  137. return err
  138. }
  139. // ________ ____ ___
  140. // \_____ \_______ ____ | | \______ ___________
  141. // / | \_ __ \/ ___\| | / ___// __ \_ __ \
  142. // / | \ | \/ /_/ > | /\___ \\ ___/| | \/
  143. // \_______ /__| \___ /|______//____ >\___ >__|
  144. // \/ /_____/ \/ \/
  145. // OrgUser represents an organization-user relation.
  146. type OrgUser struct {
  147. Id int64
  148. Uid int64 `xorm:"INDEX"`
  149. OrgId int64 `xorm:"INDEX"`
  150. IsPublic bool
  151. IsOwner bool
  152. NumTeam int
  153. }
  154. // GetOrgUsersByUserId returns all organization-user relations by user ID.
  155. func GetOrgUsersByUserId(uid int64) ([]*OrgUser, error) {
  156. ous := make([]*OrgUser, 0, 10)
  157. err := x.Where("uid=?", uid).Find(&ous)
  158. return ous, err
  159. }
  160. // GetOrgUsersByOrgId returns all organization-user relations by organization ID.
  161. func GetOrgUsersByOrgId(orgId int64) ([]*OrgUser, error) {
  162. ous := make([]*OrgUser, 0, 10)
  163. err := x.Where("org_id=?", orgId).Find(&ous)
  164. return ous, err
  165. }
  166. func GetOrganizationCount(u *User) (int64, error) {
  167. return x.Where("uid=?", u.Id).Count(new(OrgUser))
  168. }
  169. // IsOrganizationOwner returns true if given user ID is in the owner team.
  170. func IsOrganizationOwner(orgId, uid int64) bool {
  171. has, _ := x.Where("is_owner=?", true).Get(&OrgUser{Uid: uid, OrgId: orgId})
  172. return has
  173. }
  174. // ___________ ____ ___
  175. // \__ ___/___ _____ _____ | | \______ ___________
  176. // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
  177. // | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
  178. // |____| \___ >____ /__|_| /______//____ >\___ >__|
  179. // \/ \/ \/ \/ \/
  180. // TeamUser represents an team-user relation.
  181. type TeamUser struct {
  182. Id int64
  183. Uid int64
  184. OrgId int64 `xorm:"INDEX"`
  185. TeamId int64
  186. }
  187. // GetTeamMembers returns all members in given team of organization.
  188. func GetTeamMembers(orgId, teamId int64) ([]*User, error) {
  189. tus := make([]*TeamUser, 0, 10)
  190. err := x.Where("org_id=?", orgId).And("team_id=?", teamId).Find(&tus)
  191. if err != nil {
  192. return nil, err
  193. }
  194. us := make([]*User, len(tus))
  195. for i, tu := range tus {
  196. us[i], err = GetUserById(tu.Uid)
  197. if err != nil {
  198. return nil, err
  199. }
  200. }
  201. return us, nil
  202. }