From b2bb0f3e22e62d3c2f4d8f2e192030b4f135caf1 Mon Sep 17 00:00:00 2001 From: Xinyu Zhou Date: Thu, 17 Nov 2022 19:37:52 +0800 Subject: [PATCH 01/11] Prohibit fork if user reached maximum limit of repositories fixed https://github.com/go-gitea/gitea/issues/21847 Signed-off-by: Xinyu Zhou --- routers/api/v1/repo/fork.go | 2 +- routers/web/repo/pull.go | 10 ++++++++++ services/repository/fork.go | 7 +++++++ templates/repo/pulls/fork.tmpl | 9 ++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/fork.go b/routers/api/v1/repo/fork.go index 112a9562f0fdf..e4acda915f7a4 100644 --- a/routers/api/v1/repo/fork.go +++ b/routers/api/v1/repo/fork.go @@ -142,7 +142,7 @@ func CreateFork(ctx *context.APIContext) { Description: repo.Description, }) if err != nil { - if repo_model.IsErrRepoAlreadyExist(err) { + if repo_model.IsErrReachLimitOfRepo(err) || repo_model.IsErrRepoAlreadyExist(err) { ctx.Error(http.StatusConflict, "ForkRepository", err) } else { ctx.Error(http.StatusInternalServerError, "ForkRepository", err) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 41eac7cc39232..e337dec207d97 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -183,6 +183,9 @@ func getForkRepository(ctx *context.Context) *repo_model.Repository { func Fork(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("new_fork") + ctx.Data["CanCreateRepo"] = ctx.Doer.CanCreateRepo() + ctx.Data["MaxCreationLimit"] = ctx.Doer.MaxCreationLimit() + getForkRepository(ctx) if ctx.Written() { return @@ -196,6 +199,9 @@ func ForkPost(ctx *context.Context) { form := web.GetForm(ctx).(*forms.CreateRepoForm) ctx.Data["Title"] = ctx.Tr("new_fork") + ctx.Data["CanCreateRepo"] = ctx.Doer.CanCreateRepo() + ctx.Data["MaxCreationLimit"] = ctx.Doer.MaxCreationLimit() + ctxUser := checkContextUser(ctx, form.UID) if ctx.Written() { return @@ -255,6 +261,10 @@ func ForkPost(ctx *context.Context) { if err != nil { ctx.Data["Err_RepoName"] = true switch { + case repo_model.IsErrReachLimitOfRepo(err): + maxCreationLimit := ctxUser.MaxCreationLimit() + msg := ctx.TrN(maxCreationLimit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", maxCreationLimit) + ctx.RenderWithErr(msg, tplFork, &form) case repo_model.IsErrRepoAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplFork, &form) case db.IsErrNameReserved(err): diff --git a/services/repository/fork.go b/services/repository/fork.go index e597bfa449d4a..dee3e77c2b40e 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -52,6 +52,13 @@ type ForkRepoOptions struct { // ForkRepository forks a repository func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts ForkRepoOptions) (*repo_model.Repository, error) { + // Fork is prohibited, if user has reached maximum limit of repositories + if !owner.CanCreateRepo() { + return nil, repo_model.ErrReachLimitOfRepo{ + Limit: owner.MaxRepoCreation, + } + } + forkedRepo, err := repo_model.GetUserFork(ctx, opts.BaseRepo.ID, owner.ID) if err != nil { return nil, err diff --git a/templates/repo/pulls/fork.tmpl b/templates/repo/pulls/fork.tmpl index 0172e1b708298..e89ac07986110 100644 --- a/templates/repo/pulls/fork.tmpl +++ b/templates/repo/pulls/fork.tmpl @@ -9,6 +9,13 @@
{{template "base/alert" .}} + + {{if not .CanCreateRepo}} +
+

{{.locale.TrN .MaxCreationLimit "repo.form.reach_limit_of_creation_1" "repo.form.reach_limit_of_creation_n" .MaxCreationLimit}}

+
+ {{end}} +