2024-03-14 19:25:27 +00:00
|
|
|
import type { HttpContext } from '@adonisjs/core/http';
|
|
|
|
import db from '@adonisjs/lucid/services/db';
|
|
|
|
import config from '@adonisjs/core/services/config';
|
2024-04-30 09:50:50 +00:00
|
|
|
import User from '#models/user';
|
2024-03-14 19:25:27 +00:00
|
|
|
import { Exception } from '@adonisjs/core/exceptions';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
// const roleTable = Config.get('rolePermission.role_table', 'roles');
|
|
|
|
const roleTable = config.get('rolePermission.role_table', 'roles');
|
|
|
|
// const userRoleTable = Config.get('rolePermission.user_role_table', 'link_accounts_roles');
|
|
|
|
const userRoleTable = config.get('rolePermission.user_role_table', 'user_roles');
|
2023-03-03 15:54:28 +00:00
|
|
|
|
|
|
|
// node ace make:middleware role
|
|
|
|
export default class Role {
|
2023-06-22 15:20:04 +00:00
|
|
|
// .middleware(['auth', 'role:admin,moderator'])
|
2024-03-14 19:25:27 +00:00
|
|
|
public async handle({ auth, response }: HttpContext, next: () => Promise<void>, userRoles: string[]) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// Check if user is logged-in or not.
|
|
|
|
// let expression = "";
|
|
|
|
// if (Array.isArray(args)) {
|
|
|
|
// expression = args.join(" || ");
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
let user = auth.user as User;
|
2023-06-22 15:20:04 +00:00
|
|
|
if (!user) {
|
|
|
|
return response.unauthorized({ error: 'Must be logged in' });
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
let hasRole = await this.checkHasRoles(user, userRoles);
|
|
|
|
if (!hasRole) {
|
|
|
|
// return response.unauthorized({
|
|
|
|
// error: `Doesn't have required role(s): ${userRoles.join(',')}`,
|
|
|
|
// // error: `Doesn't have required role(s)`,
|
|
|
|
// });
|
2024-03-14 19:25:27 +00:00
|
|
|
throw new Exception(`Doesn't have required role(s): ${userRoles.join(',')}`, { status: 401 });
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// code for middleware goes here. ABOVE THE NEXT CALL
|
|
|
|
await next();
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
private async checkHasRoles(user: User, userRoles: string[]): Promise<boolean> {
|
|
|
|
// await user.load("roles");
|
|
|
|
// const ok = user.roles.map((role) => role.name);
|
|
|
|
// const roles = await user.getRoles();
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
let rolePlaceHolder = '(';
|
|
|
|
let placeholders = new Array(userRoles.length).fill('?');
|
|
|
|
rolePlaceHolder += placeholders.join(',');
|
|
|
|
rolePlaceHolder += ')';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// const roles = await user
|
|
|
|
// .related('roles')
|
|
|
|
// .query()
|
|
|
|
// .count('*') // .select('name')
|
|
|
|
// .whereIn('name', userRoles);
|
|
|
|
// // .groupBy('name');
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// select count(*) as roleCount
|
|
|
|
// from gba.roles
|
|
|
|
// inner join gba.link_accounts_roles
|
|
|
|
// on "roles"."id" = "link_accounts_roles"."role_id"
|
|
|
|
// where ("name" in ('administrator', 'editor')) and ("link_accounts_roles"."account_id" = 1)
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
let {
|
|
|
|
rows: {
|
|
|
|
0: { rolecount },
|
|
|
|
},
|
2024-03-14 19:25:27 +00:00
|
|
|
} = await db.rawQuery(
|
2023-06-22 15:20:04 +00:00
|
|
|
'SELECT count("r"."id") as roleCount FROM ' +
|
|
|
|
roleTable +
|
|
|
|
' r INNER JOIN ' +
|
|
|
|
userRoleTable +
|
|
|
|
' ur ON r.id=ur.role_id WHERE "ur"."account_id"=? AND "r"."name" in ' +
|
|
|
|
rolePlaceHolder +
|
|
|
|
' LIMIT 1',
|
|
|
|
[user.id, ...userRoles],
|
|
|
|
);
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
return rolecount > 0;
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|