organizations.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // Copyright 2022 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. "os"
  9. "strings"
  10. "github.com/pkg/errors"
  11. "gorm.io/gorm"
  12. "gogs.io/gogs/internal/dbutil"
  13. "gogs.io/gogs/internal/errutil"
  14. "gogs.io/gogs/internal/repoutil"
  15. "gogs.io/gogs/internal/userutil"
  16. )
  17. // OrganizationsStore is the persistent interface for organizations.
  18. type OrganizationsStore interface {
  19. // Create creates a new organization with the initial owner and persists to
  20. // database. It returns ErrNameNotAllowed if the given name or pattern of the
  21. // name is not allowed as an organization name, or ErrOrganizationAlreadyExist
  22. // when a user or an organization with same name already exists.
  23. Create(ctx context.Context, name string, ownerID int64, opts CreateOrganizationOptions) (*Organization, error)
  24. // GetByName returns the organization with given name.
  25. GetByName(ctx context.Context, name string) (*Organization, error)
  26. // SearchByName returns a list of organizations whose username or full name
  27. // matches the given keyword case-insensitively. Results are paginated by given
  28. // page and page size, and sorted by the given order (e.g. "id DESC"). A total
  29. // count of all results is also returned. If the order is not given, it's up to
  30. // the database to decide.
  31. SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error)
  32. // List returns a list of organizations filtered by options.
  33. List(ctx context.Context, opts ListOrganizationsOptions) ([]*Organization, error)
  34. // CountByUser returns the number of organizations the user is a member of.
  35. CountByUser(ctx context.Context, userID int64) (int64, error)
  36. // Count returns the total number of organizations.
  37. Count(ctx context.Context) int64
  38. // DeleteByID deletes the given organization and all their resources. It returns
  39. // ErrOrganizationOwnRepos when the user still has repository ownership.
  40. DeleteByID(ctx context.Context, orgID int64) error
  41. // AddMember adds a new member to the given organization.
  42. AddMember(ctx context.Context, orgID, userID int64) error
  43. // RemoveMember removes a member from the given organization.
  44. RemoveMember(ctx context.Context, orgID, userID int64) error
  45. // HasMember returns whether the given user is a member of the organization
  46. // (first), and whether the organization membership is public (second).
  47. HasMember(ctx context.Context, orgID, userID int64) (bool, bool)
  48. // ListMembers returns all members of the given organization, and sorted by the
  49. // given order (e.g. "id ASC").
  50. ListMembers(ctx context.Context, orgID int64, opts ListOrgMembersOptions) ([]*User, error)
  51. // IsOwnedBy returns true if the given user is an owner of the organization.
  52. IsOwnedBy(ctx context.Context, orgID, userID int64) bool
  53. // SetMemberVisibility sets the visibility of the given user in the organization.
  54. SetMemberVisibility(ctx context.Context, orgID, userID int64, public bool) error
  55. // GetTeamByName returns the team with given name under the given organization.
  56. // It returns ErrTeamNotExist whe not found.
  57. GetTeamByName(ctx context.Context, orgID int64, name string) (*Team, error)
  58. // AccessibleRepositoriesByUser returns a range of repositories in the
  59. // organization that the user has access to and the total number of it. Results
  60. // are paginated by given page and page size, and sorted by the given order
  61. // (e.g. "updated_unix DESC").
  62. AccessibleRepositoriesByUser(ctx context.Context, orgID, userID int64, page, pageSize int, opts AccessibleRepositoriesByUserOptions) ([]*Repository, int64, error)
  63. }
  64. var Organizations OrganizationsStore
  65. var _ OrganizationsStore = (*organizations)(nil)
  66. type organizations struct {
  67. *gorm.DB
  68. }
  69. // NewOrganizationsStore returns a persistent interface for orgs with given
  70. // database connection.
  71. func NewOrganizationsStore(db *gorm.DB) OrganizationsStore {
  72. return &organizations{DB: db}
  73. }
  74. func (*organizations) recountMembers(tx *gorm.DB, orgID int64) error {
  75. /*
  76. Equivalent SQL for PostgreSQL:
  77. UPDATE "user"
  78. SET num_members = (
  79. SELECT COUNT(*) FROM org_user WHERE org_id = @orgID
  80. )
  81. WHERE id = @orgID
  82. */
  83. err := tx.Model(&User{}).
  84. Where("id = ?", orgID).
  85. Update(
  86. "num_members",
  87. tx.Model(&OrgUser{}).Select("COUNT(*)").Where("org_id = ?", orgID),
  88. ).
  89. Error
  90. if err != nil {
  91. return errors.Wrap(err, `update "user.num_members"`)
  92. }
  93. return nil
  94. }
  95. func (db *organizations) AddMember(ctx context.Context, orgID, userID int64) error {
  96. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  97. ou := &OrgUser{
  98. UserID: userID,
  99. OrgID: orgID,
  100. }
  101. result := tx.FirstOrCreate(ou, ou)
  102. if result.Error != nil {
  103. return errors.Wrap(result.Error, "upsert")
  104. } else if result.RowsAffected <= 0 {
  105. return nil // Relation already exists
  106. }
  107. return db.recountMembers(tx, orgID)
  108. })
  109. }
  110. type ErrLastOrgOwner struct {
  111. args map[string]any
  112. }
  113. func IsErrLastOrgOwner(err error) bool {
  114. return errors.As(err, &ErrLastOrgOwner{})
  115. }
  116. func (err ErrLastOrgOwner) Error() string {
  117. return fmt.Sprintf("user is the last owner of the organization: %v", err.args)
  118. }
  119. func (db *organizations) RemoveMember(ctx context.Context, orgID, userID int64) error {
  120. ou, err := db.getOrgUser(ctx, orgID, userID)
  121. if err != nil {
  122. if errors.Is(err, gorm.ErrRecordNotFound) {
  123. return nil // Not a member
  124. }
  125. return errors.Wrap(err, "check organization membership")
  126. }
  127. // Check if the member to remove is the last owner.
  128. if ou.IsOwner {
  129. t, err := db.GetTeamByName(ctx, orgID, TeamNameOwners)
  130. if err != nil {
  131. return errors.Wrap(err, "get owners team")
  132. } else if t.NumMembers == 1 {
  133. return ErrLastOrgOwner{args: map[string]any{"orgID": orgID, "userID": userID}}
  134. }
  135. }
  136. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  137. repoIDsConds := db.accessibleRepositoriesByUser(tx, orgID, userID, accessibleRepositoriesByUserOptions{}).Select("repository.id")
  138. err := tx.Where("user_id = ? AND repo_id IN (?)", userID, repoIDsConds).Delete(&Watch{}).Error
  139. if err != nil {
  140. return errors.Wrap(err, "unwatch repositories")
  141. }
  142. err = tx.Table("repository").
  143. Where("id IN (?)", repoIDsConds).
  144. UpdateColumn("num_watches", gorm.Expr("num_watches - 1")).
  145. Error
  146. if err != nil {
  147. return errors.Wrap(err, `decrease "repository.num_watches"`)
  148. }
  149. err = tx.Where("user_id = ? AND repo_id IN (?)", userID, repoIDsConds).Delete(&Access{}).Error
  150. if err != nil {
  151. return errors.Wrap(err, "delete repository accesses")
  152. }
  153. err = tx.Where("user_id = ? AND repo_id IN (?)", userID, repoIDsConds).Delete(&Collaboration{}).Error
  154. if err != nil {
  155. return errors.Wrap(err, "delete repository collaborations")
  156. }
  157. /*
  158. Equivalent SQL for PostgreSQL:
  159. UPDATE "team"
  160. SET num_members = num_members - 1
  161. WHERE id IN (
  162. SELECT team_id FROM "team_user"
  163. WHERE team_user.org_id = @orgID AND uid = @userID)
  164. )
  165. */
  166. err = tx.Table("team").
  167. Where(`id IN (?)`, tx.
  168. Select("team_id").
  169. Table("team_user").
  170. Where("org_id = ? AND uid = ?", orgID, userID),
  171. ).
  172. UpdateColumn("num_members", gorm.Expr("num_members - 1")).
  173. Error
  174. if err != nil {
  175. return errors.Wrap(err, `decrease "team.num_members"`)
  176. }
  177. err = tx.Where("uid = ? AND org_id = ?", userID, orgID).Delete(&TeamUser{}).Error
  178. if err != nil {
  179. return errors.Wrap(err, "delete team membership")
  180. }
  181. err = tx.Where("uid = ? AND org_id = ?", userID, orgID).Delete(&OrgUser{}).Error
  182. if err != nil {
  183. return errors.Wrap(err, "delete organization membership")
  184. }
  185. return db.recountMembers(tx, orgID)
  186. })
  187. }
  188. type accessibleRepositoriesByUserOptions struct {
  189. orderBy string
  190. page int
  191. pageSize int
  192. }
  193. func (*organizations) accessibleRepositoriesByUser(tx *gorm.DB, orgID, userID int64, opts accessibleRepositoriesByUserOptions) *gorm.DB {
  194. /*
  195. Equivalent SQL for PostgreSQL:
  196. <SELECT * FROM "repository">
  197. JOIN team_repo ON repository.id = team_repo.repo_id
  198. WHERE
  199. owner_id = @orgID
  200. AND (
  201. team_repo.team_id IN (
  202. SELECT team_id FROM "team_user"
  203. WHERE team_user.org_id = @orgID AND uid = @userID)
  204. )
  205. OR (repository.is_private = FALSE AND repository.is_unlisted = FALSE)
  206. )
  207. [ORDER BY updated_unix DESC]
  208. [LIMIT @limit OFFSET @offset]
  209. */
  210. conds := tx.
  211. Joins("JOIN team_repo ON repository.id = team_repo.repo_id").
  212. Where("owner_id = ? AND (?)", orgID, tx.
  213. Where("team_repo.team_id IN (?)", tx.
  214. Select("team_id").
  215. Table("team_user").
  216. Where("team_user.org_id = ? AND uid = ?", orgID, userID),
  217. ).
  218. Or("repository.is_private = ? AND repository.is_unlisted = ?", false, false),
  219. )
  220. if opts.orderBy != "" {
  221. conds.Order(opts.orderBy)
  222. }
  223. if opts.page > 0 && opts.pageSize > 0 {
  224. conds.Limit(opts.pageSize).Offset((opts.page - 1) * opts.pageSize)
  225. }
  226. return conds
  227. }
  228. type AccessibleRepositoriesByUserOptions struct {
  229. // Whether to skip counting the total number of repositories.
  230. SkipCount bool
  231. }
  232. func (db *organizations) AccessibleRepositoriesByUser(ctx context.Context, orgID, userID int64, page, pageSize int, opts AccessibleRepositoriesByUserOptions) ([]*Repository, int64, error) {
  233. conds := db.accessibleRepositoriesByUser(
  234. db.DB,
  235. orgID,
  236. userID,
  237. accessibleRepositoriesByUserOptions{
  238. orderBy: "updated_unix DESC",
  239. page: page,
  240. pageSize: pageSize,
  241. },
  242. ).WithContext(ctx)
  243. repos := make([]*Repository, 0, pageSize)
  244. err := conds.Find(&repos).Error
  245. if err != nil {
  246. return nil, 0, errors.Wrap(err, "list repositories")
  247. }
  248. if opts.SkipCount {
  249. return repos, 0, nil
  250. }
  251. var count int64
  252. err = conds.Model(&Repository{}).Count(&count).Error
  253. if err != nil {
  254. return nil, 0, errors.Wrap(err, "count repositories")
  255. }
  256. return repos, count, nil
  257. }
  258. func (db *organizations) getOrgUser(ctx context.Context, orgID, userID int64) (*OrgUser, error) {
  259. var ou OrgUser
  260. return &ou, db.WithContext(ctx).Where("org_id = ? AND uid = ?", orgID, userID).First(&ou).Error
  261. }
  262. func (db *organizations) IsOwnedBy(ctx context.Context, orgID, userID int64) bool {
  263. ou, err := db.getOrgUser(ctx, orgID, userID)
  264. return err == nil && ou.IsOwner
  265. }
  266. func (db *organizations) SetMemberVisibility(ctx context.Context, orgID, userID int64, public bool) error {
  267. return db.Table("org_user").Where("org_id = ? AND uid = ?", orgID, userID).UpdateColumn("is_public", public).Error
  268. }
  269. func (db *organizations) HasMember(ctx context.Context, orgID, userID int64) (bool, bool) {
  270. ou, err := db.getOrgUser(ctx, orgID, userID)
  271. return err == nil, ou != nil && ou.IsPublic
  272. }
  273. type ListOrgMembersOptions struct {
  274. // The maximum number of members to return.
  275. Limit int
  276. }
  277. func (db *organizations) ListMembers(ctx context.Context, orgID int64, opts ListOrgMembersOptions) ([]*User, error) {
  278. /*
  279. Equivalent SQL for PostgreSQL:
  280. SELECT * FROM "user"
  281. JOIN org_user ON org_user.uid = user.id
  282. WHERE
  283. org_user.org_id = @orgID
  284. ORDER BY user.id ASC
  285. [LIMIT @limit]
  286. */
  287. conds := db.WithContext(ctx).
  288. Joins(dbutil.Quote("JOIN org_user ON org_user.uid = %s.id", "user")).
  289. Where("org_user.org_id = ?", orgID).
  290. Order(dbutil.Quote("%s.id ASC", "user"))
  291. if opts.Limit > 0 {
  292. conds.Limit(opts.Limit)
  293. }
  294. var users []*User
  295. return users, conds.Find(&users).Error
  296. }
  297. type ListOrganizationsOptions struct {
  298. // Filter by the membership with the given user ID.
  299. MemberID int64
  300. // Whether to include private memberships.
  301. IncludePrivateMembers bool
  302. // 1-based page number.
  303. Page int
  304. // Number of results per page.
  305. PageSize int
  306. }
  307. func (db *organizations) List(ctx context.Context, opts ListOrganizationsOptions) ([]*Organization, error) {
  308. if opts.MemberID <= 0 {
  309. return nil, errors.New("MemberID must be greater than 0")
  310. }
  311. /*
  312. Equivalent SQL for PostgreSQL:
  313. SELECT * FROM "user"
  314. [JOIN org_user ON org_user.org_id = user.id]
  315. WHERE
  316. type = @type
  317. [AND org_user.uid = @memberID
  318. AND org_user.is_public = @includePrivateMembers]
  319. ORDER BY user.id ASC
  320. [LIMIT @limit OFFSET @offset]
  321. */
  322. conds := db.WithContext(ctx).
  323. Where("type = ?", UserTypeOrganization).
  324. Order(dbutil.Quote("%s.id ASC", "user"))
  325. if opts.MemberID > 0 || !opts.IncludePrivateMembers {
  326. conds.Joins(dbutil.Quote("JOIN org_user ON org_user.org_id = %s.id", "user"))
  327. }
  328. if opts.MemberID > 0 {
  329. conds.Where("org_user.uid = ?", opts.MemberID)
  330. }
  331. if !opts.IncludePrivateMembers {
  332. conds.Where("org_user.is_public = ?", true)
  333. }
  334. if opts.Page > 0 && opts.PageSize > 0 {
  335. conds.Limit(opts.PageSize).Offset((opts.Page - 1) * opts.PageSize)
  336. }
  337. var orgs []*Organization
  338. return orgs, conds.Find(&orgs).Error
  339. }
  340. type CreateOrganizationOptions struct {
  341. FullName string
  342. Email string
  343. Location string
  344. Website string
  345. Description string
  346. }
  347. type ErrOrganizationAlreadyExist struct {
  348. args errutil.Args
  349. }
  350. // IsErrOrganizationAlreadyExist returns true if the underlying error has the
  351. // type ErrOrganizationAlreadyExist.
  352. func IsErrOrganizationAlreadyExist(err error) bool {
  353. return errors.As(err, &ErrOrganizationAlreadyExist{})
  354. }
  355. func (err ErrOrganizationAlreadyExist) Error() string {
  356. return fmt.Sprintf("organization already exists: %v", err.args)
  357. }
  358. func (db *organizations) Create(ctx context.Context, name string, ownerID int64, opts CreateOrganizationOptions) (*Organization, error) {
  359. err := isUsernameAllowed(name)
  360. if err != nil {
  361. return nil, err
  362. }
  363. if NewUsersStore(db.DB).IsUsernameUsed(ctx, name, 0) {
  364. return nil, ErrOrganizationAlreadyExist{
  365. args: errutil.Args{
  366. "name": name,
  367. },
  368. }
  369. }
  370. org := &Organization{
  371. LowerName: strings.ToLower(name),
  372. Name: name,
  373. FullName: opts.FullName,
  374. Email: opts.Email,
  375. Type: UserTypeOrganization,
  376. Location: opts.Location,
  377. Website: opts.Website,
  378. MaxRepoCreation: -1,
  379. IsActive: true,
  380. UseCustomAvatar: true,
  381. Description: opts.Description,
  382. NumTeams: 1, // The default "owners" team
  383. NumMembers: 1, // The initial owner
  384. }
  385. org.Rands, err = userutil.RandomSalt()
  386. if err != nil {
  387. return nil, err
  388. }
  389. org.Salt, err = userutil.RandomSalt()
  390. if err != nil {
  391. return nil, err
  392. }
  393. return org, db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  394. err := tx.Create(org).Error
  395. if err != nil {
  396. return errors.Wrap(err, "create organization")
  397. }
  398. err = tx.Create(&OrgUser{
  399. UserID: ownerID,
  400. OrgID: org.ID,
  401. IsOwner: true,
  402. NumTeams: 1,
  403. }).Error
  404. if err != nil {
  405. return errors.Wrap(err, "create org-user relation")
  406. }
  407. team := &Team{
  408. OrgID: org.ID,
  409. LowerName: strings.ToLower(TeamNameOwners),
  410. Name: TeamNameOwners,
  411. Authorize: AccessModeOwner,
  412. NumMembers: 1,
  413. }
  414. err = tx.Create(team).Error
  415. if err != nil {
  416. return errors.Wrap(err, "create owner team")
  417. }
  418. err = tx.Create(&TeamUser{
  419. UID: ownerID,
  420. OrgID: org.ID,
  421. TeamID: team.ID,
  422. }).Error
  423. if err != nil {
  424. return errors.Wrap(err, "create team-user relation")
  425. }
  426. err = userutil.GenerateRandomAvatar(org.ID, org.Name, org.Email)
  427. if err != nil {
  428. return errors.Wrap(err, "generate organization avatar")
  429. }
  430. err = os.MkdirAll(repoutil.UserPath(org.Name), os.ModePerm)
  431. if err != nil {
  432. return errors.Wrap(err, "create organization directory")
  433. }
  434. return nil
  435. })
  436. }
  437. var _ errutil.NotFound = (*ErrUserNotExist)(nil)
  438. type ErrOrganizationNotExist struct {
  439. args errutil.Args
  440. }
  441. // IsErrOrganizationNotExist returns true if the underlying error has the type
  442. // ErrOrganizationNotExist.
  443. func IsErrOrganizationNotExist(err error) bool {
  444. return errors.As(err, &ErrOrganizationNotExist{})
  445. }
  446. func (err ErrOrganizationNotExist) Error() string {
  447. return fmt.Sprintf("organization does not exist: %v", err.args)
  448. }
  449. func (ErrOrganizationNotExist) NotFound() bool {
  450. return true
  451. }
  452. func (db *organizations) GetByName(ctx context.Context, name string) (*Organization, error) {
  453. org, err := getUserByUsername(ctx, db.DB, UserTypeOrganization, name)
  454. if err != nil {
  455. if IsErrUserNotExist(err) {
  456. return nil, ErrOrganizationNotExist{args: map[string]any{"name": name}}
  457. }
  458. return nil, errors.Wrap(err, "get organization by name")
  459. }
  460. return org, nil
  461. }
  462. func (db *organizations) SearchByName(ctx context.Context, keyword string, page, pageSize int, orderBy string) ([]*Organization, int64, error) {
  463. return searchUserByName(ctx, db.DB, UserTypeOrganization, keyword, page, pageSize, orderBy)
  464. }
  465. func (db *organizations) CountByUser(ctx context.Context, userID int64) (int64, error) {
  466. var count int64
  467. return count, db.WithContext(ctx).Model(&OrgUser{}).Where("uid = ?", userID).Count(&count).Error
  468. }
  469. func (db *organizations) Count(ctx context.Context) int64 {
  470. var count int64
  471. db.WithContext(ctx).Model(&User{}).Where("type = ?", UserTypeOrganization).Count(&count)
  472. return count
  473. }
  474. type ErrOrganizationOwnRepos struct {
  475. args errutil.Args
  476. }
  477. // IsErrOrganizationOwnRepos returns true if the underlying error has the type
  478. // ErrOrganizationOwnRepos.
  479. func IsErrOrganizationOwnRepos(err error) bool {
  480. return errors.As(errors.Cause(err), &ErrOrganizationOwnRepos{})
  481. }
  482. func (err ErrOrganizationOwnRepos) Error() string {
  483. return fmt.Sprintf("organization still has repository ownership: %v", err.args)
  484. }
  485. func (db *organizations) DeleteByID(ctx context.Context, orgID int64) error {
  486. return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
  487. for _, t := range []any{&Team{}, &OrgUser{}, &TeamUser{}} {
  488. err := tx.Where("org_id = ?", orgID).Delete(t).Error
  489. if err != nil {
  490. return errors.Wrapf(err, "clean up table %T", t)
  491. }
  492. }
  493. err := NewUsersStore(tx).DeleteByID(ctx, orgID, false)
  494. if err != nil {
  495. return errors.Wrap(err, "delete organization")
  496. }
  497. return nil
  498. })
  499. }
  500. var _ errutil.NotFound = (*ErrTeamNotExist)(nil)
  501. type ErrTeamNotExist struct {
  502. args map[string]any
  503. }
  504. func IsErrTeamNotExist(err error) bool {
  505. return errors.As(err, &ErrTeamNotExist{})
  506. }
  507. func (err ErrTeamNotExist) Error() string {
  508. return fmt.Sprintf("team does not exist: %v", err.args)
  509. }
  510. func (ErrTeamNotExist) NotFound() bool {
  511. return true
  512. }
  513. func (db *organizations) GetTeamByName(ctx context.Context, orgID int64, name string) (*Team, error) {
  514. var team Team
  515. err := db.WithContext(ctx).Where("org_id = ? AND lower_name = ?", orgID, strings.ToLower(name)).First(&team).Error
  516. if err != nil {
  517. if errors.Is(err, gorm.ErrRecordNotFound) {
  518. return nil, ErrTeamNotExist{args: map[string]any{"orgID": orgID, "name": name}}
  519. }
  520. return nil, errors.Wrap(err, "get team by name")
  521. }
  522. return &team, nil
  523. }
  524. type Organization = User
  525. func (u *Organization) TableName() string {
  526. return "user"
  527. }
  528. // IsOwnedBy returns true if the given user is an owner of the organization.
  529. //
  530. // TODO(unknwon): This is also used in templates, which should be fixed by
  531. // having a dedicated type `template.Organization`.
  532. func (u *Organization) IsOwnedBy(userID int64) bool {
  533. return Organizations.IsOwnedBy(context.TODO(), u.ID, userID)
  534. }
  535. // OrgUser represents relations of organizations and their members.
  536. type OrgUser struct {
  537. ID int64 `gorm:"primaryKey"`
  538. UserID int64 `xorm:"uid INDEX UNIQUE(s)" gorm:"column:uid;uniqueIndex:org_user_user_org_unique;index;not null" json:"Uid"`
  539. OrgID int64 `xorm:"INDEX UNIQUE(s)" gorm:"uniqueIndex:org_user_user_org_unique;index;not null"`
  540. IsPublic bool `gorm:"not null;default:FALSE"`
  541. IsOwner bool `gorm:"not null;default:FALSE"`
  542. NumTeams int `gorm:"not null;default:0"`
  543. }