import { Component , OnInit , Injector } from '@angular/core' ; import { PagedListingComponentBase , PagedRequestDto , } from '@shared/common/paged-listing-component-base' ; import { UserListDto , UserServiceProxy , EntityDtoOfInt64 , PagedResultDtoOfUserListDto , } from '@shared/service-proxies/service-proxies' ; import { CreateOrEditUserModalComponent } from './create-or-edit-user-modal.component' ; import { EditUserPermissionsModalComponent } from './edit-user-permissions-modal.component' ; import { ActivatedRoute } from '@angular/router' ; import { finalize } from 'rxjs/operators' ; import { AppConsts } from '@shared/AppConsts' ; import { FileDownloadService } from '@shared/utils/file-download.service' ; import { LocalStorageService } from '@shared/utils/local-storage.service' ; @ Component ( { templateUrl : './users.component.html' , styles : [ ] , } ) export class UsersComponent extends PagedListingComponentBase < UserListDto > implements OnInit { filterText = '' ; advancedFiltersVisible = false ; selectedPermissions = [ ] ; role = '' ; onlyLockedUsers = false ; constructor ( injector , private _userServiceProxy , private _fileDownloadService , private _activatedRoute , private _localStorageService ) { super ( injector ) ; this . filterText = this . _activatedRoute . snapshot . queryParams [ 'filterText' ] || '' ; } onSearch ( ) { this . refresh ( ) ; } getRolesAsString ( roles ) { let roleNames = '' ; for ( let j = 0 ; j < roles . length ; j ++ ) { if ( roleNames . length ) { roleNames = roleNames + ', ' ; } roleNames = roleNames + roles [ j ] . roleName ; } return roleNames ; } createOrEdit ( id ? ) { this . modalHelper . createStatic ( CreateOrEditUserModalComponent , { userId : id } , { size : 'md' , includeTabs : true } ) . subscribe ( res => { if ( res ) { this . refresh ( ) ; } } ) ; } editUserPermissions ( record ) { this . modalHelper . createStatic ( EditUserPermissionsModalComponent , { userId : record . id , userName : record . userName , } , { size : 'md' } ) . subscribe ( result => { } ) ; } unlockUser ( record ) { this . _userServiceProxy . unlockUser ( new EntityDtoOfInt64 ( { id : record . id } ) ) . subscribe ( ( ) => { this . notify . success ( this . l ( 'UnlockedTheUser' , record . userName ) ) ; } ) ; } protected fetchDataList ( request , pageNumber , finishedCallback ) { this . _userServiceProxy . getUsers ( this . filterText , this . selectedPermissions , this . role !== '' ? parseInt ( this . role ) : undefined , this . onlyLockedUsers , request . sorting , request . maxResultCount , request . skipCount ) . pipe ( finalize ( finishedCallback ) ) . subscribe ( ( result ) => { this . dataList = result . items ; this . setUsersProfilePictureUrl ( this . dataList ) ; this . showPaging ( result ) ; } ) ; } batchDelete ( ) { this . message . warn ( 'method not implement!' ) ; } exportToExcel ( ) { this . _userServiceProxy . getUsersToExcel ( this . filterText , this . selectedPermissions , this . role !== '' ? parseInt ( this . role ) : undefined , this . onlyLockedUsers , undefined ) . subscribe ( result => { this . _fileDownloadService . downloadTempFile ( result ) ; } ) ; } deleteUser ( user ) { if ( user . userName === AppConsts . userManagement . defaultAdminUserName ) { this . message . warn ( this . l ( '{0}UserCannotBeDeleted' , AppConsts . userManagement . defaultAdminUserName ) ) ; return ; } this . _userServiceProxy . deleteUser ( user . id ) . subscribe ( ( ) => { this . refresh ( ) ; this . notify . success ( this . l ( 'SuccessfullyDeleted' ) ) ; } ) ; } setUsersProfilePictureUrl ( users ) { for ( let i = 0 ; i < users . length ; i ++ ) { let user = users [ i ] ; this . _localStorageService . getItem ( AppConsts . authorization . encrptedAuthTokenName , function ( err , value ) { let profilePictureUrl = AppConsts . remoteServiceBaseUrl + '/Profile/GetProfilePictureByUser?userId=' + user . id + '&' + AppConsts . authorization . encrptedAuthTokenName + '=' + encodeURIComponent ( value . token ) ; ( user as any ) . profilePictureUrl = profilePictureUrl ; } ) ; } } }