-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #2549 from scott-ray-wilson/org-default-role
Feat: Default Org Membership Role
- Loading branch information
Showing
18 changed files
with
383 additions
and
56 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
backend/src/db/migrations/20241007202149_default-org-membership-roles.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,29 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "@app/db/schemas"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
// org default role | ||
if (await knex.schema.hasTable(TableName.Organization)) { | ||
const hasDefaultRoleCol = await knex.schema.hasColumn(TableName.Organization, "defaultMembershipRole"); | ||
|
||
if (!hasDefaultRoleCol) { | ||
await knex.schema.alterTable(TableName.Organization, (tb) => { | ||
tb.string("defaultMembershipRole").notNullable().defaultTo("member"); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
// org default role | ||
if (await knex.schema.hasTable(TableName.Organization)) { | ||
const hasDefaultRoleCol = await knex.schema.hasColumn(TableName.Organization, "defaultMembershipRole"); | ||
|
||
if (hasDefaultRoleCol) { | ||
await knex.schema.alterTable(TableName.Organization, (tb) => { | ||
tb.dropColumn("defaultMembershipRole"); | ||
}); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { OrgMembershipRole } from "@app/db/schemas"; | ||
import { TFeatureSet } from "@app/ee/services/license/license-types"; | ||
import { BadRequestError, NotFoundError } from "@app/lib/errors"; | ||
import { TOrgRoleDALFactory } from "@app/services/org/org-role-dal"; | ||
|
||
const RESERVED_ORG_ROLE_SLUGS = Object.values(OrgMembershipRole).filter((role) => role !== "custom"); | ||
|
||
// this is only for updating an org | ||
export const getDefaultOrgMembershipRoleForUpdateOrg = async ({ | ||
membershipRoleSlug, | ||
orgRoleDAL, | ||
plan, | ||
orgId | ||
}: { | ||
orgId: string; | ||
membershipRoleSlug: string; | ||
orgRoleDAL: TOrgRoleDALFactory; | ||
plan: TFeatureSet; | ||
}) => { | ||
const isCustomRole = !RESERVED_ORG_ROLE_SLUGS.includes(membershipRoleSlug as OrgMembershipRole); | ||
|
||
if (isCustomRole) { | ||
if (!plan?.rbac) | ||
throw new BadRequestError({ | ||
message: | ||
"Failed to set custom default role due to plan RBAC restriction. Upgrade plan to set custom default org membership role." | ||
}); | ||
|
||
const customRole = await orgRoleDAL.findOne({ slug: membershipRoleSlug, orgId }); | ||
if (!customRole) throw new NotFoundError({ name: "UpdateOrg", message: "Organization role not found" }); | ||
|
||
// use ID for default role | ||
return customRole.id; | ||
} | ||
|
||
// not custom, use reserved slug | ||
return membershipRoleSlug; | ||
}; | ||
|
||
// this is only for creating an org membership | ||
export const getDefaultOrgMembershipRole = async ( | ||
defaultOrgMembershipRole: string // can either be ID or reserved slug | ||
) => { | ||
const isCustomRole = !RESERVED_ORG_ROLE_SLUGS.includes(defaultOrgMembershipRole as OrgMembershipRole); | ||
|
||
if (isCustomRole) | ||
return { | ||
roleId: defaultOrgMembershipRole, | ||
role: OrgMembershipRole.Custom | ||
}; | ||
|
||
// will be reserved slug | ||
return { roleId: undefined, role: defaultOrgMembershipRole as OrgMembershipRole }; | ||
}; |
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.