mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat(FE-113): create permissionCheck helper function
This commit is contained in:
@@ -6,22 +6,43 @@ type AuthStore = {
|
||||
isLoadingUser?: boolean;
|
||||
setUser: (newUserData?: UserWithRoles) => void;
|
||||
setIsLoadingUser: (isLoading?: boolean) => void;
|
||||
permissionCheck: (permissionName: string) => boolean;
|
||||
};
|
||||
|
||||
const useAuthStore = create<AuthStore>()((set) => ({
|
||||
const useAuthStore = create<AuthStore>()((set, get) => ({
|
||||
user: undefined,
|
||||
isLoadingUser: false,
|
||||
setUser: (newUserData) => set({ user: newUserData }),
|
||||
setIsLoadingUser: (isLoading) => set({ isLoadingUser: Boolean(isLoading) }),
|
||||
|
||||
permissionCheck: (name) => {
|
||||
const { user, isLoadingUser } = get();
|
||||
|
||||
if (!isLoadingUser && user) {
|
||||
const isAllowed = user.roles.some((role) => {
|
||||
const isPermissionNameAllowed = role.permissions.some(
|
||||
(permission) => permission.name === name
|
||||
);
|
||||
|
||||
return isPermissionNameAllowed;
|
||||
});
|
||||
|
||||
return isAllowed;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
}));
|
||||
|
||||
export const useAuth = () => {
|
||||
const { user, setUser, isLoadingUser, setIsLoadingUser } = useAuthStore();
|
||||
const { user, setUser, isLoadingUser, setIsLoadingUser, permissionCheck } =
|
||||
useAuthStore();
|
||||
|
||||
return {
|
||||
user,
|
||||
setUser,
|
||||
isLoadingUser,
|
||||
setIsLoadingUser,
|
||||
permissionCheck,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user