1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: add global isAdmin method for access service (#7088)

This is preparation, so we do not need to define an `isAdmin` method in
the change request context.
This commit is contained in:
Jaanus Sellin 2024-05-21 11:56:22 +03:00 committed by GitHub
parent 8a6daeee1e
commit 45c75a65ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 2 deletions

View File

@ -12,7 +12,7 @@ import FakeRoleStore from '../../../test/fixtures/fake-role-store';
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
import FakeAccessStore from '../../../test/fixtures/fake-access-store';
import FakeFeatureTagStore from '../../../test/fixtures/fake-feature-tag-store';
import type { IEventStore } from '../../types';
import type { IAccessStore, IEventStore, IRoleStore } from '../../types';
import { createEventsService } from '../events/createEventsService';
export const createAccessService = (
@ -42,7 +42,12 @@ export const createAccessService = (
export const createFakeAccessService = (
config: IUnleashConfig,
): { accessService: AccessService; eventStore: IEventStore } => {
): {
accessService: AccessService;
eventStore: IEventStore;
accessStore: IAccessStore;
roleStore: IRoleStore;
} => {
const { getLogger, flagResolver } = config;
const eventStore = new FakeEventStore();
const groupStore = new FakeGroupStore();
@ -71,5 +76,7 @@ export const createFakeAccessService = (
return {
accessService,
eventStore,
accessStore,
roleStore,
};
};

View File

@ -285,3 +285,31 @@ describe('addAccessToProject', () => {
).rejects.toThrow(BadDataError);
});
});
test('should return true if user has admin role', async () => {
const { accessService, accessStore } = getSetup();
const userId = 1;
accessStore.getRolesForUserId = jest
.fn()
.mockResolvedValue([{ id: 1, name: 'ADMIN', type: 'custom' }]);
const result = await accessService.isRootAdmin(userId);
expect(result).toBe(true);
expect(accessStore.getRolesForUserId).toHaveBeenCalledWith(userId);
});
test('should return false if user does not have admin role', async () => {
const { accessService, accessStore } = getSetup();
const userId = 2;
accessStore.getRolesForUserId = jest
.fn()
.mockResolvedValue([{ id: 2, name: 'user', type: 'custom' }]);
const result = await accessService.isRootAdmin(userId);
expect(result).toBe(false);
expect(accessStore.getRolesForUserId).toHaveBeenCalledWith(userId);
});

View File

@ -887,4 +887,11 @@ export class AccessService {
async getUserAccessOverview(): Promise<IUserAccessOverview[]> {
return this.store.getUserAccessOverview();
}
async isRootAdmin(userId: number): Promise<boolean> {
const roles = await this.store.getRolesForUserId(userId);
return roles.some(
(role) => role.name.toLowerCase() === ADMIN.toLowerCase(),
);
}
}