Skip to content
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
15 changes: 8 additions & 7 deletions src/resources/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,25 @@ export class ResourcesService {
return await this.create(user, newResource);
}

async permissionFilter(
async permissionFilter<T extends string | Resource>(
namespaceId: string,
userId: string,
resources: Resource[],
): Promise<Resource[]> {
const filteredResources: Resource[] = [];
resources: T[],
): Promise<T[]> {
const filtered: T[] = [];
for (const res of resources) {
const resourceId: string = typeof res === 'string' ? res : res.id;
const hasPermission: boolean =
await this.permissionsService.userHasPermission(
namespaceId,
res.id,
resourceId,
userId,
);
if (hasPermission) {
filteredResources.push(res);
filtered.push(res);
}
}
return filteredResources;
return filtered;
}

// get resources under parentId
Expand Down
20 changes: 7 additions & 13 deletions src/wizard/stream.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import { Message } from 'src/messages/entities/message.entity';
import { AgentRequestDto } from 'src/wizard/dto/agent-request.dto';
import { ResourcesService } from 'src/resources/resources.service';
import { Resource } from 'src/resources/resources.entity';
import { PermissionsService } from 'src/permissions/permissions.service';

export class StreamService {
constructor(
private readonly wizardBaseUrl: string,
private readonly messagesService: MessagesService,
private readonly resourcesService: ResourcesService,
private readonly permissionsService: PermissionsService,
) {}

async stream(
Expand Down Expand Up @@ -182,17 +180,13 @@ export class StreamService {
} else {
const resourceIds: string[] = [];
if (tool.resource_ids) {
for (const resourceId of tool.resource_ids) {
const hasPermission: boolean =
await this.permissionsService.userHasPermission(
tool.namespace_id,
resourceId,
user.id,
);
if (hasPermission) {
resourceIds.push(resourceId);
}
}
resourceIds.push(
...(await this.resourcesService.permissionFilter<string>(
tool.namespace_id,
user.id,
resourceIds,
)),
);
}
if (tool.parent_ids) {
for (const parentId of tool.parent_ids) {
Expand Down
2 changes: 0 additions & 2 deletions src/wizard/wizard.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { ResourcesModule } from 'src/resources/resources.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Task } from 'src/tasks/tasks.entity';
import { MessagesModule } from 'src/messages/messages.module';
import { PermissionsModule } from '../permissions/permissions.module';

@Module({
providers: [WizardService],
imports: [
NamespacesModule,
ResourcesModule,
MessagesModule,
PermissionsModule,
TypeOrmModule.forFeature([Task]),
],
controllers: [WizardController, InternalWizardController],
Expand Down
3 changes: 0 additions & 3 deletions src/wizard/wizard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { Processor } from 'src/wizard/processors/processor.abstract';
import { MessagesService } from 'src/messages/messages.service';
import { StreamService } from 'src/wizard/stream.service';
import { WizardAPIService } from 'src/wizard/api.wizard.service';
import { PermissionsService } from 'src/permissions/permissions.service';

@Injectable()
export class WizardService {
Expand All @@ -30,7 +29,6 @@ export class WizardService {
private readonly resourcesService: ResourcesService,
private readonly messagesService: MessagesService,
private readonly configService: ConfigService,
private readonly permissionsService: PermissionsService,
) {
this.processors = {
collect: new CollectProcessor(resourcesService),
Expand All @@ -44,7 +42,6 @@ export class WizardService {
baseUrl,
this.messagesService,
this.resourcesService,
this.permissionsService,
);
this.wizardApiService = new WizardAPIService(baseUrl);
}
Expand Down