mirror of
https://github.com/logto-io/logto.git
synced 2025-03-24 22:41:28 -05:00
fix(core): should not throw when not adding any new roles to a user (#6387)
This commit is contained in:
parent
5024182986
commit
2e3a0063ba
3 changed files with 13 additions and 3 deletions
|
@ -42,14 +42,19 @@ export const createUsersRolesQueries = (pool: CommonQueryMethods) => {
|
|||
${conditionalSql(limit, (value) => sql`limit ${value}`)}
|
||||
`);
|
||||
|
||||
const insertUsersRoles = async (usersRoles: CreateUsersRole[]) =>
|
||||
pool.query(sql`
|
||||
const insertUsersRoles = async (usersRoles: CreateUsersRole[]) => {
|
||||
if (usersRoles.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return pool.query(sql`
|
||||
insert into ${table} (${fields.id}, ${fields.userId}, ${fields.roleId}) values
|
||||
${sql.join(
|
||||
usersRoles.map(({ id, userId, roleId }) => sql`(${id}, ${userId}, ${roleId})`),
|
||||
sql`, `
|
||||
)}
|
||||
`);
|
||||
};
|
||||
|
||||
const deleteUsersRolesByUserIdAndRoleId = async (userId: string, roleId: string) => {
|
||||
const { rowCount } = await pool.query(sql`
|
||||
|
|
|
@ -76,6 +76,9 @@ export const deleteUserIdentity = async (userId: string, connectorTarget: string
|
|||
export const assignRolesToUser = async (userId: string, roleIds: string[]) =>
|
||||
authedAdminApi.post(`users/${userId}/roles`, { json: { roleIds } });
|
||||
|
||||
export const putRolesToUser = async (userId: string, roleIds: string[]) =>
|
||||
authedAdminApi.put(`users/${userId}/roles`, { json: { roleIds } });
|
||||
|
||||
/**
|
||||
* Get roles assigned to the user.
|
||||
*
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Prompt } from '@logto/node';
|
||||
import { InteractionEvent, demoAppApplicationId } from '@logto/schemas';
|
||||
|
||||
import { assignRolesToUser, putInteraction } from '#src/api/index.js';
|
||||
import { assignRolesToUser, putRolesToUser, putInteraction } from '#src/api/index.js';
|
||||
import { createRole } from '#src/api/role.js';
|
||||
import MockClient from '#src/client/index.js';
|
||||
import { demoAppRedirectUri } from '#src/constants.js';
|
||||
|
@ -56,6 +56,8 @@ describe('OpenID Connect ID token', () => {
|
|||
it('should be issued with correct `username` and `roles` claims', async () => {
|
||||
const role = await createRole({});
|
||||
await assignRolesToUser(userId, [role.id]);
|
||||
// Should not throw when not adding any new role(s) to a user.
|
||||
await expect(putRolesToUser(userId, [role.id])).resolves.not.toThrow();
|
||||
await fetchIdToken(['username', 'roles'], {
|
||||
username,
|
||||
roles: [role.name],
|
||||
|
|
Loading…
Add table
Reference in a new issue