Files
outline/server/policies/apiKey.ts
T
Tom MoorandClaude Opus 4.7 4883071059 Allow viewers to create and access API keys (#12278)
* Allow viewers to create and access API keys

Still guarded by their view permissions

* Drop Member role gate from apiKeys routes

Lets viewers reach the createApiKey/listApiKeys/delete policy checks now
that the policy itself permits them. Updates CleanupDemotedUserTask to
retain viewer keys and adds coverage for viewer create + guest reject.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* Tighten apiKeys test assertions and broaden viewer coverage

- Use not.toBeNull() instead of toBeTruthy() for retention check
- Add viewer coverage for apiKeys.list and apiKeys.delete

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-06 19:35:59 -04:00

45 lines
913 B
TypeScript

import { TeamPreference } from "@shared/types";
import { ApiKey, User, Team } from "@server/models";
import { allow } from "./cancan";
import {
and,
isCloudHosted,
isOwner,
isTeamModel,
isTeamMutable,
or,
} from "./utils";
allow(User, "createApiKey", Team, (actor, team) =>
and(
isTeamModel(actor, team),
isTeamMutable(actor),
!actor.isGuest,
!actor.isSuspended,
actor.isAdmin ||
!!team?.getPreference(TeamPreference.MembersCanCreateApiKey)
)
);
allow(User, "listApiKeys", Team, (actor, team) =>
and(
//
isCloudHosted(),
isTeamModel(actor, team),
actor.isAdmin
)
);
allow(User, ["read", "update", "delete"], ApiKey, (actor, apiKey) =>
and(
isTeamModel(actor, apiKey?.user),
or(
actor.isAdmin,
and(
isOwner(actor, apiKey),
!!actor.team?.getPreference(TeamPreference.MembersCanCreateApiKey)
)
)
)
);