Skip to content

Add permission check for Insights page and don't show it in the menu for non-owners #20593

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 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 18 additions & 4 deletions components/dashboard/src/Insights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { DownloadIcon } from "lucide-react";
import { Button } from "@podkit/buttons/Button";
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from "@podkit/dropdown/DropDown";
import { useInstallationConfiguration } from "./data/installation/installation-config-query";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";

export const Insights = () => {
const toDate = useMemo(() => Timestamp.fromDate(new Date()), []);
Expand All @@ -49,6 +50,9 @@ export const Insights = () => {
const grouped = Object.groupBy(sessions, (ws) => ws.workspace?.id ?? "unknown");
const [page, setPage] = useState(0);

const isLackingPermissions =
errorMessage instanceof ApplicationError && errorMessage.code === ErrorCodes.PERMISSION_DENIED;

return (
<>
<Header title="Insights" subtitle="Insights into workspace sessions in your organization" />
Expand All @@ -59,7 +63,7 @@ export const Insights = () => {
"md:flex-row md:items-center md:space-x-4 md:space-y-0",
)}
>
<DownloadUsage to={toDate} />
<DownloadUsage to={toDate} disabled={isLackingPermissions} />
</div>

<div
Expand All @@ -71,7 +75,16 @@ export const Insights = () => {

{errorMessage && (
<Alert type="error" className="mt-4">
{errorMessage instanceof Error ? errorMessage.message : "An error occurred."}
{isLackingPermissions ? (
<>
You don't have <span className="font-medium">Owner</span> permissions to access this
organization's insights.
</>
) : errorMessage instanceof Error ? (
errorMessage.message
) : (
"An error occurred."
)}
</Alert>
)}

Expand Down Expand Up @@ -151,8 +164,9 @@ export const Insights = () => {

type DownloadUsageProps = {
to: Timestamp;
disabled?: boolean;
};
export const DownloadUsage = ({ to }: DownloadUsageProps) => {
export const DownloadUsage = ({ to, disabled }: DownloadUsageProps) => {
const { data: org } = useCurrentOrg();
const { toast } = useToast();
// When we start the download, we disable the button for a short time
Expand Down Expand Up @@ -184,7 +198,7 @@ export const DownloadUsage = ({ to }: DownloadUsageProps) => {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="secondary" className="gap-1" disabled={downloadDisabled}>
<Button variant="secondary" className="gap-1" disabled={disabled || downloadDisabled}>
<DownloadIcon strokeWidth={3} className="w-4" />
<span>Export as CSV</span>
</Button>
Expand Down
20 changes: 11 additions & 9 deletions components/dashboard/src/menu/OrganizationSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function OrganizationSelector() {
const orgs = useOrganizations();
const currentOrg = useCurrentOrg();
const members = useListOrganizationMembers().data ?? [];
const owner = useIsOwner();
const isOwner = useIsOwner();
const hasMemberPermission = useHasRolePermission(OrganizationRole.MEMBER);
const { data: billingMode } = useOrgBillingMode();
const getOrgURL = useGetOrgURL();
Expand Down Expand Up @@ -78,13 +78,15 @@ export default function OrganizationSelector() {
link: "/members",
});
if (isDedicated) {
linkEntries.push({
title: "Insights",
customContent: <LinkEntry>Insights</LinkEntry>,
active: false,
separator: false,
link: "/insights",
});
if (isOwner) {
linkEntries.push({
title: "Insights",
customContent: <LinkEntry>Insights</LinkEntry>,
active: false,
separator: false,
link: "/insights",
});
}
} else {
linkEntries.push({
title: "Usage",
Expand All @@ -95,7 +97,7 @@ export default function OrganizationSelector() {
});
}
// Show billing if user is an owner of current org
if (owner) {
if (isOwner) {
if (billingMode?.mode === "usage-based") {
linkEntries.push({
title: "Billing",
Expand Down
Loading