org_team.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Copyright 2016 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 db
  5. import (
  6. "fmt"
  7. "strings"
  8. "xorm.io/xorm"
  9. "gogs.io/gogs/internal/db/errors"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. const OWNER_TEAM = "Owners"
  13. // Team represents a organization team.
  14. type Team struct {
  15. ID int64
  16. OrgID int64 `xorm:"INDEX"`
  17. LowerName string
  18. Name string
  19. Description string
  20. Authorize AccessMode
  21. Repos []*Repository `xorm:"-" json:"-"`
  22. Members []*User `xorm:"-" json:"-"`
  23. NumRepos int
  24. NumMembers int
  25. }
  26. func (t *Team) AfterSet(colName string, _ xorm.Cell) {
  27. switch colName {
  28. case "num_repos":
  29. // LEGACY [1.0]: this is backward compatibility bug fix for https://gogs.io/gogs/issues/3671
  30. if t.NumRepos < 0 {
  31. t.NumRepos = 0
  32. }
  33. }
  34. }
  35. // IsOwnerTeam returns true if team is owner team.
  36. func (t *Team) IsOwnerTeam() bool {
  37. return t.Name == OWNER_TEAM
  38. }
  39. // HasWriteAccess returns true if team has at least write level access mode.
  40. func (t *Team) HasWriteAccess() bool {
  41. return t.Authorize >= AccessModeWrite
  42. }
  43. // IsTeamMember returns true if given user is a member of team.
  44. func (t *Team) IsMember(userID int64) bool {
  45. return IsTeamMember(t.OrgID, t.ID, userID)
  46. }
  47. func (t *Team) getRepositories(e Engine) (err error) {
  48. teamRepos := make([]*TeamRepo, 0, t.NumRepos)
  49. if err = x.Where("team_id=?", t.ID).Find(&teamRepos); err != nil {
  50. return fmt.Errorf("get team-repos: %v", err)
  51. }
  52. t.Repos = make([]*Repository, 0, len(teamRepos))
  53. for i := range teamRepos {
  54. repo, err := getRepositoryByID(e, teamRepos[i].RepoID)
  55. if err != nil {
  56. return fmt.Errorf("getRepositoryById(%d): %v", teamRepos[i].RepoID, err)
  57. }
  58. t.Repos = append(t.Repos, repo)
  59. }
  60. return nil
  61. }
  62. // GetRepositories returns all repositories in team of organization.
  63. func (t *Team) GetRepositories() error {
  64. return t.getRepositories(x)
  65. }
  66. func (t *Team) getMembers(e Engine) (err error) {
  67. t.Members, err = getTeamMembers(e, t.ID)
  68. return err
  69. }
  70. // GetMembers returns all members in team of organization.
  71. func (t *Team) GetMembers() (err error) {
  72. return t.getMembers(x)
  73. }
  74. // AddMember adds new membership of the team to the organization,
  75. // the user will have membership to the organization automatically when needed.
  76. func (t *Team) AddMember(uid int64) error {
  77. return AddTeamMember(t.OrgID, t.ID, uid)
  78. }
  79. // RemoveMember removes member from team of organization.
  80. func (t *Team) RemoveMember(uid int64) error {
  81. return RemoveTeamMember(t.OrgID, t.ID, uid)
  82. }
  83. func (t *Team) hasRepository(e Engine, repoID int64) bool {
  84. return hasTeamRepo(e, t.OrgID, t.ID, repoID)
  85. }
  86. // HasRepository returns true if given repository belong to team.
  87. func (t *Team) HasRepository(repoID int64) bool {
  88. return t.hasRepository(x, repoID)
  89. }
  90. func (t *Team) addRepository(e Engine, repo *Repository) (err error) {
  91. if err = addTeamRepo(e, t.OrgID, t.ID, repo.ID); err != nil {
  92. return err
  93. }
  94. t.NumRepos++
  95. if _, err = e.ID(t.ID).AllCols().Update(t); err != nil {
  96. return fmt.Errorf("update team: %v", err)
  97. }
  98. if err = repo.recalculateTeamAccesses(e, 0); err != nil {
  99. return fmt.Errorf("recalculateAccesses: %v", err)
  100. }
  101. if err = t.getMembers(e); err != nil {
  102. return fmt.Errorf("getMembers: %v", err)
  103. }
  104. for _, u := range t.Members {
  105. if err = watchRepo(e, u.ID, repo.ID, true); err != nil {
  106. return fmt.Errorf("watchRepo: %v", err)
  107. }
  108. }
  109. return nil
  110. }
  111. // AddRepository adds new repository to team of organization.
  112. func (t *Team) AddRepository(repo *Repository) (err error) {
  113. if repo.OwnerID != t.OrgID {
  114. return errors.New("Repository does not belong to organization")
  115. } else if t.HasRepository(repo.ID) {
  116. return nil
  117. }
  118. sess := x.NewSession()
  119. defer sess.Close()
  120. if err = sess.Begin(); err != nil {
  121. return err
  122. }
  123. if err = t.addRepository(sess, repo); err != nil {
  124. return err
  125. }
  126. return sess.Commit()
  127. }
  128. func (t *Team) removeRepository(e Engine, repo *Repository, recalculate bool) (err error) {
  129. if err = removeTeamRepo(e, t.ID, repo.ID); err != nil {
  130. return err
  131. }
  132. t.NumRepos--
  133. if _, err = e.ID(t.ID).AllCols().Update(t); err != nil {
  134. return err
  135. }
  136. // Don't need to recalculate when delete a repository from organization.
  137. if recalculate {
  138. if err = repo.recalculateTeamAccesses(e, t.ID); err != nil {
  139. return err
  140. }
  141. }
  142. if err = t.getMembers(e); err != nil {
  143. return fmt.Errorf("get team members: %v", err)
  144. }
  145. // TODO: Delete me when this method is migrated to use GORM.
  146. userAccessMode := func(e Engine, userID int64, repo *Repository) (AccessMode, error) {
  147. mode := AccessModeNone
  148. // Everyone has read access to public repository
  149. if !repo.IsPrivate {
  150. mode = AccessModeRead
  151. }
  152. if userID <= 0 {
  153. return mode, nil
  154. }
  155. if userID == repo.OwnerID {
  156. return AccessModeOwner, nil
  157. }
  158. access := &Access{
  159. UserID: userID,
  160. RepoID: repo.ID,
  161. }
  162. if has, err := e.Get(access); !has || err != nil {
  163. return mode, err
  164. }
  165. return access.Mode, nil
  166. }
  167. hasAccess := func(e Engine, userID int64, repo *Repository, testMode AccessMode) (bool, error) {
  168. mode, err := userAccessMode(e, userID, repo)
  169. return mode >= testMode, err
  170. }
  171. for _, member := range t.Members {
  172. has, err := hasAccess(e, member.ID, repo, AccessModeRead)
  173. if err != nil {
  174. return err
  175. } else if has {
  176. continue
  177. }
  178. if err = watchRepo(e, member.ID, repo.ID, false); err != nil {
  179. return err
  180. }
  181. }
  182. return nil
  183. }
  184. // RemoveRepository removes repository from team of organization.
  185. func (t *Team) RemoveRepository(repoID int64) error {
  186. if !t.HasRepository(repoID) {
  187. return nil
  188. }
  189. repo, err := GetRepositoryByID(repoID)
  190. if err != nil {
  191. return err
  192. }
  193. sess := x.NewSession()
  194. defer sess.Close()
  195. if err = sess.Begin(); err != nil {
  196. return err
  197. }
  198. if err = t.removeRepository(sess, repo, true); err != nil {
  199. return err
  200. }
  201. return sess.Commit()
  202. }
  203. var reservedTeamNames = []string{"new"}
  204. // IsUsableTeamName return an error if given name is a reserved name or pattern.
  205. func IsUsableTeamName(name string) error {
  206. return isNameAllowed(reservedTeamNames, nil, name)
  207. }
  208. // NewTeam creates a record of new team.
  209. // It's caller's responsibility to assign organization ID.
  210. func NewTeam(t *Team) error {
  211. if len(t.Name) == 0 {
  212. return errors.New("empty team name")
  213. } else if t.OrgID == 0 {
  214. return errors.New("OrgID is not assigned")
  215. }
  216. if err := IsUsableTeamName(t.Name); err != nil {
  217. return err
  218. }
  219. has, err := x.Id(t.OrgID).Get(new(User))
  220. if err != nil {
  221. return err
  222. } else if !has {
  223. return ErrOrgNotExist
  224. }
  225. t.LowerName = strings.ToLower(t.Name)
  226. existingTeam := Team{}
  227. has, err = x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).Get(&existingTeam)
  228. if err != nil {
  229. return err
  230. } else if has {
  231. return ErrTeamAlreadyExist{existingTeam.ID, t.OrgID, t.LowerName}
  232. }
  233. sess := x.NewSession()
  234. defer sess.Close()
  235. if err = sess.Begin(); err != nil {
  236. return err
  237. }
  238. if _, err = sess.Insert(t); err != nil {
  239. return err
  240. }
  241. // Update organization number of teams.
  242. if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
  243. return err
  244. }
  245. return sess.Commit()
  246. }
  247. var _ errutil.NotFound = (*ErrTeamNotExist)(nil)
  248. type ErrTeamNotExist struct {
  249. args map[string]interface{}
  250. }
  251. func IsErrTeamNotExist(err error) bool {
  252. _, ok := err.(ErrTeamNotExist)
  253. return ok
  254. }
  255. func (err ErrTeamNotExist) Error() string {
  256. return fmt.Sprintf("team does not exist: %v", err.args)
  257. }
  258. func (ErrTeamNotExist) NotFound() bool {
  259. return true
  260. }
  261. func getTeamOfOrgByName(e Engine, orgID int64, name string) (*Team, error) {
  262. t := &Team{
  263. OrgID: orgID,
  264. LowerName: strings.ToLower(name),
  265. }
  266. has, err := e.Get(t)
  267. if err != nil {
  268. return nil, err
  269. } else if !has {
  270. return nil, ErrTeamNotExist{args: map[string]interface{}{"orgID": orgID, "name": name}}
  271. }
  272. return t, nil
  273. }
  274. // GetTeamOfOrgByName returns team by given team name and organization.
  275. func GetTeamOfOrgByName(orgID int64, name string) (*Team, error) {
  276. return getTeamOfOrgByName(x, orgID, name)
  277. }
  278. func getTeamByID(e Engine, teamID int64) (*Team, error) {
  279. t := new(Team)
  280. has, err := e.ID(teamID).Get(t)
  281. if err != nil {
  282. return nil, err
  283. } else if !has {
  284. return nil, ErrTeamNotExist{args: map[string]interface{}{"teamID": teamID}}
  285. }
  286. return t, nil
  287. }
  288. // GetTeamByID returns team by given ID.
  289. func GetTeamByID(teamID int64) (*Team, error) {
  290. return getTeamByID(x, teamID)
  291. }
  292. func getTeamsByOrgID(e Engine, orgID int64) ([]*Team, error) {
  293. teams := make([]*Team, 0, 3)
  294. return teams, e.Where("org_id = ?", orgID).Find(&teams)
  295. }
  296. // GetTeamsByOrgID returns all teams belong to given organization.
  297. func GetTeamsByOrgID(orgID int64) ([]*Team, error) {
  298. return getTeamsByOrgID(x, orgID)
  299. }
  300. // UpdateTeam updates information of team.
  301. func UpdateTeam(t *Team, authChanged bool) (err error) {
  302. if len(t.Name) == 0 {
  303. return errors.New("empty team name")
  304. }
  305. if len(t.Description) > 255 {
  306. t.Description = t.Description[:255]
  307. }
  308. sess := x.NewSession()
  309. defer sess.Close()
  310. if err = sess.Begin(); err != nil {
  311. return err
  312. }
  313. t.LowerName = strings.ToLower(t.Name)
  314. existingTeam := new(Team)
  315. has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(existingTeam)
  316. if err != nil {
  317. return err
  318. } else if has {
  319. return ErrTeamAlreadyExist{existingTeam.ID, t.OrgID, t.LowerName}
  320. }
  321. if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil {
  322. return fmt.Errorf("update: %v", err)
  323. }
  324. // Update access for team members if needed.
  325. if authChanged {
  326. if err = t.getRepositories(sess); err != nil {
  327. return fmt.Errorf("getRepositories:%v", err)
  328. }
  329. for _, repo := range t.Repos {
  330. if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
  331. return fmt.Errorf("recalculateTeamAccesses: %v", err)
  332. }
  333. }
  334. }
  335. return sess.Commit()
  336. }
  337. // DeleteTeam deletes given team.
  338. // It's caller's responsibility to assign organization ID.
  339. func DeleteTeam(t *Team) error {
  340. if err := t.GetRepositories(); err != nil {
  341. return err
  342. }
  343. // Get organization.
  344. org, err := GetUserByID(t.OrgID)
  345. if err != nil {
  346. return err
  347. }
  348. sess := x.NewSession()
  349. defer sess.Close()
  350. if err = sess.Begin(); err != nil {
  351. return err
  352. }
  353. // Delete all accesses.
  354. for _, repo := range t.Repos {
  355. if err = repo.recalculateTeamAccesses(sess, t.ID); err != nil {
  356. return err
  357. }
  358. }
  359. // Delete team-user.
  360. if _, err = sess.Where("org_id=?", org.ID).Where("team_id=?", t.ID).Delete(new(TeamUser)); err != nil {
  361. return err
  362. }
  363. // Delete team.
  364. if _, err = sess.ID(t.ID).Delete(new(Team)); err != nil {
  365. return err
  366. }
  367. // Update organization number of teams.
  368. if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
  369. return err
  370. }
  371. return sess.Commit()
  372. }
  373. // ___________ ____ ___
  374. // \__ ___/___ _____ _____ | | \______ ___________
  375. // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
  376. // | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
  377. // |____| \___ >____ /__|_| /______//____ >\___ >__|
  378. // \/ \/ \/ \/ \/
  379. // TeamUser represents an team-user relation.
  380. type TeamUser struct {
  381. ID int64
  382. OrgID int64 `xorm:"INDEX"`
  383. TeamID int64 `xorm:"UNIQUE(s)"`
  384. UID int64 `xorm:"UNIQUE(s)"`
  385. }
  386. func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
  387. has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("uid=?", uid).Get(new(TeamUser))
  388. return has
  389. }
  390. // IsTeamMember returns true if given user is a member of team.
  391. func IsTeamMember(orgID, teamID, uid int64) bool {
  392. return isTeamMember(x, orgID, teamID, uid)
  393. }
  394. func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
  395. teamUsers := make([]*TeamUser, 0, 10)
  396. if err = e.Sql("SELECT `id`, `org_id`, `team_id`, `uid` FROM `team_user` WHERE team_id = ?", teamID).
  397. Find(&teamUsers); err != nil {
  398. return nil, fmt.Errorf("get team-users: %v", err)
  399. }
  400. members := make([]*User, 0, len(teamUsers))
  401. for i := range teamUsers {
  402. member := new(User)
  403. if _, err = e.ID(teamUsers[i].UID).Get(member); err != nil {
  404. return nil, fmt.Errorf("get user '%d': %v", teamUsers[i].UID, err)
  405. }
  406. members = append(members, member)
  407. }
  408. return members, nil
  409. }
  410. // GetTeamMembers returns all members in given team of organization.
  411. func GetTeamMembers(teamID int64) ([]*User, error) {
  412. return getTeamMembers(x, teamID)
  413. }
  414. func getUserTeams(e Engine, orgID, userID int64) ([]*Team, error) {
  415. teamUsers := make([]*TeamUser, 0, 5)
  416. if err := e.Where("uid = ?", userID).And("org_id = ?", orgID).Find(&teamUsers); err != nil {
  417. return nil, err
  418. }
  419. teamIDs := make([]int64, len(teamUsers)+1)
  420. for i := range teamUsers {
  421. teamIDs[i] = teamUsers[i].TeamID
  422. }
  423. teamIDs[len(teamUsers)] = -1
  424. teams := make([]*Team, 0, len(teamIDs))
  425. return teams, e.Where("org_id = ?", orgID).In("id", teamIDs).Find(&teams)
  426. }
  427. // GetUserTeams returns all teams that user belongs to in given organization.
  428. func GetUserTeams(orgID, userID int64) ([]*Team, error) {
  429. return getUserTeams(x, orgID, userID)
  430. }
  431. // AddTeamMember adds new membership of given team to given organization,
  432. // the user will have membership to given organization automatically when needed.
  433. func AddTeamMember(orgID, teamID, userID int64) error {
  434. if IsTeamMember(orgID, teamID, userID) {
  435. return nil
  436. }
  437. if err := AddOrgUser(orgID, userID); err != nil {
  438. return err
  439. }
  440. // Get team and its repositories.
  441. t, err := GetTeamByID(teamID)
  442. if err != nil {
  443. return err
  444. }
  445. t.NumMembers++
  446. if err = t.GetRepositories(); err != nil {
  447. return err
  448. }
  449. sess := x.NewSession()
  450. defer sess.Close()
  451. if err = sess.Begin(); err != nil {
  452. return err
  453. }
  454. tu := &TeamUser{
  455. UID: userID,
  456. OrgID: orgID,
  457. TeamID: teamID,
  458. }
  459. if _, err = sess.Insert(tu); err != nil {
  460. return err
  461. } else if _, err = sess.ID(t.ID).Update(t); err != nil {
  462. return err
  463. }
  464. // Give access to team repositories.
  465. for _, repo := range t.Repos {
  466. if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
  467. return err
  468. }
  469. }
  470. // We make sure it exists before.
  471. ou := new(OrgUser)
  472. if _, err = sess.Where("uid = ?", userID).And("org_id = ?", orgID).Get(ou); err != nil {
  473. return err
  474. }
  475. ou.NumTeams++
  476. if t.IsOwnerTeam() {
  477. ou.IsOwner = true
  478. }
  479. if _, err = sess.ID(ou.ID).AllCols().Update(ou); err != nil {
  480. return err
  481. }
  482. return sess.Commit()
  483. }
  484. func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
  485. if !isTeamMember(e, orgID, teamID, uid) {
  486. return nil
  487. }
  488. // Get team and its repositories.
  489. t, err := getTeamByID(e, teamID)
  490. if err != nil {
  491. return err
  492. }
  493. // Check if the user to delete is the last member in owner team.
  494. if t.IsOwnerTeam() && t.NumMembers == 1 {
  495. return ErrLastOrgOwner{UID: uid}
  496. }
  497. t.NumMembers--
  498. if err = t.getRepositories(e); err != nil {
  499. return err
  500. }
  501. // Get organization.
  502. org, err := getUserByID(e, orgID)
  503. if err != nil {
  504. return err
  505. }
  506. tu := &TeamUser{
  507. UID: uid,
  508. OrgID: orgID,
  509. TeamID: teamID,
  510. }
  511. if _, err := e.Delete(tu); err != nil {
  512. return err
  513. } else if _, err = e.ID(t.ID).AllCols().Update(t); err != nil {
  514. return err
  515. }
  516. // Delete access to team repositories.
  517. for _, repo := range t.Repos {
  518. if err = repo.recalculateTeamAccesses(e, 0); err != nil {
  519. return err
  520. }
  521. }
  522. // This must exist.
  523. ou := new(OrgUser)
  524. _, err = e.Where("uid = ?", uid).And("org_id = ?", org.ID).Get(ou)
  525. if err != nil {
  526. return err
  527. }
  528. ou.NumTeams--
  529. if t.IsOwnerTeam() {
  530. ou.IsOwner = false
  531. }
  532. if _, err = e.ID(ou.ID).AllCols().Update(ou); err != nil {
  533. return err
  534. }
  535. return nil
  536. }
  537. // RemoveTeamMember removes member from given team of given organization.
  538. func RemoveTeamMember(orgID, teamID, uid int64) error {
  539. sess := x.NewSession()
  540. defer sess.Close()
  541. if err := sess.Begin(); err != nil {
  542. return err
  543. }
  544. if err := removeTeamMember(sess, orgID, teamID, uid); err != nil {
  545. return err
  546. }
  547. return sess.Commit()
  548. }
  549. // ___________ __________
  550. // \__ ___/___ _____ _____\______ \ ____ ______ ____
  551. // | |_/ __ \\__ \ / \| _// __ \\____ \ / _ \
  552. // | |\ ___/ / __ \| Y Y \ | \ ___/| |_> > <_> )
  553. // |____| \___ >____ /__|_| /____|_ /\___ > __/ \____/
  554. // \/ \/ \/ \/ \/|__|
  555. // TeamRepo represents an team-repository relation.
  556. type TeamRepo struct {
  557. ID int64
  558. OrgID int64 `xorm:"INDEX"`
  559. TeamID int64 `xorm:"UNIQUE(s)"`
  560. RepoID int64 `xorm:"UNIQUE(s)"`
  561. }
  562. func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool {
  563. has, _ := e.Where("org_id = ?", orgID).And("team_id = ?", teamID).And("repo_id = ?", repoID).Get(new(TeamRepo))
  564. return has
  565. }
  566. // HasTeamRepo returns true if given team has access to the repository of the organization.
  567. func HasTeamRepo(orgID, teamID, repoID int64) bool {
  568. return hasTeamRepo(x, orgID, teamID, repoID)
  569. }
  570. func addTeamRepo(e Engine, orgID, teamID, repoID int64) error {
  571. _, err := e.InsertOne(&TeamRepo{
  572. OrgID: orgID,
  573. TeamID: teamID,
  574. RepoID: repoID,
  575. })
  576. return err
  577. }
  578. // AddTeamRepo adds new repository relation to team.
  579. func AddTeamRepo(orgID, teamID, repoID int64) error {
  580. return addTeamRepo(x, orgID, teamID, repoID)
  581. }
  582. func removeTeamRepo(e Engine, teamID, repoID int64) error {
  583. _, err := e.Delete(&TeamRepo{
  584. TeamID: teamID,
  585. RepoID: repoID,
  586. })
  587. return err
  588. }
  589. // RemoveTeamRepo deletes repository relation to team.
  590. func RemoveTeamRepo(teamID, repoID int64) error {
  591. return removeTeamRepo(x, teamID, repoID)
  592. }
  593. // GetTeamsHaveAccessToRepo returns all teams in an organization that have given access level to the repository.
  594. func GetTeamsHaveAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) {
  595. teams := make([]*Team, 0, 5)
  596. return teams, x.Where("team.authorize >= ?", mode).
  597. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  598. And("team_repo.org_id = ?", orgID).
  599. And("team_repo.repo_id = ?", repoID).
  600. Find(&teams)
  601. }