-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from ItKlubBozoLagan/feature/organisation-perm…
…issions Feature/organisation permissions
- Loading branch information
Showing
33 changed files
with
641 additions
and
254 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
48 changes: 48 additions & 0 deletions
48
apps/backend/src/database/migrations/0040_add_organisations_permissions.ts
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,48 @@ | ||
import { OrganisationMemberV3, OrganisationPermissions, OrganisationV1 } from "@kontestis/models"; | ||
import { EMPTY_PERMISSIONS, grantPermission } from "permissio"; | ||
import { Migration } from "scyllo"; | ||
|
||
import { Database } from "../Database"; | ||
|
||
type MigrationType = { | ||
organisation_members: OrganisationMemberV3; | ||
organisations: OrganisationV1; | ||
}; | ||
|
||
export const migration_add_organisations_permissions: Migration<MigrationType> = async ( | ||
database, | ||
log | ||
) => { | ||
await database.raw("ALTER TABLE organisation_members ADD permissions BIGINT"); | ||
|
||
const organisationMembers = await database.selectFrom("organisation_members", "*", {}); | ||
|
||
const organisations = await database.selectFrom("organisations", "*", {}); | ||
|
||
const organisationsById: Record<string, OrganisationV1> = {}; | ||
|
||
for (const organisation of organisations) { | ||
organisationsById[organisation.id.toString()] = organisation; | ||
} | ||
|
||
for (const organisationMember of organisationMembers) { | ||
const organisation = organisationsById[organisationMember.organisation_id.toString()]; | ||
|
||
await Database.update( | ||
"organisation_members", | ||
{ | ||
permissions: | ||
organisationMember.id === organisation?.owner | ||
? grantPermission(EMPTY_PERMISSIONS, OrganisationPermissions.ADMIN) | ||
: EMPTY_PERMISSIONS, | ||
}, | ||
{ | ||
id: organisationMember.id, | ||
user_id: organisationMember.user_id, | ||
organisation_id: organisationMember.organisation_id, | ||
} | ||
); | ||
} | ||
|
||
log("Done"); | ||
}; |
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
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 |
---|---|---|
@@ -1,27 +1,13 @@ | ||
import { | ||
AdminPermissions, | ||
ContestMemberPermissions, | ||
hasAdminPermission, | ||
hasContestPermission, | ||
Snowflake, | ||
} from "@kontestis/models"; | ||
import { ContestMemberPermissions, Snowflake } from "@kontestis/models"; | ||
import { Request } from "express"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
import { SafeError } from "../errors/SafeError"; | ||
import { mustHaveContestPermission } from "../preconditions/hasPermission"; | ||
import { extractContest } from "./extractContest"; | ||
import { extractContestMember } from "./extractContestMember"; | ||
import { extractUser } from "./extractUser"; | ||
|
||
export const extractModifiableContest = async (req: Request, contestId?: Snowflake) => { | ||
const [user, contest] = await Promise.all([extractUser(req), extractContest(req, contestId)]); | ||
const contest = await extractContest(req, contestId); | ||
|
||
if (hasAdminPermission(user.permissions, AdminPermissions.EDIT_CONTEST)) return contest; | ||
|
||
const member = await extractContestMember(req, contest.id); | ||
|
||
if (!hasContestPermission(member.contest_permissions, ContestMemberPermissions.EDIT)) | ||
throw new SafeError(StatusCodes.FORBIDDEN); | ||
await mustHaveContestPermission(req, ContestMemberPermissions.EDIT, contest.id); | ||
|
||
return contest; | ||
}; |
15 changes: 4 additions & 11 deletions
15
apps/backend/src/extractors/extractModifiableOrganisation.ts
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 |
---|---|---|
@@ -1,20 +1,13 @@ | ||
import { AdminPermissions, hasAdminPermission, Snowflake } from "@kontestis/models"; | ||
import { OrganisationPermissions, Snowflake } from "@kontestis/models"; | ||
import { Request } from "express"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
import { SafeError } from "../errors/SafeError"; | ||
import { mustHaveOrganisationPermission } from "../preconditions/hasPermission"; | ||
import { extractOrganisation } from "./extractOrganisation"; | ||
import { extractUser } from "./extractUser"; | ||
|
||
export const extractModifiableOrganisation = async (req: Request, organisationId?: Snowflake) => { | ||
const user = await extractUser(req); | ||
const organisation = await extractOrganisation(req, organisationId); | ||
|
||
if ( | ||
hasAdminPermission(user.permissions, AdminPermissions.EDIT_ORGANISATIONS) || | ||
user.id === organisation.owner | ||
) | ||
return organisation; | ||
await mustHaveOrganisationPermission(req, OrganisationPermissions.ADMIN, organisation.id); | ||
|
||
throw new SafeError(StatusCodes.FORBIDDEN); | ||
return organisation; | ||
}; |
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,27 @@ | ||
import { Snowflake } from "@kontestis/models"; | ||
import { Request } from "express"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
import { Database } from "../database/Database"; | ||
import { SafeError } from "../errors/SafeError"; | ||
import { extractIdFromParameters } from "../utils/extractorUtils"; | ||
import { extractUser } from "./extractUser"; | ||
import { memoizedRequestExtractor } from "./MemoizedRequestExtractor"; | ||
|
||
export const extractOrganisationMember = ( | ||
req: Request, | ||
organisationId: Snowflake = extractIdFromParameters(req, "organisation_id") | ||
) => { | ||
return memoizedRequestExtractor(req, "__organisation_member_" + organisationId, async () => { | ||
const user = await extractUser(req); | ||
|
||
const organisationMember = await Database.selectOneFrom("organisation_members", "*", { | ||
organisation_id: organisationId, | ||
user_id: user.id, | ||
}); | ||
|
||
if (!organisationMember) throw new SafeError(StatusCodes.NOT_FOUND); | ||
|
||
return organisationMember; | ||
}); | ||
}; |
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.