-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core,schemas): edit and query resource scopes for org role
- Loading branch information
Showing
7 changed files
with
131 additions
and
12 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
type CreateOrganizationRole, | ||
OrganizationRoles, | ||
organizationRoleWithScopesGuard, | ||
organizationRoleWithScopesGuardDeprecated, | ||
} from '@logto/schemas'; | ||
import { generateStandardId } from '@logto/shared'; | ||
import { z } from 'zod'; | ||
|
@@ -44,7 +45,10 @@ export default function organizationRoleRoutes<T extends AuthedRouter>( | |
koaPagination(), | ||
koaGuard({ | ||
query: z.object({ q: z.string().optional() }), | ||
response: organizationRoleWithScopesGuard.array(), | ||
// TODO @wangsijie - Remove this once the feature is ready | ||
Check warning on line 48 in packages/core/src/routes/organization/roles.ts GitHub Actions / ESLint Report Analysispackages/core/src/routes/organization/roles.ts#L48
|
||
response: EnvSet.values.isDevFeaturesEnabled | ||
? organizationRoleWithScopesGuard.array() | ||
: organizationRoleWithScopesGuardDeprecated.array(), | ||
status: [200], | ||
}), | ||
async (ctx, next) => { | ||
|
@@ -63,30 +67,54 @@ export default function organizationRoleRoutes<T extends AuthedRouter>( | |
/** Allows to carry an initial set of scopes for creating a new organization role. */ | ||
type CreateOrganizationRolePayload = Omit<CreateOrganizationRole, 'id'> & { | ||
organizationScopeIds: string[]; | ||
resourceScopeIds: string[]; | ||
}; | ||
|
||
// TODO @wangsijie - Remove this once the feature is ready | ||
Check warning on line 73 in packages/core/src/routes/organization/roles.ts GitHub Actions / ESLint Report Analysispackages/core/src/routes/organization/roles.ts#L73
|
||
const originalCreateCard: z.ZodType< | ||
Omit<CreateOrganizationRolePayload, 'resourceScopeIds'> & { resourceScopeIds?: string[] }, | ||
z.ZodTypeDef, | ||
unknown | ||
> = OrganizationRoles.createGuard | ||
.omit({ | ||
id: true, | ||
}) | ||
.extend({ | ||
organizationScopeIds: z.array(z.string()).default([]), | ||
}); | ||
|
||
const createGuard: z.ZodType<CreateOrganizationRolePayload, z.ZodTypeDef, unknown> = | ||
OrganizationRoles.createGuard | ||
.omit({ | ||
id: true, | ||
}) | ||
.extend({ | ||
organizationScopeIds: z.array(z.string()).default([]), | ||
resourceScopeIds: z.array(z.string()).default([]), | ||
}); | ||
|
||
router.post( | ||
'/', | ||
koaGuard({ | ||
body: createGuard, | ||
body: EnvSet.values.isDevFeaturesEnabled ? createGuard : originalCreateCard, | ||
response: OrganizationRoles.guard, | ||
status: [201, 422], | ||
}), | ||
async (ctx, next) => { | ||
const { organizationScopeIds: scopeIds, ...data } = ctx.guard.body; | ||
const { organizationScopeIds, resourceScopeIds, ...data } = ctx.guard.body; | ||
const role = await roles.insert({ id: generateStandardId(), ...data }); | ||
|
||
if (scopeIds.length > 0) { | ||
await rolesScopes.insert(...scopeIds.map<[string, string]>((id) => [role.id, id])); | ||
if (organizationScopeIds.length > 0) { | ||
await rolesScopes.insert( | ||
...organizationScopeIds.map<[string, string]>((id) => [role.id, id]) | ||
); | ||
} | ||
|
||
// TODO @wangsijie - Remove this once the feature is ready | ||
Check warning on line 113 in packages/core/src/routes/organization/roles.ts GitHub Actions / ESLint Report Analysispackages/core/src/routes/organization/roles.ts#L113
|
||
if (EnvSet.values.isDevFeaturesEnabled && resourceScopeIds && resourceScopeIds.length > 0) { | ||
await rolesResourceScopes.insert( | ||
...resourceScopeIds.map<[string, string]>((id) => [role.id, id]) | ||
); | ||
} | ||
|
||
ctx.body = role; | ||
|
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 |
---|---|---|
|
@@ -20,10 +20,35 @@ export type OrganizationScopeEntity = { | |
name: string; | ||
}; | ||
|
||
/** | ||
* The simplified resource scope entity that is returned for some endpoints. | ||
*/ | ||
export type ResourceScopeEntity = { | ||
id: string; | ||
name: string; | ||
resource: { | ||
id: string; | ||
name: string; | ||
}; | ||
}; | ||
|
||
export type OrganizationRoleWithScopes = OrganizationRole & { | ||
scopes: OrganizationScopeEntity[]; | ||
resourceScopes: ResourceScopeEntity[]; | ||
}; | ||
|
||
// TODO @wangsijie - Remove this once the feature is ready | ||
Check warning on line 40 in packages/schemas/src/types/organization.ts GitHub Actions / ESLint Report Analysispackages/schemas/src/types/organization.ts#L40
|
||
export const organizationRoleWithScopesGuardDeprecated: ToZodObject< | ||
Omit<OrganizationRoleWithScopes, 'resourceScopes'> | ||
> = OrganizationRoles.guard.extend({ | ||
scopes: z | ||
.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
}) | ||
.array(), | ||
}); | ||
|
||
export const organizationRoleWithScopesGuard: ToZodObject<OrganizationRoleWithScopes> = | ||
OrganizationRoles.guard.extend({ | ||
scopes: z | ||
|
@@ -32,6 +57,16 @@ export const organizationRoleWithScopesGuard: ToZodObject<OrganizationRoleWithSc | |
name: z.string(), | ||
}) | ||
.array(), | ||
resourceScopes: z | ||
.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
resource: z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
}), | ||
}) | ||
.array(), | ||
}); | ||
|
||
/** | ||
|