From 4ab4d53f2f130da297ee9f3fa7f3ac7faeedaa8c Mon Sep 17 00:00:00 2001 From: Raphaelle Cantin <110461050+rcantin-w@users.noreply.github.com> Date: Mon, 11 Nov 2024 16:11:14 +0000 Subject: [PATCH] Revert "Move isUserStaffWithRestricted at the user context level" --- .../views/components/UserProvider/UserProvider.tsx | 13 ++----------- .../components/IIIFViewer/IIIFCanvasThumbnail.tsx | 5 +++-- content/webapp/components/IIIFViewer/MainViewer.tsx | 5 +++-- .../webapp/components/IIIFViewer/ViewerSidebar.tsx | 4 ++-- .../WorkDetails/WorkDetails.AvailableOnline.tsx | 10 +++++----- content/webapp/components/WorkDetails/index.tsx | 4 ++-- content/webapp/pages/works/[workId]/index.tsx | 5 +++-- content/webapp/pages/works/[workId]/items.tsx | 11 ++++++----- content/webapp/utils/iiif/v3/index.ts | 12 ++++++------ content/webapp/utils/works.ts | 8 ++++---- 10 files changed, 36 insertions(+), 41 deletions(-) diff --git a/common/views/components/UserProvider/UserProvider.tsx b/common/views/components/UserProvider/UserProvider.tsx index 2ce40007b4..c8bf5bca68 100644 --- a/common/views/components/UserProvider/UserProvider.tsx +++ b/common/views/components/UserProvider/UserProvider.tsx @@ -16,15 +16,13 @@ import { export type State = 'initial' | 'loading' | 'signedin' | 'signedout' | 'failed'; type Props = { - user?: UserInfo; - userIsStaffWithRestricted: boolean; + user: UserInfo | undefined; state: State; reload: (abortSignal?: AbortSignal) => Promise; }; const defaultUserContext: Props = { user: undefined, - userIsStaffWithRestricted: false, state: 'initial', reload: async () => undefined, }; @@ -38,8 +36,6 @@ export function useUser(): Props { const UserProvider: FunctionComponent = ({ children }) => { const [user, setUser] = useState(); - const [userIsStaffWithRestricted, setUserIsStaffWithRestricted] = - useState(false); const [state, setState] = useState('initial'); const fetchUser = async (abortSignal?: AbortSignal, refetch = false) => { @@ -63,11 +59,7 @@ const UserProvider: FunctionComponent = ({ children }) => { // There is a race condition here where the cancel can happen // after the fetch has finished but before the response has been deserialised if (!abortSignal?.aborted) { - const userData = auth0UserProfileToUserInfo(data); - setUser(userData); - setUserIsStaffWithRestricted( - userData?.role === 'StaffWithRestricted' - ); + setUser(auth0UserProfileToUserInfo(data)); setState('signedin'); } } else { @@ -100,7 +92,6 @@ const UserProvider: FunctionComponent = ({ children }) => { fetchUser(abortSignal, true), }} diff --git a/content/webapp/components/IIIFViewer/IIIFCanvasThumbnail.tsx b/content/webapp/components/IIIFViewer/IIIFCanvasThumbnail.tsx index b53f9a6c18..83935a1d04 100644 --- a/content/webapp/components/IIIFViewer/IIIFCanvasThumbnail.tsx +++ b/content/webapp/components/IIIFViewer/IIIFCanvasThumbnail.tsx @@ -76,7 +76,8 @@ const IIIFCanvasThumbnail: FunctionComponent = ({ highlightImage, }: IIIFCanvasThumbnailProps) => { const [thumbnailLoaded, setThumbnailLoaded] = useState(false); - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); + const role = user?.role; const isRestricted = canvas.hasRestrictedImage; const urlTemplate = canvas.imageServiceId ? iiifImageTemplate(canvas.imageServiceId) @@ -89,7 +90,7 @@ const IIIFCanvasThumbnail: FunctionComponent = ({ {!thumbnailLoaded && !isRestricted && ( )} - {isRestricted && !userIsStaffWithRestricted ? ( + {isRestricted && role !== 'StaffWithRestricted' ? ( <> diff --git a/content/webapp/components/IIIFViewer/MainViewer.tsx b/content/webapp/components/IIIFViewer/MainViewer.tsx index 9fb855244c..ed481611c8 100644 --- a/content/webapp/components/IIIFViewer/MainViewer.tsx +++ b/content/webapp/components/IIIFViewer/MainViewer.tsx @@ -213,7 +213,8 @@ const ItemRenderer = memo(({ style, index, data }: ItemRendererProps) => { const { scrollVelocity, canvases, externalAccessService } = data; const [mainLoaded, setMainLoaded] = useState(false); const currentCanvas = canvases[index]; - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); + const role = user?.role; const urlTemplateMain = currentCanvas.imageServiceId ? iiifImageTemplate(currentCanvas.imageServiceId) : undefined; @@ -275,7 +276,7 @@ const ItemRenderer = memo(({ style, index, data }: ItemRendererProps) => {
- ) : isRestricted && !userIsStaffWithRestricted ? ( + ) : isRestricted && role !== 'StaffWithRestricted' ? ( // We always want to show the restricted message to users without a role of 'StaffWithRestricted' // If the user has the correct role then officially we should check the probe service repsonse before trying to load the image. // https://iiif.io/api/auth/2.0/#probe-service diff --git a/content/webapp/components/IIIFViewer/ViewerSidebar.tsx b/content/webapp/components/IIIFViewer/ViewerSidebar.tsx index 8189870ad1..6b868bed5a 100644 --- a/content/webapp/components/IIIFViewer/ViewerSidebar.tsx +++ b/content/webapp/components/IIIFViewer/ViewerSidebar.tsx @@ -162,7 +162,7 @@ const ViewerSidebar: FunctionComponent = ({ const { authV2 } = useToggles(); const { work, transformedManifest, parentManifest } = useContext(ItemViewerContext); - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); const matchingManifest = parentManifest && @@ -194,7 +194,7 @@ const ViewerSidebar: FunctionComponent = ({ const isWorkVisibleWithPermission = digitalLocationInfo?.accessCondition === 'restricted' && - userIsStaffWithRestricted; + user?.role === 'StaffWithRestricted'; const authServices = getAuthServices({ auth, authV2 }); diff --git a/content/webapp/components/WorkDetails/WorkDetails.AvailableOnline.tsx b/content/webapp/components/WorkDetails/WorkDetails.AvailableOnline.tsx index 758f4e8cd1..734ecd284c 100644 --- a/content/webapp/components/WorkDetails/WorkDetails.AvailableOnline.tsx +++ b/content/webapp/components/WorkDetails/WorkDetails.AvailableOnline.tsx @@ -139,7 +139,7 @@ const ItemPageLink = ({ digitalLocationInfo, authServices, }) => { - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); const isDownloadable = digitalLocationInfo?.accessCondition !== 'open-with-advisory' && @@ -147,7 +147,7 @@ const ItemPageLink = ({ const isWorkVisibleWithPermission = digitalLocationInfo?.accessCondition === 'restricted' && - userIsStaffWithRestricted; + user?.role === 'StaffWithRestricted'; const manifestNeedsRegeneration = authServices?.external?.id === @@ -271,8 +271,8 @@ const WorkDetailsAvailableOnline = ({ locationOfWork, transformedManifest, }: Props) => { - const { userIsStaffWithRestricted } = useUser(); - + const { user } = useUser(); + const role = user?.role; const { authV2 } = useToggles(); const { collectionManifestsCount, @@ -287,7 +287,7 @@ const WorkDetailsAvailableOnline = ({ const [origin, setOrigin] = useState(); const tokenService = getIframeTokenSrc({ - userIsStaffWithRestricted, + role, workId: work.id, origin, auth, diff --git a/content/webapp/components/WorkDetails/index.tsx b/content/webapp/components/WorkDetails/index.tsx index 3fedcebb07..9f00d6b222 100644 --- a/content/webapp/components/WorkDetails/index.tsx +++ b/content/webapp/components/WorkDetails/index.tsx @@ -62,7 +62,7 @@ const WorkDetails: FunctionComponent = ({ digitalLocationInfo, transformedManifest, }: Props) => { - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); const isArchive = useContext(IsArchiveContext); const transformedIIIFImage = useTransformedIIIFImage(toWorkBasic(work)); const { canvases, rendering, bornDigitalStatus } = { @@ -158,7 +158,7 @@ const WorkDetails: FunctionComponent = ({ const treatAsRestricted = digitalLocationInfo?.accessCondition === 'restricted' && - !userIsStaffWithRestricted; + user?.role !== 'StaffWithRestricted'; const showAvailableOnlineSection = ((digitalLocation && shouldShowItemLink) || diff --git a/content/webapp/pages/works/[workId]/index.tsx b/content/webapp/pages/works/[workId]/index.tsx index 9f3e35b715..22d30f85d2 100644 --- a/content/webapp/pages/works/[workId]/index.tsx +++ b/content/webapp/pages/works/[workId]/index.tsx @@ -72,7 +72,8 @@ export const WorkPage: NextPage = ({ transformedManifest, }) => { useHotjar(true); - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); + const role = user?.role; const isArchive = !!( work.parts.length || (work.partOf.length > 0 && work.partOf[0].totalParts) @@ -97,7 +98,7 @@ export const WorkPage: NextPage = ({ const allOriginalPdfs = isAllOriginalPdfs(canvases || []); const shouldShowItemLink = showItemLink({ - userIsStaffWithRestricted, + role, allOriginalPdfs, hasIIIFManifest: !!transformedManifest, digitalLocation, diff --git a/content/webapp/pages/works/[workId]/items.tsx b/content/webapp/pages/works/[workId]/items.tsx index 3de2d75198..c3ae641ee3 100644 --- a/content/webapp/pages/works/[workId]/items.tsx +++ b/content/webapp/pages/works/[workId]/items.tsx @@ -127,7 +127,8 @@ const ItemPage: NextPage = ({ parentManifest, }) => { useHotjar(true); - const { userIsStaffWithRestricted } = useUser(); + const { user } = useUser(); + const role = user?.role; const { authV2 } = useToggles(); const transformedManifest = compressedTransformedManifest && @@ -142,7 +143,7 @@ const ItemPage: NextPage = ({ }; const needsModal = checkModalRequired({ - userIsStaffWithRestricted, + role, auth, isAnyImageOpen, authV2, @@ -166,7 +167,7 @@ const ItemPage: NextPage = ({ ((authV2 && auth?.v2.tokenService) || (!authV2 && auth?.v1.tokenService)) && origin; const tryAndGetRestrictedAuthCookie = - userIsStaffWithRestricted && + role === 'StaffWithRestricted' && authServices?.external?.id === 'https://iiif.wellcomecollection.org/auth/v2/access/restrictedlogin'; // showViewer is true by default, so the noScriptViewer is available without javascript @@ -202,7 +203,7 @@ const ItemPage: NextPage = ({ function receiveMessage(event: MessageEvent) { const data = event.data; const tokenService = getIframeTokenSrc({ - userIsStaffWithRestricted, + role, workId: work.id, origin: window.origin, auth, @@ -252,7 +253,7 @@ const ItemPage: NextPage = ({ id={iframeId} title="Authentication" src={getIframeTokenSrc({ - userIsStaffWithRestricted, + role, workId: work.id, origin, auth, diff --git a/content/webapp/utils/iiif/v3/index.ts b/content/webapp/utils/iiif/v3/index.ts index e0f397e154..1c6e0c9170 100644 --- a/content/webapp/utils/iiif/v3/index.ts +++ b/content/webapp/utils/iiif/v3/index.ts @@ -368,13 +368,13 @@ export function getAuthServices({ } export function getIframeTokenSrc({ - userIsStaffWithRestricted, + role, workId, origin, auth, authV2, }: { - userIsStaffWithRestricted: boolean; + role?: string; workId: string; origin?: string; auth: Auth | undefined; @@ -387,7 +387,7 @@ export function getIframeTokenSrc({ const useV2TokenService = (authServices?.external?.id === 'https://iiif.wellcomecollection.org/auth/v2/access/restrictedlogin' && - userIsStaffWithRestricted) || + role === 'StaffWithRestricted') || authV2; if (useV2TokenService && auth?.v2.tokenService) { return `${auth.v2.tokenService.id}?messageId=${workId}&origin=${origin}`; @@ -397,19 +397,19 @@ export function getIframeTokenSrc({ } type checkModalParams = { - userIsStaffWithRestricted: boolean; + role?: string; auth?: Auth; isAnyImageOpen?: boolean; authV2?: boolean; }; export function checkModalRequired(params: checkModalParams): boolean { - const { userIsStaffWithRestricted, auth, isAnyImageOpen, authV2 } = params; + const { role, auth, isAnyImageOpen, authV2 } = params; const authServices = getAuthServices({ auth, authV2 }); if (authServices?.active) { return true; } else if (authServices?.external) { - if (isAnyImageOpen || userIsStaffWithRestricted) { + if (isAnyImageOpen || role === 'StaffWithRestricted') { return false; } else { return true; diff --git a/content/webapp/utils/works.ts b/content/webapp/utils/works.ts index 94e1571d8f..e0c7f94006 100644 --- a/content/webapp/utils/works.ts +++ b/content/webapp/utils/works.ts @@ -359,7 +359,7 @@ export function getFirstAccessCondition( } export function showItemLink({ - userIsStaffWithRestricted, + role, allOriginalPdfs, hasIIIFManifest, digitalLocation, @@ -367,7 +367,7 @@ export function showItemLink({ canvases, bornDigitalStatus, }: { - userIsStaffWithRestricted: boolean; + role?: string; allOriginalPdfs: boolean; hasIIIFManifest: boolean; digitalLocation?: DigitalLocation; @@ -385,12 +385,12 @@ export function showItemLink({ const hasVideo = hasItemType(canvases, 'Video'); const hasSound = hasItemType(canvases, 'Sound') || hasItemType(canvases, 'Audio'); - if (accessCondition === 'restricted' && userIsStaffWithRestricted) { + if (accessCondition === 'restricted' && role === 'StaffWithRestricted') { return true; } if ( accessCondition === 'closed' || - (accessCondition === 'restricted' && !userIsStaffWithRestricted) + (accessCondition === 'restricted' && role !== 'StaffWithRestricted') ) { return false; } else if (