0
Fork 0
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:
Darcy Ye 2024-08-05 10:58:47 +08:00 committed by GitHub
parent 5024182986
commit 2e3a0063ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 3 deletions

View file

@ -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`

View file

@ -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.
*

View file

@ -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],