2022-03-15 09:33:28 +00:00
|
|
|
import { useState } from './useState';
|
2022-04-05 14:30:53 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
const state = useState();
|
2022-03-11 10:41:23 +00:00
|
|
|
|
|
|
|
export function useRole() {
|
2022-04-05 14:30:53 +00:00
|
|
|
async function fetch() {
|
|
|
|
const { data } = await axios.get('/api/accounts/acl');
|
|
|
|
const roles = data.roles.map(userRoles => userRoles.role.name);
|
|
|
|
|
|
|
|
state.setUser(data.user);
|
|
|
|
state.setRoles(roles);
|
|
|
|
}
|
|
|
|
|
2022-03-28 07:06:36 +00:00
|
|
|
function hasAny(roles) {
|
2022-04-05 14:30:53 +00:00
|
|
|
const roleStore = state.getRoles();
|
2022-03-11 10:41:23 +00:00
|
|
|
|
|
|
|
for (const role of roles) {
|
2022-03-15 09:33:28 +00:00
|
|
|
if (roleStore.value.indexOf(role) !== -1) return true;
|
2022-03-11 10:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-04-05 14:30:53 +00:00
|
|
|
fetch,
|
2022-03-11 10:41:23 +00:00
|
|
|
hasAny,
|
|
|
|
};
|
|
|
|
}
|
2022-03-15 09:33:28 +00:00
|
|
|
|