mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
fix(core): prevent empty array to be used by sql in (#2844)
This commit is contained in:
parent
7e507dbc2b
commit
bde1cbcc9d
5 changed files with 15 additions and 20 deletions
|
@ -41,9 +41,4 @@ describe('attachScopesToResources', () => {
|
|||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return empty array for empty array input', async () => {
|
||||
await expect(attachScopesToResources([])).resolves.toEqual([]);
|
||||
expect(findScopesByResourceIds).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,7 +6,7 @@ export const attachScopesToResources = async (
|
|||
resources: readonly Resource[]
|
||||
): Promise<ResourceResponse[]> => {
|
||||
const resourceIds = resources.map(({ id }) => id);
|
||||
const scopes = resourceIds.length > 0 ? await findScopesByResourceIds(resourceIds) : [];
|
||||
const scopes = await findScopesByResourceIds(resourceIds);
|
||||
|
||||
return resources.map((resource) => ({
|
||||
...resource,
|
||||
|
|
|
@ -27,11 +27,13 @@ export const findRolesByRoleIds = async (roleIds: string[]) =>
|
|||
: [];
|
||||
|
||||
export const findRolesByRoleNames = async (roleNames: string[]) =>
|
||||
envSet.pool.any<Role>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.name} in (${sql.join(roleNames, sql`, `)})
|
||||
`);
|
||||
roleNames.length > 0
|
||||
? envSet.pool.any<Role>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.name} in (${sql.join(roleNames, sql`, `)})
|
||||
`)
|
||||
: [];
|
||||
|
||||
export const findRoleByRoleName = async (roleName: string, excludeRoleId?: string) =>
|
||||
envSet.pool.maybeOne<Role>(sql`
|
||||
|
|
|
@ -20,11 +20,13 @@ export const findScopesByResourceId = async (resourceId: string) =>
|
|||
`);
|
||||
|
||||
export const findScopesByResourceIds = async (resourceIds: string[]) =>
|
||||
envSet.pool.any<Scope>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.resourceId} in (${sql.join(resourceIds, sql`, `)})
|
||||
`);
|
||||
resourceIds.length > 0
|
||||
? envSet.pool.any<Scope>(sql`
|
||||
select ${sql.join(Object.values(fields), sql`, `)}
|
||||
from ${table}
|
||||
where ${fields.resourceId} in (${sql.join(resourceIds, sql`, `)})
|
||||
`)
|
||||
: [];
|
||||
|
||||
export const findScopesByIds = async (scopeIds: string[]) =>
|
||||
scopeIds.length > 0
|
||||
|
|
|
@ -225,9 +225,5 @@ export const findUsersByRoleName = async (roleName: string) => {
|
|||
|
||||
const usersRoles = await findUsersRolesByRoleId(role.id);
|
||||
|
||||
if (usersRoles.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return findUsersByIds(usersRoles.map(({ userId }) => userId));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue