mirror of
https://github.com/logto-io/logto.git
synced 2025-02-17 22:04:19 -05:00
feat(core): add resource to response scopes (#2843)
This commit is contained in:
parent
bde1cbcc9d
commit
751f8dffcf
5 changed files with 39 additions and 3 deletions
|
@ -32,6 +32,15 @@ export const findResourceByIndicator = async (indicator: string) =>
|
|||
where ${fields.indicator}=${indicator}
|
||||
`);
|
||||
|
||||
export const findResourcesByIds = async (resourceIds: string[]) =>
|
||||
resourceIds.length > 0
|
||||
? envSet.pool.any<Resource>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.id} in (${sql.join(resourceIds, sql`, `)})
|
||||
`)
|
||||
: [];
|
||||
|
||||
export const findResourceById = buildFindEntityById<CreateResource, Resource>(Resources);
|
||||
|
||||
export const insertResource = buildInsertInto<CreateResource, Resource>(Resources, {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type { Role } from '@logto/schemas';
|
||||
import { pickDefault, createMockUtils } from '@logto/shared/esm';
|
||||
|
||||
import { mockRole, mockScope, mockUser } from '#src/__mocks__/index.js';
|
||||
import { mockRole, mockScope, mockUser, mockResource } from '#src/__mocks__/index.js';
|
||||
import { createRequester } from '#src/utils/test-utils.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
|
@ -30,6 +30,9 @@ const { findScopeById, findScopesByIds } = await mockEsmWithActual('#src/queries
|
|||
findScopeById: jest.fn(),
|
||||
findScopesByIds: jest.fn(),
|
||||
}));
|
||||
await mockEsmWithActual('#src/queries/resource.js', () => ({
|
||||
findResourcesByIds: jest.fn(async () => [mockResource]),
|
||||
}));
|
||||
const { insertRolesScopes, findRolesScopesByRoleId } = await mockEsmWithActual(
|
||||
'#src/queries/roles-scopes.js',
|
||||
() => ({
|
||||
|
@ -128,7 +131,12 @@ describe('role routes', () => {
|
|||
findScopesByIds.mockResolvedValueOnce([mockScope]);
|
||||
const response = await roleRequester.get(`/roles/${mockRole.id}/scopes`);
|
||||
expect(response.status).toEqual(200);
|
||||
expect(response.body).toEqual([mockScope]);
|
||||
expect(response.body).toEqual([
|
||||
{
|
||||
...mockScope,
|
||||
resource: mockResource,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('POST /roles/:id/scopes', async () => {
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
import { buildIdGenerator } from '@logto/core-kit';
|
||||
import type { ScopeResponse } from '@logto/schemas';
|
||||
import { Roles } from '@logto/schemas';
|
||||
import { object, string, z } from 'zod';
|
||||
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import koaGuard from '#src/middleware/koa-guard.js';
|
||||
import { findResourcesByIds } from '#src/queries/resource.js';
|
||||
import {
|
||||
deleteRolesScope,
|
||||
findRolesScopesByRoleId,
|
||||
|
@ -138,7 +140,20 @@ export default function roleRoutes<T extends AuthedRouter>(router: T) {
|
|||
|
||||
await findRoleById(id);
|
||||
const rolesScopes = await findRolesScopesByRoleId(id);
|
||||
ctx.body = await findScopesByIds(rolesScopes.map(({ scopeId }) => scopeId));
|
||||
const scopes = await findScopesByIds(rolesScopes.map(({ scopeId }) => scopeId));
|
||||
const resources = await findResourcesByIds(scopes.map(({ resourceId }) => resourceId));
|
||||
const result: ScopeResponse[] = scopes.map((scope) => {
|
||||
const resource = resources.find(({ id }) => scope.resourceId);
|
||||
|
||||
assertThat(resource, new Error(`Cannot find resource for id ${scope.resourceId}`));
|
||||
|
||||
return {
|
||||
...scope,
|
||||
resource,
|
||||
};
|
||||
});
|
||||
|
||||
ctx.body = result;
|
||||
|
||||
return next();
|
||||
}
|
||||
|
|
|
@ -6,3 +6,4 @@ export * from './logto-config.js';
|
|||
export * from './interactions.js';
|
||||
export * from './search.js';
|
||||
export * from './resource.js';
|
||||
export * from './scope.js';
|
||||
|
|
3
packages/schemas/src/types/scope.ts
Normal file
3
packages/schemas/src/types/scope.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import type { Resource, Scope } from '../db-entries/index.js';
|
||||
|
||||
export type ScopeResponse = Scope & { resource: Resource };
|
Loading…
Add table
Reference in a new issue