1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-04 13:48:56 +02:00

fix: Make project roles resolve correctly against new environments permissions structure

This commit is contained in:
sighphyre 2021-12-13 16:10:55 +02:00 committed by Ivar Conradi Østhus
parent 9d4f542f85
commit d387ac0cbd
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
4 changed files with 34 additions and 8 deletions

View File

@ -170,6 +170,13 @@ export class AccessStore implements IAccessStore {
.from<IRole>(T.ROLES); .from<IRole>(T.ROLES);
} }
async getProjectRoles(): Promise<IRole[]> {
return this.db
.select(['id', 'name', 'type', 'description'])
.from<IRole>(T.ROLES)
.andWhere('type', 'project');
}
async getRolesForProject(projectId: string): Promise<IRole[]> { async getRolesForProject(projectId: string): Promise<IRole[]> {
return this.db return this.db
.select(['id', 'name', 'type', 'project', 'description']) .select(['id', 'name', 'type', 'project', 'description'])
@ -201,11 +208,15 @@ export class AccessStore implements IAccessStore {
.where('ru.user_id', '=', userId); .where('ru.user_id', '=', userId);
} }
async getUserIdsForRole(roleId: number): Promise<number[]> { async getUserIdsForRole(
roleId: number,
projectId?: string,
): Promise<number[]> {
const rows = await this.db const rows = await this.db
.select(['user_id']) .select(['user_id'])
.from<IRole>(T.ROLE_USER) .from<IRole>(T.ROLE_USER)
.where('role_id', roleId); .where('role_id', roleId)
.andWhere('project', projectId);
return rows.map((r) => r.user_id); return rows.map((r) => r.user_id);
} }

View File

@ -218,6 +218,10 @@ export class AccessService {
return { role, permissions: rolePerms, users }; return { role, permissions: rolePerms, users };
} }
async getProjectRoles(): Promise<IRole[]> {
return this.store.getProjectRoles();
}
async getRolesForProject(projectId: string): Promise<IRole[]> { async getRolesForProject(projectId: string): Promise<IRole[]> {
return this.store.getRolesForProject(projectId); return this.store.getRolesForProject(projectId);
} }
@ -226,8 +230,14 @@ export class AccessService {
return this.store.getRolesForUserId(userId); return this.store.getRolesForUserId(userId);
} }
async getUsersForRole(roleId: number): Promise<IUser[]> { async getUsersForRole(
const userIdList = await this.store.getUserIdsForRole(roleId); roleId: number,
projectId?: string,
): Promise<IUser[]> {
const userIdList = await this.store.getUserIdsForRole(
roleId,
projectId,
);
if (userIdList.length > 0) { if (userIdList.length > 0) {
return this.userStore.getAllWithId(userIdList); return this.userStore.getAllWithId(userIdList);
} }
@ -238,11 +248,11 @@ export class AccessService {
async getProjectRoleUsers( async getProjectRoleUsers(
projectId: string, projectId: string,
): Promise<[IRole[], IUserWithRole[]]> { ): Promise<[IRole[], IUserWithRole[]]> {
const roles = await this.store.getRolesForProject(projectId); const roles = await this.store.getProjectRoles();
const users = await Promise.all( const users = await Promise.all(
roles.map(async (role) => { roles.map(async (role) => {
const usrs = await this.getUsersForRole(role.id); const usrs = await this.getUsersForRole(role.id, projectId);
return usrs.map((u) => ({ ...u, roleId: role.id })); return usrs.map((u) => ({ ...u, roleId: role.id }));
}), }),
); );

View File

@ -24,10 +24,11 @@ export interface IAccessStore extends Store<IRole, number> {
getPermissionsForRole(roleId: number): Promise<IUserPermission[]>; getPermissionsForRole(roleId: number): Promise<IUserPermission[]>;
getRoles(): Promise<IRole[]>; getRoles(): Promise<IRole[]>;
getRolesForProject(projectId: string): Promise<IRole[]>; getRolesForProject(projectId: string): Promise<IRole[]>;
getProjectRoles(): Promise<IRole[]>;
getRootRoles(): Promise<IRole[]>; getRootRoles(): Promise<IRole[]>;
removeRolesForProject(projectId: string): Promise<void>; removeRolesForProject(projectId: string): Promise<void>;
getRolesForUserId(userId: number): Promise<IRole[]>; getRolesForUserId(userId: number): Promise<IRole[]>;
getUserIdsForRole(roleId: number): Promise<number[]>; getUserIdsForRole(roleId: number, projectId?: string): Promise<number[]>;
addEnvironmentPermissionsToRole( addEnvironmentPermissionsToRole(
role_id: number, role_id: number,
permissions: IPermission[], permissions: IPermission[],

View File

@ -9,6 +9,10 @@ import {
import { IAvailablePermissions, IPermission } from 'lib/types/model'; import { IAvailablePermissions, IPermission } from 'lib/types/model';
class AccessStoreMock implements IAccessStore { class AccessStoreMock implements IAccessStore {
getProjectRoles(): Promise<IRole[]> {
throw new Error('Method not implemented.');
}
addEnvironmentPermissionsToRole( addEnvironmentPermissionsToRole(
role_id: number, role_id: number,
permissions: IPermission[], permissions: IPermission[],
@ -59,7 +63,7 @@ class AccessStoreMock implements IAccessStore {
return Promise.resolve([]); return Promise.resolve([]);
} }
getUserIdsForRole(roleId: number): Promise<number[]> { getUserIdsForRole(roleId: number, projectId: string): Promise<number[]> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }