Skip to content

Commit e7b033e

Browse files
committed
Created function to test ACL or ACP system and accord function calls accordingly
1 parent 6c52731 commit e7b033e

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

src/shell/commands/perms.ts

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { Command } from 'commander';
22
import * as acl_perms from '../../commands/solid-perms_acl';
33
import { setPermission, listPermissions, IPermissionOperation, IPermissionListing } from '../../commands/solid-perms';
44
import authenticate from '../../authentication/authenticate';
5-
import { addEnvOptions, changeUrlPrefixes, getAndNormalizeURL } from '../../utils/shellutils';
6-
import { writeErrorString } from '../../utils/util';
5+
import { addEnvOptions, changeUrlPrefixes } from '../../utils/shellutils';
6+
import { discoverAccessMechanism, writeErrorString } from '../../utils/util';
77
import chalk from 'chalk';
88
import SolidCommand from './SolidCommand';
9-
import list from '../../commands/solid-list';
9+
import { acp_ess_2, hasAccessibleAcl, WithAcl } from "@inrupt/solid-client";
1010
const Table = require('cli-table');
1111

1212
export default class PermsCommand extends SolidCommand {
@@ -32,26 +32,36 @@ export default class PermsCommand extends SolidCommand {
3232
const authenticationInfo = await authenticate(programOpts)
3333
options.fetch = authenticationInfo.fetch
3434
url = await changeUrlPrefixes(authenticationInfo, url)
35-
// try WAC
36-
try {
37-
const listings = await acl_perms.listPermissions(url, options)
38-
if (listings?.access.agent || listings?.access.public) {
39-
await formatACLPermissionListing(url, listings, options)
40-
return;
41-
}
42-
} catch (e) {
43-
if (options.verbose) writeErrorString('Unable to list permissions for WAC', e, options)
35+
36+
37+
const { acp, acl } = await discoverAccessMechanism(url, options.fetch)
38+
if ( !acp && !acl ) {
39+
if (options.verbose) writeErrorString(`Could not list permissions for ${url}`, { message: "Could not find attached WAC or ACP management resource." }, options)
40+
return;
4441
}
45-
// try Universal
46-
try {
47-
const listings = await listPermissions(url, options)
48-
if (listings?.access.agent || listings?.access.public) {
49-
await formatPermissionListing(url, listings, options)
50-
return;
42+
if (acl) {
43+
try {
44+
const listings = await acl_perms.listPermissions(url, options)
45+
if (listings?.access.agent || listings?.access.public) {
46+
await formatACLPermissionListing(url, listings, options)
47+
return;
48+
}
49+
} catch (e) {
50+
if (options.verbose) writeErrorString('Unable to list permissions for WAC', e, options)
5151
}
52-
} catch (e) {
53-
if (options.verbose) writeErrorString('Unable to list permissions for ACP', e, options)
5452
}
53+
if (acp) {
54+
try {
55+
const listings = await listPermissions(url, options)
56+
if (listings?.access.agent || listings?.access.public) {
57+
await formatPermissionListing(url, listings, options)
58+
return;
59+
}
60+
} catch (e) {
61+
if (options.verbose) writeErrorString('Unable to list permissions for ACP', e, options)
62+
}
63+
}
64+
5565
if (this.mayExit) process.exit(0)
5666
})
5767

@@ -66,9 +76,8 @@ export default class PermsCommand extends SolidCommand {
6676
For the current authenticated user please set id to "u".
6777
For specific agents, set id to be the agent webid.
6878
`)
69-
.option('--acl', 'Enables ACL specific operations --default and --group')
70-
.option('--default', 'Set the defined permissions as default (only in --acl mode)')
71-
.option('--group', 'Process identifier as a group identifier (only in --acl mode)')
79+
.option('--default', 'Set the defined permissions as default (only for pods on a WAC-based solid server)')
80+
.option('--group', 'Process identifier as a group identifier (only for pods on a WAC-based solid server)')
7281
.option('-v, --verbose', 'Log all operations') // Should this be default?
7382
.action( async (url: string, permissions: string[], options: any) => {
7483

@@ -109,18 +118,27 @@ export default class PermsCommand extends SolidCommand {
109118
return ({ type, id, read, write, append, control, default: def } as IPermissionOperation)
110119
})
111120
for (let permission of parsedPermissions) {
112-
try {
113-
await acl_perms.changePermissions(url, [permission], options)
114-
return;
115-
} catch (e) {
116-
if (options.verbose) writeErrorString(`Could not set permissions for ${permission.id} using WAC`, e, options)
121+
const { acp, acl } = await discoverAccessMechanism(url, options.fetch)
122+
if ( !acp && !acl ) {
123+
if (options.verbose) writeErrorString(`Could not set permissions for ${permission.id}`, { message: "Could not find attached WAC or ACP management resource." }, options)
124+
continue;
117125
}
118-
try {
119-
if (options.group || options.default) throw new Error("Cannot set WAC-specific options such as group and default for non-WAC environments ")
120-
await setPermission(url, [permission], options)
121-
return;
122-
} catch (e) {
123-
if (options.verbose) writeErrorString(`Could not set permissions for ${permission.id} using ACP`, e, options)
126+
if (acp) {
127+
try {
128+
if (options.group || options.default) throw new Error("Cannot set WAC-specific options such as group and default for non-WAC environments ")
129+
await setPermission(url, [permission], options)
130+
return;
131+
} catch (e) {
132+
if (options.verbose) writeErrorString(`Could not set permissions for ${permission.id} using ACP`, e, options)
133+
}
134+
}
135+
if (acl) {
136+
try {
137+
await acl_perms.changePermissions(url, [permission], options)
138+
return;
139+
} catch (e) {
140+
if (options.verbose) writeErrorString(`Could not set permissions for ${permission.id} using WAC`, e, options)
141+
}
124142
}
125143
}
126144
}

src/utils/util.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getSolidDataset, getContainedResourceUrlAll, getUrl, getUrlAll, getThing, getThingAll, getDatetime, getInteger, SolidDataset } from '@inrupt/solid-client';
1+
import { getSolidDataset, getContainedResourceUrlAll, getUrl, getUrlAll, getThing, getThingAll, getDatetime, getInteger, SolidDataset, acp_ess_2, hasAccessibleAcl } from '@inrupt/solid-client';
22
import { requestUserIdp } from './userInteractions';
33
import type { Logger } from '../logger';
44

@@ -503,4 +503,18 @@ export async function isRDFResource(fileInfo: FileInfo, fetch: any) {
503503
if(!contentType) return false;
504504
else if (parseableExtensions.indexOf(mime.extension(contentType)) !== -1) return true;
505505
return false
506+
}
507+
508+
export async function discoverAccessMechanism(url: string, fetch: any) {
509+
// We need to first check acp because Inrupt libs are kinda wack and reuse of acl rel header is confusing their own libs.
510+
const acpInfo = await acp_ess_2.getResourceInfoWithAcr(url, { fetch })
511+
const acp = acp_ess_2.hasAccessibleAcr(acpInfo)
512+
if (acp) return({ acp: true, acl: false })
513+
514+
// Now we check acl
515+
const dataset = await acp_ess_2.getResourceInfoWithAccessDatasets(url, { fetch })
516+
const acl = hasAccessibleAcl(dataset) && !acp
517+
if (acl) return({ acp: false, acl: true })
518+
519+
return ({ acp: false, acl: false })
506520
}

0 commit comments

Comments
 (0)