From a64f9af1ae1593b1116b5b17999ab427b96ab13c Mon Sep 17 00:00:00 2001 From: "David M. Weigl" Date: Tue, 27 Jul 2021 13:08:30 +0200 Subject: [PATCH] Implement isPublicReadable and userControlsAccess methods on SolidClient --- src/API/SolidAPI.tsx | 25 ++++++++++++++++++++++++- src/examples/Annotator.tsx | 25 ++++++++++++++++++++++--- src/examples/SessionExample.tsx | 3 +-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/API/SolidAPI.tsx b/src/API/SolidAPI.tsx index 109651f..1ac98d4 100644 --- a/src/API/SolidAPI.tsx +++ b/src/API/SolidAPI.tsx @@ -71,6 +71,30 @@ export default class SolidClient { }) } + isPublicReadable = async (resourceUri: URL) => { + return await access.getPublicAccess(resourceUri.toString(), {fetch: this.session.fetch}) + .then(access => { + if(access === null) { + console.warn("Could not check whether the following resource is public readable. User may not have view access, or resource access may have been modified with incompatible Solid authn library.", resourceUri) + return false; + } else { + return access.read; + } + }) + } + + userControlsAccess = async (resourceUri: URL) => { + return await access.getAgentAccess(resourceUri.toString(), this.session.info.webId.toString(), {fetch:this.session.fetch}) + .then(access => { + if(access === null) { + console.warn("Could not check whether the user controls access over the following resource. Have you turned on 'Control' for this URI in the Solid Pod's trusted application preferences? Otherwise, resource access may have been modified with incompatible Solid authn library.", resourceUri) + return false; + } else { + return access.controlRead; + } + }) + } + grantPublicReadable = async(resourceUri: URL) => { access.setPublicAccess( resourceUri.toString(), @@ -105,7 +129,6 @@ export default class SolidClient { const profileDataset = await getSolidDataset(profileDocUri, { fetch: this.session.fetch}); const profile = getThing(profileDataset, this.session.info!.webId!); let postUrl:URL; - if (!container.startsWith('/')) { container = '/' + container; } diff --git a/src/examples/Annotator.tsx b/src/examples/Annotator.tsx index 8e430dd..1e1033c 100644 --- a/src/examples/Annotator.tsx +++ b/src/examples/Annotator.tsx @@ -162,12 +162,31 @@ class Annotator extends Component { /*this.props.trompaClient.saveAnnotation(annotation);*/ const solidAnnotation = annotation.toJSONLD(this.props.resource); - this.props.solidClient.saveAnnotation(solidAnnotation, this.props.solidSession, this.props.container) + this.props.solidClient.saveAnnotation(solidAnnotation, this.props.container) .then((resp:any) => { - this.props.solidClient.fetchAnnotations(new URL(new URL(this.props.solidSession.info!.webId!).origin + this.props.container), this.props.solidSession, {}) + this.props.solidClient.fetchAnnotations(new URL(new URL(this.props.solidSession.info!.webId!).origin + this.props.container), {}) .then((annos:any[]) => { console.debug("Fetched annotations: ", annos) - annos.forEach(a => this.props.solidClient.revokePublicReadable(new URL(a["@id"]), this.props.solidSession)) + annos.forEach(a => { + let annoUri = a["@id"]; + const publicAccess = this.props.solidClient.isPublicReadable(annoUri); + publicAccess.then((pubAcc:boolean) => { + console.log("Public access for annotation ", annoUri, " is ", pubAcc); + const userControl = this.props.solidClient.userControlsAccess(annoUri); + userControl.then((usrCtrl:boolean) => { + console.log("User control for annotation ", annoUri, " is ", usrCtrl); + // if the user is able to, toggle the public readable state of the resource + if(usrCtrl) { + if(pubAcc) { + this.props.solidClient.revokePublicReadable(annoUri); + } else { + this.props.solidClient.grantPublicReadable(annoUri); + } + } + + }) + }) + }) }) // TODO do something with them! diff --git a/src/examples/SessionExample.tsx b/src/examples/SessionExample.tsx index e17c6e5..dcdb16f 100644 --- a/src/examples/SessionExample.tsx +++ b/src/examples/SessionExample.tsx @@ -36,12 +36,11 @@ const client = new ApolloClient({ }); const trompaClient = new TrompaClient(AUTH_PROXY_URL, client); -const solidClient = new SolidClient(); - const containerInSolidPod = "/public/" // TODO make user configurable function SessionExample(props: {trompaClient: TrompaClient}) { const {session} = useSession(); + const solidClient = new SolidClient(session); const userId = session.info.webId; const [resource, setResource] = useState(); const [showSearch, setShowSearch] = useState(true);