The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/providers/AuthorizationProvider.tsx

41 lines
1.6 KiB

import { AuthorizationContext, useUserId } from '@rocket.chat/ui-contexts';
import type { ReactNode } from 'react';
import { useMemo } from 'react';
import { hasPermission, hasAtLeastOnePermission, hasAllPermission, hasRole } from '../../app/authorization/client';
import { PermissionsCachedStore } from '../cachedStores';
import { createReactiveSubscriptionFactory } from '../lib/createReactiveSubscriptionFactory';
import { Roles } from '../stores';
type AuthorizationProviderProps = {
children?: ReactNode;
};
const AuthorizationProvider = ({ children }: AuthorizationProviderProps) => {
const isLoading = !PermissionsCachedStore.useReady();
if (isLoading) {
throw (async () => {
PermissionsCachedStore.listen();
await PermissionsCachedStore.init();
})();
}
const userId = useUserId();
const contextValue = useMemo(
() => ({
queryPermission: createReactiveSubscriptionFactory((permission, scope, scopeRoles) => hasPermission(permission, scope, scopeRoles)),
queryAtLeastOnePermission: createReactiveSubscriptionFactory((permissions, scope) => hasAtLeastOnePermission(permissions, scope)),
queryAllPermissions: createReactiveSubscriptionFactory((permissions, scope) => hasAllPermission(permissions, scope)),
queryRole: createReactiveSubscriptionFactory((role, scope?) => !!userId && hasRole(userId, role, scope)),
getRoles: () => Roles.state.records,
subscribeToRoles: (callback: () => void) => Roles.use.subscribe(callback),
}),
[userId],
);
return <AuthorizationContext.Provider children={children} value={contextValue} />;
};
export default AuthorizationProvider;