Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add global isAdmin method for access service #7088

Merged
merged 2 commits into from
May 21, 2024
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
11 changes: 9 additions & 2 deletions src/lib/features/access/createAccessService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -71,5 +76,7 @@ export const createFakeAccessService = (
return {
accessService,
eventStore,
accessStore,
roleStore,
};
};
28 changes: 28 additions & 0 deletions src/lib/services/access-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
7 changes: 7 additions & 0 deletions src/lib/services/access-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
Loading