org_team.go 18 KB

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