Skip to content

Commit

Permalink
Added support for new permission request
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBomholtz committed Apr 14, 2023
1 parent b49a518 commit ba4bb76
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ export default class Stream {
case 1100006:
case 1100004:
break;
case 1100011:
callback({ status: `Notification: ${data.custom.message}` });
break;
default:
callback(data);
}
Expand Down
40 changes: 34 additions & 6 deletions src/util/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@ import tui from './tui';
import { Manifest } from '../types/custom.d';
import { OauthExternal21, OauthClient21 } from '../types/application.d';
import Spinner from './spinner';
import Wappsto from '../wappsto';

type Request = {
method: string[],

Check failure on line 9 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `,` with `;`

Check failure on line 9 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `,` with `;`

Check failure on line 9 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `,` with `;`
collection: string;
message: string;
data?: Record<string, any>[],

Check failure on line 12 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `,` with `;`

Check failure on line 12 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `,` with `;`

Check failure on line 12 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `,` with `;`
name_installation: string;
type: string;
};

class Questions {
private async ask(questions: any[]): Promise<any | false> {
Spinner.stop();
const start = Spinner.stop();
let done = false;
return new Promise<any | false>((resolve) => {
const onCancel = () => {
Spinner.start();
if (start) {
Spinner.start();
}
done = true;
resolve(false);
return false;
};
prompts(questions, { onCancel }).then((answers) => {
if (!done) {
done = true;
Spinner.start();
if (start) {
Spinner.start();
}
resolve(answers);
}
});
Expand Down Expand Up @@ -543,8 +550,9 @@ class Questions {
]);
}

precisePermissionRequest(
request: Request
async precisePermissionRequest(
request: Request,
wappsto: Wappsto,

Check failure on line 555 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Delete `,`

Check failure on line 555 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Delete `,`

Check failure on line 555 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Delete `,`
): Promise<{ accept: boolean } | false> {
let msg = '';
let type = 'data';
Expand All @@ -556,7 +564,27 @@ class Questions {
if (request.message) {
msg = request.message;
} else {
msg = `${request.name_installation} would like to save ${type} under your account. Allow?`;
const method = request.method?.length ? request.method[0] : 'save';
msg = `${request.name_installation} would like to ${method} ${type} under your account. Allow?`;
if (method === 'retrieve') {
if (request.data) {
const type = request.data[0].meta.type;
const id = request.data[0].meta.id;
const model = await wappsto.getModel(type, id);
if (model) {
if(model.name) {

Check failure on line 575 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `·`

Check failure on line 575 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Insert `·`

Check failure on line 575 in src/util/questions.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Insert `·`
msg = `${request.name_installation} would like access to the ${type} ${model.name} (${id}). Allow?`;
} else {
msg = `${request.name_installation} would like access to the ${type} with id ${id}. Allow?`;
}
} else {
Spinner.stop();
tui.unblock();
tui.showError(`Failed to find a ${type} with id ${id}`);
return false;
}
}
}
}

return this.ask([
Expand Down
19 changes: 13 additions & 6 deletions src/util/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ class Spinner {
let i = 0;

this.timer = setInterval(() => {
const str = this.frames[i % len];
i += 1;
clearLine(process.stdout, 0);
cursorTo(process.stdout, 0);
tui.write(`${str} ${this.title}`);
if (this.timer) {
const str = this.frames[i % len];
i += 1;
clearLine(process.stdout, 0);
cursorTo(process.stdout, 0);
tui.write(`${str} ${this.title}`);
}
}, 80);
}

stop(): void {
stop(): boolean {
if (!this.timer) {
return false;
}

clearLine(process.stdout, 0);
cursorTo(process.stdout, 0);
clearInterval(this.timer);
this.timer = undefined;
return true;
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/util/tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ class Tui {
}

clear(): void {
clearLine(process.stdout, 0);
cursorTo(process.stdout, 0);
if (this.blocked) {
this.blocked.push('');
} else {
clearLine(process.stdout, 0);
cursorTo(process.stdout, 0);
}
}

header(text: string): Promise<void> {
Expand Down Expand Up @@ -50,8 +54,13 @@ class Tui {

if (tmp) {
tmp.forEach((item) => {
/* istanbul ignore next */
this.write(item);
if (item === '') {
/* istanbul ignore next */
this.clear();
} else {
/* istanbul ignore next */
this.write(item);
}
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const VERSION = '2.0.18';
const VERSION = '2.0.19';
export { VERSION };
28 changes: 17 additions & 11 deletions src/wapp.serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,35 @@ export default class ServeWapp extends UpdateWapp {
);
}
} else if (data.req.collection) {
const answers = await questions.precisePermissionRequest(data.req);
const answers = await questions.precisePermissionRequest(data.req, this.wappsto);

Check failure on line 152 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `data.req,·this.wappsto` with `⏎············data.req,⏎············this.wappsto⏎··········`

Check failure on line 152 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `data.req,·this.wappsto` with `⏎············data.req,⏎············this.wappsto⏎··········`

Check failure on line 152 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `data.req,·this.wappsto` with `⏎············data.req,⏎············this.wappsto⏎··········`
if (answers === false) {
/* istanbul ignore next */
return;
}

if (answers.accept) {
if (data.req.method[0] === 'add') {
await this.wappsto.updateACLRestriction(
data.installation,
data.req.collection
);
} else {
tui.showWarning(
`Unknown '${data.req.method[0]}' permission request`
);
switch(data.req.method[0]) {

Check failure on line 159 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Insert `·`

Check failure on line 159 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Insert `·`

Check failure on line 159 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Insert `·`
case 'add':

Check failure on line 160 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Delete `·`

Check failure on line 160 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Delete `·`

Check failure on line 160 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Delete `·`
await this.wappsto.updateACLRestriction(
data.installation,
data.req.collection
);
break;
case 'retrieve':
await this.wappsto.updateACLAccess(data.req.data[0].meta.id, data.installation);

Check failure on line 167 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 16.x and ubuntu-latest

Replace `data.req.data[0].meta.id,·data.installation` with `⏎··················data.req.data[0].meta.id,⏎··················data.installation⏎················`

Check failure on line 167 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `data.req.data[0].meta.id,·data.installation` with `⏎··················data.req.data[0].meta.id,⏎··················data.installation⏎················`

Check failure on line 167 in src/wapp.serve.ts

View workflow job for this annotation

GitHub Actions / Build, lint, and test on Node 18.x and ubuntu-latest

Replace `data.req.data[0].meta.id,·data.installation` with `⏎··················data.req.data[0].meta.id,⏎··················data.installation⏎················`
break;
default:
tui.showWarning(
`Unknown '${data.req.method[0]}' permission request`
);
break;
}
await this.wappsto.readNotification(data.id, 'accepted');
} else {
await this.wappsto.readNotification(data.id, 'denied');
}
} else if (data.req.name_installation) {
const answers = await questions.precisePermissionRequest(data.req);
const answers = await questions.precisePermissionRequest(data.req, this.wappsto);
if (answers === false) {
/* istanbul ignore next */
return;
Expand Down
36 changes: 36 additions & 0 deletions src/wappsto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ export default class Wappsto {
}
}

async updateACLAccess(
modelID: string,
installationID: string
): Promise<void> {
try {
// Append the new user to the acl restriction for the item
await HTTP.patch(
`${this.HOST}/services/2.1/acl?id=${modelID}&propagate=true`,
{
permission: [
{
meta: { id: installationID },
restriction: [{ method: { retrieve: true } }],
},
],
}
);
} catch (err) {
tui.showError('Failed to update ACL Access', err);
}
}

async find(
type: string,
search: string,
Expand Down Expand Up @@ -134,4 +156,18 @@ export default class Wappsto {
}
}
}

async getModel(
type: string,
id: string
): Promise<Record<string, any> | undefined> {
let result = undefined;
try {
const response = await HTTP.get(`${this.HOST}/services/${type}/${id}`);
result = response.data;
} catch (err) {
tui.showError('Failed to get model', err);
}
return result;
}
}

0 comments on commit ba4bb76

Please sign in to comment.