Skip to content

Commit

Permalink
Implement isPublicReadable and userControlsAccess methods on SolidClient
Browse files Browse the repository at this point in the history
  • Loading branch information
musicog committed Jul 27, 2021
1 parent d0403c7 commit a64f9af
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
25 changes: 24 additions & 1 deletion src/API/SolidAPI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 22 additions & 3 deletions src/examples/Annotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,31 @@ class Annotator extends Component<AnnotatorProps, AnnotatorState> {

/*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!

Expand Down
3 changes: 1 addition & 2 deletions src/examples/SessionExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrompaAnnotationComponents.Resource>();
const [showSearch, setShowSearch] = useState(true);
Expand Down

0 comments on commit a64f9af

Please sign in to comment.