-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add access level to db * fix white space 🤦 * fix migrations * migrations * project visibility * merge * read project access level * remove unneccesary change * refresh status * add badges * 404 page * 404 page * fix height * remove import * fix tests * fix webpack * refactor access * fix tests * send permissions model to the client * fix conflict issue * fix lock * remove unneeded dep * split pr * Revert "send permissions model to the client" This reverts commit 275a138. * remove enum * remove enums * refactor enum * allow the creator to access their own projects * fix import * fix import * add tests * add tests * add mock * tests * tests * tests * change to docker * clear db * clear order * fix structure * fix env * readme * env vars * fix * fix CR * cr * check project ownership * test * fix import * text * cr fixes * minor * add to env sample * use validators * imports * add validators * change validator * handle anonymous user id * don't return a boolean from validators * unify session id logic * get only the owner id * fix leftover * change validators * change to optional * change to optional * fix db * tests * fix owner id * tests * minor * fix tests * get only owner id * remove uneeded db call * fix test * cr changes * fix cr * add strings to env vars * add tests * add anonymous test * add anonymous test * add validators to routes * fix access * cr fixes * added tests for deleted * typo * minor code style changes * cr fixes * cr fixes * remove 'clearDb' function * fix link issue
- Loading branch information
Showing
63 changed files
with
7,213 additions
and
1,909 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE if not exists project_access ( | ||
id serial, | ||
project_id text not null references project(proj_id) ON DELETE CASCADE, | ||
access_level integer not null default 0, | ||
modified_at timestamp with time zone not null default now() | ||
); | ||
|
||
ALTER TABLE ONLY "project_access" | ||
ADD CONSTRAINT "unique_project_access" UNIQUE ("project_id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function OpenFgaClient() { | ||
return { | ||
write: jest.fn(), | ||
check: jest.fn(), | ||
} | ||
} | ||
|
||
export const CredentialsMethod = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AccessValidator, ensure, getUser } from '../util/api.server' | ||
import { UserProjectPermission } from '../types' | ||
import { Params } from '@remix-run/react' | ||
import { Status } from '../util/statusCodes' | ||
import { hasUserProjectPermission } from '../services/permissionsService.server' | ||
import { getProjectOwnerById } from '../models/project.server' | ||
|
||
export function validateProjectAccess( | ||
permission: UserProjectPermission, | ||
{ | ||
errorMessage, | ||
status, | ||
getProjectId, | ||
includeDeleted = false, | ||
}: { | ||
errorMessage?: string | ||
status?: number | ||
getProjectId: (params: Params<string>) => string | null | undefined | ||
includeDeleted?: boolean | ||
}, | ||
): AccessValidator { | ||
return async function (req: Request, params: Params<string>) { | ||
const projectId = getProjectId(params) | ||
ensure(projectId != null, 'project id is null', Status.BAD_REQUEST) | ||
|
||
const ownerId = await getProjectOwnerById({ id: projectId }, { includeDeleted: includeDeleted }) | ||
ensure(ownerId != null, `Project ${projectId} not found or has no owner`, Status.NOT_FOUND) | ||
|
||
const user = await getUser(req) | ||
const userId = user?.user_id ?? null | ||
const isCreator = userId ? ownerId === userId : false | ||
|
||
const allowed = isCreator || (await hasUserProjectPermission(projectId, userId, permission)) | ||
ensure(allowed, errorMessage ?? 'Unauthorized Access', status ?? Status.UNAUTHORIZED) | ||
} | ||
} | ||
|
||
export const ALLOW: AccessValidator = async (request: Request, params: Params<string>) => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
jest.mock('@openfga/sdk') | ||
import { prisma } from '../db.server' | ||
import { | ||
createTestProject, | ||
createTestProjectAccess, | ||
createTestUser, | ||
truncateTables, | ||
} from '../test-util' | ||
import { setProjectAccess } from './projectAccess.server' | ||
import * as permissionsService from '../services/permissionsService.server' | ||
|
||
describe('projectAccess model', () => { | ||
afterAll(async () => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
describe('setProjectAccess', () => { | ||
beforeAll(async () => { | ||
await truncateTables([ | ||
prisma.projectAccess, | ||
prisma.projectCollaborator, | ||
prisma.userDetails, | ||
prisma.persistentSession, | ||
prisma.project, | ||
prisma.projectID, | ||
]) | ||
}) | ||
beforeEach(async () => { | ||
await createTestUser(prisma, { id: 'bob' }) | ||
await createTestUser(prisma, { id: 'alice' }) | ||
await createTestProject(prisma, { id: 'one', ownerId: 'bob' }) | ||
await createTestProject(prisma, { id: 'two', ownerId: 'bob' }) | ||
await createTestProjectAccess(prisma, { projectId: 'one', accessLevel: 0 }) | ||
await createTestProjectAccess(prisma, { projectId: 'two', accessLevel: 1 }) | ||
jest.spyOn(permissionsService, 'setProjectAccess').mockResolvedValue() | ||
}) | ||
afterEach(async () => { | ||
await truncateTables([ | ||
prisma.projectAccess, | ||
prisma.projectCollaborator, | ||
prisma.userDetails, | ||
prisma.persistentSession, | ||
prisma.project, | ||
prisma.projectID, | ||
]) | ||
jest.spyOn(permissionsService, 'setProjectAccess').mockRestore() | ||
}) | ||
it('sets the access level for a project', async () => { | ||
await setProjectAccess({ projectId: 'one', accessLevel: 1 }) | ||
const projectAccess = await prisma.projectAccess.findFirst({ | ||
where: { project_id: 'one' }, | ||
}) | ||
expect(projectAccess?.access_level).toEqual(1) | ||
expect(permissionsService.setProjectAccess).toHaveBeenCalledWith('one', 1) | ||
}) | ||
it('updates the modified_at field', async () => { | ||
await setProjectAccess({ projectId: 'one', accessLevel: 1 }) | ||
const projectAccess = await prisma.projectAccess.findFirst({ | ||
where: { project_id: 'one' }, | ||
}) | ||
expect(projectAccess?.modified_at).not.toBeNull() | ||
}) | ||
it('sets the access level on the project model itself', async () => { | ||
await setProjectAccess({ projectId: 'one', accessLevel: 1 }) | ||
const project = await prisma.project.findFirst({ | ||
where: { proj_id: 'one' }, | ||
include: { ProjectAccess: true }, | ||
}) | ||
expect(project?.ProjectAccess?.access_level).toEqual(1) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { AccessLevel } from '../types' | ||
import { prisma } from '../db.server' | ||
import * as permissionsService from '../services/permissionsService.server' | ||
|
||
export async function setProjectAccess(params: { | ||
projectId: string | ||
accessLevel: AccessLevel | ||
}): Promise<void> { | ||
await prisma.$transaction(async (tx) => { | ||
await tx.projectAccess.upsert({ | ||
where: { | ||
project_id: params.projectId, | ||
}, | ||
update: { | ||
access_level: params.accessLevel, | ||
modified_at: new Date(), | ||
}, | ||
create: { | ||
project_id: params.projectId, | ||
access_level: params.accessLevel, | ||
modified_at: new Date(), | ||
}, | ||
}) | ||
await permissionsService.setProjectAccess(params.projectId, params.accessLevel) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.