Skip to content

Commit

Permalink
Revert "Move isUserStaffWithRestricted at the user context level"
Browse files Browse the repository at this point in the history
rcantin-w authored Nov 11, 2024
1 parent afb083c commit 4ab4d53
Showing 10 changed files with 36 additions and 41 deletions.
13 changes: 2 additions & 11 deletions common/views/components/UserProvider/UserProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<void>;
};

const defaultUserContext: Props = {
user: undefined,
userIsStaffWithRestricted: false,
state: 'initial',
reload: async () => undefined,
};
@@ -38,8 +36,6 @@ export function useUser(): Props {

const UserProvider: FunctionComponent<PropsWithChildren> = ({ children }) => {
const [user, setUser] = useState<UserInfo>();
const [userIsStaffWithRestricted, setUserIsStaffWithRestricted] =
useState(false);
const [state, setState] = useState<State>('initial');

const fetchUser = async (abortSignal?: AbortSignal, refetch = false) => {
@@ -63,11 +59,7 @@ const UserProvider: FunctionComponent<PropsWithChildren> = ({ 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<PropsWithChildren> = ({ children }) => {
<UserContext.Provider
value={{
user,
userIsStaffWithRestricted,
state,
reload: (abortSignal?: AbortSignal) => fetchUser(abortSignal, true),
}}
5 changes: 3 additions & 2 deletions content/webapp/components/IIIFViewer/IIIFCanvasThumbnail.tsx
Original file line number Diff line number Diff line change
@@ -76,7 +76,8 @@ const IIIFCanvasThumbnail: FunctionComponent<IIIFCanvasThumbnailProps> = ({
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<IIIFCanvasThumbnailProps> = ({
{!thumbnailLoaded && !isRestricted && (
<LL $small={true} $lighten={true} />
)}
{isRestricted && !userIsStaffWithRestricted ? (
{isRestricted && role !== 'StaffWithRestricted' ? (
<>
<Padlock />
<span className="visually-hidden">
5 changes: 3 additions & 2 deletions content/webapp/components/IIIFViewer/MainViewer.tsx
Original file line number Diff line number Diff line change
@@ -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) => {
<div style={{ display: 'flex', justifyContent: 'center' }}>
<LL $lighten={true} />
</div>
) : 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
4 changes: 2 additions & 2 deletions content/webapp/components/IIIFViewer/ViewerSidebar.tsx
Original file line number Diff line number Diff line change
@@ -162,7 +162,7 @@ const ViewerSidebar: FunctionComponent<ViewerSidebarProps> = ({
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<ViewerSidebarProps> = ({

const isWorkVisibleWithPermission =
digitalLocationInfo?.accessCondition === 'restricted' &&
userIsStaffWithRestricted;
user?.role === 'StaffWithRestricted';

const authServices = getAuthServices({ auth, authV2 });

Original file line number Diff line number Diff line change
@@ -139,15 +139,15 @@ const ItemPageLink = ({
digitalLocationInfo,
authServices,
}) => {
const { userIsStaffWithRestricted } = useUser();
const { user } = useUser();

const isDownloadable =
digitalLocationInfo?.accessCondition !== 'open-with-advisory' &&
downloadOptions.length > 0;

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<string | undefined>();

const tokenService = getIframeTokenSrc({
userIsStaffWithRestricted,
role,
workId: work.id,
origin,
auth,
4 changes: 2 additions & 2 deletions content/webapp/components/WorkDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ const WorkDetails: FunctionComponent<Props> = ({
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<Props> = ({

const treatAsRestricted =
digitalLocationInfo?.accessCondition === 'restricted' &&
!userIsStaffWithRestricted;
user?.role !== 'StaffWithRestricted';

const showAvailableOnlineSection =
((digitalLocation && shouldShowItemLink) ||
5 changes: 3 additions & 2 deletions content/webapp/pages/works/[workId]/index.tsx
Original file line number Diff line number Diff line change
@@ -72,7 +72,8 @@ export const WorkPage: NextPage<Props> = ({
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<Props> = ({
const allOriginalPdfs = isAllOriginalPdfs(canvases || []);

const shouldShowItemLink = showItemLink({
userIsStaffWithRestricted,
role,
allOriginalPdfs,
hasIIIFManifest: !!transformedManifest,
digitalLocation,
11 changes: 6 additions & 5 deletions content/webapp/pages/works/[workId]/items.tsx
Original file line number Diff line number Diff line change
@@ -127,7 +127,8 @@ const ItemPage: NextPage<Props> = ({
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<Props> = ({
};

const needsModal = checkModalRequired({
userIsStaffWithRestricted,
role,
auth,
isAnyImageOpen,
authV2,
@@ -166,7 +167,7 @@ const ItemPage: NextPage<Props> = ({
((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<Props> = ({
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<Props> = ({
id={iframeId}
title="Authentication"
src={getIframeTokenSrc({
userIsStaffWithRestricted,
role,
workId: work.id,
origin,
auth,
12 changes: 6 additions & 6 deletions content/webapp/utils/iiif/v3/index.ts
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 4 additions & 4 deletions content/webapp/utils/works.ts
Original file line number Diff line number Diff line change
@@ -359,15 +359,15 @@ export function getFirstAccessCondition(
}

export function showItemLink({
userIsStaffWithRestricted,
role,
allOriginalPdfs,
hasIIIFManifest,
digitalLocation,
accessCondition,
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 (

0 comments on commit 4ab4d53

Please sign in to comment.