- additional functionality for DatasetController.ts
All checks were successful
CI Pipeline / japa-tests (push) Successful in 47s
All checks were successful
CI Pipeline / japa-tests (push) Successful in 47s
- additional validation rules like 'uniqueArray' - additional Lucid models like BaseModel.ts for filling attributes, Title.ts, Description.ts - npm updates for @adonisjs/core
This commit is contained in:
parent
c4f4eff0d9
commit
e0ff71b117
44 changed files with 2002 additions and 1556 deletions
|
@ -15,71 +15,74 @@ const userRoleTable = Config.get('rolePermission.user_role_table', 'link_account
|
|||
* Should be called after auth middleware
|
||||
*/
|
||||
export default class Can {
|
||||
/**
|
||||
* Handle request
|
||||
*/
|
||||
public async handle(
|
||||
{ auth, response }: HttpContextContract,
|
||||
next: () => Promise<void>,
|
||||
permissionNames: string[]
|
||||
) {
|
||||
/**
|
||||
* Check if user is logged-in
|
||||
*/
|
||||
let user = await auth.user;
|
||||
if (!user) {
|
||||
return response.unauthorized({ error: 'Must be logged in' });
|
||||
}
|
||||
let hasPermission = await this.checkHasPermissions(user, permissionNames);
|
||||
if (!hasPermission) {
|
||||
// return response.unauthorized({
|
||||
// error: `Doesn't have required role(s): ${permissionNames.join(',')}`,
|
||||
// });
|
||||
throw new Exception(`Doesn't have required permission(s): ${permissionNames.join(',')}`, 401);
|
||||
}
|
||||
await next();
|
||||
}
|
||||
/**
|
||||
* Handle request
|
||||
*/
|
||||
public async handle({ auth, response }: HttpContextContract, next: () => Promise<void>, permissionNames: string[]) {
|
||||
/**
|
||||
* Check if user is logged-in
|
||||
*/
|
||||
let user = await auth.user;
|
||||
if (!user) {
|
||||
return response.unauthorized({ error: 'Must be logged in' });
|
||||
}
|
||||
let hasPermission = await this.checkHasPermissions(user, permissionNames);
|
||||
if (!hasPermission) {
|
||||
// return response.unauthorized({
|
||||
// error: `Doesn't have required role(s): ${permissionNames.join(',')}`,
|
||||
// });
|
||||
throw new Exception(`Doesn't have required permission(s): ${permissionNames.join(',')}`, 401);
|
||||
}
|
||||
await next();
|
||||
}
|
||||
|
||||
private async checkHasPermissions(user: User, permissionNames: Array<string>): Promise<boolean> {
|
||||
let rolePlaceHolder = '(';
|
||||
let placeholders = new Array(permissionNames.length).fill('?');
|
||||
rolePlaceHolder += placeholders.join(',');
|
||||
rolePlaceHolder += ')';
|
||||
private async checkHasPermissions(user: User, permissionNames: Array<string>): Promise<boolean> {
|
||||
let rolePlaceHolder = '(';
|
||||
let placeholders = new Array(permissionNames.length).fill('?');
|
||||
rolePlaceHolder += placeholders.join(',');
|
||||
rolePlaceHolder += ')';
|
||||
|
||||
// let test = user
|
||||
// .related('roles')
|
||||
// .query()
|
||||
// .count('permissions.name')
|
||||
// .innerJoin('gba.role_has_permissions', function () {
|
||||
// this.on('gba.role_has_permissions.role_id', 'roles.id');
|
||||
// })
|
||||
// .innerJoin('gba.permissions', function () {
|
||||
// this.on('role_has_permissions.permission_id', 'permissions.id');
|
||||
// })
|
||||
// .andWhereIn('permissions.name', permissionNames);
|
||||
|
||||
// let test = user
|
||||
// .related('roles')
|
||||
// .query()
|
||||
// .count('permissions.name')
|
||||
// .innerJoin('gba.role_has_permissions', function () {
|
||||
// this.on('gba.role_has_permissions.role_id', 'roles.id');
|
||||
// })
|
||||
// .innerJoin('gba.permissions', function () {
|
||||
// this.on('role_has_permissions.permission_id', 'permissions.id');
|
||||
// })
|
||||
// .andWhereIn('permissions.name', permissionNames);
|
||||
|
||||
// select "permissions"."name"
|
||||
// from "gba"."roles"
|
||||
// inner join "gba"."link_accounts_roles" on "roles"."id" = "link_accounts_roles"."role_id"
|
||||
// inner join "gba"."role_has_permissions" on "gba"."role_has_permissions"."role_id" = "roles"."id"
|
||||
// inner join "gba"."permissions" on "role_has_permissions"."permission_id" = "permissions"."id"
|
||||
// where ("permissions"."name" in ('dataset-list', 'dataset-publish'))
|
||||
// from "gba"."roles"
|
||||
// inner join "gba"."link_accounts_roles" on "roles"."id" = "link_accounts_roles"."role_id"
|
||||
// inner join "gba"."role_has_permissions" on "gba"."role_has_permissions"."role_id" = "roles"."id"
|
||||
// inner join "gba"."permissions" on "role_has_permissions"."permission_id" = "permissions"."id"
|
||||
// where ("permissions"."name" in ('dataset-list', 'dataset-publish'))
|
||||
// and ("link_accounts_roles"."account_id" = 1)
|
||||
|
||||
let {
|
||||
rows: {
|
||||
0: { permissioncount },
|
||||
},
|
||||
} = await Database.rawQuery(
|
||||
'SELECT count("p"."name") as permissionCount FROM ' + roleTable +
|
||||
' r INNER JOIN ' + userRoleTable + ' ur ON ur.role_id=r.id AND "ur"."account_id"=? ' +
|
||||
' INNER JOIN ' + rolePermissionTable + ' rp ON rp.role_id=r.id ' +
|
||||
' INNER JOIN ' + permissionTable + ' p ON rp.permission_id=p.id AND "p"."name" in ' +
|
||||
rolePlaceHolder +
|
||||
' LIMIT 1',
|
||||
[user.id, ...permissionNames]
|
||||
);
|
||||
let {
|
||||
rows: {
|
||||
0: { permissioncount },
|
||||
},
|
||||
} = await Database.rawQuery(
|
||||
'SELECT count("p"."name") as permissionCount FROM ' +
|
||||
roleTable +
|
||||
' r INNER JOIN ' +
|
||||
userRoleTable +
|
||||
' ur ON ur.role_id=r.id AND "ur"."account_id"=? ' +
|
||||
' INNER JOIN ' +
|
||||
rolePermissionTable +
|
||||
' rp ON rp.role_id=r.id ' +
|
||||
' INNER JOIN ' +
|
||||
permissionTable +
|
||||
' p ON rp.permission_id=p.id AND "p"."name" in ' +
|
||||
rolePlaceHolder +
|
||||
' LIMIT 1',
|
||||
[user.id, ...permissionNames],
|
||||
);
|
||||
|
||||
return permissioncount > 0;
|
||||
}
|
||||
return permissioncount > 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue