mirror of
https://github.com/samanhappy/mcphub.git
synced 2025-12-24 02:39:19 -05:00
refactor: Simplify DataService implementation and remove unused DataServicex (#448)
This commit is contained in:
@@ -187,7 +187,7 @@ export async function exampleUserConfigOperations() {
|
|||||||
console.log('All user configs:', Object.keys(allUserConfigs));
|
console.log('All user configs:', Object.keys(allUserConfigs));
|
||||||
|
|
||||||
// Get specific section for user
|
// Get specific section for user
|
||||||
const userRoutingConfig = await userConfigDao.getSection('admin', 'routing');
|
const userRoutingConfig = await userConfigDao.getSection('admin', 'routing' as never);
|
||||||
console.log('Admin routing config:', userRoutingConfig);
|
console.log('Admin routing config:', userRoutingConfig);
|
||||||
|
|
||||||
// Delete user configuration
|
// Delete user configuration
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
import { DataService } from './dataService.js';
|
|
||||||
import { getDataService } from './services.js';
|
|
||||||
import './services.js';
|
|
||||||
|
|
||||||
describe('DataService', () => {
|
|
||||||
test('should get default implementation and call foo method', async () => {
|
|
||||||
const dataService: DataService = await getDataService();
|
|
||||||
const consoleSpy = jest.spyOn(console, 'log');
|
|
||||||
dataService.foo();
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith('default implementation');
|
|
||||||
consoleSpy.mockRestore();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,31 +1,69 @@
|
|||||||
import { IUser, McpSettings } from '../types/index.js';
|
import { IUser, McpSettings } from '../types/index.js';
|
||||||
|
import { UserContextService } from './userContextService.js';
|
||||||
|
import { UserConfig } from '../types/index.js';
|
||||||
|
|
||||||
export interface DataService {
|
export class DataService {
|
||||||
foo(): void;
|
filterData(data: any[], user?: IUser): any[] {
|
||||||
filterData(data: any[], user?: IUser): any[];
|
// Use passed user parameter if available, otherwise fall back to context
|
||||||
filterSettings(settings: McpSettings, user?: IUser): McpSettings;
|
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
||||||
mergeSettings(all: McpSettings, newSettings: McpSettings, user?: IUser): McpSettings;
|
if (!currentUser || currentUser.isAdmin) {
|
||||||
getPermissions(user: IUser): string[];
|
return data;
|
||||||
}
|
} else {
|
||||||
|
return data.filter((item) => item.owner === currentUser?.username);
|
||||||
export class DataServiceImpl implements DataService {
|
}
|
||||||
foo() {
|
|
||||||
console.log('default implementation');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filterData(data: any[], _user?: IUser): any[] {
|
filterSettings(settings: McpSettings, user?: IUser): McpSettings {
|
||||||
return data;
|
// Use passed user parameter if available, otherwise fall back to context
|
||||||
|
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
||||||
|
if (!currentUser || currentUser.isAdmin) {
|
||||||
|
const result = { ...settings };
|
||||||
|
delete result.userConfigs;
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
const result = { ...settings };
|
||||||
|
// TODO: apply userConfig to filter settings as needed
|
||||||
|
// const userConfig = settings.userConfigs?.[currentUser?.username || ''];
|
||||||
|
delete result.userConfigs;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
filterSettings(settings: McpSettings, _user?: IUser): McpSettings {
|
mergeSettings(all: McpSettings, newSettings: McpSettings, user?: IUser): McpSettings {
|
||||||
return settings;
|
// Use passed user parameter if available, otherwise fall back to context
|
||||||
|
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
||||||
|
if (!currentUser || currentUser.isAdmin) {
|
||||||
|
const result = { ...all };
|
||||||
|
result.mcpServers = newSettings.mcpServers;
|
||||||
|
result.users = newSettings.users;
|
||||||
|
result.systemConfig = newSettings.systemConfig;
|
||||||
|
result.groups = newSettings.groups;
|
||||||
|
result.oauthClients = newSettings.oauthClients;
|
||||||
|
result.oauthTokens = newSettings.oauthTokens;
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
const result = JSON.parse(JSON.stringify(all));
|
||||||
|
if (!result.userConfigs) {
|
||||||
|
result.userConfigs = {};
|
||||||
|
}
|
||||||
|
const systemConfig = newSettings.systemConfig || {};
|
||||||
|
const userConfig: UserConfig = {
|
||||||
|
routing: systemConfig.routing
|
||||||
|
? {
|
||||||
|
// TODO: only allow modifying certain fields based on userConfig permissions
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
result.userConfigs[currentUser?.username || ''] = userConfig;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mergeSettings(all: McpSettings, newSettings: McpSettings, _user?: IUser): McpSettings {
|
getPermissions(user: IUser): string[] {
|
||||||
return newSettings;
|
if (user && user.isAdmin) {
|
||||||
}
|
return ['*', 'x'];
|
||||||
|
} else {
|
||||||
getPermissions(_user: IUser): string[] {
|
return [''];
|
||||||
return ['*'];
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
import { IUser, McpSettings, UserConfig } from '../types/index.js';
|
|
||||||
import { DataService } from './dataService.js';
|
|
||||||
import { UserContextService } from './userContextService.js';
|
|
||||||
|
|
||||||
export class DataServicex implements DataService {
|
|
||||||
foo() {
|
|
||||||
console.log('default implementation');
|
|
||||||
}
|
|
||||||
|
|
||||||
filterData(data: any[], user?: IUser): any[] {
|
|
||||||
// Use passed user parameter if available, otherwise fall back to context
|
|
||||||
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
|
||||||
if (!currentUser || currentUser.isAdmin) {
|
|
||||||
return data;
|
|
||||||
} else {
|
|
||||||
return data.filter((item) => item.owner === currentUser?.username);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
filterSettings(settings: McpSettings, user?: IUser): McpSettings {
|
|
||||||
// Use passed user parameter if available, otherwise fall back to context
|
|
||||||
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
|
||||||
if (!currentUser || currentUser.isAdmin) {
|
|
||||||
const result = { ...settings };
|
|
||||||
delete result.userConfigs;
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
const result = { ...settings };
|
|
||||||
// TODO: apply userConfig to filter settings as needed
|
|
||||||
// const userConfig = settings.userConfigs?.[currentUser?.username || ''];
|
|
||||||
delete result.userConfigs;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mergeSettings(all: McpSettings, newSettings: McpSettings, user?: IUser): McpSettings {
|
|
||||||
// Use passed user parameter if available, otherwise fall back to context
|
|
||||||
const currentUser = user || UserContextService.getInstance().getCurrentUser();
|
|
||||||
if (!currentUser || currentUser.isAdmin) {
|
|
||||||
const result = { ...all };
|
|
||||||
result.mcpServers = newSettings.mcpServers;
|
|
||||||
result.users = newSettings.users;
|
|
||||||
result.systemConfig = newSettings.systemConfig;
|
|
||||||
result.groups = newSettings.groups;
|
|
||||||
result.oauthClients = newSettings.oauthClients;
|
|
||||||
result.oauthTokens = newSettings.oauthTokens;
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
const result = JSON.parse(JSON.stringify(all));
|
|
||||||
if (!result.userConfigs) {
|
|
||||||
result.userConfigs = {};
|
|
||||||
}
|
|
||||||
const systemConfig = newSettings.systemConfig || {};
|
|
||||||
const userConfig: UserConfig = {
|
|
||||||
routing: systemConfig.routing
|
|
||||||
? {
|
|
||||||
// TODO: only allow modifying certain fields based on userConfig permissions
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
};
|
|
||||||
result.userConfigs[currentUser?.username || ''] = userConfig;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getPermissions(user: IUser): string[] {
|
|
||||||
if (user && user.isAdmin) {
|
|
||||||
return ['*', 'x'];
|
|
||||||
} else {
|
|
||||||
return [''];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
import { registerService, getService } from './registry.js';
|
import { DataService } from './dataService.js';
|
||||||
import { DataService, DataServiceImpl } from './dataService.js';
|
|
||||||
|
|
||||||
await registerService('dataService', {
|
|
||||||
defaultImpl: DataServiceImpl,
|
|
||||||
});
|
|
||||||
|
|
||||||
export function getDataService(): DataService {
|
export function getDataService(): DataService {
|
||||||
return getService<DataService>('dataService');
|
return new DataService();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user