Skip to content
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

Production deployment #338

Merged
merged 1 commit into from
Feb 25, 2024
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
263 changes: 170 additions & 93 deletions app/src/_lib/auth/comparePermissionSets.test.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,180 @@
import { expect, test } from "vitest";
import { describe, expect, test } from "vitest";
import comparePermissionSets from "./comparePermissionSets";

test("simple", () => {
expect(comparePermissionSets([], [])).toBe(true);
describe("comparePermissionSets", () => {
test("simple", () => {
expect(comparePermissionSets([], [])).toBe(true);

expect(
comparePermissionSets([{ resource: "lastSeen", operation: "read" }], []),
).toBe(false);
expect(
comparePermissionSets([{ resource: "lastSeen", operation: "read" }], []),
).toBe(false);

expect(
comparePermissionSets(
[{ resource: "lastSeen", operation: "read" }],
[{ resource: "lastSeen", operation: "read" }],
),
).toBe(true);
});
expect(
comparePermissionSets(
[{ resource: "lastSeen", operation: "read" }],
[{ resource: "lastSeen", operation: "read" }],
),
).toBe(true);
});

test.skip("notes", () => {
expect(
comparePermissionSets(
[{ resource: "note", operation: "create" }],
[{ resource: "note", operation: "manage" }],
),
).toBe(true);
test("notes", () => {
expect(
comparePermissionSets(
[{ resource: "note", operation: "create" }],
[{ resource: "note", operation: "manage" }],
),
).toBe(true);

expect(
comparePermissionSets(
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "manage",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
),
).toBe(true);
expect(
comparePermissionSets(
[{ resource: "note", operation: "create" }],
[
{
resource: "note",
operation: "manage",
attributes: [{ key: "noteTypeId", value: "1" }],
},
],
),
).toBe(false);

expect(
comparePermissionSets(
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "manage",
attributes: [
{
key: "noteTypeId",
value: "2",
},
],
},
],
),
).toBe(false);
});
expect(
comparePermissionSets(
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "manage",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
),
).toBe(true);

expect(
comparePermissionSets(
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "manage",
attributes: [
{
key: "noteTypeId",
value: "2",
},
],
},
],
),
).toBe(false);

expect(
comparePermissionSets(
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "*",
},
],
},
],
),
).toBe(true);

expect(
comparePermissionSets(
[
{
resource: "note",
operation: "read",
attributes: [
{
key: "noteTypeId",
value: "1",
},
],
},
],
[
{
resource: "note",
operation: "create",
attributes: [
{
key: "noteTypeId",
value: "*",
},
],
},
],
),
).toBe(false);
});

test("login", () => {
expect(
comparePermissionSets(
[{ resource: "login", operation: "manage" }],
[{ resource: "login", operation: "manage" }],
),
).toBe(true);

test("login", () => {
expect(
comparePermissionSets(
[{ resource: "login", operation: "manage" }],
[{ resource: "login", operation: "manage" }],
),
).toBe(true);
expect(
comparePermissionSets([{ resource: "login", operation: "manage" }], []),
).toBe(false);

expect(
comparePermissionSets(
[{ resource: "login", operation: "manage" }],
[
{ resource: "login", operation: "manage" },
{ resource: "login", operation: "negate" },
],
),
).toBe(false);
expect(
comparePermissionSets(
[{ resource: "login", operation: "manage" }],
[
{ resource: "login", operation: "manage" },
{ resource: "login", operation: "negate" },
],
),
).toBe(false);
});
});
37 changes: 15 additions & 22 deletions app/src/_lib/auth/comparePermissionSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,33 @@ export default function comparePermissionSets(
givenPermissionSet.resource === requiredPermissionSet.resource,
);

// Has negate operation
if (
givenPermissionSetsForResource.some(
(givenPermissionSet) => givenPermissionSet.operation === "negate",
)
)
return false;

// Has manage operation
if (
givenPermissionSetsForResource.some(
(givenPermissionSet) => givenPermissionSet.operation === "manage",
)
) {
if (requiredPermissionSet.operation === "negate") return false;
return true;
}

// Has matching operation
if (
givenPermissionSetsForResource.some((givenPermissionSet) => {
if (givenPermissionSet.operation !== requiredPermissionSet.operation)
return false;

if (!requiredPermissionSet.attributes) return true;
if (requiredPermissionSet.attributes) {
if (!givenPermissionSet.attributes) return false;

if (!givenPermissionSet.attributes) return false;
const result = hasMatchingAttributes(
requiredPermissionSet.attributes,
givenPermissionSet.attributes,
);
if (!result) return false;
}

const result = hasMatchingAttributes(
requiredPermissionSet.attributes,
givenPermissionSet.attributes,
);
if (!requiredPermissionSet.attributes && givenPermissionSet.attributes)
return false;

if (!result) return false;
if (
requiredPermissionSet.operation !== givenPermissionSet.operation &&
givenPermissionSet.operation !== "manage"
)
return false;

return true;
})
Expand Down
10 changes: 2 additions & 8 deletions app/src/app/(app)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ export default async function AppLayout({
await requireConfirmedEmailForPage(authentication.session);

if (
authentication.authorize([
!authentication.authorize([
{
resource: "login",
operation: "manage",
},
]) === false ||
authentication.authorize([
{
resource: "login",
operation: "negate",
},
]) === true
])
)
redirect("/clearance");

Expand Down
2 changes: 1 addition & 1 deletion app/src/app/(app)/roles/_components/Suggestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const Suggestions = ({ className, onClick }: Props) => {

return (
<>
<div className={clsx(className, "flex items-center gap-4")}>
<div className={clsx(className, "flex items-center gap-2")}>
<p className="flex items-center font-bold gap-2">
<RiBardFill /> Vorschläge
</p>
Expand Down
Loading
Loading