Skip to content

Commit

Permalink
Merge pull request #37 from DevoteamNL/feat/plugin-user-restriction
Browse files Browse the repository at this point in the history
Restricted the selectable user plugins
  • Loading branch information
hardik-id authored Oct 11, 2024
2 parents 9412655 + afd6fa9 commit 155b366
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/common/types/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { PluginDisplayNameT } from '../../plugin/plugin.constants';

export class User {
id: number;
providerId: string;
username: string;
name?: string;
plugins?: PluginDisplayNameT[];
created_at: Date;
updated_at: Date;
}
6 changes: 6 additions & 0 deletions src/plugin/plugin.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum PluginDisplayName {
JOAN = 'Joan',
CVS = 'CVs',
}

export type PluginDisplayNameT = keyof typeof PluginDisplayName;
9 changes: 6 additions & 3 deletions src/plugin/plugin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, UseGuards } from '@nestjs/common';
import { Controller, Get, Request, UseGuards } from '@nestjs/common';
import { GoogleTokenGuard } from 'src/auth/guards/google-token.guard';
import { PluginService } from './plugin.service';

Expand All @@ -8,7 +8,10 @@ export class PluginController {
constructor(private readonly pluginService: PluginService) {}

@Get()
listPlugins() {
return this.pluginService.availablePlugins;
listPlugins(@Request() req) {
const { plugins = [] } = req.user;
return this.pluginService.availablePlugins.filter((plugin) => {
return plugins.length > 0 && plugins.includes(plugin.displayName);
});
}
}
5 changes: 2 additions & 3 deletions src/plugin/plugins/cv.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import axios, { AxiosInstance } from 'axios';
import moment from 'moment-timezone';
import { ConfigService } from '../types';
import { Definition } from '../definition.decorator';
import { Plugin } from '../plugin.decorator';
import { Logger } from '@nestjs/common';
import { CognitiveSearchService } from '../../cognitive-search/cognitive-search.service';
import { PluginDisplayName } from '../plugin.constants';

@Plugin({ displayName: 'CVs' })
@Plugin({ displayName: PluginDisplayName.CVS })
export class CVsPlugin {
private readonly logger = new Logger(CVsPlugin.name);

Expand Down
3 changes: 2 additions & 1 deletion src/plugin/plugins/joan.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConfigService } from '../types';
import { Definition } from '../definition.decorator';
import { Plugin } from '../plugin.decorator';
import { Logger } from '@nestjs/common';
import { PluginDisplayName } from '../plugin.constants';

interface Desk {
id: string;
Expand All @@ -19,7 +20,7 @@ interface ParkingSpotOptions {
timeslot?: string;
}

@Plugin({ displayName: 'Joan' })
@Plugin({ displayName: PluginDisplayName.JOAN })
export class JoanPlugin {
private axiosInstance: AxiosInstance;
private token: string | null = null;
Expand Down
3 changes: 3 additions & 0 deletions src/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { PluginDisplayNameT } from '../../plugin/plugin.constants';

export class CreateUserDto {
providerId: string;
username: string;
google_token: string;
refresh_token: string;
name?: string;
is_active?: boolean;
plugins?: PluginDisplayNameT[];
}
14 changes: 12 additions & 2 deletions src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { IsEmail } from 'class-validator';
import { Thread } from '../../thread/entities/thread.entity';
import { Feedback } from '../../message/feedback/entities/feedback.entity';
import { PluginDisplayNameT } from '../../plugin/plugin.constants';

@Entity()
@Unique('UQ_USERNAME_PROVIDER', ['username', 'providerId'])
Expand Down Expand Up @@ -46,6 +47,14 @@ export class User {
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
createdAt: Date;

// Whitelist of the plugins' names the user is permitted to access
@Column({
type: 'varchar',
array: true,
nullable: true,
})
plugins: PluginDisplayNameT[];

constructor(
providerId: string,
username: string,
Expand All @@ -59,13 +68,14 @@ export class User {
}

toJSON(): UserDTO {
const { id, username, providerId } = this;
return { id, username, providerId };
const { id, username, providerId, plugins } = this;
return { id, username, providerId, plugins };
}
}

interface UserDTO {
id: number;
username: string;
providerId: string;
plugins: PluginDisplayNameT[];
}

0 comments on commit 155b366

Please sign in to comment.