1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-19 01:17:18 +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, IAccessInfo,
IAccessStore, IAccessStore,
IRole, IRole,
IRoleWithProject,
IUserPermission, IUserPermission,
IUserRole, IUserRole,
} from '../types/stores/access-store'; } 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 return this.db
.select(['id', 'name', 'type', 'project', 'description']) .select(['id', 'name', 'type', 'project', 'description'])
.from<IRole[]>(T.ROLES) .from<IRole[]>(T.ROLES)

View File

@ -289,7 +289,8 @@ class ProjectStore implements IProjectStore {
} }
async getProjectsByUser(userId: number): Promise<string[]> { async getProjectsByUser(userId: number): Promise<string[]> {
const members = await this.db.from((db) => { const members = await this.db
.from((db) => {
db.select('project') db.select('project')
.from('role_user') .from('role_user')
.leftJoin('roles', 'role_user.role_id', 'roles.id') .leftJoin('roles', 'role_user.role_id', 'roles.id')
@ -308,7 +309,8 @@ class ProjectStore implements IProjectStore {
.where('user_id', userId); .where('user_id', userId);
}) })
.as('query'); .as('query');
}); })
.pluck('project');
return members; return members;
} }

View File

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

View File

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

View File

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

View File

@ -5,6 +5,7 @@ import {
IAccessStore, IAccessStore,
IRole, IRole,
IRoleWithPermissions, IRoleWithPermissions,
IRoleWithProject,
IUserPermission, IUserPermission,
IUserRole, IUserRole,
} from '../types/stores/access-store'; } 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); const userRoles = await this.store.getRolesForUserId(userId);
return userRoles.filter((r) => r.type === RoleType.ROOT); return userRoles.filter((r) => r.type === RoleType.ROOT);
} }

View File

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

View File

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

View File

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