Skip to content

Commit

Permalink
[v9.3.x] Login: Fix failure to login a new user via an external provi…
Browse files Browse the repository at this point in the history
…der if quota are enabled (grafana#60086)

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

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

(cherry picked from commit 341d7b0)

Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
  • Loading branch information
2 people authored and GuaYounesPW committed Feb 8, 2023
1 parent f2a2961 commit f9d0371
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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

0 comments on commit f9d0371

Please sign in to comment.