fix(user pin service): fixed CanPin function to return an error

This commit is contained in:
Daniel Carvalho 2024-05-17 16:08:20 +00:00
parent 21400623ec
commit b941b04269
2 changed files with 17 additions and 7 deletions

View File

@ -1186,7 +1186,10 @@ func loadPinData(ctx *context.Context) error {
}
ctx.Data["IsPinningRepo"] = repo_model.IsPinned(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
ctx.Data["CanPinRepo"] = user_service.CanPin(ctx, ctx.Doer, ctx.Repo.Repository)
ctx.Data["CanPinRepo"], err = user_service.CanPin(ctx, ctx.Doer, ctx.Repo.Repository)
if err != nil {
return err
}
if ctx.Repo.Repository.Owner.IsOrganization() {
org := organization.OrgFromUser(ctx.Repo.Repository.Owner)
@ -1199,7 +1202,10 @@ func loadPinData(ctx *context.Context) error {
if isAdmin {
ctx.Data["CanUserPinToOrg"] = true
ctx.Data["IsOrgPinningRepo"] = repo_model.IsPinned(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.ID)
ctx.Data["CanOrgPinRepo"] = user_service.CanPin(ctx, ctx.Repo.Repository.Owner, ctx.Repo.Repository)
ctx.Data["CanOrgPinRepo"], err = user_service.CanPin(ctx, ctx.Repo.Repository.Owner, ctx.Repo.Repository)
if err != nil {
return err
}
}
}

View File

@ -18,7 +18,7 @@ const maxPins = 6
// Check if a user have a new pinned repo in it's profile, meaning that it
// has permissions to pin said repo and also has enough space on the pinned list.
func CanPin(ctx *context.Context, u *user_model.User, r *repo_model.Repository) bool {
func CanPin(ctx *context.Context, u *user_model.User, r *repo_model.Repository) (bool, error) {
repos, err := repo_model.GetPinnedRepos(*ctx, &repo_model.PinnedReposOptions{
ListOptions: db.ListOptions{
ListAll: true,
@ -27,14 +27,14 @@ func CanPin(ctx *context.Context, u *user_model.User, r *repo_model.Repository)
})
if err != nil {
ctx.ServerError("GetPinnedRepos", err)
return false
return false, err
}
if len(repos) >= maxPins {
return false
return false, nil
}
return HasPermsToPin(ctx, u, r)
return HasPermsToPin(ctx, u, r), nil
}
// Checks if the user has permission to have the repo pinned in it's profile.
@ -139,7 +139,11 @@ func PinRepo(ctx *context.Context, doer *user_model.User, repo *repo_model.Repos
}
if pin {
if !CanPin(ctx, targetUser, repo) {
canPin, err := CanPin(ctx, targetUser, repo)
if err != nil {
return err
}
if !canPin {
return errors.New("user cannot pin this repository")
}
}