@backstage/plugin-permission-react 0.4.40 → 0.4.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @backstage/plugin-permission-react
|
|
2
2
|
|
|
3
|
+
## 0.4.41
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 5fec07d: Permission checks made in the same tick are now batched into a single call to the permission backend.
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @backstage/core-plugin-api@1.12.4
|
|
10
|
+
- @backstage/plugin-permission-common@0.9.7
|
|
11
|
+
|
|
12
|
+
## 0.4.41-next.0
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies
|
|
17
|
+
- @backstage/config@1.3.6
|
|
18
|
+
- @backstage/core-plugin-api@1.12.4-next.0
|
|
19
|
+
- @backstage/plugin-permission-common@0.9.6
|
|
20
|
+
|
|
3
21
|
## 0.4.40
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
import DataLoader from 'dataloader';
|
|
1
2
|
import { PermissionClient } from '@backstage/plugin-permission-common';
|
|
2
3
|
|
|
3
4
|
class IdentityPermissionApi {
|
|
4
|
-
|
|
5
|
-
identityApi;
|
|
5
|
+
loader;
|
|
6
6
|
constructor(permissionClient, identityApi) {
|
|
7
|
-
this.
|
|
8
|
-
|
|
7
|
+
this.loader = new DataLoader(
|
|
8
|
+
async (requests) => {
|
|
9
|
+
const credentials = await identityApi.getCredentials();
|
|
10
|
+
return permissionClient.authorize([...requests], credentials);
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
cache: false
|
|
14
|
+
}
|
|
15
|
+
);
|
|
9
16
|
}
|
|
10
17
|
static create(options) {
|
|
11
18
|
const { config, discovery, identity } = options;
|
|
@@ -13,11 +20,7 @@ class IdentityPermissionApi {
|
|
|
13
20
|
return new IdentityPermissionApi(permissionClient, identity);
|
|
14
21
|
}
|
|
15
22
|
async authorize(request) {
|
|
16
|
-
|
|
17
|
-
[request],
|
|
18
|
-
await this.identityApi.getCredentials()
|
|
19
|
-
);
|
|
20
|
-
return response[0];
|
|
23
|
+
return await this.loader.load(request);
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
26
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IdentityPermissionApi.esm.js","sources":["../../src/apis/IdentityPermissionApi.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizePermissionRequest,\n AuthorizePermissionResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which
|
|
1
|
+
{"version":3,"file":"IdentityPermissionApi.esm.js","sources":["../../src/apis/IdentityPermissionApi.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport DataLoader from 'dataloader';\nimport { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';\nimport { PermissionApi } from './PermissionApi';\nimport {\n AuthorizePermissionRequest,\n AuthorizePermissionResponse,\n PermissionClient,\n} from '@backstage/plugin-permission-common';\nimport { Config } from '@backstage/config';\n\n/**\n * The default implementation of the PermissionApi, which batches calls to\n * {@link @backstage/plugin-permission-common#PermissionClient} that are made\n * within the same microtask into a single HTTP request.\n * @public\n */\nexport class IdentityPermissionApi implements PermissionApi {\n private readonly loader: DataLoader<\n AuthorizePermissionRequest,\n AuthorizePermissionResponse\n >;\n\n private constructor(\n permissionClient: PermissionClient,\n identityApi: IdentityApi,\n ) {\n this.loader = new DataLoader(\n async (requests: readonly AuthorizePermissionRequest[]) => {\n const credentials = await identityApi.getCredentials();\n return permissionClient.authorize([...requests], credentials);\n },\n {\n cache: false,\n },\n );\n }\n\n static create(options: {\n config: Config;\n discovery: DiscoveryApi;\n identity: IdentityApi;\n }) {\n const { config, discovery, identity } = options;\n const permissionClient = new PermissionClient({ discovery, config });\n return new IdentityPermissionApi(permissionClient, identity);\n }\n\n async authorize(\n request: AuthorizePermissionRequest,\n ): Promise<AuthorizePermissionResponse>;\n async authorize(\n request: AuthorizePermissionRequest,\n ): Promise<AuthorizePermissionResponse> {\n return await this.loader.load(request);\n }\n}\n"],"names":[],"mappings":";;;AAgCO,MAAM,qBAAA,CAA+C;AAAA,EACzC,MAAA;AAAA,EAKT,WAAA,CACN,kBACA,WAAA,EACA;AACA,IAAA,IAAA,CAAK,SAAS,IAAI,UAAA;AAAA,MAChB,OAAO,QAAA,KAAoD;AACzD,QAAA,MAAM,WAAA,GAAc,MAAM,WAAA,CAAY,cAAA,EAAe;AACrD,QAAA,OAAO,iBAAiB,SAAA,CAAU,CAAC,GAAG,QAAQ,GAAG,WAAW,CAAA;AAAA,MAC9D,CAAA;AAAA,MACA;AAAA,QACE,KAAA,EAAO;AAAA;AACT,KACF;AAAA,EACF;AAAA,EAEA,OAAO,OAAO,OAAA,EAIX;AACD,IAAA,MAAM,EAAE,MAAA,EAAQ,SAAA,EAAW,QAAA,EAAS,GAAI,OAAA;AACxC,IAAA,MAAM,mBAAmB,IAAI,gBAAA,CAAiB,EAAE,SAAA,EAAW,QAAQ,CAAA;AACnE,IAAA,OAAO,IAAI,qBAAA,CAAsB,gBAAA,EAAkB,QAAQ,CAAA;AAAA,EAC7D;AAAA,EAKA,MAAM,UACJ,OAAA,EACsC;AACtC,IAAA,OAAO,MAAM,IAAA,CAAK,MAAA,CAAO,IAAA,CAAK,OAAO,CAAA;AAAA,EACvC;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePermission.esm.js","sources":["../../src/hooks/usePermission.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n isResourcePermission,\n Permission,\n ResourcePermission,\n} from '@backstage/plugin-permission-common';\nimport useSWR from 'swr';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utility for authorization. Given either a non-resource\n * {@link @backstage/plugin-permission-common#Permission} or a\n * {@link @backstage/plugin-permission-common#ResourcePermission} and an\n * optional resourceRef, it will return whether or not access is allowed (for\n * the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for\n * more details.\n *\n * The resourceRef field is optional to allow calling this hook with an\n * entity that might be loading asynchronously, but when resourceRef is not\n * supplied, the value of `allowed` will always be false.\n *\n * Note: This hook uses stale-while-revalidate to help avoid flicker in UI\n * elements that would be conditionally rendered based on the `allowed` result\n * of this hook.\n * @public\n */\nexport function usePermission(\n input:\n | {\n permission: Exclude<Permission, ResourcePermission>;\n resourceRef?: never;\n }\n | {\n permission: ResourcePermission;\n resourceRef: string | undefined;\n },\n): AsyncPermissionResult {\n const permissionApi = useApi(permissionApiRef);\n const { data, error } = useSWR(input, async (args: typeof input) => {\n // We could make the resourceRef parameter required to avoid this check, but\n // it would make using this hook difficult in situations where the entity\n // must be asynchronously loaded, so instead we short-circuit to a deny when\n // no resourceRef is supplied, on the assumption that the resourceRef is\n // still loading outside the hook.\n if (isResourcePermission(args.permission) && !args.resourceRef) {\n return AuthorizeResult.DENY;\n }\n\n const { result } = await permissionApi.authorize(args);\n return result;\n });\n\n if (error) {\n return { error, loading: false, allowed: false };\n }\n if (data === undefined) {\n return { loading: true, allowed: false };\n }\n return { loading: false, allowed: data === AuthorizeResult.ALLOW };\n}\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePermission.esm.js","sources":["../../src/hooks/usePermission.ts"],"sourcesContent":["/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useApi } from '@backstage/core-plugin-api';\nimport { permissionApiRef } from '../apis';\nimport {\n AuthorizeResult,\n isResourcePermission,\n Permission,\n ResourcePermission,\n} from '@backstage/plugin-permission-common';\nimport useSWR from 'swr';\n\n/** @public */\nexport type AsyncPermissionResult = {\n loading: boolean;\n allowed: boolean;\n error?: Error;\n};\n\n/**\n * React hook utility for authorization. Given either a non-resource\n * {@link @backstage/plugin-permission-common#Permission} or a\n * {@link @backstage/plugin-permission-common#ResourcePermission} and an\n * optional resourceRef, it will return whether or not access is allowed (for\n * the given resource, if resourceRef is provided). See\n * {@link @backstage/plugin-permission-common/PermissionClient#authorize} for\n * more details.\n *\n * The resourceRef field is optional to allow calling this hook with an\n * entity that might be loading asynchronously, but when resourceRef is not\n * supplied, the value of `allowed` will always be false.\n *\n * Note: This hook uses stale-while-revalidate to help avoid flicker in UI\n * elements that would be conditionally rendered based on the `allowed` result\n * of this hook.\n * @public\n */\nexport function usePermission(\n input:\n | {\n permission: Exclude<Permission, ResourcePermission>;\n resourceRef?: never;\n }\n | {\n permission: ResourcePermission;\n resourceRef: string | undefined;\n },\n): AsyncPermissionResult {\n const permissionApi = useApi(permissionApiRef);\n const { data, error } = useSWR(input, async (args: typeof input) => {\n // We could make the resourceRef parameter required to avoid this check, but\n // it would make using this hook difficult in situations where the entity\n // must be asynchronously loaded, so instead we short-circuit to a deny when\n // no resourceRef is supplied, on the assumption that the resourceRef is\n // still loading outside the hook.\n if (isResourcePermission(args.permission) && !args.resourceRef) {\n return AuthorizeResult.DENY;\n }\n\n const { result } = await permissionApi.authorize(args);\n return result;\n });\n\n if (error) {\n return { error, loading: false, allowed: false };\n }\n if (data === undefined) {\n return { loading: true, allowed: false };\n }\n return { loading: false, allowed: data === AuthorizeResult.ALLOW };\n}\n"],"names":[],"mappings":";;;;;;AAmDO,SAAS,cACd,KAAA,EASuB;AACvB,EAAA,MAAM,aAAA,GAAgB,OAAO,gBAAgB,CAAA;AAC7C,EAAA,MAAM,EAAE,IAAA,EAAM,KAAA,KAAU,MAAA,CAAO,KAAA,EAAO,OAAO,IAAA,KAAuB;AAMlE,IAAA,IAAI,qBAAqB,IAAA,CAAK,UAAU,CAAA,IAAK,CAAC,KAAK,WAAA,EAAa;AAC9D,MAAA,OAAO,eAAA,CAAgB,IAAA;AAAA,IACzB;AAEA,IAAA,MAAM,EAAE,MAAA,EAAO,GAAI,MAAM,aAAA,CAAc,UAAU,IAAI,CAAA;AACrD,IAAA,OAAO,MAAA;AAAA,EACT,CAAC,CAAA;AAED,EAAA,IAAI,KAAA,EAAO;AACT,IAAA,OAAO,EAAE,KAAA,EAAO,OAAA,EAAS,KAAA,EAAO,SAAS,KAAA,EAAM;AAAA,EACjD;AACA,EAAA,IAAI,SAAS,MAAA,EAAW;AACtB,IAAA,OAAO,EAAE,OAAA,EAAS,IAAA,EAAM,OAAA,EAAS,KAAA,EAAM;AAAA,EACzC;AACA,EAAA,OAAO,EAAE,OAAA,EAAS,KAAA,EAAO,OAAA,EAAS,IAAA,KAAS,gBAAgB,KAAA,EAAM;AACnE;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -105,13 +105,13 @@ type PermissionApi = {
|
|
|
105
105
|
declare const permissionApiRef: ApiRef<PermissionApi>;
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
* The default implementation of the PermissionApi, which
|
|
109
|
-
* {@link @backstage/plugin-permission-common#PermissionClient}
|
|
108
|
+
* The default implementation of the PermissionApi, which batches calls to
|
|
109
|
+
* {@link @backstage/plugin-permission-common#PermissionClient} that are made
|
|
110
|
+
* within the same microtask into a single HTTP request.
|
|
110
111
|
* @public
|
|
111
112
|
*/
|
|
112
113
|
declare class IdentityPermissionApi implements PermissionApi {
|
|
113
|
-
private readonly
|
|
114
|
-
private readonly identityApi;
|
|
114
|
+
private readonly loader;
|
|
115
115
|
private constructor();
|
|
116
116
|
static create(options: {
|
|
117
117
|
config: Config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-permission-react",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.41",
|
|
4
4
|
"backstage": {
|
|
5
5
|
"role": "web-library",
|
|
6
6
|
"pluginId": "permission",
|
|
@@ -43,13 +43,14 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@backstage/config": "^1.3.6",
|
|
46
|
-
"@backstage/core-plugin-api": "^1.12.
|
|
47
|
-
"@backstage/plugin-permission-common": "^0.9.
|
|
46
|
+
"@backstage/core-plugin-api": "^1.12.4",
|
|
47
|
+
"@backstage/plugin-permission-common": "^0.9.7",
|
|
48
|
+
"dataloader": "^2.0.0",
|
|
48
49
|
"swr": "^2.0.0"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
|
-
"@backstage/cli": "^0.
|
|
52
|
-
"@backstage/test-utils": "^1.7.
|
|
52
|
+
"@backstage/cli": "^0.36.0",
|
|
53
|
+
"@backstage/test-utils": "^1.7.16",
|
|
53
54
|
"@testing-library/jest-dom": "^6.0.0",
|
|
54
55
|
"@testing-library/react": "^16.0.0",
|
|
55
56
|
"@types/react": "^18.0.0",
|