1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-15 01:16:22 +02:00

Add some extra fields for profile (#2119)

First version
This commit is contained in:
sjaanus 2022-09-30 13:36:45 +02:00 committed by GitHub
parent 47152cf05b
commit ddc759ac63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 41 deletions

View File

@ -7,6 +7,7 @@ import {
IAccessInfo,
IAccessStore,
IRole,
IRoleWithProject,
IUserPermission,
IUserRole,
} from '../types/stores/access-store';
@ -229,7 +230,7 @@ export class AccessStore implements IAccessStore {
}));
}
async getRolesForUserId(userId: number): Promise<IRole[]> {
async getRolesForUserId(userId: number): Promise<IRoleWithProject[]> {
return this.db
.select(['id', 'name', 'type', 'project', 'description'])
.from<IRole[]>(T.ROLES)

View File

@ -289,26 +289,28 @@ class ProjectStore implements IProjectStore {
}
async getProjectsByUser(userId: number): Promise<string[]> {
const members = await this.db.from((db) => {
db.select('project')
.from('role_user')
.leftJoin('roles', 'role_user.role_id', 'roles.id')
.where('type', 'root')
.andWhere('name', 'Editor')
.andWhere('user_id', userId)
.union((queryBuilder) => {
queryBuilder
.select('project')
.from('group_role')
.leftJoin(
'group_user',
'group_user.group_id',
'group_role.group_id',
)
.where('user_id', userId);
})
.as('query');
});
const members = await this.db
.from((db) => {
db.select('project')
.from('role_user')
.leftJoin('roles', 'role_user.role_id', 'roles.id')
.where('type', 'root')
.andWhere('name', 'Editor')
.andWhere('user_id', userId)
.union((queryBuilder) => {
queryBuilder
.select('project')
.from('group_role')
.leftJoin(
'group_user',
'group_user.group_id',
'group_role.group_id',
)
.where('user_id', userId);
})
.as('query');
})
.pluck('project');
return members;
}

View File

@ -1,10 +1,13 @@
import { validateSchema } from '../validate';
import { ProfileSchema } from './profile-schema';
import { RoleName } from '../../types/model';
test('profileSchema', () => {
const data: ProfileSchema = {
rootRole: 'Editor' as RoleName,
rootRole: {
id: 1,
type: 'root',
name: 'Admin',
},
projects: ['default', 'secretproject'],
features: [
{ name: 'firstFeature', project: 'default' },

View File

@ -1,6 +1,6 @@
import { FromSchema } from 'json-schema-to-ts';
import { featureSchema } from './feature-schema';
import { RoleName } from '../../types/model';
import { roleSchema } from './role-schema';
export const profileSchema = {
$id: '#/components/schemas/profileSchema',
@ -9,8 +9,7 @@ export const profileSchema = {
required: ['rootRole', 'projects', 'features'],
properties: {
rootRole: {
type: 'string',
enum: Object.values(RoleName),
$ref: '#/components/schemas/roleSchema',
},
projects: {
type: 'array',
@ -28,6 +27,7 @@ export const profileSchema = {
components: {
schemas: {
featureSchema,
roleSchema,
},
},
} as const;

View File

@ -21,7 +21,6 @@ import {
ProfileSchema,
} from '../../../openapi/spec/profile-schema';
import ProjectService from '../../../services/project-service';
import { RoleName } from '../../../types/model';
class UserController extends Controller {
private accessService: AccessService;
@ -148,10 +147,10 @@ class UserController extends Controller {
const projects = await this.projectService.getProjectsByUser(user.id);
const roles = await this.accessService.getUserRootRoles(user.id);
const { project, ...rootRole } = roles[0];
const responseData: ProfileSchema = {
projects,
rootRole: roles[0].name as RoleName,
rootRole,
features: [],
};

View File

@ -5,6 +5,7 @@ import {
IAccessStore,
IRole,
IRoleWithPermissions,
IRoleWithProject,
IUserPermission,
IUserRole,
} from '../types/stores/access-store';
@ -243,7 +244,7 @@ export class AccessService {
}
}
async getUserRootRoles(userId: number): Promise<IRole[]> {
async getUserRootRoles(userId: number): Promise<IRoleWithProject[]> {
const userRoles = await this.store.getRolesForUserId(userId);
return userRoles.filter((r) => r.type === RoleType.ROOT);
}

View File

@ -14,6 +14,10 @@ export interface IRole {
type: string;
}
export interface IRoleWithProject extends IRole {
project: string;
}
export interface IRoleWithPermissions extends IRole {
permissions: IPermission[];
}
@ -49,7 +53,7 @@ export interface IAccessStore extends Store<IRole, number> {
unlinkUserRoles(userId: number): Promise<void>;
getRolesForUserId(userId: number): Promise<IRole[]>;
getRolesForUserId(userId: number): Promise<IRoleWithProject[]>;
getProjectUsersForRole(
roleId: number,

View File

@ -2167,14 +2167,7 @@ exports[`should serve the OpenAPI spec 1`] = `
"type": "array",
},
"rootRole": {
"enum": [
"Admin",
"Editor",
"Viewer",
"Owner",
"Member",
],
"type": "string",
"$ref": "#/components/schemas/roleSchema",
},
},
"required": [

View File

@ -4,10 +4,11 @@ import {
IAccessInfo,
IAccessStore,
IRole,
IRoleWithProject,
IUserPermission,
IUserRole,
} from '../../lib/types/stores/access-store';
import { IAvailablePermissions, IPermission } from 'lib/types/model';
import { IPermission } from 'lib/types/model';
class AccessStoreMock implements IAccessStore {
addAccessToProject(
@ -123,7 +124,7 @@ class AccessStoreMock implements IAccessStore {
throw new Error('Method not implemented.');
}
getRolesForUserId(userId: number): Promise<IRole[]> {
getRolesForUserId(userId: number): Promise<IRoleWithProject[]> {
return Promise.resolve([]);
}