mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
Merge pull request #6072 from logto-io/gao-refactor-routes
refactor(core): update naming and fix typos
This commit is contained in:
commit
cc37247dbc
15 changed files with 32 additions and 28 deletions
|
@ -174,7 +174,7 @@ export class OrganizationInvitationLibrary {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (entity.organizationRoles.length > 0) {
|
if (entity.organizationRoles.length > 0) {
|
||||||
await organizationQueries.relations.rolesUsers.insert(
|
await organizationQueries.relations.usersRoles.insert(
|
||||||
...entity.organizationRoles.map((role) => ({
|
...entity.organizationRoles.map((role) => ({
|
||||||
organizationId: entity.organizationId,
|
organizationId: entity.organizationId,
|
||||||
organizationRoleId: role.id,
|
organizationRoleId: role.id,
|
||||||
|
|
|
@ -138,7 +138,7 @@ export const createUserLibrary = (queries: Queries) => {
|
||||||
const usersRoles = await findUsersRolesByUserId(userId);
|
const usersRoles = await findUsersRolesByUserId(userId);
|
||||||
const rolesScopes = await findRolesScopesByRoleIds(usersRoles.map(({ roleId }) => roleId));
|
const rolesScopes = await findRolesScopesByRoleIds(usersRoles.map(({ roleId }) => roleId));
|
||||||
const organizationScopes = findFromOrganizations
|
const organizationScopes = findFromOrganizations
|
||||||
? await organizations.relations.rolesUsers.getUserResourceScopes(
|
? await organizations.relations.usersRoles.getUserResourceScopes(
|
||||||
userId,
|
userId,
|
||||||
resourceIndicator,
|
resourceIndicator,
|
||||||
organizationId
|
organizationId
|
||||||
|
@ -295,7 +295,7 @@ export const createUserLibrary = (queries: Queries) => {
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
if (data.length > 0) {
|
if (data.length > 0) {
|
||||||
await organizations.relations.rolesUsers.insert(...data);
|
await organizations.relations.usersRoles.insert(...data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return jitOrganizations;
|
return jitOrganizations;
|
||||||
|
|
|
@ -317,7 +317,7 @@ describe('organization token grant', () => {
|
||||||
|
|
||||||
Sinon.stub(tenant.queries.organizations.relations.users, 'exists').resolves(true);
|
Sinon.stub(tenant.queries.organizations.relations.users, 'exists').resolves(true);
|
||||||
Sinon.stub(tenant.queries.applications, 'findApplicationById').resolves(mockApplication);
|
Sinon.stub(tenant.queries.applications, 'findApplicationById').resolves(mockApplication);
|
||||||
Sinon.stub(tenant.queries.organizations.relations.rolesUsers, 'getUserScopes').resolves([
|
Sinon.stub(tenant.queries.organizations.relations.usersRoles, 'getUserScopes').resolves([
|
||||||
{ tenantId: 'default', id: 'foo', name: 'foo', description: 'foo' },
|
{ tenantId: 'default', id: 'foo', name: 'foo', description: 'foo' },
|
||||||
{ tenantId: 'default', id: 'bar', name: 'bar', description: 'bar' },
|
{ tenantId: 'default', id: 'bar', name: 'bar', description: 'bar' },
|
||||||
{ tenantId: 'default', id: 'baz', name: 'baz', description: 'baz' },
|
{ tenantId: 'default', id: 'baz', name: 'baz', description: 'baz' },
|
||||||
|
|
|
@ -338,7 +338,7 @@ export const buildHandler: (
|
||||||
/* === RFC 0001 === */
|
/* === RFC 0001 === */
|
||||||
const audience = buildOrganizationUrn(organizationId);
|
const audience = buildOrganizationUrn(organizationId);
|
||||||
/** All available scopes for the user in the organization. */
|
/** All available scopes for the user in the organization. */
|
||||||
const availableScopes = await queries.organizations.relations.rolesUsers
|
const availableScopes = await queries.organizations.relations.usersRoles
|
||||||
.getUserScopes(organizationId, account.accountId)
|
.getUserScopes(organizationId, account.accountId)
|
||||||
.then((scopes) => scopes.map(({ name }) => name));
|
.then((scopes) => scopes.map(({ name }) => name));
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { type CommonQueryMethods, sql } from '@silverhand/slonik';
|
||||||
import RelationQueries from '#src/utils/RelationQueries.js';
|
import RelationQueries from '#src/utils/RelationQueries.js';
|
||||||
import { convertToIdentifiers } from '#src/utils/sql.js';
|
import { convertToIdentifiers } from '#src/utils/sql.js';
|
||||||
|
|
||||||
export class RoleApplicationRelationQueries extends RelationQueries<
|
export class ApplicationRoleRelationQueries extends RelationQueries<
|
||||||
[typeof Organizations, typeof OrganizationRoles, typeof Applications]
|
[typeof Organizations, typeof OrganizationRoles, typeof Applications]
|
||||||
> {
|
> {
|
||||||
constructor(pool: CommonQueryMethods) {
|
constructor(pool: CommonQueryMethods) {
|
|
@ -32,11 +32,11 @@ import { TwoRelationsQueries } from '#src/utils/RelationQueries.js';
|
||||||
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
import SchemaQueries from '#src/utils/SchemaQueries.js';
|
||||||
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';
|
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';
|
||||||
|
|
||||||
|
import { ApplicationRoleRelationQueries } from './application-role-relations.js';
|
||||||
import { EmailDomainQueries } from './email-domains.js';
|
import { EmailDomainQueries } from './email-domains.js';
|
||||||
import { RoleApplicationRelationQueries } from './role-application-relations.js';
|
|
||||||
import { RoleUserRelationQueries } from './role-user-relations.js';
|
|
||||||
import { SsoConnectorQueries } from './sso-connectors.js';
|
import { SsoConnectorQueries } from './sso-connectors.js';
|
||||||
import { UserRelationQueries } from './user-relations.js';
|
import { UserRelationQueries } from './user-relations.js';
|
||||||
|
import { UserRoleRelationQueries } from './user-role-relations.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The schema field keys that can be used for searching roles.
|
* The schema field keys that can be used for searching roles.
|
||||||
|
@ -285,9 +285,8 @@ export default class OrganizationQueries extends SchemaQueries<
|
||||||
),
|
),
|
||||||
/** Queries for organization - user relations. */
|
/** Queries for organization - user relations. */
|
||||||
users: new UserRelationQueries(this.pool),
|
users: new UserRelationQueries(this.pool),
|
||||||
// TODO: Rename to `usersRoles`
|
|
||||||
/** Queries for organization - organization role - user relations. */
|
/** Queries for organization - organization role - user relations. */
|
||||||
rolesUsers: new RoleUserRelationQueries(this.pool),
|
usersRoles: new UserRoleRelationQueries(this.pool),
|
||||||
/** Queries for organization - application relations. */
|
/** Queries for organization - application relations. */
|
||||||
apps: new TwoRelationsQueries(
|
apps: new TwoRelationsQueries(
|
||||||
this.pool,
|
this.pool,
|
||||||
|
@ -295,9 +294,8 @@ export default class OrganizationQueries extends SchemaQueries<
|
||||||
Organizations,
|
Organizations,
|
||||||
Applications
|
Applications
|
||||||
),
|
),
|
||||||
// TODO: Rename to `appsRoles`
|
|
||||||
/** Queries for organization - organization role - application relations. */
|
/** Queries for organization - organization role - application relations. */
|
||||||
rolesApps: new RoleApplicationRelationQueries(this.pool),
|
appsRoles: new ApplicationRoleRelationQueries(this.pool),
|
||||||
invitationsRoles: new TwoRelationsQueries(
|
invitationsRoles: new TwoRelationsQueries(
|
||||||
this.pool,
|
this.pool,
|
||||||
OrganizationInvitationRoleRelations.table,
|
OrganizationInvitationRoleRelations.table,
|
||||||
|
|
|
@ -16,7 +16,7 @@ import { sql, type CommonQueryMethods } from '@silverhand/slonik';
|
||||||
import RelationQueries from '#src/utils/RelationQueries.js';
|
import RelationQueries from '#src/utils/RelationQueries.js';
|
||||||
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';
|
import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';
|
||||||
|
|
||||||
export class RoleUserRelationQueries extends RelationQueries<
|
export class UserRoleRelationQueries extends RelationQueries<
|
||||||
[typeof Organizations, typeof OrganizationRoles, typeof Users]
|
[typeof Organizations, typeof OrganizationRoles, typeof Users]
|
||||||
> {
|
> {
|
||||||
constructor(pool: CommonQueryMethods) {
|
constructor(pool: CommonQueryMethods) {
|
|
@ -178,7 +178,7 @@ async function handleSubmitRegister(
|
||||||
// This is only for Cloud integration tests and data alignment, OSS still uses the legacy Management API user role.
|
// This is only for Cloud integration tests and data alignment, OSS still uses the legacy Management API user role.
|
||||||
const organizationId = getTenantOrganizationId(defaultTenantId);
|
const organizationId = getTenantOrganizationId(defaultTenantId);
|
||||||
await organizations.relations.users.insert({ organizationId, userId: id });
|
await organizations.relations.users.insert({ organizationId, userId: id });
|
||||||
await organizations.relations.rolesUsers.insert({
|
await organizations.relations.usersRoles.insert({
|
||||||
organizationId,
|
organizationId,
|
||||||
organizationRoleId: getTenantRole(TenantRole.Admin).id,
|
organizationRoleId: getTenantRole(TenantRole.Admin).id,
|
||||||
userId: id,
|
userId: id,
|
||||||
|
|
|
@ -44,7 +44,7 @@ export default function applicationRoleRelationRoutes(
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, applicationId } = ctx.guard.params;
|
const { id, applicationId } = ctx.guard.params;
|
||||||
|
|
||||||
const [totalCount, entities] = await organizations.relations.rolesApps.getEntities(
|
const [totalCount, entities] = await organizations.relations.appsRoles.getEntities(
|
||||||
OrganizationRoles,
|
OrganizationRoles,
|
||||||
{
|
{
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
|
@ -71,7 +71,7 @@ export default function applicationRoleRelationRoutes(
|
||||||
const { id, applicationId } = ctx.guard.params;
|
const { id, applicationId } = ctx.guard.params;
|
||||||
const { organizationRoleIds } = ctx.guard.body;
|
const { organizationRoleIds } = ctx.guard.body;
|
||||||
|
|
||||||
await organizations.relations.rolesApps.insert(
|
await organizations.relations.appsRoles.insert(
|
||||||
...organizationRoleIds.map((organizationRoleId) => ({
|
...organizationRoleIds.map((organizationRoleId) => ({
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
applicationId,
|
applicationId,
|
||||||
|
@ -97,7 +97,7 @@ export default function applicationRoleRelationRoutes(
|
||||||
const { id, applicationId } = ctx.guard.params;
|
const { id, applicationId } = ctx.guard.params;
|
||||||
const { organizationRoleIds } = ctx.guard.body;
|
const { organizationRoleIds } = ctx.guard.body;
|
||||||
|
|
||||||
await organizations.relations.rolesApps.replace(id, applicationId, organizationRoleIds);
|
await organizations.relations.appsRoles.replace(id, applicationId, organizationRoleIds);
|
||||||
|
|
||||||
ctx.status = 204;
|
ctx.status = 204;
|
||||||
return next();
|
return next();
|
||||||
|
@ -113,7 +113,7 @@ export default function applicationRoleRelationRoutes(
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, applicationId, organizationRoleId } = ctx.guard.params;
|
const { id, applicationId, organizationRoleId } = ctx.guard.params;
|
||||||
|
|
||||||
await organizations.relations.rolesApps.delete({
|
await organizations.relations.appsRoles.delete({
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
applicationId,
|
applicationId,
|
||||||
organizationRoleId,
|
organizationRoleId,
|
||||||
|
|
|
@ -148,7 +148,10 @@
|
||||||
"description": "The role was removed from the application in the organization successfully."
|
"description": "The role was removed from the application in the organization successfully."
|
||||||
},
|
},
|
||||||
"422": {
|
"422": {
|
||||||
"description": "The role could not be removed. The role may not exist."
|
"description": "The application is not associated with the organization."
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Cannot find the record to delete."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ export default function emailDomainRoutes(
|
||||||
`${pathname}/:emailDomain`,
|
`${pathname}/:emailDomain`,
|
||||||
koaGuard({
|
koaGuard({
|
||||||
params: z.object({ ...params, emailDomain: z.string().min(1) }),
|
params: z.object({ ...params, emailDomain: z.string().min(1) }),
|
||||||
status: [204],
|
status: [204, 404],
|
||||||
}),
|
}),
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, emailDomain } = ctx.guard.params;
|
const { id, emailDomain } = ctx.guard.params;
|
||||||
|
|
|
@ -129,7 +129,7 @@ export default function organizationRoutes<T extends ManagementApiRouter>(
|
||||||
const { id } = ctx.guard.params;
|
const { id } = ctx.guard.params;
|
||||||
const { userIds, organizationRoleIds } = ctx.guard.body;
|
const { userIds, organizationRoleIds } = ctx.guard.body;
|
||||||
|
|
||||||
await organizations.relations.rolesUsers.insert(
|
await organizations.relations.usersRoles.insert(
|
||||||
...organizationRoleIds.flatMap((roleId) =>
|
...organizationRoleIds.flatMap((roleId) =>
|
||||||
userIds.map((userId) => ({ organizationId: id, organizationRoleId: roleId, userId }))
|
userIds.map((userId) => ({ organizationId: id, organizationRoleId: roleId, userId }))
|
||||||
)
|
)
|
||||||
|
|
|
@ -43,7 +43,7 @@ export default function userRoleRelationRoutes(
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, userId } = ctx.guard.params;
|
const { id, userId } = ctx.guard.params;
|
||||||
|
|
||||||
const [totalCount, entities] = await organizations.relations.rolesUsers.getEntities(
|
const [totalCount, entities] = await organizations.relations.usersRoles.getEntities(
|
||||||
OrganizationRoles,
|
OrganizationRoles,
|
||||||
{
|
{
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
|
@ -69,7 +69,7 @@ export default function userRoleRelationRoutes(
|
||||||
const { id, userId } = ctx.guard.params;
|
const { id, userId } = ctx.guard.params;
|
||||||
const { organizationRoleIds } = ctx.guard.body;
|
const { organizationRoleIds } = ctx.guard.body;
|
||||||
|
|
||||||
await organizations.relations.rolesUsers.insert(
|
await organizations.relations.usersRoles.insert(
|
||||||
...organizationRoleIds.map((roleId) => ({
|
...organizationRoleIds.map((roleId) => ({
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
organizationRoleId: roleId,
|
organizationRoleId: roleId,
|
||||||
|
@ -93,7 +93,7 @@ export default function userRoleRelationRoutes(
|
||||||
const { id, userId } = ctx.guard.params;
|
const { id, userId } = ctx.guard.params;
|
||||||
const { organizationRoleIds } = ctx.guard.body;
|
const { organizationRoleIds } = ctx.guard.body;
|
||||||
|
|
||||||
await organizations.relations.rolesUsers.replace(id, userId, organizationRoleIds);
|
await organizations.relations.usersRoles.replace(id, userId, organizationRoleIds);
|
||||||
|
|
||||||
ctx.status = 204;
|
ctx.status = 204;
|
||||||
return next();
|
return next();
|
||||||
|
@ -109,7 +109,7 @@ export default function userRoleRelationRoutes(
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, roleId, userId } = ctx.guard.params;
|
const { id, roleId, userId } = ctx.guard.params;
|
||||||
|
|
||||||
await organizations.relations.rolesUsers.delete({
|
await organizations.relations.usersRoles.delete({
|
||||||
organizationId: id,
|
organizationId: id,
|
||||||
organizationRoleId: roleId,
|
organizationRoleId: roleId,
|
||||||
userId,
|
userId,
|
||||||
|
@ -130,7 +130,7 @@ export default function userRoleRelationRoutes(
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
const { id, userId } = ctx.guard.params;
|
const { id, userId } = ctx.guard.params;
|
||||||
|
|
||||||
const scopes = await organizations.relations.rolesUsers.getUserScopes(id, userId);
|
const scopes = await organizations.relations.usersRoles.getUserScopes(id, userId);
|
||||||
|
|
||||||
ctx.body = scopes;
|
ctx.body = scopes;
|
||||||
return next();
|
return next();
|
||||||
|
|
|
@ -190,8 +190,11 @@
|
||||||
"204": {
|
"204": {
|
||||||
"description": "The role was removed from the user successfully."
|
"description": "The role was removed from the user successfully."
|
||||||
},
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "The user is not a member of the organization."
|
||||||
|
},
|
||||||
"404": {
|
"404": {
|
||||||
"description": "The user is not a member of the organization; or the user does not have the role."
|
"description": "Cannot find the record to delete."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,7 +112,7 @@ devFeatureTest.describe('organization application APIs', () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
const response = await organizationApi
|
const response = await organizationApi
|
||||||
.addApplicationRoles(organization.id, '0', [application.id])
|
.addApplicationRoles(organization.id, application.id, ['0'])
|
||||||
.catch((error: unknown) => error);
|
.catch((error: unknown) => error);
|
||||||
assert(response instanceof HTTPError);
|
assert(response instanceof HTTPError);
|
||||||
expect(response.response.status).toBe(422);
|
expect(response.response.status).toBe(422);
|
||||||
|
|
Loading…
Reference in a new issue