Skip to content
Snippets Groups Projects
Commit cbe60efa authored by Max Frederiksen's avatar Max Frederiksen Committed by Max Frederiksen
Browse files

Add typing: /src/components/auth

parent 6a460085
No related branches found
No related tags found
2 merge requests!612Release 5.0.0,!590CE-3429: Convert to typescript
import { useContext } from "react"; import { useContext, type ReactNode } from "react";
import { userContext } from "@ess-ics/ce-ui-common"; import { userContext } from "@ess-ics/ce-ui-common";
import { AccessDenied } from "../AccessDenied"; import { AccessDenied } from "../AccessDenied";
import { UserContext } from "../../../types/common";
const checkPermissions = (userRoles, allowedRoles) => { interface AllowedUserWithRoles {
user: string;
role: string;
}
interface AccessControlProps {
allowedRoles: string[];
allowedUsersWithRoles?: AllowedUserWithRoles[];
children: ReactNode;
renderNoAccess?: () => ReactNode;
}
const checkPermissions = (
userRoles: UserContext["userRoles"],
allowedRoles: string[]
) => {
if (allowedRoles.length === 0) { if (allowedRoles.length === 0) {
return true; return true;
} }
...@@ -10,15 +26,23 @@ const checkPermissions = (userRoles, allowedRoles) => { ...@@ -10,15 +26,23 @@ const checkPermissions = (userRoles, allowedRoles) => {
return userRoles?.some((role) => allowedRoles.includes(role)); return userRoles?.some((role) => allowedRoles.includes(role));
}; };
const checkUser = (user, userRoles, allowedUsersWithRoles) => { const checkUser = (
if (!allowedUsersWithRoles || allowedUsersWithRoles.length === 0) { user: UserContext["user"],
userRoles: UserContext["userRoles"],
allowedUsersWithRoles?: AllowedUserWithRoles[]
) => {
if (
!allowedUsersWithRoles ||
allowedUsersWithRoles.length === 0 ||
(userRoles && userRoles.length === 0)
) {
return false; return false;
} }
return allowedUsersWithRoles.find( return allowedUsersWithRoles.find(
(userWithRole) => (userWithRole) =>
userWithRole.user === user?.loginName && userWithRole.user === user?.loginName &&
userRoles.includes(userWithRole.role) userRoles?.includes(userWithRole.role)
); );
}; };
...@@ -27,8 +51,8 @@ export const AccessControl = ({ ...@@ -27,8 +51,8 @@ export const AccessControl = ({
allowedUsersWithRoles, allowedUsersWithRoles,
children, children,
renderNoAccess renderNoAccess
}) => { }: AccessControlProps) => {
const { user, userRoles } = useContext(userContext); const { user, userRoles } = useContext<UserContext>(userContext);
const permitted = const permitted =
checkPermissions(userRoles, allowedRoles) || checkPermissions(userRoles, allowedRoles) ||
......
...@@ -2,9 +2,10 @@ import { useContext, useEffect } from "react"; ...@@ -2,9 +2,10 @@ import { useContext, useEffect } from "react";
import { userContext } from "@ess-ics/ce-ui-common"; import { userContext } from "@ess-ics/ce-ui-common";
import env from "../../../config/env"; import env from "../../../config/env";
import { useTokenRenewMutation } from "../../../store/deployApi"; import { useTokenRenewMutation } from "../../../store/deployApi";
import { UserContext } from "../../../types/common";
export const TokenRenew = () => { export const TokenRenew = () => {
const { user } = useContext(userContext); const { user } = useContext<UserContext>(userContext);
const [renewToken] = useTokenRenewMutation(); const [renewToken] = useTokenRenewMutation();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment