/*! * @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 } from '../models/identity-group.model'; import { IdentityUserModel } from '../models/identity-user.model'; import { IdentityRoleModel } from '../models/identity-role.model'; 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'; @Injectable({ providedIn: 'root' }) export class IdentityUserServiceMock implements IdentityUserServiceInterface { getCurrentUserInfo(): IdentityUserModel { return mockIdentityUser1; } findUsersByName(search: string): Observable { if (search === '') { return of([]); } return of(mockIdentityUsers.filter(user => user.username.toUpperCase().includes(search.toUpperCase()) )); } findUserByUsername(username: string): Observable { if (username === '') { return of([]); } return of(mockIdentityUsers.filter(user => user.username === username)); } findUserByEmail(email: string): Observable { if (email === '') { return of([]); } return of(mockIdentityUsers.filter(user => user.email === email)); } findUserById(id: string): Observable { if (id === '') { return of([]); } return of(mockIdentityUsers.find(user => user.id === id)); } getClientRoles(userId: string, _clientId: string): Observable { if (userId === 'mock-user-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('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([]); } }