org_team.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. "context"
  7. "fmt"
  8. "strings"
  9. "github.com/pkg/errors"
  10. "xorm.io/xorm"
  11. dberrors "gogs.io/gogs/internal/db/errors"
  12. )
  13. const TeamNameOwners = "Owners"
  14. // Team represents a organization team.
  15. type Team struct {
  16. ID int64 `gorm:"primaryKey"`
  17. OrgID int64 `xorm:"INDEX" gorm:"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 == TeamNameOwners
  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 dberrors.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 dberrors.New("empty team name")
  216. } else if t.OrgID == 0 {
  217. return dberrors.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 errors.New("organization does not exist")
  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. func getTeamByID(e Engine, teamID int64) (*Team, error) {
  251. t := new(Team)
  252. has, err := e.ID(teamID).Get(t)
  253. if err != nil {
  254. return nil, err
  255. } else if !has {
  256. return nil, ErrTeamNotExist{args: map[string]any{"teamID": teamID}}
  257. }
  258. return t, nil
  259. }
  260. // GetTeamByID returns team by given ID.
  261. func GetTeamByID(teamID int64) (*Team, error) {
  262. return getTeamByID(x, teamID)
  263. }
  264. // getTeamsByOrgID returns all teams belong to given organization.
  265. func getTeamsByOrgID(e Engine, orgID int64) ([]*Team, error) {
  266. teams := make([]*Team, 0, 3)
  267. return teams, e.Where("org_id = ?", orgID).Find(&teams)
  268. }
  269. // UpdateTeam updates information of team.
  270. func UpdateTeam(t *Team, authChanged bool) (err error) {
  271. if t.Name == "" {
  272. return dberrors.New("empty team name")
  273. }
  274. if len(t.Description) > 255 {
  275. t.Description = t.Description[:255]
  276. }
  277. sess := x.NewSession()
  278. defer sess.Close()
  279. if err = sess.Begin(); err != nil {
  280. return err
  281. }
  282. t.LowerName = strings.ToLower(t.Name)
  283. existingTeam := new(Team)
  284. has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(existingTeam)
  285. if err != nil {
  286. return err
  287. } else if has {
  288. return ErrTeamAlreadyExist{existingTeam.ID, t.OrgID, t.LowerName}
  289. }
  290. if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil {
  291. return fmt.Errorf("update: %v", err)
  292. }
  293. // Update access for team members if needed.
  294. if authChanged {
  295. if err = t.getRepositories(sess); err != nil {
  296. return fmt.Errorf("getRepositories:%v", err)
  297. }
  298. for _, repo := range t.Repos {
  299. if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
  300. return fmt.Errorf("recalculateTeamAccesses: %v", err)
  301. }
  302. }
  303. }
  304. return sess.Commit()
  305. }
  306. // DeleteTeam deletes given team.
  307. // It's caller's responsibility to assign organization ID.
  308. func DeleteTeam(t *Team) error {
  309. if err := t.GetRepositories(); err != nil {
  310. return err
  311. }
  312. // Get organization.
  313. org, err := Users.GetByID(context.TODO(), t.OrgID)
  314. if err != nil {
  315. return err
  316. }
  317. sess := x.NewSession()
  318. defer sess.Close()
  319. if err = sess.Begin(); err != nil {
  320. return err
  321. }
  322. // Delete all accesses.
  323. for _, repo := range t.Repos {
  324. if err = repo.recalculateTeamAccesses(sess, t.ID); err != nil {
  325. return err
  326. }
  327. }
  328. // Delete team-user.
  329. if _, err = sess.Where("org_id=?", org.ID).Where("team_id=?", t.ID).Delete(new(TeamUser)); err != nil {
  330. return err
  331. }
  332. // Delete team.
  333. if _, err = sess.ID(t.ID).Delete(new(Team)); err != nil {
  334. return err
  335. }
  336. // Update organization number of teams.
  337. if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
  338. return err
  339. }
  340. return sess.Commit()
  341. }
  342. // ___________ ____ ___
  343. // \__ ___/___ _____ _____ | | \______ ___________
  344. // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
  345. // | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
  346. // |____| \___ >____ /__|_| /______//____ >\___ >__|
  347. // \/ \/ \/ \/ \/
  348. // TeamUser represents an team-user relation.
  349. type TeamUser struct {
  350. ID int64 `gorm:"primaryKey"`
  351. OrgID int64 `xorm:"INDEX" gorm:"index"`
  352. TeamID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:team_user_unique"`
  353. UID int64 `xorm:"UNIQUE(s)" gorm:"uniqueIndex:team_user_unique"`
  354. }
  355. func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
  356. has, _ := e.Where("org_id=?", orgID).And("team_id=?", teamID).And("uid=?", uid).Get(new(TeamUser))
  357. return has
  358. }
  359. // IsTeamMember returns true if given user is a member of team.
  360. func IsTeamMember(orgID, teamID, uid int64) bool {
  361. return isTeamMember(x, orgID, teamID, uid)
  362. }
  363. func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
  364. teamUsers := make([]*TeamUser, 0, 10)
  365. if err = e.Sql("SELECT `id`, `org_id`, `team_id`, `uid` FROM `team_user` WHERE team_id = ?", teamID).
  366. Find(&teamUsers); err != nil {
  367. return nil, fmt.Errorf("get team-users: %v", err)
  368. }
  369. members := make([]*User, 0, len(teamUsers))
  370. for i := range teamUsers {
  371. member := new(User)
  372. if _, err = e.ID(teamUsers[i].UID).Get(member); err != nil {
  373. return nil, fmt.Errorf("get user '%d': %v", teamUsers[i].UID, err)
  374. }
  375. members = append(members, member)
  376. }
  377. return members, nil
  378. }
  379. // GetTeamMembers returns all members in given team of organization.
  380. func GetTeamMembers(teamID int64) ([]*User, error) {
  381. return getTeamMembers(x, teamID)
  382. }
  383. func getUserTeams(e Engine, orgID, userID int64) ([]*Team, error) {
  384. teamUsers := make([]*TeamUser, 0, 5)
  385. if err := e.Where("uid = ?", userID).And("org_id = ?", orgID).Find(&teamUsers); err != nil {
  386. return nil, err
  387. }
  388. teamIDs := make([]int64, len(teamUsers)+1)
  389. for i := range teamUsers {
  390. teamIDs[i] = teamUsers[i].TeamID
  391. }
  392. teamIDs[len(teamUsers)] = -1
  393. teams := make([]*Team, 0, len(teamIDs))
  394. return teams, e.Where("org_id = ?", orgID).In("id", teamIDs).Find(&teams)
  395. }
  396. // GetUserTeams returns all teams that user belongs to in given organization.
  397. func GetUserTeams(orgID, userID int64) ([]*Team, error) {
  398. return getUserTeams(x, orgID, userID)
  399. }
  400. // AddTeamMember adds new membership of given team to given organization,
  401. // the user will have membership to given organization automatically when needed.
  402. func AddTeamMember(orgID, teamID, userID int64) error {
  403. if IsTeamMember(orgID, teamID, userID) {
  404. return nil
  405. }
  406. if err := Organizations.AddMember(context.TODO(), orgID, userID); err != nil {
  407. return err
  408. }
  409. // Get team and its repositories.
  410. t, err := GetTeamByID(teamID)
  411. if err != nil {
  412. return err
  413. }
  414. t.NumMembers++
  415. if err = t.GetRepositories(); err != nil {
  416. return err
  417. }
  418. sess := x.NewSession()
  419. defer sess.Close()
  420. if err = sess.Begin(); err != nil {
  421. return err
  422. }
  423. tu := &TeamUser{
  424. UID: userID,
  425. OrgID: orgID,
  426. TeamID: teamID,
  427. }
  428. if _, err = sess.Insert(tu); err != nil {
  429. return err
  430. } else if _, err = sess.ID(t.ID).Update(t); err != nil {
  431. return err
  432. }
  433. // Give access to team repositories.
  434. for _, repo := range t.Repos {
  435. if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
  436. return err
  437. }
  438. }
  439. // We make sure it exists before.
  440. ou := new(OrgUser)
  441. if _, err = sess.Where("uid = ?", userID).And("org_id = ?", orgID).Get(ou); err != nil {
  442. return err
  443. }
  444. ou.NumTeams++
  445. if t.IsOwnerTeam() {
  446. ou.IsOwner = true
  447. }
  448. if _, err = sess.ID(ou.ID).AllCols().Update(ou); err != nil {
  449. return err
  450. }
  451. return sess.Commit()
  452. }
  453. func removeTeamMember(e Engine, orgID, teamID, userID int64) error {
  454. if !isTeamMember(e, orgID, teamID, userID) {
  455. return nil
  456. }
  457. // Get team and its repositories.
  458. t, err := getTeamByID(e, teamID)
  459. if err != nil {
  460. return err
  461. }
  462. // Check if the user to delete is the last member in owner team.
  463. if t.IsOwnerTeam() && t.NumMembers == 1 {
  464. return ErrLastOrgOwner{args: map[string]any{"orgID": orgID, "userID": userID}}
  465. }
  466. t.NumMembers--
  467. if err = t.getRepositories(e); err != nil {
  468. return err
  469. }
  470. // Get organization.
  471. org, err := getUserByID(e, orgID)
  472. if err != nil {
  473. return err
  474. }
  475. tu := &TeamUser{
  476. UID: userID,
  477. OrgID: orgID,
  478. TeamID: teamID,
  479. }
  480. if _, err := e.Delete(tu); err != nil {
  481. return err
  482. } else if _, err = e.ID(t.ID).AllCols().Update(t); err != nil {
  483. return err
  484. }
  485. // Delete access to team repositories.
  486. for _, repo := range t.Repos {
  487. if err = repo.recalculateTeamAccesses(e, 0); err != nil {
  488. return err
  489. }
  490. }
  491. // This must exist.
  492. ou := new(OrgUser)
  493. _, err = e.Where("uid = ?", userID).And("org_id = ?", org.ID).Get(ou)
  494. if err != nil {
  495. return err
  496. }
  497. ou.NumTeams--
  498. if t.IsOwnerTeam() {
  499. ou.IsOwner = false
  500. }
  501. if _, err = e.ID(ou.ID).AllCols().Update(ou); err != nil {
  502. return err
  503. }
  504. return nil
  505. }
  506. // RemoveTeamMember removes member from given team of given organization.
  507. func RemoveTeamMember(orgID, teamID, uid int64) error {
  508. sess := x.NewSession()
  509. defer sess.Close()
  510. if err := sess.Begin(); err != nil {
  511. return err
  512. }
  513. if err := removeTeamMember(sess, orgID, teamID, uid); err != nil {
  514. return err
  515. }
  516. return sess.Commit()
  517. }
  518. // ___________ __________
  519. // \__ ___/___ _____ _____\______ \ ____ ______ ____
  520. // | |_/ __ \\__ \ / \| _// __ \\____ \ / _ \
  521. // | |\ ___/ / __ \| Y Y \ | \ ___/| |_> > <_> )
  522. // |____| \___ >____ /__|_| /____|_ /\___ > __/ \____/
  523. // \/ \/ \/ \/ \/|__|
  524. // TeamRepo represents an team-repository relation.
  525. type TeamRepo struct {
  526. ID int64
  527. OrgID int64 `xorm:"INDEX"`
  528. TeamID int64 `xorm:"UNIQUE(s)"`
  529. RepoID int64 `xorm:"UNIQUE(s)"`
  530. }
  531. // hasTeamRepo returns true if given team has access to the repository of the organization.
  532. func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool {
  533. has, _ := e.Where("org_id = ?", orgID).And("team_id = ?", teamID).And("repo_id = ?", repoID).Get(new(TeamRepo))
  534. return has
  535. }
  536. // addTeamRepo adds new repository relation to team.
  537. func addTeamRepo(e Engine, orgID, teamID, repoID int64) error {
  538. _, err := e.InsertOne(&TeamRepo{
  539. OrgID: orgID,
  540. TeamID: teamID,
  541. RepoID: repoID,
  542. })
  543. return err
  544. }
  545. // removeTeamRepo deletes repository relation to team.
  546. func removeTeamRepo(e Engine, teamID, repoID int64) error {
  547. _, err := e.Delete(&TeamRepo{
  548. TeamID: teamID,
  549. RepoID: repoID,
  550. })
  551. return err
  552. }
  553. // GetTeamsHaveAccessToRepo returns all teams in an organization that have given access level to the repository.
  554. func GetTeamsHaveAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) {
  555. teams := make([]*Team, 0, 5)
  556. return teams, x.Where("team.authorize >= ?", mode).
  557. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  558. And("team_repo.org_id = ?", orgID).
  559. And("team_repo.repo_id = ?", repoID).
  560. Find(&teams)
  561. }
  562. func (u *User) getTeams(e Engine) (err error) {
  563. u.Teams, err = getTeamsByOrgID(e, u.ID)
  564. return err
  565. }
  566. // GetTeams returns all teams that belong to organization.
  567. func (u *User) GetTeams() error {
  568. return u.getTeams(x)
  569. }
  570. // TeamsHaveAccessToRepo returns all teams that have given access level to the repository.
  571. func (u *User) TeamsHaveAccessToRepo(repoID int64, mode AccessMode) ([]*Team, error) {
  572. return GetTeamsHaveAccessToRepo(u.ID, repoID, mode)
  573. }
  574. func (u *User) getUserTeams(e Engine, userID int64, cols ...string) ([]*Team, error) {
  575. teams := make([]*Team, 0, u.NumTeams)
  576. return teams, e.Where("team_user.org_id = ?", u.ID).
  577. And("team_user.uid = ?", userID).
  578. Join("INNER", "team_user", "team_user.team_id = team.id").
  579. Cols(cols...).Find(&teams)
  580. }
  581. // GetTeams returns all teams that belong to organization,
  582. // and that the user has joined.
  583. func (u *User) GetUserTeams(userID int64) ([]*Team, error) {
  584. return u.getUserTeams(x, userID)
  585. }