Commit 6fbc07f7 authored by Matthieu Fesselier's avatar Matthieu Fesselier
Browse files

feature: load permissions async

parent d6376257
Pipeline #11922 passed with stage
in 7 minutes and 10 seconds
......@@ -13,4 +13,9 @@ describe('solid-ac-checker', function() {
cy.get('#ac-checker-2').should('not.have.attr', 'hidden');
cy.get('#test2').should('be.visible');
});
it('fetches permissions', () => {
cy.get('#load').click();
cy.get('#ac-checker-3').should('not.have.attr', 'hidden');
cy.get('#test3').should('be.visible');
});
})
\ No newline at end of file
......@@ -18,13 +18,7 @@
"@id": "/examples/data/list/skill-2.jsonld",
"name": "CSS",
"order": 5,
"@type": "hd:skill",
"permissions": [
{ "mode": { "@type": "view" }},
{ "mode": { "@type": "change" }},
{ "mode": { "@type": "control" }},
{ "mode": { "@type": "delete" }}
]
"@type": "hd:skill"
},
{
"@id": "/examples/data/list/skill-3.jsonld",
......
......@@ -33,5 +33,22 @@
></solid-display>
</solid-ac-checker>
</div>
<div>
<h3>Fetch permissions</h3>
<solid-display
data-src="/examples/data/list/skills.jsonld"
fields="name"
></solid-display>
<button id="load">Load data</button>
<solid-ac-checker id="ac-checker-3" permission="acl:Read" data-src>
<div id="test3">OK</div>
</solid-ac-checker>
</div>
<script>
load.onclick= () => {
document.querySelector('#ac-checker-3').dataset.src = "/examples/data/list/skill-2.jsonld";
}
</script>
</body>
</html>
......@@ -22,23 +22,16 @@ export const SolidAcChecker = {
const ContextParser = JSONLDContextParser.ContextParser;
const myParser = new ContextParser();
const context = await myParser.parse(this.context);
const permissions = await this.resource.permissions;
if (this.permission) { // User has permission of ...
displayElement = this.resource.permissions.some((p:any) => {
displayElement = permissions.some((p:any) => {
return ContextParser.compactIri(p, context) === this.permission;
});
/* displayElement = await asyncSome(
(permission: object) => ContextParser.compactIri(permission.toString(), context) === this.permission,
this.resource.permissions.mode['@type']
)*/
} else if (this.noPermission) { // User has no permission of ...
displayElement = this.resource.permissions.every((p:any) => {
displayElement = permissions.every((p:any) => {
return ContextParser.compactIri(p, context) !== this.noPermission;
});
/*displayElement = await asyncEvery(
(permission: object) => ContextParser.compactIri(permission.toString(), context) !== this.noPermission,
this.resource.permissions.mode['@type']
)*/
} else { // No parameter provided
console.warn('solid-ac-checker: you should define at least one of "permission" or "no-permission" attribute.');
return;
......
......@@ -45,16 +45,18 @@ class Store {
* @param id - uri of the resource to fetch
* @param context - context used to expand id and predicates when accessing the resource
* @param idParent - uri of the parent caller used to expand uri for local files
* @param localData - data to put in cache
* @param forceFetch - force the fetch of data
*
* @returns The fetched resource
*
* @async
*/
async getData(id: string, context:any = {}, idParent = "", localData?: object): Promise<Resource|null> {
async getData(id: string, context:any = {}, idParent = "", localData?: object, forceFetch: boolean = false): Promise<Resource|null> {
if (localData == null && this.cache.has(id) && !this.loadingList.has(id)) {
const resource = this.get(id);
if (resource && resource.isFullResource()) return resource; // if resource is not complete, re-fetch it
if (resource && resource.isFullResource() && !forceFetch) return resource; // if resource is not complete, re-fetch it
}
return new Promise(async (resolve) => {
......@@ -542,9 +544,10 @@ class CustomGetter {
* @param id
* @param context
* @param iriParent
* @param forceFetch
*/
async getResource(id: string, context: object, iriParent: string): Promise<Resource | null> {
return store.getData(id, context, iriParent);
async getResource(id: string, context: object, iriParent: string, forceFetch: boolean = false): Promise<Resource | null> {
return store.getData(id, context, iriParent, undefined, forceFetch);
}
/**
......@@ -617,8 +620,13 @@ class CustomGetter {
return Object.keys(this.resource).filter(p => !p.startsWith('@')).length > 0;
}
getPermissions(): string[] {
const permissions = this.resource[this.getExpandedPredicate("permissions")];
async getPermissions(): Promise<string[]> {
const permissionPredicate = this.getExpandedPredicate("permissions");
let permissions = this.resource[permissionPredicate];
if (!permissions) { // if no permission, re-fetch data
await this.getResource(this.resourceId, this.clientContext, this.parentId, true);
permissions = this.resource[permissionPredicate];
}
return permissions ? permissions.map(perm => ContextParser.expandTerm(perm.mode['@type'], this.serverContext, true)) : [];
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment