Skip to content

feat: add organization-scoped permission checks to deployment settings #14063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use old permission for old pages
Originally I thought I should update these to use the org-based
permissions query but I think maybe I should scope my changes to just
the multi-org settings page and leave the old pages alone for now.
  • Loading branch information
code-asher committed Aug 6, 2024
commit 0b4b00b4adb5ae4846fcce8b9d1cdbd1072adcb3
7 changes: 7 additions & 0 deletions site/src/contexts/auth/permissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const checks = {
createOrganization: "createOrganization",
editAnyOrganization: "editAnyOrganization",
viewAnyGroup: "viewAnyGroup",
createGroup: "createGroup",
viewAllLicenses: "viewAllLicenses",
} as const;

Expand Down Expand Up @@ -118,6 +119,12 @@ export const permissionsToCheck = {
},
action: "read",
},
[checks.createGroup]: {
object: {
resource_type: "group",
},
action: "create",
},
[checks.viewAllLicenses]: {
object: {
resource_type: "license",
Expand Down
21 changes: 2 additions & 19 deletions site/src/pages/GroupsPage/GroupsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
import { getErrorMessage } from "api/errors";
import { groups } from "api/queries/groups";
import { organizationPermissions } from "api/queries/organizations";
import { displayError } from "components/GlobalSnackbar/utils";
import { Loader } from "components/Loader/Loader";
import { useDashboard } from "modules/dashboard/useDashboard";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
import { pageTitle } from "utils/page";
import GroupsPageView from "./GroupsPageView";

export const GroupsPage: FC = () => {
const { organizations } = useDashboard();
const { permissions } = useAuthenticated();
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility();
const organization = organizations.find((o) => o.is_default);
const groupsQuery = useQuery(groups("default"));
const permissionsQuery = useQuery(organizationPermissions(organization?.id));

useEffect(() => {
if (groupsQuery.error) {
Expand All @@ -26,19 +22,6 @@ export const GroupsPage: FC = () => {
}
}, [groupsQuery.error]);

useEffect(() => {
if (permissionsQuery.error) {
displayError(
getErrorMessage(permissionsQuery.error, "Unable to load permissions."),
);
}
}, [permissionsQuery.error]);

const permissions = permissionsQuery.data;
if (!permissions) {
return <Loader />;
}

return (
<>
<Helmet>
Expand Down
8 changes: 2 additions & 6 deletions site/src/pages/UsersPage/UsersLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import GroupAdd from "@mui/icons-material/GroupAddOutlined";
import PersonAdd from "@mui/icons-material/PersonAddOutlined";
import Button from "@mui/material/Button";
import { type FC, Suspense } from "react";
import { useQuery } from "react-query";
import {
Link as RouterLink,
Outlet,
useNavigate,
useLocation,
} from "react-router-dom";
import { organizationPermissions } from "api/queries/organizations";
import { Loader } from "components/Loader/Loader";
import { Margins } from "components/Margins/Margins";
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader";
Expand All @@ -21,13 +19,11 @@ import { linkToUsers } from "modules/navigation";

export const UsersLayout: FC = () => {
const { permissions } = useAuthenticated();
const { experiments, organizations } = useDashboard();
const { experiments } = useDashboard();
const navigate = useNavigate();
const feats = useFeatureVisibility();
const location = useLocation();
const activeTab = location.pathname.endsWith("groups") ? "groups" : "users";
const organization = organizations.find((o) => o.is_default);
const permissionsQuery = useQuery(organizationPermissions(organization?.id));

const canViewOrganizations =
feats.multiple_organizations && experiments.includes("multi-organization");
Expand All @@ -48,7 +44,7 @@ export const UsersLayout: FC = () => {
Create user
</Button>
)}
{permissionsQuery.data?.createGroup && feats.template_rbac && (
{permissions.createGroup && feats.template_rbac && (
<Button
component={RouterLink}
startIcon={<GroupAdd />}
Expand Down
1 change: 1 addition & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,7 @@ export const MockPermissions: Permissions = {
createOrganization: true,
editAnyOrganization: true,
viewAnyGroup: true,
createGroup: true,
viewAllLicenses: true,
};

Expand Down
Loading