Login: Fix failure to login a new user via an external provider if quota are enabled (#60015)

* Login: Fix failure to login a new user via an external provider if quota are enabled
This commit is contained in:
Sofia Papagiannaki 2022-12-09 13:30:27 +02:00 committed by GitHub
parent 6280780e0c
commit 341d7b01ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -11,7 +11,9 @@ import (
var ErrBadRequest = errutil.NewBase(errutil.StatusBadRequest, "quota.bad-request") var ErrBadRequest = errutil.NewBase(errutil.StatusBadRequest, "quota.bad-request")
var ErrInvalidTargetSrv = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-target") var ErrInvalidTargetSrv = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-target")
var ErrInvalidScope = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-scope") var ErrInvalidScope = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-scope")
var ErrFailedToGetScope = errutil.NewBase(errutil.StatusInternal, "quota.failed-get-scope")
var ErrInvalidTarget = errutil.NewBase(errutil.StatusInternal, "quota.invalid-target-table") var ErrInvalidTarget = errutil.NewBase(errutil.StatusInternal, "quota.invalid-target-table")
var ErrUsageFoundForTarget = errutil.NewBase(errutil.StatusNotFound, "quota.missing-target-usage")
var ErrTargetSrvConflict = errutil.NewBase(errutil.StatusBadRequest, "quota.target-srv-conflict") var ErrTargetSrvConflict = errutil.NewBase(errutil.StatusBadRequest, "quota.target-srv-conflict")
var ErrDisabled = errutil.NewBase(errutil.StatusForbidden, "quota.disabled", errutil.WithPublicMessage("Quotas not enabled")) var ErrDisabled = errutil.NewBase(errutil.StatusForbidden, "quota.disabled", errutil.WithPublicMessage("Quotas not enabled"))
var ErrInvalidTagFormat = errutil.NewBase(errutil.StatusInternal, "quota.invalid-invalid-tag-format") var ErrInvalidTagFormat = errutil.NewBase(errutil.StatusInternal, "quota.invalid-invalid-tag-format")

View File

@ -2,7 +2,6 @@ package quotaimpl
import ( import (
"context" "context"
"fmt"
"sync" "sync"
"github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/infra/db"
@ -205,9 +204,24 @@ func (s *service) CheckQuotaReached(ctx context.Context, targetSrv quota.TargetS
case limit == 0: case limit == 0:
return true, nil return true, nil
default: default:
scope, err := t.GetScope()
if err != nil {
return false, quota.ErrFailedToGetScope.Errorf("failed to get the scope for target: %s", t)
}
// do not check user quota if the user information is not available (eg no user is signed in)
if scope == quota.UserScope && (scopeParams == nil || scopeParams.UserID == 0) {
continue
}
// do not check user quota if the org information is not available (eg no user is signed in)
if scope == quota.OrgScope && (scopeParams == nil || scopeParams.OrgID == 0) {
continue
}
u, ok := targetUsage.Get(t) u, ok := targetUsage.Get(t)
if !ok { if !ok {
return false, fmt.Errorf("no usage for target:%s", t) return false, quota.ErrUsageFoundForTarget.Errorf("no usage for target:%s", t)
} }
if u >= limit { if u >= limit {
return true, nil return true, nil