From a40de80658cc4f948daf3ea473837a60e45c9e97 Mon Sep 17 00:00:00 2001 From: tomgny <49343696+tomgny@users.noreply.github.com> Date: Wed, 24 Nov 2021 11:18:20 +0100 Subject: [PATCH] [AAE-6389] Remove duplicated identity user service mock (#7373) * [AAE-6389] removed duplicate services and refactored existing * [AAE-6389] improved and moved mocks to proper files * [AAE-6389] fixed circular dependency --- lib/core/mock/identity-group.mock.ts | 76 ++++ lib/core/mock/identity-group.service.mock.ts | 193 +++++----- lib/core/mock/identity-user.mock.ts | 54 +++ lib/core/mock/identity-user.service.mock.ts | 344 +++++++++++------- lib/core/mock/oauth2.service.mock.ts | 171 +++++++++ lib/core/mock/public-api.ts | 3 + .../services/identity-group.service.spec.ts | 18 +- lib/core/services/identity-group.service.ts | 2 +- .../identity-user.service.interface.ts | 1 + .../services/identity-user.service.spec.ts | 32 +- .../group-cloud.component.stories.ts | 3 +- .../group/mock/identity-group.service.mock.ts | 146 -------- .../people-cloud.component.stories.ts | 16 +- .../people/mock/identity-user.service.mock.ts | 265 -------------- 14 files changed, 672 insertions(+), 652 deletions(-) create mode 100644 lib/core/mock/identity-group.mock.ts create mode 100644 lib/core/mock/identity-user.mock.ts create mode 100644 lib/core/mock/oauth2.service.mock.ts delete mode 100644 lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts delete mode 100644 lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts diff --git a/lib/core/mock/identity-group.mock.ts b/lib/core/mock/identity-group.mock.ts new file mode 100644 index 0000000000..2dae1063df --- /dev/null +++ b/lib/core/mock/identity-group.mock.ts @@ -0,0 +1,76 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { IdentityGroupModel, IdentityGroupCountModel } from '../models/identity-group.model'; +import { IdentityRoleModel } from '../models/identity-role.model'; +import { IdentityJoinGroupRequestModel } from '../services/identity-user.service.interface'; + +export const mockIdentityGroup1 = { + id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] +}; + +export const mockIdentityGroup2 = { + id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] +}; + +export const mockIdentityGroup3 = { + id: 'mock-group-id-3', name: 'Mock Group 3', path: '', subGroups: [] +}; + +export const mockIdentityGroup4 = { + id: 'mock-group-id-4', name: 'Mock Group 4', path: '', subGroups: [] +}; + +export const mockIdentityGroup5 = { + id: 'mock-group-id-5', name: 'Mock Group 5', path: '', subGroups: [] +}; + +export const mockIdentityGroupsCount = { count: 10 }; + +export const mockIdentityGroups = [ + mockIdentityGroup1, mockIdentityGroup2, mockIdentityGroup3, mockIdentityGroup4, mockIdentityGroup5 +]; + +export const roleMappingMock = [ + { id: 'role-id-1', name: 'role-name-1' }, { id: 'role-id-2', name: 'role-name-2' } +]; + +export const mockIdentityRoles = [ + new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE'}), + new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-USER-ROLE'}), + new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ROLE-1'}) +]; + +export const clientRoles: IdentityRoleModel[] = [ + new IdentityRoleModel({ name: 'MOCK-ADMIN-ROLE' }), + new IdentityRoleModel({ name: 'MOCK-USER-ROLE' }) +]; + +export const mockJoinGroupRequest: IdentityJoinGroupRequestModel = {userId: 'mock-hser-id', groupId: 'mock-group-id', realm: 'mock-realm-name'}; + +export const mockGroup1 = { + id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] +}; + +export const mockGroup2 = { + id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] +}; + +export const mockGroups = [ + { id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] }, + { id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] } +]; diff --git a/lib/core/mock/identity-group.service.mock.ts b/lib/core/mock/identity-group.service.mock.ts index 139a075498..38d6b58ef8 100644 --- a/lib/core/mock/identity-group.service.mock.ts +++ b/lib/core/mock/identity-group.service.mock.ts @@ -15,104 +15,131 @@ * limitations under the License. */ -import { IdentityGroupModel, IdentityGroupCountModel } from '../models/identity-group.model'; +import { Injectable } from '@angular/core'; +import { mockIdentityGroups, mockIdentityGroupsCount, mockIdentityRoles } from './identity-group.mock'; +import { Observable, of } from 'rxjs'; +import { map } from 'rxjs/operators'; +import { IdentityGroupServiceInterface } from '../services/identity-group.interface'; +import { + IdentityGroupModel, + IdentityGroupQueryResponse, + IdentityGroupQueryCloudRequestModel, + IdentityGroupSearchParam, + IdentityGroupCountModel +} from '../models/identity-group.model'; import { IdentityRoleModel } from '../models/identity-role.model'; -export const mockIdentityGroup1 = { - id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] -}; +Injectable({ providedIn: 'root' }); +export class IdentityGroupServiceMock implements IdentityGroupServiceInterface { -export const mockIdentityGroup2 = { - id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] -}; - -export const mockIdentityGroup3 = { - id: 'mock-group-id-3', name: 'Mock Group 3', path: '', subGroups: [] -}; - -export const mockIdentityGroup4 = { - id: 'mock-group-id-4', name: 'Mock Group 4', path: '', subGroups: [] -}; - -export const mockIdentityGroup5 = { - id: 'mock-group-id-5', name: 'Mock Group 5', path: '', subGroups: [] -}; - -export const mockIdentityGroupsCount = { count: 10 }; - -export const mockIdentityGroups = [ - mockIdentityGroup1, mockIdentityGroup2, mockIdentityGroup3, mockIdentityGroup4, mockIdentityGroup5 -]; - -export const mockApplicationDetails = {id: 'mock-app-id', name: 'mock-app-name'}; - -export const roleMappingMock = [ - { id: 'role-id-1', name: 'role-name-1' }, { id: 'role-id-2', name: 'role-name-2' } -]; - -export const roleMappingApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(roleMappingMock); - } + getGroups(): Observable { + return of(mockIdentityGroups); } -}; -export const noRoleMappingApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve([]); - } + getAvailableRoles(_groupId: string): Observable { + return of(mockIdentityRoles); } -}; -export const groupsMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockIdentityGroups); - } + getAssignedRoles(_groupId: string): Observable { + return of(mockIdentityRoles); } -}; -export const createGroupMappingApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + assignRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable { + return of(); } -}; -export const updateGroupMappingApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + removeRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable { + return of(); } -}; -export const deleteGroupMappingApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + getEffectiveRoles(_groupId: string): Observable { + return of(mockIdentityRoles); } -}; -export const applicationDetailsMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve([mockApplicationDetails]); - } + queryGroups(_requestQuery: IdentityGroupQueryCloudRequestModel): Observable { + return of(); } -}; -export const mockIdentityRoles = [ - new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE'}), - new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-USER-ROLE'}), - new IdentityRoleModel({id: 'mock-role-id', name: 'MOCK-ROLE-1'}) -]; + getTotalGroupsCount(): Observable { + return of(mockIdentityGroupsCount); + } -export const clientRoles: IdentityRoleModel[] = [ - new IdentityRoleModel({ name: 'MOCK-ADMIN-ROLE' }), - new IdentityRoleModel({ name: 'MOCK-USER-ROLE' }) -]; + createGroup(_newGroup: IdentityGroupModel): Observable { + return of(); + } + + updateGroup(_groupId: string, _updatedGroup: IdentityGroupModel): Observable { + return of(); + } + + deleteGroup(_groupId: string): Observable { + return of(); + } + + findGroupsByName(searchParams: IdentityGroupSearchParam): Observable { + if (searchParams.name === '') { + return of([]); + } + + return of(mockIdentityGroups.filter(group => + group.name.toUpperCase().includes(searchParams.name.toUpperCase()) + )); + } + + getGroupRoles(_groupId: string): Observable { + return of(mockIdentityRoles); + } + + checkGroupHasRole(groupId: string, roleNames: string[]): Observable { + return this.getGroupRoles(groupId).pipe(map((groupRoles) => { + let hasRole = false; + if (groupRoles?.length > 0) { + roleNames.forEach((roleName: string) => { + const role = groupRoles.find(({ name }) => roleName === name); + if (role) { + hasRole = true; + return; + } + }); + } + return hasRole; + })); + } + + getClientIdByApplicationName(_applicationName: string): Observable { + return of('fake-client-id'); + } + + getClientRoles(groupId: string, _clientId: string): Observable { + if (['mock-group-id-1', 'mock-group-id-2'].includes(groupId)) { + return of([{ id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE' }]); + } + + return of([{ id: 'mock-role-id', name: 'MOCK-USER-ROLE' }]); + } + + checkGroupHasClientApp(groupId: string, clientId: string): Observable { + return this.getClientRoles(groupId, clientId).pipe( + map((response) => response && response.length > 0) + ); + } + + checkGroupHasAnyClientAppRole(groupId: string, clientId: string, roleNames: string[]): Observable { + return this.getClientRoles(groupId, clientId).pipe( + map((clientRoles: any[]) => { + let hasRole = false; + if (clientRoles.length > 0) { + roleNames.forEach((roleName) => { + const role = clientRoles.find(({ name }) => name === roleName); + + if (role) { + hasRole = true; + return; + } + }); + } + return hasRole; + }) + ); + } +} diff --git a/lib/core/mock/identity-user.mock.ts b/lib/core/mock/identity-user.mock.ts new file mode 100644 index 0000000000..7e7902e36b --- /dev/null +++ b/lib/core/mock/identity-user.mock.ts @@ -0,0 +1,54 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { IdentityUserModel } from '../models/identity-user.model'; +import { IdentityRoleModel } from '../models/identity-role.model'; + +export const mockIdentityUser1: IdentityUserModel = { id: 'mock-user-id-1', username: 'userName1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com' }; +export const mockIdentityUser2: IdentityUserModel = { id: 'mock-user-id-2', username: 'userName2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com'}; +export const mockIdentityUser3: IdentityUserModel = { id: 'mock-user-id-3', username: 'userName3', firstName: 'first-name-3', lastName: 'last-name-3', email: 'abcde@xyz.com' }; +export const mockIdentityUser4: IdentityUserModel = { id: 'mock-user-id-4', username: 'userName4', firstName: 'first-name-4', lastName: 'last-name-4', email: 'abcde@xyz.com' }; +export let mockIdentityUser5: IdentityUserModel = { id: 'mock-user-id-5', username: 'userName5', firstName: 'first-name-5', lastName: 'last-name-5', email: 'abcde@xyz.com' }; + +export const mockIdentityUsers: IdentityUserModel[] = [ + mockIdentityUser1, + mockIdentityUser2, + mockIdentityUser3, + mockIdentityUser4, + mockIdentityUser5 +]; + +export const mockIdentityRole = new IdentityRoleModel({ id: 'id-1', name: 'MOCK-ADMIN-ROLE'}); + +export const mockAvailableRoles = [ + new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}), + new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK-USER-ROLE'}), + new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK_MODELER-ROLE' }), + new IdentityRoleModel({ id: 'mock-role-id-5', name: 'MOCK-ROLE-2'}) +]; + +export const mockAssignedRoles = [ + new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}), + new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK_MODELER-ROLE' }), + new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK-ROLE-1' }) +]; + +export const mockEffectiveRoles = [ + new IdentityRoleModel({id: 'mock-role-id-1', name: 'MOCK-ACTIVE-ADMIN-ROLE'}), + new IdentityRoleModel({id: 'mock-role-id-2', name: 'MOCK-ACTIVE-USER-ROLE'}), + new IdentityRoleModel({id: 'mock-role-id-3', name: 'MOCK-ROLE-1'}) +]; diff --git a/lib/core/mock/identity-user.service.mock.ts b/lib/core/mock/identity-user.service.mock.ts index 0345a63bed..16f94b59f3 100644 --- a/lib/core/mock/identity-user.service.mock.ts +++ b/lib/core/mock/identity-user.service.mock.ts @@ -15,153 +15,251 @@ * limitations under the License. */ +import { Injectable } from '@angular/core'; +import { IdentityGroupModel } from '../models/identity-group.model'; import { IdentityUserModel } from '../models/identity-user.model'; import { IdentityRoleModel } from '../models/identity-role.model'; -import { IdentityGroupModel } from '../models/identity-group.model'; -import { IdentityJoinGroupRequestModel } from '../services/identity-user.service.interface'; +import { + IdentityUserServiceInterface, + IdentityUserQueryCloudRequestModel, + IdentityUserQueryResponse, + IdentityUserPasswordModel, + IdentityJoinGroupRequestModel +} from '../services/identity-user.service.interface'; +import { mockIdentityGroups } from '../mock/identity-group.mock'; +import { Observable, of } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import { mockAssignedRoles, mockAvailableRoles, mockEffectiveRoles, mockIdentityUser1, mockIdentityUsers } from './identity-user.mock'; -export const mockIdentityUser1: IdentityUserModel = { id: 'mock-user-id-1', username: 'userName1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com' }; -export const mockIdentityUser2: IdentityUserModel = { id: 'mock-user-id-2', username: 'userName2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com'}; -export const mockIdentityUser3: IdentityUserModel = { id: 'mock-user-id-3', username: 'userName3', firstName: 'first-name-3', lastName: 'last-name-3', email: 'abcde@xyz.com' }; -export const mockIdentityUser4: IdentityUserModel = { id: 'mock-user-id-4', username: 'userName4', firstName: 'first-name-4', lastName: 'last-name-4', email: 'abcde@xyz.com' }; -export let mockIdentityUser5: IdentityUserModel = { id: 'mock-user-id-5', username: 'userName5', firstName: 'first-name-5', lastName: 'last-name-5', email: 'abcde@xyz.com' }; +@Injectable({ + providedIn: 'root' +}) +export class IdentityUserServiceMock implements IdentityUserServiceInterface { -export const mockIdentityUsers: IdentityUserModel[] = [ - mockIdentityUser1, - mockIdentityUser2, - mockIdentityUser3, - mockIdentityUser4, - mockIdentityUser5 -]; - -export const mockIdentityRole = new IdentityRoleModel({ id: 'id-1', name: 'MOCK-ADMIN-ROLE'}); - -export const mockAvailableRoles = [ - new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}), - new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK-USER-ROLE'}), - new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK_MODELER-ROLE' }), - new IdentityRoleModel({ id: 'mock-role-id-5', name: 'MOCK-ROLE-2'}) -]; - -export const mockAssignedRoles = [ - new IdentityRoleModel({ id: 'mock-role-id-1', name: 'MOCK-ADMIN-ROLE'}), - new IdentityRoleModel({ id: 'mock-role-id-2', name: 'MOCK_MODELER-ROLE' }), - new IdentityRoleModel({ id: 'mock-role-id-3', name: 'MOCK-ROLE-1' }) -]; - -export const mockEffectiveRoles = [ - new IdentityRoleModel({id: 'mock-role-id-1', name: 'MOCK-ACTIVE-ADMIN-ROLE'}), - new IdentityRoleModel({id: 'mock-role-id-2', name: 'MOCK-ACTIVE-USER-ROLE'}), - new IdentityRoleModel({id: 'mock-role-id-3', name: 'MOCK-ROLE-1'}) -]; - -export const mockJoinGroupRequest: IdentityJoinGroupRequestModel = {userId: 'mock-hser-id', groupId: 'mock-group-id', realm: 'mock-realm-name'}; - -export const mockGroup1 = { - id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] -}; - -export const mockGroup2 = { - id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] -}; - -export const mockGroups = [ - { id: 'mock-group-id-1', name: 'Mock Group 1', path: '/mock', subGroups: [] }, - { id: 'mock-group-id-2', name: 'Mock Group 2', path: '', subGroups: [] } -]; - -export const queryUsersMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockIdentityUsers); - } + getCurrentUserInfo(): IdentityUserModel { + return mockIdentityUser1; } -}; -export const createUserMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); + findUsersByName(search: string): Observable { + if (search === '') { + return of([]); } - } -}; -export const updateUserMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + return of(mockIdentityUsers.filter(user => + user.username.toUpperCase().includes(search.toUpperCase()) + )); } -}; -export const deleteUserMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); + findUserByUsername(username: string): Observable { + if (username === '') { + return of([]); } - } -}; -export const getInvolvedGroupsMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockGroups); - } + return of(mockIdentityUsers.filter(user => user.username === username)); } -}; -export const joinGroupMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); + findUserByEmail(email: string): Observable { + if (email === '') { + return of([]); } - } -}; -export const leaveGroupMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + return of(mockIdentityUsers.filter(user => user.email === email)); } -}; -export const getAvailableRolesMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockAvailableRoles); + findUserById(id: string): Observable { + if (id === '') { + return of([]); } - } -}; -export const getAssignedRolesMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockAssignedRoles); - } + return of(mockIdentityUsers.find(user => user.id === id)); } -}; -export const getEffectiveRolesMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(mockEffectiveRoles); + getClientRoles(userId: string, _clientId: string): Observable { + if (userId === 'mock-user-id-1') { + return of([{ id: 'id-1', name: 'MOCK-ADMIN-ROLE' }]); } - } -}; -export const assignRolesMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + return of([{ id: 'id-2', name: 'MOCK-USER-ROLE' }]); } -}; -export const removeRolesMockApi: any = { - oauth2Auth: { - callCustomApi: () => { - return Promise.resolve(); - } + checkUserHasClientApp(userId: string, clientId: string): Observable { + return this.getClientRoles(userId, clientId).pipe( + map((clientRoles) => clientRoles.length > 0) + ); } -}; + + checkUserHasAnyClientAppRole(userId: string, clientId: string, roleNames: string[]): Observable { + return this.getClientRoles(userId, clientId).pipe( + map((clientRoles: any[]) => { + let hasRole = false; + if (clientRoles.length > 0) { + roleNames.forEach((roleName) => { + const role = clientRoles.find(({ name }) => name === roleName); + + if (role) { + hasRole = true; + return; + } + }); + } + return hasRole; + }) + ); + } + + getClientIdByApplicationName(_applicationName: string): Observable { + return of('mock-user-id-1'); + } + + checkUserHasApplicationAccess(userId: string, applicationName: string): Observable { + return this.getClientIdByApplicationName(applicationName).pipe( + switchMap((clientId: string) => { + return this.checkUserHasClientApp(userId, clientId); + }) + ); + } + + checkUserHasAnyApplicationRole(userId: string, applicationName: string, roleNames: string[]): Observable { + return this.getClientIdByApplicationName(applicationName).pipe( + switchMap((clientId: string) => { + return this.checkUserHasAnyClientAppRole(userId, clientId, roleNames); + }) + ); + } + + getUsers(): Observable { + return of(mockIdentityUsers); + } + + getUserRoles(_userId: string): Observable { + return of(mockAvailableRoles); + } + + async getUsersByRolesWithCurrentUser(roleNames: string[]): Promise { + const filteredUsers: IdentityUserModel[] = []; + if (roleNames && roleNames.length > 0) { + const users = await this.getUsers().toPromise(); + + for (let i = 0; i < users.length; i++) { + const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames); + if (hasAnyRole) { + filteredUsers.push(users[i]); + } + } + } + + return filteredUsers; + } + + async getUsersByRolesWithoutCurrentUser(roleNames: string[]): Promise { + const filteredUsers: IdentityUserModel[] = []; + if (roleNames && roleNames.length > 0) { + const currentUser = this.getCurrentUserInfo(); + let users = await this.getUsers().toPromise(); + + users = users.filter(({ username }) => username !== currentUser.username); + + for (let i = 0; i < users.length; i++) { + const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames); + if (hasAnyRole) { + filteredUsers.push(users[i]); + } + } + } + + return filteredUsers; + } + + private async userHasAnyRole(userId: string, roleNames: string[]): Promise { + const userRoles = await this.getUserRoles(userId).toPromise(); + const hasAnyRole = roleNames.some((roleName) => { + const filteredRoles = userRoles.filter((userRole) => { + return userRole.name.toLocaleLowerCase() === roleName.toLocaleLowerCase(); + }); + + return filteredRoles.length > 0; + }); + + return hasAnyRole; + } + + checkUserHasRole(userId: string, roleNames: string[]): Observable { + return this.getUserRoles(userId).pipe(map((userRoles: IdentityRoleModel[]) => { + let hasRole = false; + if (userRoles && userRoles.length > 0) { + roleNames.forEach((roleName: string) => { + const role = userRoles.find(({ name }) => roleName === name); + if (role) { + hasRole = true; + return; + } + }); + } + return hasRole; + })); + } + + queryUsers(_requestQuery: IdentityUserQueryCloudRequestModel): Observable { + return of(); + } + + getTotalUsersCount(): Observable { + return of(mockIdentityUsers.length); + } + + createUser(newUser: IdentityUserModel): Observable { + window.alert(`Create new user: ${newUser}`); + return of([]); + } + + updateUser(userId: string, updatedUser: IdentityUserModel): Observable { + window.alert(`Update user: ${updatedUser} with ID: ${userId}`); + return of([]); + } + + deleteUser(userId: string): Observable { + window.alert(`Delete user with ID: ${userId}`); + return of([]); + } + + changePassword(userId: string, newPassword: IdentityUserPasswordModel): Observable { + window.alert(`New password: ${newPassword} for user with ID: ${userId}`); + return of([]); + } + + getInvolvedGroups(_userId: string): Observable { + return of(mockIdentityGroups); + } + + joinGroup(joinGroupRequest: IdentityJoinGroupRequestModel): Observable { + window.alert(`Join group request: ${joinGroupRequest}`); + return of([]); + } + + leaveGroup(userId: any, groupId: string): Observable { + window.alert(`Leave group: ${groupId} for user with ID: ${userId}`); + return of([]); + } + + getAvailableRoles(_userId: string): Observable { + return of(mockAvailableRoles); + } + + getAssignedRoles(_userId: string): Observable { + return of(mockAssignedRoles); + } + + getEffectiveRoles(_userId: string): Observable { + return of(mockEffectiveRoles); + } + + assignRoles(userId: string, roles: IdentityRoleModel[]): Observable { + window.alert(`Assign roles: ${roles} for user with ID: ${userId}`); + return of([]); + } + + removeRoles(userId: string, removedRoles: IdentityRoleModel[]): Observable { + window.alert(`Remove roles: ${removedRoles} for user with ID: ${userId}`); + return of([]); + } +} diff --git a/lib/core/mock/oauth2.service.mock.ts b/lib/core/mock/oauth2.service.mock.ts new file mode 100644 index 0000000000..e59c5b2c4a --- /dev/null +++ b/lib/core/mock/oauth2.service.mock.ts @@ -0,0 +1,171 @@ +/*! + * @license + * Copyright 2019 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { mockGroups, mockIdentityGroups, roleMappingMock } from './identity-group.mock'; +import { mockAssignedRoles, mockAvailableRoles, mockEffectiveRoles, mockIdentityUsers } from './identity-user.mock'; + +export const queryUsersMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockIdentityUsers); + } + } +}; + +export const createUserMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const updateUserMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const deleteUserMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const getInvolvedGroupsMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockGroups); + } + } +}; + +export const joinGroupMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const leaveGroupMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const getAvailableRolesMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockAvailableRoles); + } + } +}; + +export const getAssignedRolesMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockAssignedRoles); + } + } +}; + +export const getEffectiveRolesMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockEffectiveRoles); + } + } +}; + +export const assignRolesMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const removeRolesMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const roleMappingApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(roleMappingMock); + } + } +}; + +export const noRoleMappingApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve([]); + } + } +}; + +export const groupsMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(mockIdentityGroups); + } + } +}; + +export const createGroupMappingApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const updateGroupMappingApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const deleteGroupMappingApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve(); + } + } +}; + +export const applicationDetailsMockApi: any = { + oauth2Auth: { + callCustomApi: () => { + return Promise.resolve([{id: 'mock-app-id', name: 'mock-app-name'}]); + } + } +}; diff --git a/lib/core/mock/public-api.ts b/lib/core/mock/public-api.ts index 34714f6134..1f1fb1520d 100644 --- a/lib/core/mock/public-api.ts +++ b/lib/core/mock/public-api.ts @@ -37,4 +37,7 @@ export * from './form/start-form.component.mock'; export * from './form/form.service.mock'; export * from './form/widget-visibility.service.mock'; export * from './jwt-helper.service.spec'; +export * from './identity-group.mock'; +export * from './identity-user.mock'; export * from './identity-group.service.mock'; +export * from './identity-user.service.mock'; diff --git a/lib/core/services/identity-group.service.spec.ts b/lib/core/services/identity-group.service.spec.ts index a712c00f28..6234d3fe1d 100644 --- a/lib/core/services/identity-group.service.spec.ts +++ b/lib/core/services/identity-group.service.spec.ts @@ -24,20 +24,22 @@ import { IdentityGroupSearchParam } from '../models/identity-group.model'; import { HttpErrorResponse } from '@angular/common/http'; import { throwError, of } from 'rxjs'; import { - noRoleMappingApi, mockIdentityRoles, - groupsMockApi, - roleMappingApi, clientRoles, - applicationDetailsMockApi, mockIdentityGroup1, - createGroupMappingApi, - updateGroupMappingApi, - deleteGroupMappingApi, mockIdentityGroupsCount -} from '../mock/identity-group.service.mock'; +} from '../mock/identity-group.mock'; import { CoreTestingModule } from '../testing/core.testing.module'; import { TranslateModule } from '@ngx-translate/core'; +import { + applicationDetailsMockApi, + createGroupMappingApi, + deleteGroupMappingApi, + groupsMockApi, + noRoleMappingApi, + roleMappingApi, + updateGroupMappingApi +} from '../mock/oauth2.service.mock'; describe('IdentityGroupService', () => { let service: IdentityGroupService; diff --git a/lib/core/services/identity-group.service.ts b/lib/core/services/identity-group.service.ts index 2b02340216..cbc5280725 100644 --- a/lib/core/services/identity-group.service.ts +++ b/lib/core/services/identity-group.service.ts @@ -210,7 +210,7 @@ export class IdentityGroupService implements IdentityGroupServiceInterface { checkGroupHasRole(groupId: string, roleNames: string[]): Observable { return this.getGroupRoles(groupId).pipe(map((groupRoles) => { let hasRole = false; - if (groupRoles && groupRoles.length > 0) { + if (groupRoles?.length > 0) { roleNames.forEach((roleName: string) => { const role = groupRoles.find(({ name }) => roleName === name); if (role) { diff --git a/lib/core/services/identity-user.service.interface.ts b/lib/core/services/identity-user.service.interface.ts index f4b341232a..34fcd7e095 100644 --- a/lib/core/services/identity-user.service.interface.ts +++ b/lib/core/services/identity-user.service.interface.ts @@ -46,6 +46,7 @@ export interface IdentityJoinGroupRequestModel { export interface IdentityUserServiceInterface { getCurrentUserInfo(): IdentityUserModel; + findUserById(id: string): Observable; findUsersByName(search: string): Observable; findUserByUsername(username: string): Observable; findUserByEmail(email: string): Observable; diff --git a/lib/core/services/identity-user.service.spec.ts b/lib/core/services/identity-user.service.spec.ts index 4a64fdc8f4..138f96a121 100644 --- a/lib/core/services/identity-user.service.spec.ts +++ b/lib/core/services/identity-user.service.spec.ts @@ -19,24 +19,12 @@ import { TestBed } from '@angular/core/testing'; import { HttpErrorResponse } from '@angular/common/http'; import { throwError, of } from 'rxjs'; import { - queryUsersMockApi, - createUserMockApi, mockIdentityUser1, - updateUserMockApi, mockIdentityUser2, - deleteUserMockApi, - getInvolvedGroupsMockApi, - joinGroupMockApi, - leaveGroupMockApi, - getAvailableRolesMockApi, - getAssignedRolesMockApi, - getEffectiveRolesMockApi, - assignRolesMockApi, mockIdentityRole, - removeRolesMockApi, - mockIdentityUsers, - mockJoinGroupRequest -} from 'core/mock/identity-user.service.mock'; + mockIdentityUsers +} from '../mock/identity-user.mock'; +import { mockJoinGroupRequest } from '../mock/identity-group.mock'; import { IdentityUserService } from '../services/identity-user.service'; import { setupTestBed } from '../testing/setup-test-bed'; import { AlfrescoApiService } from './alfresco-api.service'; @@ -44,6 +32,20 @@ import { mockToken } from '../mock/jwt-helper.service.spec'; import { IdentityRoleModel } from '../models/identity-role.model'; import { CoreTestingModule } from '../testing/core.testing.module'; import { TranslateModule } from '@ngx-translate/core'; +import { + assignRolesMockApi, + createUserMockApi, + deleteUserMockApi, + getAssignedRolesMockApi, + getAvailableRolesMockApi, + getEffectiveRolesMockApi, + getInvolvedGroupsMockApi, + joinGroupMockApi, + leaveGroupMockApi, + queryUsersMockApi, + removeRolesMockApi, + updateUserMockApi +} from '../mock/oauth2.service.mock'; describe('IdentityUserService', () => { diff --git a/lib/process-services-cloud/src/lib/group/components/group-cloud.component.stories.ts b/lib/process-services-cloud/src/lib/group/components/group-cloud.component.stories.ts index 9c415f70de..3668c91005 100644 --- a/lib/process-services-cloud/src/lib/group/components/group-cloud.component.stories.ts +++ b/lib/process-services-cloud/src/lib/group/components/group-cloud.component.stories.ts @@ -16,10 +16,9 @@ */ import { Meta, moduleMetadata, Story } from '@storybook/angular'; -import { IdentityGroupService, mockIdentityGroups } from '@alfresco/adf-core'; +import { IdentityGroupService, mockIdentityGroups, IdentityGroupServiceMock } from '@alfresco/adf-core'; import { GroupCloudModule } from '../group-cloud.module'; import { GroupCloudComponent } from './group-cloud.component'; -import { IdentityGroupServiceMock } from '../mock/identity-group.service.mock'; import { ProcessServicesCloudStoryModule } from '../../testing/process-services-cloud-story.module'; export default { diff --git a/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts b/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts deleted file mode 100644 index adeff8329d..0000000000 --- a/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts +++ /dev/null @@ -1,146 +0,0 @@ -/*! - * @license - * Copyright 2019 Alfresco Software, Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Injectable } from '@angular/core'; -import { - IdentityGroupCountModel, - IdentityGroupModel, - IdentityGroupQueryCloudRequestModel, - IdentityGroupQueryResponse, - IdentityGroupSearchParam, - IdentityRoleModel, - mockIdentityGroups, - mockIdentityRoles, - IdentityGroupServiceInterface -} from '@alfresco/adf-core'; -import { Observable, of } from 'rxjs'; -import { map } from 'rxjs/operators'; - -Injectable({ providedIn: 'root' }); -export class IdentityGroupServiceMock implements IdentityGroupServiceInterface { - - getGroups(): Observable { - return of(mockIdentityGroups); - } - - getAvailableRoles(_groupId: string): Observable { - return of(mockIdentityRoles); - } - - getAssignedRoles(_groupId: string): Observable { - return of(mockIdentityRoles); - } - - assignRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable { - return of(); - } - - removeRoles(_groupId: string, _roles: IdentityRoleModel[]): Observable { - return of(); - } - - getEffectiveRoles(_groupId: string): Observable { - return of(mockIdentityRoles); - } - - queryGroups(_requestQuery: IdentityGroupQueryCloudRequestModel): Observable { - return of(); - } - - getTotalGroupsCount(): Observable { - return of({ count: mockIdentityGroups.length }); - } - - createGroup(_newGroup: IdentityGroupModel): Observable { - return of(); - } - - updateGroup(_groupId: string, _updatedGroup: IdentityGroupModel): Observable { - return of(); - } - - deleteGroup(_groupId: string): Observable { - return of(); - } - - findGroupsByName(searchParams: IdentityGroupSearchParam): Observable { - if (searchParams.name === '') { - return of([]); - } - - return of(mockIdentityGroups.filter(group => - group.name.toUpperCase().includes(searchParams.name.toUpperCase()) - )); - } - - getGroupRoles(_groupId: string): Observable { - return of(mockIdentityRoles); - } - - checkGroupHasRole(groupId: string, roleNames: string[]): Observable { - return this.getGroupRoles(groupId).pipe(map((groupRoles) => { - let hasRole = false; - if (groupRoles && groupRoles.length > 0) { - roleNames.forEach((roleName: string) => { - const role = groupRoles.find(({ name }) => roleName === name); - if (role) { - hasRole = true; - return; - } - }); - } - return hasRole; - })); - } - - getClientIdByApplicationName(_applicationName: string): Observable { - return of('fake-client-id'); - } - - getClientRoles(groupId: string, _clientId: string): Observable { - if (['mock-group-id-1', 'mock-group-id-2'].includes(groupId)) { - return of([{ id: 'mock-role-id', name: 'MOCK-ADMIN-ROLE' }]); - } - - return of([{ id: 'mock-role-id', name: 'MOCK-USER-ROLE' }]); - } - - checkGroupHasClientApp(groupId: string, clientId: string): Observable { - return this.getClientRoles(groupId, clientId).pipe( - map((response) => response && response.length > 0) - ); - } - - checkGroupHasAnyClientAppRole(groupId: string, clientId: string, roleNames: string[]): Observable { - return this.getClientRoles(groupId, clientId).pipe( - map((clientRoles: any[]) => { - let hasRole = false; - if (clientRoles.length > 0) { - roleNames.forEach((roleName) => { - const role = clientRoles.find(({ name }) => name === roleName); - - if (role) { - hasRole = true; - return; - } - }); - } - return hasRole; - }) - ); - } -} diff --git a/lib/process-services-cloud/src/lib/people/components/people-cloud.component.stories.ts b/lib/process-services-cloud/src/lib/people/components/people-cloud.component.stories.ts index ab2bb30e55..296642425c 100644 --- a/lib/process-services-cloud/src/lib/people/components/people-cloud.component.stories.ts +++ b/lib/process-services-cloud/src/lib/people/components/people-cloud.component.stories.ts @@ -16,11 +16,9 @@ */ import { Meta, moduleMetadata, Story } from '@storybook/angular'; -import { IdentityUserService } from '@alfresco/adf-core'; +import { IdentityUserService, IdentityUserServiceMock, mockIdentityUsers } from '@alfresco/adf-core'; import { PeopleCloudComponent } from './people-cloud.component'; import { PeopleCloudModule } from '../people-cloud.module'; -import { IdentityUserServiceMock } from '../mock/identity-user.service.mock'; -import { mockUsers } from '../mock/user-cloud.mock'; import { ProcessServicesCloudStoryModule } from '../../testing/process-services-cloud-story.module'; export default { @@ -73,7 +71,7 @@ validPreselectedUsers.args = { ...primary.args, validate: true, mode: 'multiple', - preSelectUsers: mockUsers + preSelectUsers: mockIdentityUsers }; export const mandatoryPreselectedUsers = template.bind({}); @@ -81,8 +79,8 @@ mandatoryPreselectedUsers.args = { ...primary.args, validate: true, mode: 'multiple', - preSelectUsers: [{ id: 'fake-id-1', username: 'first-name-1 last-name-1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com', readonly: true }, - { id: 'fake-id-2', username: 'first-name-2 last-name-2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com' }] + preSelectUsers: [{ id: 'mock-user-id-1', username: 'userName1', firstName: 'first-name-1', lastName: 'last-name-1', email: 'abc@xyz.com', readonly: true }, + { id: 'mock-user-id-2', username: 'userName2', firstName: 'first-name-2', lastName: 'last-name-2', email: 'abcd@xyz.com' }] }; export const invalidPreselectedUsers = template.bind({}); @@ -97,8 +95,8 @@ export const excludedUsers = template.bind({}); excludedUsers.args = { ...primary.args, excludedUsers: [ - { id: 'fake-id-2' }, - { id: 'fake-id-3' } + { id: 'mock-user-id-2' }, + { id: 'mock-user-id-3' } ] }; @@ -111,7 +109,7 @@ adminRoleUser.args = { export const noUsers = template.bind({}); noUsers.args = { ...primary.args, - excludedUsers: mockUsers + excludedUsers: mockIdentityUsers }; export const invalidOrEmptyAppName = template.bind({}); diff --git a/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts b/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts deleted file mode 100644 index 560e6e266e..0000000000 --- a/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts +++ /dev/null @@ -1,265 +0,0 @@ -/*! - * @license - * Copyright 2019 Alfresco Software, Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Injectable } from '@angular/core'; -import { - IdentityGroupModel, - IdentityRoleModel, - IdentityUserModel, - mockIdentityGroups, - IdentityJoinGroupRequestModel, - IdentityUserServiceInterface, - IdentityUserPasswordModel, - IdentityUserQueryCloudRequestModel, - IdentityUserQueryResponse -} from '@alfresco/adf-core'; -import { Observable, of } from 'rxjs'; -import { map, switchMap } from 'rxjs/operators'; -import { cloudMockUser, mockRoles, mockUsers } from './user-cloud.mock'; - -@Injectable({ - providedIn: 'root' -}) -export class IdentityUserServiceMock implements IdentityUserServiceInterface { - - getCurrentUserInfo(): IdentityUserModel { - return cloudMockUser; - } - - findUsersByName(search: string): Observable { - if (search === '') { - return of([]); - } - - return of(mockUsers.filter(user => - user.username.toUpperCase().includes(search.toUpperCase()) - )); - } - - findUserByUsername(username: string): Observable { - if (username === '') { - return of([]); - } - - return of(mockUsers.filter(user => user.username === username)); - } - - findUserByEmail(email: string): Observable { - if (email === '') { - return of([]); - } - - return of(mockUsers.filter(user => user.email === email)); - } - - findUserById(id: string): Observable { - if (id === '') { - return of([]); - } - - return of(mockUsers.find(user => user.id === id)); - } - - getClientRoles(userId: string, _clientId: string): Observable { - if (userId === 'fake-id-1') { - return of([{ id: 'id-1', name: 'MOCK-ADMIN-ROLE' }]); - } - - return of([{ id: 'id-2', name: 'MOCK-USER-ROLE' }]); - } - - checkUserHasClientApp(userId: string, clientId: string): Observable { - return this.getClientRoles(userId, clientId).pipe( - map((clientRoles) => clientRoles.length > 0) - ); - } - - checkUserHasAnyClientAppRole(userId: string, clientId: string, roleNames: string[]): Observable { - return this.getClientRoles(userId, clientId).pipe( - map((clientRoles: any[]) => { - let hasRole = false; - if (clientRoles.length > 0) { - roleNames.forEach((roleName) => { - const role = clientRoles.find(({ name }) => name === roleName); - - if (role) { - hasRole = true; - return; - } - }); - } - return hasRole; - }) - ); - } - - getClientIdByApplicationName(_applicationName: string): Observable { - return of('fake-id-1'); - } - - checkUserHasApplicationAccess(userId: string, applicationName: string): Observable { - return this.getClientIdByApplicationName(applicationName).pipe( - switchMap((clientId: string) => { - return this.checkUserHasClientApp(userId, clientId); - }) - ); - } - - checkUserHasAnyApplicationRole(userId: string, applicationName: string, roleNames: string[]): Observable { - return this.getClientIdByApplicationName(applicationName).pipe( - switchMap((clientId: string) => { - return this.checkUserHasAnyClientAppRole(userId, clientId, roleNames); - }) - ); - } - - getUsers(): Observable { - return of(mockUsers); - } - - getUserRoles(_userId: string): Observable { - return of(mockRoles); - } - - async getUsersByRolesWithCurrentUser(roleNames: string[]): Promise { - const filteredUsers: IdentityUserModel[] = []; - if (roleNames && roleNames.length > 0) { - const users = await this.getUsers().toPromise(); - - for (let i = 0; i < users.length; i++) { - const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames); - if (hasAnyRole) { - filteredUsers.push(users[i]); - } - } - } - - return filteredUsers; - } - - async getUsersByRolesWithoutCurrentUser(roleNames: string[]): Promise { - const filteredUsers: IdentityUserModel[] = []; - if (roleNames && roleNames.length > 0) { - const currentUser = this.getCurrentUserInfo(); - let users = await this.getUsers().toPromise(); - - users = users.filter(({ username }) => username !== currentUser.username); - - for (let i = 0; i < users.length; i++) { - const hasAnyRole = await this.userHasAnyRole(users[i].id, roleNames); - if (hasAnyRole) { - filteredUsers.push(users[i]); - } - } - } - - return filteredUsers; - } - - private async userHasAnyRole(userId: string, roleNames: string[]): Promise { - const userRoles = await this.getUserRoles(userId).toPromise(); - const hasAnyRole = roleNames.some((roleName) => { - const filteredRoles = userRoles.filter((userRole) => { - return userRole.name.toLocaleLowerCase() === roleName.toLocaleLowerCase(); - }); - - return filteredRoles.length > 0; - }); - - return hasAnyRole; - } - - checkUserHasRole(userId: string, roleNames: string[]): Observable { - return this.getUserRoles(userId).pipe(map((userRoles: IdentityRoleModel[]) => { - let hasRole = false; - if (userRoles && userRoles.length > 0) { - roleNames.forEach((roleName: string) => { - const role = userRoles.find(({ name }) => roleName === name); - if (role) { - hasRole = true; - return; - } - }); - } - return hasRole; - })); - } - - queryUsers(_requestQuery: IdentityUserQueryCloudRequestModel): Observable { - return of(); - } - - getTotalUsersCount(): Observable { - return of(mockUsers.length); - } - - createUser(newUser: IdentityUserModel): Observable { - window.alert(`Create new user: ${newUser}`); - return of([]); - } - - updateUser(userId: string, updatedUser: IdentityUserModel): Observable { - window.alert(`Update user: ${updatedUser} with ID: ${userId}`); - return of([]); - } - - deleteUser(userId: string): Observable { - window.alert(`Delete user with ID: ${userId}`); - return of([]); - } - - changePassword(userId: string, newPassword: IdentityUserPasswordModel): Observable { - window.alert(`New password: ${newPassword} for user with ID: ${userId}`); - return of([]); - } - - getInvolvedGroups(_userId: string): Observable { - return of(mockIdentityGroups); - } - - joinGroup(joinGroupRequest: IdentityJoinGroupRequestModel): Observable { - window.alert(`Join group request: ${joinGroupRequest}`); - return of([]); - } - - leaveGroup(userId: any, groupId: string): Observable { - window.alert(`Leave group: ${groupId} for user with ID: ${userId}`); - return of([]); - } - - getAvailableRoles(_userId: string): Observable { - return of(mockRoles); - } - - getAssignedRoles(_userId: string): Observable { - return of(mockRoles); - } - - getEffectiveRoles(_userId: string): Observable { - return of(mockRoles); - } - - assignRoles(userId: string, roles: IdentityRoleModel[]): Observable { - window.alert(`Assign roles: ${roles} for user with ID: ${userId}`); - return of([]); - } - - removeRoles(userId: string, removedRoles: IdentityRoleModel[]): Observable { - window.alert(`Remove roles: ${removedRoles} for user with ID: ${userId}`); - return of([]); - } -}