Skip to content

fix ui bug when user can't create repo but org can #15924

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

Closed
wants to merge 1 commit into from

Conversation

a1012112796
Copy link
Member

fix #15504

@codecov-commenter

This comment was marked as outdated.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 19, 2021
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels May 20, 2021
@techknowlogick techknowlogick added this to the 1.15.0 milestone May 20, 2021
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 20, 2021
Copy link
Member

@techknowlogick techknowlogick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking per chat in case this may be a breaking change.

@noerw noerw added the pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! label May 20, 2021
@a1012112796
Copy link
Member Author

Blocking per chat in case this may be a breaking change.

@techknowlogick Would you please give more detail? Thanks.

@a1012112796 a1012112796 added the status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR label May 21, 2021
@noerw
Copy link
Member

noerw commented May 21, 2021

@a1012112796 I raised that concern on discord:
It's not documented if the old behaviour is by design or a bug, but I seem to remember that the behaviour was like this since at least 1.12.x. (But I may be wrong, didn't verify with such an old release yet.)

Changing the behaviour would be a breaking change in permission handling, just take gitea.com for example: policy would need to be changed that users can't create organizations in order to keep the 5-repos-per-user limitation.

@noerw
Copy link
Member

noerw commented May 21, 2021

Ok I stand corrected:
Up to gitea 1.13.7 limited users were able to create repos in orgs (the error on the repo-create page was displayed, but the submit button not disabled, the repo creation goes through). Due to the displayed error message it seems like unintended behaviour to me that users can still create repos, but who knows 🤔
So maybe this isn't a breaking change after all.

edit: #12492 introduced the disabled button, but didn't touch on this topic either

@a1012112796
Copy link
Member Author

a1012112796 commented May 22, 2021

@noerw I see, so you means the org's repos number should be calculate as owner's repo, then if owner can't create repo, his org also can't create repo, Isn't it?
But in fact a org maybe have more than one owner, how to handle it?
Add sadly gitea hasn't release this feature.

In fact, gitea only check the org's repo number limt when create org repo, see here:

// CreateRepository creates a repository for the user/organization.
func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*models.Repository, error) {
if !doer.IsAdmin && !u.CanCreateRepo() {
return nil, models.ErrReachLimitOfRepo{
Limit: u.MaxRepoCreation,
}
}

and this pull request just fix ui logic to make it same with post handle, hasn't change the repo create logic in post handle. I wonder why it breake this logic ...
That's just my thought, feel sorry if I missunderstood you, Thanks.

@a1012112796

This comment has been minimized.

a1012112796 added a commit to a1012112796/gitea that referenced this pull request May 22, 2021
follow go-gitea#12492, do same thing on repo migrate page
- readd ``form.reach_limit_of_creation`` because it's still be used
- part of go-gitea#15924 also

Signed-off-by: a1012112796 <[email protected]>
@lunny
Copy link
Member

lunny commented May 25, 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
+}

This still be a wired design and I cannot accept that.

@a1012112796
Copy link
Member Author

@lunny That's out of the topic of this pull request. now I think we should make sure when a user can't create repo, should we allow this user to create repo for the org which can create repo? if yes, It's okay to merge this pull request, or it's a big problem and need rewrite many things ...

@Commifreak
Copy link

Anything new to that?

@lunny
Copy link
Member

lunny commented May 16, 2022

Please resolve the conflicts.

@a1012112796 a1012112796 force-pushed the ui_create_repo_bug branch from 379c7e5 to 383e713 Compare May 17, 2022 14:31
@wxiaoguang
Copy link
Contributor

@techknowlogick there is one requested change from you. What should be done to continue?

if !canCreateRepo && ctx.Doer.ID == ctxUser.ID {
orgs, has := ctx.Data["Orgs"].([]*organization.Organization)
if has && len(orgs) > 0 {
ctxUser = orgs[0].AsUser()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the first organizaiton?

Copy link
Member Author

@a1012112796 a1012112796 May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because curent user can't create repo, and user hasn't choose an organizaiton by query option, so use the the first organizaiton as default.
image

@lunny
Copy link
Member

lunny commented May 18, 2022

I think the logic is still confusing.

@a1012112796 a1012112796 force-pushed the ui_create_repo_bug branch from 383e713 to 3f0ed55 Compare May 18, 2022 11:56
@wxiaoguang wxiaoguang modified the milestones: 1.17.0, 1.18.0 Jun 15, 2022
@wxiaoguang
Copy link
Contributor

Asked by @blackshot , #15504 (comment)

is it possible to improve this PR and get it merged in 1.17?

@a1012112796
Copy link
Member Author

a1012112796 commented Jun 16, 2022

But looks noone think this pull request is right. so I'd like close it.

@a1012112796 a1012112796 removed this from the 1.18.0 milestone Jun 16, 2022
@wxiaoguang
Copy link
Contributor

Hmm ... I haven't look into the details for it, just saw some discussions above, no idea about what is right.

@a1012112796 a1012112796 deleted the ui_create_repo_bug branch January 7, 2023 07:26
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! status/blocked This PR cannot be merged yet, i.e. because it depends on another unmerged PR type/bug
Projects
None yet
10 participants