-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
348 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import { rest } from 'msw'; | ||
import { render, screen, waitFor } from '../../../testUtils/render'; | ||
import { server } from '../../mocks/test-server'; | ||
import { SignedInUser } from './hankeUser'; | ||
import UserRightsCheck from './UserRightsCheck'; | ||
|
||
test('Should render children if user has required right', async () => { | ||
render( | ||
<UserRightsCheck requiredRight="EDIT" hankeTunnus="HAI22-2"> | ||
<p>Children</p> | ||
</UserRightsCheck>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText('Children')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('Should not render children if user does not have required right', async () => { | ||
server.use( | ||
rest.get('/api/hankkeet/:hankeTunnus/whoami', async (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.json<SignedInUser>({ | ||
hankeKayttajaId: '3fa85f64-5717-4562-b3fc-2c963f66afa6', | ||
kayttooikeustaso: 'KATSELUOIKEUS', | ||
kayttooikeudet: ['VIEW'], | ||
}), | ||
); | ||
}), | ||
); | ||
|
||
render( | ||
<UserRightsCheck requiredRight="EDIT" hankeTunnus="HAI22-2"> | ||
<p>Children</p> | ||
</UserRightsCheck>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Children')).not.toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import useUserRightsForHanke from './hooks/useUserRightsForHanke'; | ||
import { Rights } from './hankeUser'; | ||
|
||
/** | ||
* Check that user has required rights. | ||
* If they have, render children. | ||
*/ | ||
function UserRightsCheck({ | ||
requiredRight, | ||
hankeTunnus, | ||
children, | ||
}: { | ||
/** User right that is required to render children */ | ||
requiredRight: keyof typeof Rights; | ||
/** hankeTunnus of the hanke that the right is required for */ | ||
hankeTunnus?: string; | ||
children: React.ReactElement; | ||
}) { | ||
const { data: signedInUser } = useUserRightsForHanke(hankeTunnus); | ||
|
||
if (signedInUser?.kayttooikeudet.includes(requiredRight)) { | ||
return children; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export default UserRightsCheck; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/domain/hanke/hankeUsers/hooks/useUserRightsForHanke.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useQuery } from 'react-query'; | ||
import { SignedInUser } from '../hankeUser'; | ||
import { getSignedInUserForHanke } from '../hankeUsersApi'; | ||
import { useFeatureFlags } from '../../../../common/components/featureFlags/FeatureFlagsContext'; | ||
|
||
export default function useSignedInUserRightsForHanke(hankeTunnus?: string) { | ||
const features = useFeatureFlags(); | ||
|
||
return useQuery<SignedInUser>( | ||
['signedInUser', hankeTunnus], | ||
() => getSignedInUserForHanke(hankeTunnus), | ||
{ | ||
enabled: Boolean(hankeTunnus) && features.accessRights, | ||
}, | ||
); | ||
} |
Oops, something went wrong.