mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	
							parent
							
								
									47152cf05b
								
							
						
					
					
						commit
						ddc759ac63
					
				| @ -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) | ||||
|  | ||||
| @ -289,7 +289,8 @@ class ProjectStore implements IProjectStore { | ||||
|     } | ||||
| 
 | ||||
|     async getProjectsByUser(userId: number): Promise<string[]> { | ||||
|         const members = await this.db.from((db) => { | ||||
|         const members = await this.db | ||||
|             .from((db) => { | ||||
|                 db.select('project') | ||||
|                     .from('role_user') | ||||
|                     .leftJoin('roles', 'role_user.role_id', 'roles.id') | ||||
| @ -308,7 +309,8 @@ class ProjectStore implements IProjectStore { | ||||
|                             .where('user_id', userId); | ||||
|                     }) | ||||
|                     .as('query'); | ||||
|         }); | ||||
|             }) | ||||
|             .pluck('project'); | ||||
|         return members; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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' }, | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -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: [], | ||||
|         }; | ||||
| 
 | ||||
|  | ||||
| @ -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); | ||||
|     } | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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": [ | ||||
|  | ||||
							
								
								
									
										5
									
								
								src/test/fixtures/fake-access-store.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								src/test/fixtures/fake-access-store.ts
									
									
									
									
										vendored
									
									
								
							| @ -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([]); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user