Skip to content

Commit

Permalink
Merge pull request #2158 from City-of-Helsinki/HL-849
Browse files Browse the repository at this point in the history
HL-849 | Fix insufficient user permissions causing handler application to freeze
  • Loading branch information
sirtawast authored Aug 11, 2023
2 parents 1c955cb + 4a84f92 commit 23bc3b6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/benefit/users/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Meta:
"terms_of_service_approvals",
"terms_of_service_approval_needed",
"terms_of_service_in_effect",
"is_staff",
]
read_only_fields = [
"id",
Expand All @@ -34,6 +35,7 @@ class Meta:
"terms_of_service_approvals",
"terms_of_service_approval_needed",
"terms_of_service_in_effect",
"is_staff",
]

terms_of_service_in_effect = serializers.SerializerMethodField(
Expand Down
4 changes: 2 additions & 2 deletions frontend/benefit/handler/src/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import AuthContext from 'shared/auth/AuthContext';
const AuthProvider = <P,>({
children,
}: React.PropsWithChildren<P>): JSX.Element => {
const userQuery = useUserQuery((user) => Boolean(user));
const userQuery = useUserQuery((user) => user);
return (
<AuthContext.Provider
value={{
isAuthenticated: userQuery.isSuccess && userQuery.data,
isAuthenticated: userQuery.isSuccess && Boolean(userQuery.data),
isLoading: userQuery.isLoading,
isError: userQuery.isError,
}}
Expand Down
28 changes: 21 additions & 7 deletions frontend/benefit/handler/src/hooks/useUserQuery.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
import { AxiosError } from 'axios';
import { BackendEndpoint } from 'benefit-shared/backend-api/backend-api';
import { useRouter } from 'next/router';
import { useQuery, UseQueryResult } from 'react-query';
import useBackendAPI from 'shared/hooks/useBackendAPI';
import useLocale from 'shared/hooks/useLocale';
import User from 'shared/types/user';

import { ROUTES } from '../constants';
import useLogout from './useLogout';

// check that authentication is still alive in every 5 minutes
const FIVE_MINUTES = 5 * 60 * 1000;

const useUserQuery = <T = User>(
const useUserQuery = <T extends User>(
select?: (user: User) => T
): UseQueryResult<T, Error> => {
): UseQueryResult<T | User, AxiosError> => {
const router = useRouter();
const locale = useLocale();
const noPermissionLogout = useLogout();

// Don't fetch user state if status is logged out
const logout =
(router.route === '/login' || router.route === `${locale}/login`) &&
(router.route === ROUTES.LOGIN ||
router.route === `${locale}${ROUTES.LOGIN}`) &&
(router.asPath.includes('logout=true') ||
router.asPath.includes('userStateError=true'));
const { axios, handleResponse } = useBackendAPI();

const handleError = (error: Error): void => {
const handleError = (error: AxiosError): void => {
if (logout) {
void router.push(`${locale}/login?logout=true`);
void router.push(`${locale}${ROUTES.LOGIN}?logout=true`);
} else if (/40[13]/.test(error.message)) {
void router.push(`${locale}/login`);
void router.push(`${locale}${ROUTES.LOGIN}`);
} else if (
!process.env.NEXT_PUBLIC_MOCK_FLAG ||
process.env.NEXT_PUBLIC_MOCK_FLAG === '0'
) {
void router.push(`${locale}/login?userStateError=true`);
void router.push(`${locale}${ROUTES.LOGIN}?userStateError=true`);
}
};

const checkForStaffStatus = (user: User): void => {
if (user && !user.is_staff) {
void noPermissionLogout();
}
};

Expand All @@ -41,6 +54,7 @@ const useUserQuery = <T = User>(
enabled: !logout,
retry: false,
select,
onSuccess: checkForStaffStatus,
onError: (error) => handleError(error),
}
);
Expand Down
1 change: 1 addition & 0 deletions frontend/shared/src/types/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ type User = {
family_name: string;
name: string;
organization_name?: string;
is_staff?: boolean;
};
export default User;

0 comments on commit 23bc3b6

Please sign in to comment.