Check performs a permission check for a given request, using the cached results if available.
(ctx context.Context, request *base.PermissionCheckRequest)
| 45 | |
| 46 | // Check performs a permission check for a given request, using the cached results if available. |
| 47 | func (c *CheckEngineWithCache) Check(ctx context.Context, request *base.PermissionCheckRequest) (response *base.PermissionCheckResponse, err error) { |
| 48 | // Retrieve entity definition |
| 49 | var en *base.EntityDefinition |
| 50 | en, _, err = c.schemaReader.ReadEntityDefinition(ctx, request.GetTenantId(), request.GetEntity().GetType(), request.GetMetadata().GetSchemaVersion()) |
| 51 | if err != nil { |
| 52 | return &base.PermissionCheckResponse{ |
| 53 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
| 54 | Metadata: &base.PermissionCheckResponseMetadata{ |
| 55 | CheckCount: 0, |
| 56 | }, |
| 57 | }, err |
| 58 | } |
| 59 | |
| 60 | isRelational := engines.IsRelational(en, request.GetPermission()) |
| 61 | |
| 62 | // Try to get the cached result for the given request. |
| 63 | res, found := c.getCheckKey(request, isRelational) |
| 64 | |
| 65 | // If a cached result is found, handle exclusion and return the result. |
| 66 | if found { |
| 67 | // Increase the hit count in the metrics. |
| 68 | c.cacheHitHistogram.Record(ctx, 1) |
| 69 | |
| 70 | // If the request doesn't have the exclusion flag set, return the cached result. |
| 71 | return &base.PermissionCheckResponse{ |
| 72 | Can: res.GetCan(), |
| 73 | Metadata: &base.PermissionCheckResponseMetadata{}, |
| 74 | }, nil |
| 75 | } |
| 76 | |
| 77 | // Perform the actual permission check using the provided request. |
| 78 | cres, err := c.checker.Check(ctx, request) |
| 79 | // Check if there's an error or the response is nil, and return the result. |
| 80 | if err != nil { |
| 81 | return &base.PermissionCheckResponse{ |
| 82 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
| 83 | Metadata: &base.PermissionCheckResponseMetadata{ |
| 84 | CheckCount: 0, |
| 85 | }, |
| 86 | }, err |
| 87 | } |
| 88 | |
| 89 | // Add to histogram the response |
| 90 | |
| 91 | c.setCheckKey(request, &base.PermissionCheckResponse{ |
| 92 | Can: cres.GetCan(), |
| 93 | Metadata: &base.PermissionCheckResponseMetadata{}, |
| 94 | }, isRelational) |
| 95 | // Return the result of the permission check. |
| 96 | return cres, err |
| 97 | } |
| 98 | |
| 99 | // GetCheckKey retrieves the value for the given key from the EngineKeys cache. |
| 100 | // It returns the PermissionCheckResponse if the key is found, and a boolean value |
nothing calls this directly
no test coverage detected