feat(FE) : setting access page master
This commit is contained in:
@@ -144,24 +144,6 @@ export const useAuth = () => {
|
||||
return !!userData
|
||||
}
|
||||
|
||||
// Helper function to check if user has specific role
|
||||
const hasRole = (role: string): boolean => {
|
||||
if (!user.value) return false
|
||||
|
||||
// Check in roles array
|
||||
if (user.value.roles?.includes(role)) return true
|
||||
|
||||
// Check in realm_access.roles
|
||||
if (user.value.realm_access?.roles?.includes(role)) return true
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Helper function to check if user has any of the specified roles
|
||||
const hasAnyRole = (roles: string[]): boolean => {
|
||||
return roles.some(role => hasRole(role))
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
user: readonly(user),
|
||||
@@ -177,8 +159,5 @@ export const useAuth = () => {
|
||||
refreshUser,
|
||||
clearError,
|
||||
|
||||
// Utilities
|
||||
hasRole,
|
||||
hasAnyRole
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
interface KeycloakRole {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
composite: boolean;
|
||||
clientRole: boolean;
|
||||
containerId: string;
|
||||
}
|
||||
|
||||
export const useKeycloakRoles = () => {
|
||||
const roles = ref<KeycloakRole[]>([]);
|
||||
const loading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
const config = useRuntimeConfig();
|
||||
|
||||
const fetchRoles = async () => {
|
||||
loading.value = true;
|
||||
error.value = null;
|
||||
|
||||
try {
|
||||
// Get access token from localStorage or session
|
||||
const accessToken = localStorage.getItem('accessToken');
|
||||
|
||||
if (!accessToken) {
|
||||
throw new Error('Access token not found');
|
||||
}
|
||||
|
||||
if(!config.public.keycloakClientUuid) {
|
||||
throw new Error('Keycloak client UUID is not configured');
|
||||
}
|
||||
|
||||
if(!config.public.keycloakAdminRealmUrl) {
|
||||
throw new Error('Keycloak admin realm URL is not configured');
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`${config.public.keycloakAdminRealmUrl}/clients/${config.public.keycloakClientUuid}/roles`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'accept': 'application/json, text/plain, */*',
|
||||
'authorization': `Bearer ${accessToken}`,
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch roles: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
roles.value = data;
|
||||
|
||||
return data;
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Unknown error';
|
||||
console.error('Error fetching Keycloak roles:', err);
|
||||
return [];
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const getRoleNames = (): string[] => {
|
||||
return roles.value.map(role => role.name);
|
||||
};
|
||||
|
||||
const getRoleOptions = () => {
|
||||
return roles.value.map(role => ({
|
||||
value: role.name,
|
||||
title: role.name,
|
||||
description: role.description
|
||||
}));
|
||||
};
|
||||
|
||||
return {
|
||||
roles,
|
||||
loading,
|
||||
error,
|
||||
fetchRoles,
|
||||
getRoleNames,
|
||||
getRoleOptions
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user