Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] a way to limit org and org repo number #15950

Open
1 task
a1012112796 opened this issue May 22, 2021 · 1 comment
Open
1 task

[Proposal] a way to limit org and org repo number #15950

a1012112796 opened this issue May 22, 2021 · 1 comment
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.

Comments

@a1012112796
Copy link
Member

a1012112796 commented May 22, 2021

Proposal sollution about limit org's repo number.

Add a new identity named as main owner in a org, which is the creater of org, and can be changed but should be only one in a org, The orgs repo will be calculate as main owner's repo to limit repo number. Only when the main owner can create repo can this org create repo. and we can also limit the number of org one user can main owned to limit org create.

main logic:

diff --git a/models/user.go b/models/user.go
index c7b44bdb7..efedcc38b 100644
--- a/models/user.go
+++ b/models/user.go
@@ -161,6 +161,8 @@ type User struct {
 	MembersIsPublic           map[int64]bool      `xorm:"-"`
 	Visibility                structs.VisibleType `xorm:"NOT NULL DEFAULT 0"`
 	RepoAdminChangeTeamAccess bool                `xorm:"NOT NULL DEFAULT false"`
+	MainOwnerID               int64               `xorm:"INDEX"`
+	MainOwner                 *User               `xorm:"-"`
 
 	// Preferences
 	DiffViewStyle       string `xorm:"NOT NULL DEFAULT ''"`
@@ -264,19 +266,51 @@ func (u *User) MaxCreationLimit() int {
 	return u.MaxRepoCreation
 }
 
+// LoadMainOwner load main owner of a org
+func (u *User) LoadMainOwner() (err error) {
+	if !u.IsOrganization() || u.MainOwner != nil {
+		return nil
+	}
+
+	u.MainOwner, err = GetUserByID(u.MainOwnerID)
+	return err
+}
+
 // CanCreateRepo returns if user login can create a repository
 // NOTE: functions calling this assume a failure due to repository count limit; if new checks are added, those functions should be revised
 func (u *User) CanCreateRepo() bool {
 	if u.IsAdmin {
 		return true
 	}
+
+	total := 0
+	var err error
+	if u.IsOrganization() {
+		err = u.LoadMainOwner()
+		if err != nil {
+			log.Error("LoadMainOwner(): %v", err)
+			return false
+		}
+		if !u.MainOwner.CanCreateRepo() {
+			return false
+		}
+	} else {
+		total, err = u.TotalRepoNumInMainOwnedOrgs()
+		if err != nil {
+			log.Error("TotalRepoNumInMainOwnedOrgs(): %v", err)
+			return false
+		}
+	}
+
+	total += u.NumRepos
+
 	if u.MaxRepoCreation <= -1 {
 		if setting.Repository.MaxCreationLimit <= -1 {
 			return true
 		}
-		return u.NumRepos < setting.Repository.MaxCreationLimit
+		return total < setting.Repository.MaxCreationLimit
 	}
-	return u.NumRepos < u.MaxRepoCreation
+	return total < u.MaxRepoCreation
 }
 
 // CanCreateOrganization returns true if user can create organisation.
@@ -2035,3 +2069,10 @@ func IterateUser(f func(user *User) error) error {
 		}
 	}
 }
+
+// TotalRepoNumInMainOwnedOrgs calculate all repo number in org that
+// user main owned
+func (u *User) TotalRepoNumInMainOwnedOrgs() (int, error) {
+	total, err := x.Where("main_owner_id").SumInt(new(User), "num_repos")
+	return int(total), err
+}

Originally posted by @a1012112796 in #15924 (comment)

  • test issue todo list item
@a1012112796 a1012112796 added the type/proposal The new feature has not been accepted yet but needs to be discussed first. label May 22, 2021
@a1012112796
Copy link
Member Author

add some reasons of this isssue.

because of some reasons the manager need limit the number of repos that a user can create? yes now we have this feature, but maybe we forgot that if user can create a org, and the org can create repo,then this limit will become not so usefull...
so looks that maybe we should limit the number of orgs that one user can create? but how to calculate the number of orgs that one user created ?...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/proposal The new feature has not been accepted yet but needs to be discussed first.
Projects
None yet
Development

No branches or pull requests

1 participant