-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (permissions): change the permissions mechanism
Related to issue #110
- Loading branch information
Showing
19 changed files
with
427 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
|
||
const uuidv4 = require('uuid/v4') | ||
|
||
|
||
const Permission = use('App/Models/v1/Permission') | ||
const Environment = use('App/Models/Environment') | ||
|
||
|
||
class PermissionController { | ||
async store ({ request, response }) { | ||
try { | ||
let permission = new Permission() | ||
permission.id = await uuidv4() | ||
|
||
permission.clearance = request.input('clearance') | ||
permission.subject_grade = request.input('subject_grade') | ||
permission.resource = request.input('resource') | ||
permission.resource_id = request.input('resource_id') | ||
|
||
|
||
let environment = await Environment.findBy('name', request.input('environment')) | ||
permission.environment_id = environment.id | ||
|
||
|
||
|
||
await permission.save() | ||
|
||
response.json('permission successfully created') | ||
} catch (e) { | ||
console.log(e) | ||
return response.status(500).json({ message: e.message }) | ||
} | ||
} | ||
} | ||
|
||
module.exports = PermissionController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
/** @typedef {import('@adonisjs/framework/src/Request')} Request */ | ||
/** @typedef {import('@adonisjs/framework/src/Response')} Response */ | ||
/** @typedef {import('@adonisjs/framework/src/View')} View */ | ||
|
||
const Database = use('Database') | ||
|
||
class CheckPermission { | ||
/** | ||
* @param {object} ctx | ||
* @param {Request} ctx.request | ||
* @param {Function} next | ||
*/ | ||
async handle ({ params, request, response, auth }, next, properties) { | ||
try { | ||
let resourceId = params.id | ||
if (resourceId == undefined){ | ||
resourceId = request.input('caseId') | ||
} | ||
const user = await auth.user | ||
const resource = properties[0] | ||
const clearance = properties[1] | ||
// console.log(user.environment_id) | ||
const environment = await user.environment | ||
// const environment = await user.environment().fetch() | ||
|
||
// c.versions = await c.versions().fetch() | ||
console.log('clearance: '+clearance ) | ||
console.log('resource: '+resource) | ||
console.log('resourceId: '+resourceId) | ||
console.log('environment: '+user.environment_id) | ||
|
||
let queryResult | ||
const clearances = ['read', 'comment', 'share', 'write', 'delete'] | ||
const clearanceIindex = clearances.indexOf(clearance) | ||
// console.log('clearance '+ clearanceIindex) | ||
|
||
queryResult = await Database | ||
.from('permissions') | ||
.leftJoin('environments', 'environments.id', 'permissions.environment_id') | ||
// .where('environments.id', environment.id) | ||
|
||
.leftJoin('users', 'users.environment_id', 'environments.id') | ||
.where('users.environment_id', user.environment_id) | ||
.where('permissions.clearance', '>=', clearanceIindex) | ||
.where('permissions.resource', resource) | ||
.where('permissions.resource_id', resourceId) | ||
.count() | ||
|
||
console.log('queryResult '+queryResult[0]['count(*)']) | ||
if (queryResult[0]['count(*)'] === 0) { | ||
return response.status(500).json('you dont have permission to ' + clearance + ' such ' + resource) | ||
} else { | ||
await next() | ||
} | ||
|
||
} catch (e) { | ||
console.log(e) | ||
return response.status(500).json(e) | ||
} | ||
} | ||
} | ||
|
||
module.exports = CheckPermission |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */ | ||
const Model = use('Model') | ||
|
||
class Environment extends Model { | ||
static get incrementing () { | ||
return false | ||
} | ||
} | ||
|
||
module.exports = Environment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/adonisjs/database/migrations/1616133252647_drop_users_groups_schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
/** @type {import('@adonisjs/lucid/src/Schema')} */ | ||
const Schema = use('Schema') | ||
|
||
class DropUsersGroupsSchema extends Schema { | ||
up () { | ||
this.dropIfExists('users_groups') | ||
} | ||
|
||
down () { | ||
this.create('users_groups', (table) => { | ||
table.uuid('user_id').references('id').inTable('users').index('user_id') | ||
table.uuid('group_id').references('id').inTable('groups').index('group_id') | ||
table.primary(['group_id', 'user_id']) | ||
|
||
table.timestamps() | ||
}) | ||
} | ||
} | ||
|
||
module.exports = DropUsersGroupsSchema |
Oops, something went wrong.