2022-03-15 09:33:28 +00:00
|
|
|
import { useState } from './useState';
|
2022-04-05 14:30:53 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
|
2022-03-11 10:41:23 +00:00
|
|
|
export function useRole() {
|
2022-04-08 13:53:21 +00:00
|
|
|
const state = useState();
|
|
|
|
|
2022-04-05 14:30:53 +00:00
|
|
|
async function fetch() {
|
2023-04-26 10:04:55 +00:00
|
|
|
const { data } = await axios.get('VnUsers/acl');
|
2022-11-24 06:21:45 +00:00
|
|
|
const roles = data.roles.map((userRoles) => userRoles.role.name);
|
2022-04-05 14:30:53 +00:00
|
|
|
|
2022-04-20 11:10:37 +00:00
|
|
|
const userData = {
|
|
|
|
id: data.user.id,
|
|
|
|
name: data.user.name,
|
|
|
|
nickname: data.user.nickname,
|
2022-04-20 11:58:53 +00:00
|
|
|
lang: data.user.lang || 'es',
|
2022-11-24 06:21:45 +00:00
|
|
|
};
|
2022-04-20 11:10:37 +00:00
|
|
|
state.setUser(userData);
|
2022-04-05 14:30:53 +00:00
|
|
|
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-11-24 06:21:45 +00:00
|
|
|
state,
|
2022-03-11 10:41:23 +00:00
|
|
|
};
|
2022-11-24 06:21:45 +00:00
|
|
|
}
|