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:
parent
8a6daeee1e
commit
45c75a65ce
@ -12,7 +12,7 @@ import FakeRoleStore from '../../../test/fixtures/fake-role-store';
|
|||||||
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
|
import FakeEnvironmentStore from '../project-environments/fake-environment-store';
|
||||||
import FakeAccessStore from '../../../test/fixtures/fake-access-store';
|
import FakeAccessStore from '../../../test/fixtures/fake-access-store';
|
||||||
import FakeFeatureTagStore from '../../../test/fixtures/fake-feature-tag-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';
|
import { createEventsService } from '../events/createEventsService';
|
||||||
|
|
||||||
export const createAccessService = (
|
export const createAccessService = (
|
||||||
@ -42,7 +42,12 @@ export const createAccessService = (
|
|||||||
|
|
||||||
export const createFakeAccessService = (
|
export const createFakeAccessService = (
|
||||||
config: IUnleashConfig,
|
config: IUnleashConfig,
|
||||||
): { accessService: AccessService; eventStore: IEventStore } => {
|
): {
|
||||||
|
accessService: AccessService;
|
||||||
|
eventStore: IEventStore;
|
||||||
|
accessStore: IAccessStore;
|
||||||
|
roleStore: IRoleStore;
|
||||||
|
} => {
|
||||||
const { getLogger, flagResolver } = config;
|
const { getLogger, flagResolver } = config;
|
||||||
const eventStore = new FakeEventStore();
|
const eventStore = new FakeEventStore();
|
||||||
const groupStore = new FakeGroupStore();
|
const groupStore = new FakeGroupStore();
|
||||||
@ -71,5 +76,7 @@ export const createFakeAccessService = (
|
|||||||
return {
|
return {
|
||||||
accessService,
|
accessService,
|
||||||
eventStore,
|
eventStore,
|
||||||
|
accessStore,
|
||||||
|
roleStore,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -285,3 +285,31 @@ describe('addAccessToProject', () => {
|
|||||||
).rejects.toThrow(BadDataError);
|
).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);
|
||||||
|
});
|
||||||
|
@ -887,4 +887,11 @@ export class AccessService {
|
|||||||
async getUserAccessOverview(): Promise<IUserAccessOverview[]> {
|
async getUserAccessOverview(): Promise<IUserAccessOverview[]> {
|
||||||
return this.store.getUserAccessOverview();
|
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(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user