Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions packages/cli/src/UserManagement/PermissionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,20 @@ export class PermissionChecker {

if (user.hasGlobalScope('workflow:execute')) return;

const isSharingEnabled = this.license.isSharingEnabled();

// allow if all creds used in this workflow are a subset of
// all creds accessible to users who have access to this workflow

let workflowUserIds = [userId];

if (workflow.id && this.license.isSharingEnabled()) {
const workflowSharings = await this.sharedWorkflowRepository.find({
relations: ['workflow'],
where: { workflowId: workflow.id },
select: ['userId'],
});
workflowUserIds = workflowSharings.map((s) => s.userId);
if (workflow.id && isSharingEnabled) {
workflowUserIds = await this.sharedWorkflowRepository.getSharedUserIds(workflow.id);
}

const credentialSharings =
await this.sharedCredentialsRepository.findOwnedSharings(workflowUserIds);

const accessibleCredIds = credentialSharings.map((s) => s.credentialsId);
const accessibleCredIds = isSharingEnabled
? await this.sharedCredentialsRepository.getAccessibleCredentialIds(workflowUserIds)
: await this.sharedCredentialsRepository.getOwnedCredentialIds(workflowUserIds);

const inaccessibleCredIds = workflowCredIds.filter((id) => !accessibleCredIds.includes(id));

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/credentials/credentials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class CredentialsService {
: credentials;
}

const ids = await this.sharedCredentialsRepository.getAccessibleCredentials(user.id);
const ids = await this.sharedCredentialsRepository.getAccessibleCredentialIds([user.id]);

const credentials = await this.credentialsRepository.findMany(
options.listQueryOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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']),
},
});
/** Get the IDs of all credentials owned by a user */
async getOwnedCredentialIds(userIds: string[]) {
return await this.getCredentialIdsByUserAndRole(userIds, ['credential:owner']);
}

return sharings.map((s) => s.credentialsId);
/** 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',
]);
}

async findOwnedSharings(userIds: string[]) {
return await this.find({
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
netroy marked this conversation as resolved.
Show resolved Hide resolved
const sharedWorkflows = await this.find({
select: ['userId'],
where: { workflowId },
});
return sharedWorkflows.map((sharing) => sharing.userId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we switch to our fork, I'd love an option to return an array of what we specifically selected instead of having to pick it out from the result every time.

}

async getSharedWorkflowIds(workflowIds: string[]) {
const sharedWorkflows = await this.find({
select: ['workflowId'],
Expand Down
Loading
Loading