2021-12-14 19:14:53 +00:00
|
|
|
import jwtDecode from 'jwt-decode';
|
|
|
|
import { useCurrentStateAndParams } from '@uirouter/react';
|
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
ReactNode,
|
|
|
|
useContext,
|
2021-12-16 18:52:54 +00:00
|
|
|
useMemo,
|
2022-06-23 07:25:56 +00:00
|
|
|
PropsWithChildren,
|
2021-12-14 19:14:53 +00:00
|
|
|
} from 'react';
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
import { isAdmin } from '@/portainer/users/user.helpers';
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2023-01-26 02:10:05 +00:00
|
|
|
import { User } from '@/portainer/users/types';
|
|
|
|
import { useUser as useLoadUser } from '@/portainer/users/queries/useUser';
|
2021-12-14 19:14:53 +00:00
|
|
|
|
|
|
|
import { useLocalStorage } from './useLocalStorage';
|
|
|
|
|
|
|
|
interface State {
|
2022-03-16 06:35:32 +00:00
|
|
|
user?: User;
|
2021-12-14 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const UserContext = createContext<State | null>(null);
|
2022-09-20 18:14:24 +00:00
|
|
|
UserContext.displayName = 'UserContext';
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
/**
|
|
|
|
* @deprecated use `useCurrentUser` instead
|
|
|
|
*/
|
|
|
|
export const useUser = useCurrentUser;
|
|
|
|
|
|
|
|
export function useCurrentUser() {
|
2021-12-14 19:14:53 +00:00
|
|
|
const context = useContext(UserContext);
|
|
|
|
|
|
|
|
if (context === null) {
|
|
|
|
throw new Error('should be nested under UserProvider');
|
|
|
|
}
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
const { user } = context;
|
|
|
|
if (typeof user === 'undefined') {
|
|
|
|
throw new Error('should be authenticated');
|
|
|
|
}
|
|
|
|
|
2022-03-16 06:35:32 +00:00
|
|
|
return useMemo(
|
2022-06-23 07:25:56 +00:00
|
|
|
() => ({
|
|
|
|
user,
|
|
|
|
isAdmin: isAdmin(user),
|
|
|
|
}),
|
|
|
|
[user]
|
2022-03-16 06:35:32 +00:00
|
|
|
);
|
2021-12-14 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 06:02:23 +00:00
|
|
|
export function useAuthorizations(
|
|
|
|
authorizations: string | string[],
|
2022-07-22 02:14:31 +00:00
|
|
|
forceEnvironmentId?: EnvironmentId,
|
2022-01-24 06:02:23 +00:00
|
|
|
adminOnlyCE = false
|
|
|
|
) {
|
2021-12-14 19:14:53 +00:00
|
|
|
const { user } = useUser();
|
2022-06-23 07:25:56 +00:00
|
|
|
const {
|
|
|
|
params: { endpointId },
|
|
|
|
} = useCurrentStateAndParams();
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2022-03-16 06:35:32 +00:00
|
|
|
if (!user) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-22 02:14:31 +00:00
|
|
|
return hasAuthorizations(
|
|
|
|
user,
|
|
|
|
authorizations,
|
|
|
|
forceEnvironmentId || endpointId,
|
|
|
|
adminOnlyCE
|
|
|
|
);
|
2022-06-23 07:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isEnvironmentAdmin(
|
|
|
|
user: User,
|
|
|
|
environmentId: EnvironmentId,
|
2022-08-03 10:19:27 +00:00
|
|
|
adminOnlyCE = true
|
2022-06-23 07:25:56 +00:00
|
|
|
) {
|
|
|
|
return hasAuthorizations(
|
|
|
|
user,
|
|
|
|
['EndpointResourcesAccess'],
|
|
|
|
environmentId,
|
|
|
|
adminOnlyCE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasAuthorizations(
|
|
|
|
user: User,
|
|
|
|
authorizations: string | string[],
|
|
|
|
environmentId?: EnvironmentId,
|
|
|
|
adminOnlyCE = false
|
|
|
|
) {
|
|
|
|
const authorizationsArray =
|
|
|
|
typeof authorizations === 'string' ? [authorizations] : authorizations;
|
|
|
|
|
|
|
|
if (authorizationsArray.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-24 06:02:23 +00:00
|
|
|
if (process.env.PORTAINER_EDITION === 'CE') {
|
|
|
|
return !adminOnlyCE || isAdmin(user);
|
|
|
|
}
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
if (!environmentId) {
|
2021-12-14 19:14:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAdmin(user)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!user.EndpointAuthorizations ||
|
2022-06-23 07:25:56 +00:00
|
|
|
!user.EndpointAuthorizations[environmentId]
|
2021-12-14 19:14:53 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
const userEndpointAuthorizations = user.EndpointAuthorizations[environmentId];
|
2021-12-14 19:14:53 +00:00
|
|
|
return authorizationsArray.some(
|
|
|
|
(authorization) => userEndpointAuthorizations[authorization]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AuthorizedProps {
|
|
|
|
authorizations: string | string[];
|
2022-07-22 02:14:31 +00:00
|
|
|
environmentId?: EnvironmentId;
|
2022-06-23 07:25:56 +00:00
|
|
|
adminOnlyCE?: boolean;
|
2022-10-17 21:46:27 +00:00
|
|
|
childrenUnauthorized?: ReactNode;
|
2021-12-14 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
export function Authorized({
|
|
|
|
authorizations,
|
2022-07-22 02:14:31 +00:00
|
|
|
environmentId,
|
2022-06-23 07:25:56 +00:00
|
|
|
adminOnlyCE = false,
|
|
|
|
children,
|
2022-10-17 21:46:27 +00:00
|
|
|
childrenUnauthorized = null,
|
2022-06-23 07:25:56 +00:00
|
|
|
}: PropsWithChildren<AuthorizedProps>) {
|
2022-07-22 02:14:31 +00:00
|
|
|
const isAllowed = useAuthorizations(
|
|
|
|
authorizations,
|
|
|
|
environmentId,
|
|
|
|
adminOnlyCE
|
|
|
|
);
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2022-10-17 21:46:27 +00:00
|
|
|
return isAllowed ? <>{children}</> : <>{childrenUnauthorized}</>;
|
2021-12-14 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface UserProviderProps {
|
|
|
|
children: ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function UserProvider({ children }: UserProviderProps) {
|
|
|
|
const [jwt] = useLocalStorage('JWT', '');
|
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
const tokenPayload = useMemo(() => jwtDecode(jwt) as { id: number }, [jwt]);
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
const userQuery = useLoadUser(tokenPayload.id, {
|
|
|
|
staleTime: Infinity, // should reload te user details only on page load
|
|
|
|
});
|
2021-12-16 18:52:54 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
const providerState = useMemo(
|
|
|
|
() => ({ user: userQuery.data }),
|
|
|
|
[userQuery.data]
|
|
|
|
);
|
2021-12-14 19:14:53 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
if (jwt === '' || !providerState.user) {
|
2021-12-14 19:14:53 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-12-16 18:52:54 +00:00
|
|
|
<UserContext.Provider value={providerState}>
|
|
|
|
{children}
|
|
|
|
</UserContext.Provider>
|
2021-12-14 19:14:53 +00:00
|
|
|
);
|
|
|
|
}
|