fix(core): Fix PermissionChecker.check, and add additional unit tests (#8528)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-02-02 12:21:53 +01:00
committed by GitHub
parent 612771e032
commit 5832d3ca46
6 changed files with 520 additions and 378 deletions

View File

@@ -1,7 +1,7 @@
import { Service } from 'typedi';
import type { EntityManager } from 'typeorm';
import { DataSource, In, Not, Repository } from 'typeorm';
import { SharedCredentials } from '../entities/SharedCredentials';
import { type CredentialSharingRole, SharedCredentials } from '../entities/SharedCredentials';
import type { User } from '../entities/User';
@Service()
@@ -36,27 +36,27 @@ export class SharedCredentialsRepository extends Repository<SharedCredentials> {
return await this.update({ userId: Not(user.id), role: 'credential:owner' }, { user });
}
/**
* Get the IDs of all credentials owned by or shared with a user.
*/
async getAccessibleCredentials(userId: string) {
const sharings = await this.find({
where: {
userId,
role: In(['credential:owner', 'credential:user']),
},
});
return sharings.map((s) => s.credentialsId);
/** Get the IDs of all credentials owned by a user */
async getOwnedCredentialIds(userIds: string[]) {
return await this.getCredentialIdsByUserAndRole(userIds, ['credential:owner']);
}
async findOwnedSharings(userIds: string[]) {
return await this.find({
/** Get the IDs of all credentials owned by or shared with a user */
async getAccessibleCredentialIds(userIds: string[]) {
return await this.getCredentialIdsByUserAndRole(userIds, [
'credential:owner',
'credential:user',
]);
}
private async getCredentialIdsByUserAndRole(userIds: string[], roles: CredentialSharingRole[]) {
const sharings = await this.find({
where: {
userId: In(userIds),
role: 'credential:owner',
role: In(roles),
},
});
return sharings.map((s) => s.credentialsId);
}
async deleteByIds(transaction: EntityManager, sharedCredentialsIds: string[], user?: User) {

View File

@@ -22,6 +22,15 @@ export class SharedWorkflowRepository extends Repository<SharedWorkflow> {
return await this.exist({ where });
}
/** Get the IDs of all users this workflow is shared with */
async getSharedUserIds(workflowId: string) {
const sharedWorkflows = await this.find({
select: ['userId'],
where: { workflowId },
});
return sharedWorkflows.map((sharing) => sharing.userId);
}
async getSharedWorkflowIds(workflowIds: string[]) {
const sharedWorkflows = await this.find({
select: ['workflowId'],