0
Fork 0
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:
wangsijie 2023-01-07 12:13:13 +08:00 committed by GitHub
parent 7e507dbc2b
commit bde1cbcc9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 20 deletions

View file

@ -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();
});
});

View file

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

View file

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

View file

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

View file

@ -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));
};