Skip to content

Commit

Permalink
docs(abac): update CheckRequestViewer component to account for context
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-whit committed Dec 7, 2023
1 parent 94d7cec commit 0f60c6f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/content/modeling/abac-conditions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Now that we have written a [Conditional Relationship Tuple](../concepts#what-is-
user={'user:anne'}
relation={'viewer'}
object={'document:1'}
context={{current_time: "2023-01-01T00:09:50Z"}}
allowed={true}
skipSetup={true}
allowedLanguages={[
Expand All @@ -162,6 +163,7 @@ but if the current time is outside the grant window then you get a deny decision
user={'user:anne'}
relation={'viewer'}
object={'document:1'}
context={{current_time: "2023-01-01T00:10:01Z"}}
allowed={false}
skipSetup={true}
allowedLanguages={[
Expand Down
19 changes: 6 additions & 13 deletions src/components/Docs/SnippetViewer/CheckRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ interface CheckRequestViewerOpts {
object: string;
allowed: boolean;
contextualTuples?: TupleKey[];
context?:Record<string, any>;
context?: Record<string, any>;
skipSetup?: boolean;
allowedLanguages?: SupportedLanguage[];
}

function checkRequestViewer(lang: SupportedLanguage, opts: CheckRequestViewerOpts): string {
const { user, relation, object, allowed, contextualTuples } = opts;
const { user, relation, object, allowed, contextualTuples, context } = opts;
const modelId = opts.authorizationModelId ? opts.authorizationModelId : DefaultAuthorizationModelId;

switch (lang) {
Expand Down Expand Up @@ -66,21 +66,14 @@ const { allowed } = await fgaClient.check({
relation: '${relation}',
object: '${object}',${
!contextualTuples
? `
`
? ``
: `
contextual_tuples: [${contextualTuples
contextual_tuples: [\n ${contextualTuples
.map(
(tuple) => `
{
user: "${tuple.user}",
relation: "${tuple.relation}",
object: "${tuple.object}"
}`,
(tuple) => `${JSON.stringify(tuple)}`
)
.join(',')}
]`
}}, {
],`}${!context ? `\n }` : `,\n context: ${JSON.stringify(context)}\n }` }, {
authorization_model_id: '${modelId}',
});
Expand Down

0 comments on commit 0f60c6f

Please sign in to comment.