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.

809 lines
19 KiB

  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Copyright 2016 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. "github.com/go-xorm/xorm"
  12. )
  13. const ownerTeamName = "Owners"
  14. // Team represents a organization team.
  15. type Team struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. OrgID int64 `xorm:"INDEX"`
  18. LowerName string
  19. Name string
  20. Description string
  21. Authorize AccessMode
  22. Repos []*Repository `xorm:"-"`
  23. Members []*User `xorm:"-"`
  24. NumRepos int
  25. NumMembers int
  26. Units []*TeamUnit `xorm:"-"`
  27. }
  28. func (t *Team) getUnits(e Engine) (err error) {
  29. if t.Units != nil {
  30. return nil
  31. }
  32. t.Units, err = getUnitsByTeamID(e, t.ID)
  33. return err
  34. }
  35. // GetUnitNames returns the team units names
  36. func (t *Team) GetUnitNames() (res []string) {
  37. for _, u := range t.Units {
  38. res = append(res, Units[u.Type].NameKey)
  39. }
  40. return
  41. }
  42. // HasWriteAccess returns true if team has at least write level access mode.
  43. func (t *Team) HasWriteAccess() bool {
  44. return t.Authorize >= AccessModeWrite
  45. }
  46. // IsOwnerTeam returns true if team is owner team.
  47. func (t *Team) IsOwnerTeam() bool {
  48. return t.Name == ownerTeamName
  49. }
  50. // IsMember returns true if given user is a member of team.
  51. func (t *Team) IsMember(userID int64) bool {
  52. isMember, err := IsTeamMember(t.OrgID, t.ID, userID)
  53. if err != nil {
  54. log.Error(4, "IsMember: %v", err)
  55. return false
  56. }
  57. return isMember
  58. }
  59. func (t *Team) getRepositories(e Engine) error {
  60. return e.Join("INNER", "team_repo", "repository.id = team_repo.repo_id").
  61. Where("team_repo.team_id=?", t.ID).Find(&t.Repos)
  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(userID int64) error {
  78. return AddTeamMember(t, userID)
  79. }
  80. // RemoveMember removes member from team of organization.
  81. func (t *Team) RemoveMember(userID int64) error {
  82. return RemoveTeamMember(t, userID)
  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. if _, err = e.Incr("num_repos").ID(t.ID).Update(new(Team)); err != nil {
  96. return fmt.Errorf("update team: %v", err)
  97. }
  98. t.NumRepos++
  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).Cols("num_repos").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. teamUsers, err := getTeamUsersByTeamID(e, t.ID)
  144. if err != nil {
  145. return fmt.Errorf("getTeamUsersByTeamID: %v", err)
  146. }
  147. for _, teamUser := range teamUsers {
  148. has, err := hasAccess(e, teamUser.UID, repo)
  149. if err != nil {
  150. return err
  151. } else if has {
  152. continue
  153. }
  154. if err = watchRepo(e, teamUser.UID, repo.ID, false); err != nil {
  155. return err
  156. }
  157. // Remove all IssueWatches a user has subscribed to in the repositories
  158. if err := removeIssueWatchersByRepoID(e, teamUser.UID, repo.ID); err != nil {
  159. return err
  160. }
  161. }
  162. return nil
  163. }
  164. // RemoveRepository removes repository from team of organization.
  165. func (t *Team) RemoveRepository(repoID int64) error {
  166. if !t.HasRepository(repoID) {
  167. return nil
  168. }
  169. repo, err := GetRepositoryByID(repoID)
  170. if err != nil {
  171. return err
  172. }
  173. sess := x.NewSession()
  174. defer sess.Close()
  175. if err = sess.Begin(); err != nil {
  176. return err
  177. }
  178. if err = t.removeRepository(sess, repo, true); err != nil {
  179. return err
  180. }
  181. return sess.Commit()
  182. }
  183. // UnitEnabled returns if the team has the given unit type enabled
  184. func (t *Team) UnitEnabled(tp UnitType) bool {
  185. return t.unitEnabled(x, tp)
  186. }
  187. func (t *Team) unitEnabled(e Engine, tp UnitType) bool {
  188. if err := t.getUnits(e); err != nil {
  189. log.Warn("Error loading repository (ID: %d) units: %s", t.ID, err.Error())
  190. }
  191. for _, unit := range t.Units {
  192. if unit.Type == tp {
  193. return true
  194. }
  195. }
  196. return false
  197. }
  198. // IsUsableTeamName tests if a name could be as team name
  199. func IsUsableTeamName(name string) error {
  200. switch name {
  201. case "new":
  202. return ErrNameReserved{name}
  203. default:
  204. return nil
  205. }
  206. }
  207. // NewTeam creates a record of new team.
  208. // It's caller's responsibility to assign organization ID.
  209. func NewTeam(t *Team) (err error) {
  210. if len(t.Name) == 0 {
  211. return errors.New("empty team name")
  212. }
  213. if err = IsUsableTeamName(t.Name); err != nil {
  214. return err
  215. }
  216. has, err := x.ID(t.OrgID).Get(new(User))
  217. if err != nil {
  218. return err
  219. } else if !has {
  220. return ErrOrgNotExist{t.OrgID, ""}
  221. }
  222. t.LowerName = strings.ToLower(t.Name)
  223. has, err = x.
  224. Where("org_id=?", t.OrgID).
  225. And("lower_name=?", t.LowerName).
  226. Get(new(Team))
  227. if err != nil {
  228. return err
  229. } else if has {
  230. return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
  231. }
  232. sess := x.NewSession()
  233. defer sess.Close()
  234. if err = sess.Begin(); err != nil {
  235. return err
  236. }
  237. if _, err = sess.Insert(t); err != nil {
  238. sess.Rollback()
  239. return err
  240. }
  241. // insert units for team
  242. if len(t.Units) > 0 {
  243. for _, unit := range t.Units {
  244. unit.TeamID = t.ID
  245. }
  246. if _, err = sess.Insert(&t.Units); err != nil {
  247. sess.Rollback()
  248. return err
  249. }
  250. }
  251. // Update organization number of teams.
  252. if _, err = sess.Exec("UPDATE `user` SET num_teams=num_teams+1 WHERE id = ?", t.OrgID); err != nil {
  253. sess.Rollback()
  254. return err
  255. }
  256. return sess.Commit()
  257. }
  258. func getTeam(e Engine, orgID int64, name string) (*Team, error) {
  259. t := &Team{
  260. OrgID: orgID,
  261. LowerName: strings.ToLower(name),
  262. }
  263. has, err := e.Get(t)
  264. if err != nil {
  265. return nil, err
  266. } else if !has {
  267. return nil, ErrTeamNotExist
  268. }
  269. return t, nil
  270. }
  271. // GetTeam returns team by given team name and organization.
  272. func GetTeam(orgID int64, name string) (*Team, error) {
  273. return getTeam(x, orgID, name)
  274. }
  275. func getTeamByID(e Engine, teamID int64) (*Team, error) {
  276. t := new(Team)
  277. has, err := e.ID(teamID).Get(t)
  278. if err != nil {
  279. return nil, err
  280. } else if !has {
  281. return nil, ErrTeamNotExist
  282. }
  283. return t, nil
  284. }
  285. // GetTeamByID returns team by given ID.
  286. func GetTeamByID(teamID int64) (*Team, error) {
  287. return getTeamByID(x, teamID)
  288. }
  289. // UpdateTeam updates information of team.
  290. func UpdateTeam(t *Team, authChanged bool) (err error) {
  291. if len(t.Name) == 0 {
  292. return errors.New("empty team name")
  293. }
  294. if len(t.Description) > 255 {
  295. t.Description = t.Description[:255]
  296. }
  297. sess := x.NewSession()
  298. defer sess.Close()
  299. if err = sess.Begin(); err != nil {
  300. return err
  301. }
  302. t.LowerName = strings.ToLower(t.Name)
  303. has, err := sess.
  304. Where("org_id=?", t.OrgID).
  305. And("lower_name=?", t.LowerName).
  306. And("id!=?", t.ID).
  307. Get(new(Team))
  308. if err != nil {
  309. return err
  310. } else if has {
  311. return ErrTeamAlreadyExist{t.OrgID, t.LowerName}
  312. }
  313. if _, err = sess.ID(t.ID).AllCols().Update(t); err != nil {
  314. return fmt.Errorf("update: %v", err)
  315. }
  316. // update units for team
  317. if len(t.Units) > 0 {
  318. for _, unit := range t.Units {
  319. unit.TeamID = t.ID
  320. }
  321. // Delete team-unit.
  322. if _, err := sess.
  323. Where("team_id=?", t.ID).
  324. Delete(new(TeamUnit)); err != nil {
  325. return err
  326. }
  327. if _, err = sess.Insert(&t.Units); err != nil {
  328. sess.Rollback()
  329. return err
  330. }
  331. }
  332. // Update access for team members if needed.
  333. if authChanged {
  334. if err = t.getRepositories(sess); err != nil {
  335. return fmt.Errorf("getRepositories: %v", err)
  336. }
  337. for _, repo := range t.Repos {
  338. if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
  339. return fmt.Errorf("recalculateTeamAccesses: %v", err)
  340. }
  341. }
  342. }
  343. return sess.Commit()
  344. }
  345. // DeleteTeam deletes given team.
  346. // It's caller's responsibility to assign organization ID.
  347. func DeleteTeam(t *Team) error {
  348. if err := t.GetRepositories(); 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. if err := t.getMembers(sess); err != nil {
  357. return err
  358. }
  359. // Delete all accesses.
  360. for _, repo := range t.Repos {
  361. if err := repo.recalculateTeamAccesses(sess, t.ID); err != nil {
  362. return err
  363. }
  364. // Remove watches from all users and now unaccessible repos
  365. for _, user := range t.Members {
  366. has, err := hasAccess(sess, user.ID, repo)
  367. if err != nil {
  368. return err
  369. } else if has {
  370. continue
  371. }
  372. if err = watchRepo(sess, user.ID, repo.ID, false); err != nil {
  373. return err
  374. }
  375. // Remove all IssueWatches a user has subscribed to in the repositories
  376. if err = removeIssueWatchersByRepoID(sess, user.ID, repo.ID); err != nil {
  377. return err
  378. }
  379. }
  380. }
  381. // Delete team-repo
  382. if _, err := sess.
  383. Where("team_id=?", t.ID).
  384. Delete(new(TeamRepo)); err != nil {
  385. return err
  386. }
  387. // Delete team-user.
  388. if _, err := sess.
  389. Where("org_id=?", t.OrgID).
  390. Where("team_id=?", t.ID).
  391. Delete(new(TeamUser)); err != nil {
  392. return err
  393. }
  394. // Delete team-unit.
  395. if _, err := sess.
  396. Where("team_id=?", t.ID).
  397. Delete(new(TeamUnit)); err != nil {
  398. return err
  399. }
  400. // Delete team.
  401. if _, err := sess.ID(t.ID).Delete(new(Team)); err != nil {
  402. return err
  403. }
  404. // Update organization number of teams.
  405. if _, err := sess.Exec("UPDATE `user` SET num_teams=num_teams-1 WHERE id=?", t.OrgID); err != nil {
  406. return err
  407. }
  408. return sess.Commit()
  409. }
  410. // ___________ ____ ___
  411. // \__ ___/___ _____ _____ | | \______ ___________
  412. // | |_/ __ \\__ \ / \| | / ___// __ \_ __ \
  413. // | |\ ___/ / __ \| Y Y \ | /\___ \\ ___/| | \/
  414. // |____| \___ >____ /__|_| /______//____ >\___ >__|
  415. // \/ \/ \/ \/ \/
  416. // TeamUser represents an team-user relation.
  417. type TeamUser struct {
  418. ID int64 `xorm:"pk autoincr"`
  419. OrgID int64 `xorm:"INDEX"`
  420. TeamID int64 `xorm:"UNIQUE(s)"`
  421. UID int64 `xorm:"UNIQUE(s)"`
  422. }
  423. func isTeamMember(e Engine, orgID, teamID, userID int64) (bool, error) {
  424. return e.
  425. Where("org_id=?", orgID).
  426. And("team_id=?", teamID).
  427. And("uid=?", userID).
  428. Table("team_user").
  429. Exist()
  430. }
  431. // IsTeamMember returns true if given user is a member of team.
  432. func IsTeamMember(orgID, teamID, userID int64) (bool, error) {
  433. return isTeamMember(x, orgID, teamID, userID)
  434. }
  435. func getTeamUsersByTeamID(e Engine, teamID int64) ([]*TeamUser, error) {
  436. teamUsers := make([]*TeamUser, 0, 10)
  437. return teamUsers, e.
  438. Where("team_id=?", teamID).
  439. Find(&teamUsers)
  440. }
  441. func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
  442. teamUsers, err := getTeamUsersByTeamID(e, teamID)
  443. if err != nil {
  444. return nil, fmt.Errorf("get team-users: %v", err)
  445. }
  446. members := make([]*User, len(teamUsers))
  447. for i, teamUser := range teamUsers {
  448. member, err := getUserByID(e, teamUser.UID)
  449. if err != nil {
  450. return nil, fmt.Errorf("get user '%d': %v", teamUser.UID, err)
  451. }
  452. members[i] = member
  453. }
  454. return members, nil
  455. }
  456. // GetTeamMembers returns all members in given team of organization.
  457. func GetTeamMembers(teamID int64) ([]*User, error) {
  458. return getTeamMembers(x, teamID)
  459. }
  460. func getUserTeams(e Engine, orgID, userID int64) (teams []*Team, err error) {
  461. return teams, e.
  462. Join("INNER", "team_user", "team_user.team_id = team.id").
  463. Where("team.org_id = ?", orgID).
  464. And("team_user.uid=?", userID).
  465. Find(&teams)
  466. }
  467. func getUserRepoTeams(e Engine, orgID, userID, repoID int64) (teams []*Team, err error) {
  468. return teams, e.
  469. Join("INNER", "team_user", "team_user.team_id = team.id").
  470. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  471. Where("team.org_id = ?", orgID).
  472. And("team_user.uid=?", userID).
  473. And("team_repo.repo_id=?", repoID).
  474. Find(&teams)
  475. }
  476. // GetUserTeams returns all teams that user belongs to in given organization.
  477. func GetUserTeams(orgID, userID int64) ([]*Team, error) {
  478. return getUserTeams(x, orgID, userID)
  479. }
  480. // AddTeamMember adds new membership of given team to given organization,
  481. // the user will have membership to given organization automatically when needed.
  482. func AddTeamMember(team *Team, userID int64) error {
  483. isAlreadyMember, err := IsTeamMember(team.OrgID, team.ID, userID)
  484. if err != nil || isAlreadyMember {
  485. return err
  486. }
  487. if err := AddOrgUser(team.OrgID, userID); err != nil {
  488. return err
  489. }
  490. // Get team and its repositories.
  491. if err := team.GetRepositories(); err != nil {
  492. return err
  493. }
  494. sess := x.NewSession()
  495. defer sess.Close()
  496. if err := sess.Begin(); err != nil {
  497. return err
  498. }
  499. if _, err := sess.Insert(&TeamUser{
  500. UID: userID,
  501. OrgID: team.OrgID,
  502. TeamID: team.ID,
  503. }); err != nil {
  504. return err
  505. } else if _, err := sess.Incr("num_members").ID(team.ID).Update(new(Team)); err != nil {
  506. return err
  507. }
  508. team.NumMembers++
  509. // Give access to team repositories.
  510. for _, repo := range team.Repos {
  511. if err := repo.recalculateTeamAccesses(sess, 0); err != nil {
  512. return err
  513. }
  514. if err = watchRepo(sess, userID, repo.ID, true); err != nil {
  515. return err
  516. }
  517. }
  518. return sess.Commit()
  519. }
  520. func removeTeamMember(e *xorm.Session, team *Team, userID int64) error {
  521. isMember, err := isTeamMember(e, team.OrgID, team.ID, userID)
  522. if err != nil || !isMember {
  523. return err
  524. }
  525. // Check if the user to delete is the last member in owner team.
  526. if team.IsOwnerTeam() && team.NumMembers == 1 {
  527. return ErrLastOrgOwner{UID: userID}
  528. }
  529. team.NumMembers--
  530. if err := team.getRepositories(e); err != nil {
  531. return err
  532. }
  533. if _, err := e.Delete(&TeamUser{
  534. UID: userID,
  535. OrgID: team.OrgID,
  536. TeamID: team.ID,
  537. }); err != nil {
  538. return err
  539. } else if _, err = e.
  540. ID(team.ID).
  541. Cols("num_members").
  542. Update(team); err != nil {
  543. return err
  544. }
  545. // Delete access to team repositories.
  546. for _, repo := range team.Repos {
  547. if err := repo.recalculateTeamAccesses(e, 0); err != nil {
  548. return err
  549. }
  550. // Remove watches from now unaccessible
  551. has, err := hasAccess(e, userID, repo)
  552. if err != nil {
  553. return err
  554. } else if has {
  555. continue
  556. }
  557. if err = watchRepo(e, userID, repo.ID, false); err != nil {
  558. return err
  559. }
  560. // Remove all IssueWatches a user has subscribed to in the repositories
  561. if err := removeIssueWatchersByRepoID(e, userID, repo.ID); err != nil {
  562. return err
  563. }
  564. }
  565. // Check if the user is a member of any team in the organization.
  566. if count, err := e.Count(&TeamUser{
  567. UID: userID,
  568. OrgID: team.OrgID,
  569. }); err != nil {
  570. return err
  571. } else if count == 0 {
  572. return removeOrgUser(e, team.OrgID, userID)
  573. }
  574. return nil
  575. }
  576. // RemoveTeamMember removes member from given team of given organization.
  577. func RemoveTeamMember(team *Team, userID int64) error {
  578. sess := x.NewSession()
  579. defer sess.Close()
  580. if err := sess.Begin(); err != nil {
  581. return err
  582. }
  583. if err := removeTeamMember(sess, team, userID); err != nil {
  584. return err
  585. }
  586. return sess.Commit()
  587. }
  588. // IsUserInTeams returns if a user in some teams
  589. func IsUserInTeams(userID int64, teamIDs []int64) (bool, error) {
  590. return x.Where("uid=?", userID).In("team_id", teamIDs).Exist(new(TeamUser))
  591. }
  592. // UsersInTeamsCount counts the number of users which are in userIDs and teamIDs
  593. func UsersInTeamsCount(userIDs []int64, teamIDs []int64) (count int64, err error) {
  594. if count, err = x.In("uid", userIDs).In("team_id", teamIDs).Count(new(TeamUser)); err != nil {
  595. return 0, err
  596. }
  597. return
  598. }
  599. // ___________ __________
  600. // \__ ___/___ _____ _____\______ \ ____ ______ ____
  601. // | |_/ __ \\__ \ / \| _// __ \\____ \ / _ \
  602. // | |\ ___/ / __ \| Y Y \ | \ ___/| |_> > <_> )
  603. // |____| \___ >____ /__|_| /____|_ /\___ > __/ \____/
  604. // \/ \/ \/ \/ \/|__|
  605. // TeamRepo represents an team-repository relation.
  606. type TeamRepo struct {
  607. ID int64 `xorm:"pk autoincr"`
  608. OrgID int64 `xorm:"INDEX"`
  609. TeamID int64 `xorm:"UNIQUE(s)"`
  610. RepoID int64 `xorm:"UNIQUE(s)"`
  611. }
  612. func hasTeamRepo(e Engine, orgID, teamID, repoID int64) bool {
  613. has, _ := e.
  614. Where("org_id=?", orgID).
  615. And("team_id=?", teamID).
  616. And("repo_id=?", repoID).
  617. Get(new(TeamRepo))
  618. return has
  619. }
  620. // HasTeamRepo returns true if given repository belongs to team.
  621. func HasTeamRepo(orgID, teamID, repoID int64) bool {
  622. return hasTeamRepo(x, orgID, teamID, repoID)
  623. }
  624. func addTeamRepo(e Engine, orgID, teamID, repoID int64) error {
  625. _, err := e.InsertOne(&TeamRepo{
  626. OrgID: orgID,
  627. TeamID: teamID,
  628. RepoID: repoID,
  629. })
  630. return err
  631. }
  632. func removeTeamRepo(e Engine, teamID, repoID int64) error {
  633. _, err := e.Delete(&TeamRepo{
  634. TeamID: teamID,
  635. RepoID: repoID,
  636. })
  637. return err
  638. }
  639. // GetTeamsWithAccessToRepo returns all teams in an organization that have given access level to the repository.
  640. func GetTeamsWithAccessToRepo(orgID, repoID int64, mode AccessMode) ([]*Team, error) {
  641. teams := make([]*Team, 0, 5)
  642. return teams, x.Where("team.authorize >= ?", mode).
  643. Join("INNER", "team_repo", "team_repo.team_id = team.id").
  644. And("team_repo.org_id = ?", orgID).
  645. And("team_repo.repo_id = ?", repoID).
  646. Find(&teams)
  647. }
  648. // ___________ ____ ___ .__ __
  649. // \__ ___/___ _____ _____ | | \____ |__|/ |_
  650. // | |_/ __ \\__ \ / \| | / \| \ __\
  651. // | |\ ___/ / __ \| Y Y \ | / | \ || |
  652. // |____| \___ >____ /__|_| /______/|___| /__||__|
  653. // \/ \/ \/ \/
  654. // TeamUnit describes all units of a repository
  655. type TeamUnit struct {
  656. ID int64 `xorm:"pk autoincr"`
  657. OrgID int64 `xorm:"INDEX"`
  658. TeamID int64 `xorm:"UNIQUE(s)"`
  659. Type UnitType `xorm:"UNIQUE(s)"`
  660. }
  661. // Unit returns Unit
  662. func (t *TeamUnit) Unit() Unit {
  663. return Units[t.Type]
  664. }
  665. func getUnitsByTeamID(e Engine, teamID int64) (units []*TeamUnit, err error) {
  666. return units, e.Where("team_id = ?", teamID).Find(&units)
  667. }
  668. // UpdateTeamUnits updates a teams's units
  669. func UpdateTeamUnits(team *Team, units []TeamUnit) (err error) {
  670. sess := x.NewSession()
  671. defer sess.Close()
  672. if err = sess.Begin(); err != nil {
  673. return err
  674. }
  675. if _, err = sess.Where("team_id = ?", team.ID).Delete(new(TeamUnit)); err != nil {
  676. return err
  677. }
  678. if _, err = sess.Insert(units); err != nil {
  679. sess.Rollback()
  680. return err
  681. }
  682. return sess.Commit()
  683. }