import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core'; import { AppConsts } from '@shared/AppConsts'; import { AppComponentBase } from '@shared/common/app-component-base'; import { CreateOrUpdateUserInput, OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserRoleDto, UserServiceProxy, SubsystemUserDto } from '@shared/service-proxies/service-proxies'; import { ModalDirective } from 'ngx-bootstrap'; import * as _ from 'lodash'; import { finalize } from 'rxjs/operators'; import { CreateOrEditSubsystemUserModalComponent } from './create-or-edit-SubsystemUser-modal.component'; @Component({ selector: 'createOrEditUserModal', templateUrl: './create-or-edit-user-modal.component.html', styles: [`.user-edit-dialog-profile-image { margin-bottom: 20px; }` ] }) export class CreateOrEditUserModalComponent extends AppComponentBase { @ViewChild('createOrEditModal') modal: ModalDirective; @ViewChild('createOrEditSubsystemUserModal') createOrEditSubsystemUserModal: CreateOrEditSubsystemUserModalComponent; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; canChangeUserName = true; isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled'); isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled'); passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting(); user: UserEditDto = new UserEditDto(); roles: UserRoleDto[]; sendActivationEmail = false; setRandomPassword = false; passwordComplexityInfo = ''; profilePicture: string; subSystemUser: SubsystemUserDto[] = []; allOrganizationUnits: OrganizationUnitDto[]; memberedOrganizationUnits: string[]; constructor( injector: Injector, private _userService: UserServiceProxy, private _profileService: ProfileServiceProxy ) { super(injector); } show(userId?: number): void { this.subSystemUser = []; if (!userId) { this.active = true; this.setRandomPassword = true; this.sendActivationEmail = true; } this._userService.getUserForEdit(userId).subscribe(userResult => { this.user = userResult.user; this.roles = userResult.roles; this.canChangeUserName = this.user.userName !== AppConsts.userManagement.defaultAdminUserName; if (userResult.listGetSubsystemUser) { this.subSystemUser = userResult.listGetSubsystemUser; } this.allOrganizationUnits = userResult.allOrganizationUnits; this.memberedOrganizationUnits = userResult.memberedOrganizationUnits; this.getProfilePicture(userResult.profilePictureId); if (userId) { this.active = true; setTimeout(() => { this.setRandomPassword = false; }, 0); this.sendActivationEmail = false; } else { this.user.userName = ''; this.user.password = ''; } this._profileService.getPasswordComplexitySetting().subscribe(passwordComplexityResult => { this.passwordComplexitySetting = passwordComplexityResult.setting; this.setPasswordComplexityInfo(); this.modal.show(); }); }); } AddSubsystemUser(): void { this.createOrEditSubsystemUserModal.show(null); } UpdateSubsystemUser(index: number): void { this.createOrEditSubsystemUserModal.show(this.subSystemUser[index]); } subsystemUserAdd(subSystem: SubsystemUserDto): void { if (subSystem) { for (let i = 0; i < this.subSystemUser.length; i++) { if ((subSystem.id && this.subSystemUser[i].id === subSystem.id) || this.subSystemUser[i].subAppId === subSystem.subAppId) { this.subSystemUser.splice(i, 1); } } this.subSystemUser.push(subSystem); } } DeleteSubsystemUser(index: number): void { this.message.confirm( '', (isConfirmed) => { if (isConfirmed) { this.subSystemUser.splice(index, 1); this.notify.success(this.l('SuccessfullyDeleted')); } } ); } setPasswordComplexityInfo(): void { this.passwordComplexityInfo = ''; } getProfilePicture(profilePictureId: string): void { if (!profilePictureId) { this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png'; } else { this._profileService.getProfilePictureById(profilePictureId).subscribe(result => { if (result && result.profilePicture) { this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture; } else { this.profilePicture = this.appRootUrl() + 'assets/common/images/default-profile-picture.png'; } }); } } onShown(): void { document.getElementById('UserName').focus(); } save(): void { let input = new CreateOrUpdateUserInput(); this.user.isTwoFactorEnabled = false; this.user.surname = this.user.name; input.user = this.user; input.listSubsystemUser = this.subSystemUser; input.setRandomPassword = false; input.sendActivationEmail = false; input.assignedRoleNames = _.map( _.filter(this.roles, { isAssigned: true }), role => role.roleName ); this.saving = true; this._userService.createOrUpdateUser(input) .pipe(finalize(() => { this.saving = false; })) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } close(): void { this.subSystemUser = null; this.active = false; this.modal.hide(); } getAssignedRoleCount(): number { return _.filter(this.roles, { isAssigned: true }).length; } }