Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/ui/components/AuthGuard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { AcmAppShell, AcmAppShellProps } from "@ui/components/AppShell";
import FullScreenLoader from "@ui/components/AuthContext/LoadingScreen";
import { getRunEnvironmentConfig, ValidService } from "@ui/config";
import { useApi } from "@ui/util/api";
import { AppRoles } from "@common/roles";
import { AppRoles, OrgRoleDefinition } from "@common/roles";

export const CACHE_KEY_PREFIX = "auth_response_cache_";
const CACHE_DURATION = 2 * 60 * 60 * 1000; // 2 hours in milliseconds
const CACHE_DURATION = 30 * 60 * 1000; // 30 minutes in milliseconds

type CacheData = {
data: any; // Just the JSON response data
Expand Down Expand Up @@ -87,7 +87,6 @@ export const clearAuthCache = () => {
/**
* Retrieves the user's roles from the session cache for a specific service.
* @param service The service to check the cache for.
* @param route The authentication check route.
* @returns A promise that resolves to an array of roles, or null if not found in cache.
*/
export const getUserRoles = async (
Expand All @@ -105,6 +104,25 @@ export const getUserRoles = async (
return null;
};

/**
* Retrieves the user's org roles from the session cache for Core API.
* @returns A promise that resolves to an array of roles, or null if not found in cache.
*/
export const getCoreOrgRoles = async (): Promise<
OrgRoleDefinition[] | null
> => {
const { authCheckRoute } =
getRunEnvironmentConfig().ServiceConfiguration.core;
if (!authCheckRoute) {
throw new Error("no auth check route");
}
const cachedData = await getCachedResponse("core", authCheckRoute);
if (cachedData?.data?.orgRoles && Array.isArray(cachedData.data.orgRoles)) {
return cachedData.data.orgRoles;
}
return null;
};

export const AuthGuard: React.FC<
{
resourceDef: ResourceDefinition;
Expand Down
16 changes: 11 additions & 5 deletions src/ui/pages/organization/OrgInfo.page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { useState, useEffect } from "react";
import { Title, Stack, Container, Select } from "@mantine/core";
import { AuthGuard, getUserRoles } from "@ui/components/AuthGuard";
import {
AuthGuard,
getUserRoles,
getCoreOrgRoles,
} from "@ui/components/AuthGuard";
import { useApi } from "@ui/util/api";
import { AppRoles } from "@common/roles";
import { notifications } from "@mantine/notifications";
import { IconAlertCircle } from "@tabler/icons-react";
import FullScreenLoader from "@ui/components/AuthContext/LoadingScreen";
import { AllOrganizationNameList, OrganizationName } from "@acm-uiuc/js-shared";
import { useAuth } from "@ui/components/AuthContext";
import { ManageOrganizationForm } from "./ManageOrganizationForm";
import {
LeadEntry,
Expand All @@ -21,7 +24,6 @@ type OrganizationData = z.infer<typeof setOrganizationMetaBody>;

export const OrgInfoPage = () => {
const api = useApi("core");
const { orgRoles } = useAuth();
const [searchParams, setSearchParams] = useSearchParams();
const [manageableOrgs, setManagableOrgs] = useState<
OrganizationName[] | null
Expand Down Expand Up @@ -112,15 +114,19 @@ export const OrgInfoPage = () => {
useEffect(() => {
(async () => {
const appRoles = await getUserRoles("core");
if (appRoles?.includes(AppRoles.ALL_ORG_MANAGER)) {
const orgRoles = await getCoreOrgRoles();
if (appRoles === null || orgRoles === null) {
return;
}
if (appRoles.includes(AppRoles.ALL_ORG_MANAGER)) {
setManagableOrgs(AllOrganizationNameList);
return;
}
setManagableOrgs(
orgRoles.filter((x) => x.role === "LEAD").map((x) => x.org),
);
})();
}, [orgRoles]);
}, []);

// Update URL when selected org changes
const handleOrgChange = (org: OrganizationName | null) => {
Expand Down
20 changes: 18 additions & 2 deletions src/ui/pages/roomRequest/RoomRequestLanding.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ import {
type RoomRequestStatus,
} from "@common/types/roomRequest";
import { OrganizationName } from "@acm-uiuc/js-shared";
import { useSearchParams } from "react-router-dom";

export const ManageRoomRequestsPage: React.FC = () => {
const api = useApi("core");
const [semester, setSemester] = useState<string | null>(null); // TODO: Create a selector for this
const [semester, setSemesterState] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const nextSemesters = getSemesters();
const semesterOptions = [...getPreviousSemesters(), ...nextSemesters];
const [searchParams, setSearchParams] = useSearchParams();
const setSemester = (semester: string | null) => {
setSemesterState(semester);
if (semester) {
setSearchParams({ semester });
}
};
const createRoomRequest = async (
payload: RoomRequestFormValues,
): Promise<RoomRequestPostResponse> => {
Expand All @@ -45,7 +53,15 @@ export const ManageRoomRequestsPage: React.FC = () => {
};

useEffect(() => {
setSemester(nextSemesters[0].value);
const semeseterFromUrl = searchParams.get("semester") as string | null;
if (
semeseterFromUrl &&
semesterOptions.map((x) => x.value).includes(semeseterFromUrl)
) {
setSemester(semeseterFromUrl);
} else {
setSemester(nextSemesters[0].value);
}
}, []);
return (
<AuthGuard
Expand Down
Loading