Skip to content

Commit

Permalink
update tell
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 17, 2024
1 parent c0d487f commit 55981cf
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions backend/tell.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const Archetype = require('archetype');
const assert = require('assert');
const oso = require('../oso');

const TellParams = new Archetype({
sessionId: {
Expand Down Expand Up @@ -40,37 +39,64 @@ const TellParams = new Archetype({
}
}).compile('TellParams');

module.exports = async function handler(params) {
const validatedParams = new TellParams(params);
assert.ok(
validatedParams.attribute == null || ['is_public', 'is_protected'].includes(validatedParams.attribute),
'Invalid attribute'
);
module.exports = async function tell(params) {
params = new TellParams(params);
await connect();

Check failure on line 44 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'connect' is not defined

if (validatedParams.factType === 'role') {
const resourceId = validatedParams.resourceType === 'Repository' ? `${validatedParams.sessionId}_${validatedParams.resourceId}` : validatedParams.resourceId;
await Log.info(`Tell ${inspect(params)}`, { ...params, function: 'tell' });

Check failure on line 46 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'Log' is not defined

Check failure on line 46 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'inspect' is not defined

if (validatedParams.role === 'superadmin') {
await oso.tell(
'has_role',
{ type: 'User', id: `${validatedParams.sessionId}_${validatedParams.userId}` },
validatedParams.role
);
try {
const { sessionId } = params;

const player = await Player.findOne({ sessionId }).orFail();

Check failure on line 51 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'Player' is not defined

if (params.factType === 'role') {
if (params.role === 'superadmin') {
player.contextFacts.push([
'has_role',
{ type: params.actorType, id: params.userId },
params.role
]);
} else {
player.contextFacts.push([
'has_role',
{ type: params.actorType, id: params.userId },
params.role,
{ type: params.resourceType, id: params.resourceId }
]);
}
} else if (params.attribute === 'has_default_role') {
player.contextFacts.push([
params.attribute,
{ type: params.resourceType, id: params.resourceId },
params.attributeValue
]);
} else if (params.attribute === 'has_group') {
player.contextFacts.push([
params.attribute,
{ type: params.resourceType, id: params.resourceId },
{ type: 'Group', id: params.attributeValue }
]);
} else {
await oso.tell(
'has_role',
{ type: 'User', id: `${validatedParams.sessionId}_${validatedParams.userId}` },
validatedParams.role,
{ type: validatedParams.resourceType, id: resourceId }
);
player.contextFacts.push([
params.attribute,
{ type: params.resourceType, id: params.resourceId },
{ type: 'Boolean', id: params.attributeValue }
]);
}
} else {
await oso.tell(
validatedParams.attribute,
{ type: 'Repository', id: `${validatedParams.sessionId}_${validatedParams.resourceId}` },
{ type: 'Boolean', id: !!validatedParams.attributeValue + '' }
);

await player.save();

return { ok: true };
} catch (err) {
await Log.error(`tell: ${err.message}`, {

Check failure on line 92 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'Log' is not defined
...params,
function: 'tell',
message: err.message,
stack: err.stack,
err: inspect(err)

Check failure on line 97 in backend/tell.js

View workflow job for this annotation

GitHub Actions / Lint (18, ubuntu-20.04)

'inspect' is not defined
});

throw err;
}

return { ok: true };
};

0 comments on commit 55981cf

Please sign in to comment.