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

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

Merged
merged 2 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/services/quota/model.go
Expand Up @@ -11,7 +11,9 @@ import (
var ErrBadRequest = errutil.NewBase(errutil.StatusBadRequest, "quota.bad-request")
var ErrInvalidTargetSrv = errutil.NewBase(errutil.StatusBadRequest, "quota.invalid-target")
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 ErrUsageFoundForTarget = errutil.NewBase(errutil.StatusNotFound, "quota.missing-target-usage")
var ErrTargetSrvConflict = errutil.NewBase(errutil.StatusBadRequest, "quota.target-srv-conflict")
var ErrDisabled = errutil.NewBase(errutil.StatusForbidden, "quota.disabled", errutil.WithPublicMessage("Quotas not enabled"))
var ErrInvalidTagFormat = errutil.NewBase(errutil.StatusInternal, "quota.invalid-invalid-tag-format")
Expand Down
18 changes: 16 additions & 2 deletions pkg/services/quota/quotaimpl/quota.go
Expand Up @@ -2,7 +2,6 @@ package quotaimpl

import (
"context"
"fmt"
"sync"

"github.com/grafana/grafana/pkg/infra/db"
Expand Down Expand Up @@ -205,9 +204,24 @@ func (s *service) CheckQuotaReached(ctx context.Context, targetSrv quota.TargetS
case limit == 0:
return true, nil
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)
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 {
return true, nil
Expand Down