mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-21 13:47:39 +02:00
feat: Add in support for updating roles
This commit is contained in:
parent
f8550790c5
commit
b953324428
@ -379,9 +379,6 @@ export class AccessStore implements IAccessStore {
|
||||
[environment, permission],
|
||||
);
|
||||
|
||||
console.log('Gett results for ', environment, permission);
|
||||
console.log('My result is', result);
|
||||
|
||||
const permissionId = result.first();
|
||||
|
||||
return this.db(T.ROLE_PERMISSION)
|
||||
@ -392,6 +389,14 @@ export class AccessStore implements IAccessStore {
|
||||
.delete();
|
||||
}
|
||||
|
||||
async wipePermissionsFromRole(role_id: number): Promise<void> {
|
||||
return this.db(T.ROLE_PERMISSION)
|
||||
.where({
|
||||
role_id,
|
||||
})
|
||||
.delete();
|
||||
}
|
||||
|
||||
async getRootRoleForAllUsers(): Promise<IUserRole[]> {
|
||||
const rows = await this.db
|
||||
.select('id', 'user_id')
|
||||
|
@ -3,7 +3,10 @@ import { Knex } from 'knex';
|
||||
import { Logger, LogProvider } from '../logger';
|
||||
import NotFoundError from '../error/notfound-error';
|
||||
import { ICustomRole } from 'lib/types/model';
|
||||
import { ICustomRoleInsert } from 'lib/types/stores/role-store';
|
||||
import {
|
||||
ICustomRoleInsert,
|
||||
ICustomRoleUpdate,
|
||||
} from 'lib/types/stores/role-store';
|
||||
|
||||
const TABLE = 'roles';
|
||||
const COLUMNS = ['id', 'name', 'description', 'type'];
|
||||
@ -57,6 +60,20 @@ export default class RoleStore {
|
||||
return this.mapRow(rows[0]);
|
||||
}
|
||||
|
||||
async update(role: ICustomRoleUpdate): Promise<ICustomRole> {
|
||||
const rows = await this.db(TABLE)
|
||||
.where({
|
||||
id: role.id,
|
||||
})
|
||||
.update({
|
||||
id: role.id,
|
||||
name: role.name,
|
||||
description: role.description,
|
||||
})
|
||||
.returning('*');
|
||||
return this.mapRow(rows[0]);
|
||||
}
|
||||
|
||||
async exists(id: number): Promise<boolean> {
|
||||
const result = await this.db.raw(
|
||||
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE id = ?) AS present`,
|
||||
@ -72,7 +89,7 @@ export default class RoleStore {
|
||||
|
||||
mapRow(row: IRoleRow): ICustomRole {
|
||||
if (!row) {
|
||||
throw new NotFoundError('No project found');
|
||||
throw new NotFoundError('No row');
|
||||
}
|
||||
|
||||
return {
|
||||
|
@ -14,6 +14,12 @@ interface IRoleCreation {
|
||||
permissions?: IPermission[];
|
||||
}
|
||||
|
||||
interface IRoleUpdate {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
permissions?: IPermission[];
|
||||
}
|
||||
export default class RoleService {
|
||||
private logger: Logger;
|
||||
|
||||
@ -65,6 +71,25 @@ export default class RoleService {
|
||||
return newRole;
|
||||
}
|
||||
|
||||
async update(role: IRoleUpdate): Promise<ICustomRole> {
|
||||
const baseRole = {
|
||||
id: role.id,
|
||||
name: role.name,
|
||||
description: role.description,
|
||||
roleType: 'custom',
|
||||
};
|
||||
const permissions = role.permissions;
|
||||
const newRole = await this.store.update(baseRole);
|
||||
if (permissions) {
|
||||
this.accessStore.wipePermissionsFromRole(newRole.id);
|
||||
this.accessStore.addEnvironmentPermissionsToRole(
|
||||
newRole.id,
|
||||
permissions,
|
||||
);
|
||||
}
|
||||
return newRole;
|
||||
}
|
||||
|
||||
async delete(id: number): Promise<void> {
|
||||
return this.store.delete(id);
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ export interface IAccessStore extends Store<IRole, number> {
|
||||
getRolesForUserId(userId: number): Promise<IRole[]>;
|
||||
getProjectUserIdsForRole(roleId: number, projectId?: string);
|
||||
getUserIdsForRole(roleId: number, projectId?: string): Promise<number[]>;
|
||||
wipePermissionsFromRole(role_id: number): Promise<void>;
|
||||
addEnvironmentPermissionsToRole(
|
||||
role_id: number,
|
||||
permissions: IPermission[],
|
||||
|
@ -7,8 +7,16 @@ export interface ICustomRoleInsert {
|
||||
roleType: string;
|
||||
}
|
||||
|
||||
export interface ICustomRoleUpdate {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
roleType: string;
|
||||
}
|
||||
|
||||
export interface IRoleStore extends Store<ICustomRole, number> {
|
||||
getAll(): Promise<ICustomRole[]>;
|
||||
create(role: ICustomRoleInsert): Promise<ICustomRole>;
|
||||
update(role: ICustomRoleUpdate): Promise<ICustomRole>;
|
||||
delete(id: number): Promise<void>;
|
||||
}
|
||||
|
4
src/test/fixtures/fake-access-store.ts
vendored
4
src/test/fixtures/fake-access-store.ts
vendored
@ -9,6 +9,10 @@ import {
|
||||
import { IAvailablePermissions, IPermission } from 'lib/types/model';
|
||||
|
||||
class AccessStoreMock implements IAccessStore {
|
||||
wipePermissionsFromRole(role_id: number): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
unlinkUserRoles(userId: number): Promise<void> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
10
src/test/fixtures/fake-role-store.ts
vendored
10
src/test/fixtures/fake-role-store.ts
vendored
@ -1,5 +1,9 @@
|
||||
import { ICustomRole } from 'lib/types/model';
|
||||
import { ICustomRoleInsert, IRoleStore } from 'lib/types/stores/role-store';
|
||||
import {
|
||||
ICustomRoleInsert,
|
||||
ICustomRoleUpdate,
|
||||
IRoleStore,
|
||||
} from 'lib/types/stores/role-store';
|
||||
import {
|
||||
IUserFeedback,
|
||||
IUserFeedbackKey,
|
||||
@ -7,6 +11,10 @@ import {
|
||||
} from '../../lib/types/stores/user-feedback-store';
|
||||
|
||||
export default class FakeRoleStore implements IRoleStore {
|
||||
async update(role: ICustomRoleUpdate): Promise<ICustomRole> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
async get(key: number): Promise<ICustomRole> {
|
||||
return Promise.resolve({
|
||||
id: 1,
|
||||
|
Loading…
Reference in New Issue
Block a user