-
-
Notifications
You must be signed in to change notification settings - Fork 453
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 #6071 from logto-io/gao-org-app-role-apis
feat(core): organization app role apis
- Loading branch information
Showing
12 changed files
with
484 additions
and
31 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
64 changes: 64 additions & 0 deletions
64
packages/core/src/queries/organization/role-application-relations.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,64 @@ | ||
import { | ||
Organizations, | ||
OrganizationRoles, | ||
Applications, | ||
OrganizationRoleApplicationRelations, | ||
} from '@logto/schemas'; | ||
import { type CommonQueryMethods, sql } from '@silverhand/slonik'; | ||
|
||
import RelationQueries from '#src/utils/RelationQueries.js'; | ||
import { convertToIdentifiers } from '#src/utils/sql.js'; | ||
|
||
export class RoleApplicationRelationQueries extends RelationQueries< | ||
[typeof Organizations, typeof OrganizationRoles, typeof Applications] | ||
> { | ||
constructor(pool: CommonQueryMethods) { | ||
super( | ||
pool, | ||
OrganizationRoleApplicationRelations.table, | ||
Organizations, | ||
OrganizationRoles, | ||
Applications | ||
); | ||
} | ||
|
||
/** Replace the roles of an application in an organization. */ | ||
async replace(organizationId: string, applicationId: string, roleIds: readonly string[]) { | ||
const applications = convertToIdentifiers(Applications); | ||
const relations = convertToIdentifiers(OrganizationRoleApplicationRelations); | ||
|
||
return this.pool.transaction(async (transaction) => { | ||
// Lock application | ||
await transaction.query(sql` | ||
select 1 | ||
from ${applications.table} | ||
where ${applications.fields.id} = ${applicationId} | ||
for update | ||
`); | ||
|
||
// Delete old relations | ||
await transaction.query(sql` | ||
delete from ${relations.table} | ||
where ${relations.fields.organizationId} = ${organizationId} | ||
and ${relations.fields.applicationId} = ${applicationId} | ||
`); | ||
|
||
// Insert new relations | ||
if (roleIds.length === 0) { | ||
return; | ||
} | ||
|
||
await transaction.query(sql` | ||
insert into ${relations.table} ( | ||
${relations.fields.organizationId}, | ||
${relations.fields.applicationId}, | ||
${relations.fields.organizationRoleId} | ||
) | ||
values ${sql.join( | ||
roleIds.map((roleId) => sql`(${organizationId}, ${applicationId}, ${roleId})`), | ||
sql`, ` | ||
)} | ||
`); | ||
}); | ||
} | ||
} |
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
126 changes: 126 additions & 0 deletions
126
packages/core/src/routes/organization/index.application-role-relations.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,126 @@ | ||
import { OrganizationRoles } from '@logto/schemas'; | ||
import type Router from 'koa-router'; | ||
import { z } from 'zod'; | ||
|
||
import RequestError from '#src/errors/RequestError/index.js'; | ||
import koaGuard from '#src/middleware/koa-guard.js'; | ||
import { type WithHookContext } from '#src/middleware/koa-management-api-hooks.js'; | ||
import koaPagination from '#src/middleware/koa-pagination.js'; | ||
import type OrganizationQueries from '#src/queries/organization/index.js'; | ||
|
||
// Consider building a class to handle these relations. See `index.user-role-relations.ts` for more information. | ||
export default function applicationRoleRelationRoutes( | ||
router: Router<unknown, WithHookContext>, | ||
organizations: OrganizationQueries | ||
) { | ||
const params = Object.freeze({ | ||
id: z.string().min(1), | ||
applicationId: z.string().min(1), | ||
} as const); | ||
const pathname = '/:id/applications/:applicationId/roles'; | ||
|
||
// The pathname of `.use()` will not match the end of the path, for example: | ||
// `.use('/foo', ...)` will match both `/foo` and `/foo/bar`. | ||
// See https://github.com/koajs/router/blob/02ad6eedf5ced6ec1eab2138380fd67c63e3f1d7/lib/router.js#L330-L333 | ||
router.use(pathname, koaGuard({ params: z.object(params) }), async (ctx, next) => { | ||
const { id, applicationId } = ctx.guard.params; | ||
|
||
// Ensure membership | ||
if (!(await organizations.relations.apps.exists({ organizationId: id, applicationId }))) { | ||
throw new RequestError({ code: 'organization.require_membership', status: 422 }); | ||
} | ||
|
||
return next(); | ||
}); | ||
|
||
router.get( | ||
pathname, | ||
koaPagination(), | ||
koaGuard({ | ||
params: z.object(params), | ||
response: OrganizationRoles.guard.array(), | ||
status: [200, 422], | ||
}), | ||
async (ctx, next) => { | ||
const { id, applicationId } = ctx.guard.params; | ||
|
||
const [totalCount, entities] = await organizations.relations.rolesApps.getEntities( | ||
OrganizationRoles, | ||
{ | ||
organizationId: id, | ||
applicationId, | ||
} | ||
); | ||
|
||
ctx.pagination.totalCount = totalCount; | ||
ctx.body = entities; | ||
return next(); | ||
} | ||
); | ||
|
||
router.post( | ||
pathname, | ||
koaGuard({ | ||
params: z.object(params), | ||
body: z.object({ | ||
organizationRoleIds: z.string().min(1).array().nonempty(), | ||
}), | ||
status: [201, 422], | ||
}), | ||
async (ctx, next) => { | ||
const { id, applicationId } = ctx.guard.params; | ||
const { organizationRoleIds } = ctx.guard.body; | ||
|
||
await organizations.relations.rolesApps.insert( | ||
...organizationRoleIds.map((organizationRoleId) => ({ | ||
organizationId: id, | ||
applicationId, | ||
organizationRoleId, | ||
})) | ||
); | ||
|
||
ctx.status = 201; | ||
return next(); | ||
} | ||
); | ||
|
||
router.put( | ||
pathname, | ||
koaGuard({ | ||
params: z.object(params), | ||
body: z.object({ | ||
organizationRoleIds: z.string().min(1).array().nonempty(), | ||
}), | ||
status: [204, 422], | ||
}), | ||
async (ctx, next) => { | ||
const { id, applicationId } = ctx.guard.params; | ||
const { organizationRoleIds } = ctx.guard.body; | ||
|
||
await organizations.relations.rolesApps.replace(id, applicationId, organizationRoleIds); | ||
|
||
ctx.status = 204; | ||
return next(); | ||
} | ||
); | ||
|
||
router.delete( | ||
`${pathname}/:organizationRoleId`, | ||
koaGuard({ | ||
params: z.object({ ...params, organizationRoleId: z.string().min(1) }), | ||
status: [204, 422, 404], | ||
}), | ||
async (ctx, next) => { | ||
const { id, applicationId, organizationRoleId } = ctx.guard.params; | ||
|
||
await organizations.relations.rolesApps.delete({ | ||
organizationId: id, | ||
applicationId, | ||
organizationRoleId, | ||
}); | ||
|
||
ctx.status = 204; | ||
return next(); | ||
} | ||
); | ||
} |
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
Oops, something went wrong.