@@ -9,11 +9,8 @@ import (
99 "fmt"
1010 "strings"
1111
12- actions_model "code.gitea.io/gitea/models/actions"
1312 "code.gitea.io/gitea/models/db"
1413 "code.gitea.io/gitea/models/perm"
15- repo_model "code.gitea.io/gitea/models/repo"
16- secret_model "code.gitea.io/gitea/models/secret"
1714 "code.gitea.io/gitea/models/unit"
1815 user_model "code.gitea.io/gitea/models/user"
1916 "code.gitea.io/gitea/modules/log"
@@ -407,33 +404,6 @@ func GetOrgByName(ctx context.Context, name string) (*Organization, error) {
407404 return u , nil
408405}
409406
410- // DeleteOrganization deletes models associated to an organization.
411- func DeleteOrganization (ctx context.Context , org * Organization ) error {
412- if org .Type != user_model .UserTypeOrganization {
413- return fmt .Errorf ("%s is a user not an organization" , org .Name )
414- }
415-
416- if err := db .DeleteBeans (ctx ,
417- & Team {OrgID : org .ID },
418- & OrgUser {OrgID : org .ID },
419- & TeamUser {OrgID : org .ID },
420- & TeamUnit {OrgID : org .ID },
421- & TeamInvite {OrgID : org .ID },
422- & secret_model.Secret {OwnerID : org .ID },
423- & user_model.Blocking {BlockerID : org .ID },
424- & actions_model.ActionRunner {OwnerID : org .ID },
425- & actions_model.ActionRunnerToken {OwnerID : org .ID },
426- ); err != nil {
427- return fmt .Errorf ("DeleteBeans: %w" , err )
428- }
429-
430- if _ , err := db .GetEngine (ctx ).ID (org .ID ).Delete (new (user_model.User )); err != nil {
431- return fmt .Errorf ("Delete: %w" , err )
432- }
433-
434- return nil
435- }
436-
437407// GetOrgUserMaxAuthorizeLevel returns highest authorize level of user in an organization
438408func (org * Organization ) GetOrgUserMaxAuthorizeLevel (ctx context.Context , uid int64 ) (perm.AccessMode , error ) {
439409 var authorize perm.AccessMode
@@ -604,7 +574,9 @@ func RemoveOrgRepo(ctx context.Context, orgID, repoID int64) error {
604574 return err
605575}
606576
607- func (org * Organization ) getUserTeams (ctx context.Context , userID int64 , cols ... string ) ([]* Team , error ) {
577+ // GetUserTeams returns all teams that belong to user,
578+ // and that the user has joined.
579+ func (org * Organization ) GetUserTeams (ctx context.Context , userID int64 , cols ... string ) ([]* Team , error ) {
608580 teams := make ([]* Team , 0 , org .NumTeams )
609581 return teams , db .GetEngine (ctx ).
610582 Where ("`team_user`.org_id = ?" , org .ID ).
@@ -616,7 +588,8 @@ func (org *Organization) getUserTeams(ctx context.Context, userID int64, cols ..
616588 Find (& teams )
617589}
618590
619- func (org * Organization ) getUserTeamIDs (ctx context.Context , userID int64 ) ([]int64 , error ) {
591+ // GetUserTeamIDs returns of all team IDs of the organization that user is member of.
592+ func (org * Organization ) GetUserTeamIDs (ctx context.Context , userID int64 ) ([]int64 , error ) {
620593 teamIDs := make ([]int64 , 0 , org .NumTeams )
621594 return teamIDs , db .GetEngine (ctx ).
622595 Table ("team" ).
@@ -640,175 +613,3 @@ func getUserTeamIDsQueryBuilder(orgID, userID int64) *builder.Builder {
640613func (org * Organization ) TeamsWithAccessToRepo (ctx context.Context , repoID int64 , mode perm.AccessMode ) ([]* Team , error ) {
641614 return GetTeamsWithAccessToRepo (ctx , org .ID , repoID , mode )
642615}
643-
644- // GetUserTeamIDs returns of all team IDs of the organization that user is member of.
645- func (org * Organization ) GetUserTeamIDs (ctx context.Context , userID int64 ) ([]int64 , error ) {
646- return org .getUserTeamIDs (ctx , userID )
647- }
648-
649- // GetUserTeams returns all teams that belong to user,
650- // and that the user has joined.
651- func (org * Organization ) GetUserTeams (ctx context.Context , userID int64 ) ([]* Team , error ) {
652- return org .getUserTeams (ctx , userID )
653- }
654-
655- // AccessibleReposEnvironment operations involving the repositories that are
656- // accessible to a particular user
657- type AccessibleReposEnvironment interface {
658- CountRepos () (int64 , error )
659- RepoIDs (page , pageSize int ) ([]int64 , error )
660- Repos (page , pageSize int ) (repo_model.RepositoryList , error )
661- MirrorRepos () (repo_model.RepositoryList , error )
662- AddKeyword (keyword string )
663- SetSort (db.SearchOrderBy )
664- }
665-
666- type accessibleReposEnv struct {
667- org * Organization
668- user * user_model.User
669- team * Team
670- teamIDs []int64
671- ctx context.Context
672- keyword string
673- orderBy db.SearchOrderBy
674- }
675-
676- // AccessibleReposEnv builds an AccessibleReposEnvironment for the repositories in `org`
677- // that are accessible to the specified user.
678- func AccessibleReposEnv (ctx context.Context , org * Organization , userID int64 ) (AccessibleReposEnvironment , error ) {
679- var user * user_model.User
680-
681- if userID > 0 {
682- u , err := user_model .GetUserByID (ctx , userID )
683- if err != nil {
684- return nil , err
685- }
686- user = u
687- }
688-
689- teamIDs , err := org .getUserTeamIDs (ctx , userID )
690- if err != nil {
691- return nil , err
692- }
693- return & accessibleReposEnv {
694- org : org ,
695- user : user ,
696- teamIDs : teamIDs ,
697- ctx : ctx ,
698- orderBy : db .SearchOrderByRecentUpdated ,
699- }, nil
700- }
701-
702- // AccessibleTeamReposEnv an AccessibleReposEnvironment for the repositories in `org`
703- // that are accessible to the specified team.
704- func (org * Organization ) AccessibleTeamReposEnv (ctx context.Context , team * Team ) AccessibleReposEnvironment {
705- return & accessibleReposEnv {
706- org : org ,
707- team : team ,
708- ctx : ctx ,
709- orderBy : db .SearchOrderByRecentUpdated ,
710- }
711- }
712-
713- func (env * accessibleReposEnv ) cond () builder.Cond {
714- cond := builder .NewCond ()
715- if env .team != nil {
716- cond = cond .And (builder.Eq {"team_repo.team_id" : env .team .ID })
717- } else {
718- if env .user == nil || ! env .user .IsRestricted {
719- cond = cond .Or (builder.Eq {
720- "`repository`.owner_id" : env .org .ID ,
721- "`repository`.is_private" : false ,
722- })
723- }
724- if len (env .teamIDs ) > 0 {
725- cond = cond .Or (builder .In ("team_repo.team_id" , env .teamIDs ))
726- }
727- }
728- if env .keyword != "" {
729- cond = cond .And (builder.Like {"`repository`.lower_name" , strings .ToLower (env .keyword )})
730- }
731- return cond
732- }
733-
734- func (env * accessibleReposEnv ) CountRepos () (int64 , error ) {
735- repoCount , err := db .GetEngine (env .ctx ).
736- Join ("INNER" , "team_repo" , "`team_repo`.repo_id=`repository`.id" ).
737- Where (env .cond ()).
738- Distinct ("`repository`.id" ).
739- Count (& repo_model.Repository {})
740- if err != nil {
741- return 0 , fmt .Errorf ("count user repositories in organization: %w" , err )
742- }
743- return repoCount , nil
744- }
745-
746- func (env * accessibleReposEnv ) RepoIDs (page , pageSize int ) ([]int64 , error ) {
747- if page <= 0 {
748- page = 1
749- }
750-
751- repoIDs := make ([]int64 , 0 , pageSize )
752- return repoIDs , db .GetEngine (env .ctx ).
753- Table ("repository" ).
754- Join ("INNER" , "team_repo" , "`team_repo`.repo_id=`repository`.id" ).
755- Where (env .cond ()).
756- GroupBy ("`repository`.id,`repository`." + strings .Fields (string (env .orderBy ))[0 ]).
757- OrderBy (string (env .orderBy )).
758- Limit (pageSize , (page - 1 )* pageSize ).
759- Cols ("`repository`.id" ).
760- Find (& repoIDs )
761- }
762-
763- func (env * accessibleReposEnv ) Repos (page , pageSize int ) (repo_model.RepositoryList , error ) {
764- repoIDs , err := env .RepoIDs (page , pageSize )
765- if err != nil {
766- return nil , fmt .Errorf ("GetUserRepositoryIDs: %w" , err )
767- }
768-
769- repos := make ([]* repo_model.Repository , 0 , len (repoIDs ))
770- if len (repoIDs ) == 0 {
771- return repos , nil
772- }
773-
774- return repos , db .GetEngine (env .ctx ).
775- In ("`repository`.id" , repoIDs ).
776- OrderBy (string (env .orderBy )).
777- Find (& repos )
778- }
779-
780- func (env * accessibleReposEnv ) MirrorRepoIDs () ([]int64 , error ) {
781- repoIDs := make ([]int64 , 0 , 10 )
782- return repoIDs , db .GetEngine (env .ctx ).
783- Table ("repository" ).
784- Join ("INNER" , "team_repo" , "`team_repo`.repo_id=`repository`.id AND `repository`.is_mirror=?" , true ).
785- Where (env .cond ()).
786- GroupBy ("`repository`.id, `repository`.updated_unix" ).
787- OrderBy (string (env .orderBy )).
788- Cols ("`repository`.id" ).
789- Find (& repoIDs )
790- }
791-
792- func (env * accessibleReposEnv ) MirrorRepos () (repo_model.RepositoryList , error ) {
793- repoIDs , err := env .MirrorRepoIDs ()
794- if err != nil {
795- return nil , fmt .Errorf ("MirrorRepoIDs: %w" , err )
796- }
797-
798- repos := make ([]* repo_model.Repository , 0 , len (repoIDs ))
799- if len (repoIDs ) == 0 {
800- return repos , nil
801- }
802-
803- return repos , db .GetEngine (env .ctx ).
804- In ("`repository`.id" , repoIDs ).
805- Find (& repos )
806- }
807-
808- func (env * accessibleReposEnv ) AddKeyword (keyword string ) {
809- env .keyword = keyword
810- }
811-
812- func (env * accessibleReposEnv ) SetSort (orderBy db.SearchOrderBy ) {
813- env .orderBy = orderBy
814- }
0 commit comments