2023-03-03 15:54:28 +00:00
|
|
|
import Database, {
|
|
|
|
// DatabaseQueryBuilderContract,
|
|
|
|
QueryClientContract,
|
|
|
|
TransactionClientContract,
|
2023-03-24 10:41:52 +00:00
|
|
|
} from '@ioc:Adonis/Lucid/Database';
|
|
|
|
import Config from '@ioc:Adonis/Core/Config';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-03-24 10:41:52 +00:00
|
|
|
export function getUserRoles(userId: number, trx?: TransactionClientContract): Promise<Array<string>> {
|
|
|
|
const { userRole } = Config.get('acl.joinTables');
|
2023-03-03 15:54:28 +00:00
|
|
|
return ((trx || Database) as QueryClientContract | TransactionClientContract)
|
2023-03-24 10:41:52 +00:00
|
|
|
.query()
|
|
|
|
.from('roles')
|
|
|
|
.distinct('roles.slug')
|
|
|
|
.leftJoin(userRole, `${userRole}.role_id`, 'roles.id')
|
|
|
|
.where(`${userRole}.user_id`, userId)
|
|
|
|
.then((res) => {
|
|
|
|
return res.map((r) => r.slug);
|
|
|
|
});
|
|
|
|
}
|