/** @file * @author Edouard DUPIN * @copyright 2018, Edouard DUPIN, all right reserved * @license PROPRIETARY (see license file) */ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { ApplicationService, ApplicationModel, ApplicationTokenService, AdminUserService } from 'base/service'; import { ApplicationTokenModel } from 'base/service/application-token'; import { AsyncActionState } from 'common/component'; import { CheckerParameterType, SettingsItem, SettingType } from 'common/component/render-settings/render-settings'; import { ModelResponseHttp, NotificationService, PopInService } from 'common/service'; import { isNumber, isNumeric, isString } from 'common/utils'; @Component({ selector: 'application-setting-user-edit', templateUrl: './application-user-edit.html', styleUrls: ['./application-user-edit.less'], changeDetection: ChangeDetectionStrategy.Default, }) export class ApplicationUserEditScene implements OnInit { id: number = undefined; users: any[] = undefined; notUsers: any[] = undefined; application: ApplicationModel = undefined; applicationRef: ApplicationModel = undefined; tokens: ApplicationTokenModel[] = []; constructor( private adminUserService: AdminUserService, private applicationService: ApplicationService, private applicationTokenService: ApplicationTokenService, private activatedRoute: ActivatedRoute, private cdr: ChangeDetectorRef, private popInService: PopInService, private notificationService: NotificationService, ) { } ngOnInit() { this.id = Number(this.activatedRoute.snapshot.paramMap.get('applicationId')); const self = this; this.applicationService .getUsers(this.id) .then((userListToFilter: number[]) => { console.log(`??? get full response: ${JSON.stringify(userListToFilter, null, 4)}`); self.adminUserService .getUsers() .then((response: any[]) => { console.log(`All user available for this application: ${JSON.stringify(response, null, 4)}`); self.users = []; self.notUsers = []; for (let iii = 0; iii < response.length; iii++) { if (response[iii].blocked === true) { continue; } if (userListToFilter.indexOf(response[iii].id) >= 0) { self.users.push(response[iii]); } else { self.notUsers.push(response[iii]); } } }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); self.checkName(self.application.name); this.configureEditInput(); }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); /* this.applicationService .get(this.id) .then((response: ApplicationModel) => { console.log(`??? get full response: ${JSON.stringify(response, null, 4)}`); self.application = response; self.checkName(self.application.name); this.configureEditInput(); }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); this.applicationTokenService .gets(this.id) .then((response: ApplicationTokenModel[]) => { console.log(`??? get full response: ${JSON.stringify(response, null, 4)}`); self.tokens = response; }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); this.configureInput(); */ } onAddApplicationUser(value: boolean, user: any) { console.log(`changeState : ${JSON.stringify(value, null, 2)}`); j'en suis la ... add user une application //this.createTokenDisabled = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } formatTimestamp(unix_timestamp: number) { return new Date(unix_timestamp).toISOString().replace('T', ' ').replace('Z', ' GMT').replace('.000', ''); } editApplicationMenu: SettingsItem[] = undefined; // this permit to clear the input menu... configureEditInput() { this.editApplicationMenu = [ { type: SettingType.VALUE, title: 'ID:', value: this.application?.id, }, { type: SettingType.STRING, title: 'Name:', placeholder: 'Enter application name', key: 'name', value: this.application?.name, checker: (value) => this.checkName(value), require: true, }, { type: SettingType.STRING, title: 'Description:', key: 'description', value: this.application?.description, }, { type: SettingType.STRING, title: 'Redirect:', description: 'Redirect when login (http://):', placeholder: 'Enter http redirect addresses', key: 'redirect', value: this.application?.redirect, checker: (value: CheckerParameterType) => this.checkRedirect(value), require: true, }, { type: SettingType.STRING, title: 'Redirect (dev):', description: 'Redirect development (http://):', placeholder: 'Enter http redirect addresses', key: 'redirectDev', value: this.application?.redirectDev, }, { type: SettingType.STRING, title: 'Notification:', description: 'Redirect development (http://):', placeholder: 'http://xxx/sso-event', key: 'notification', value: this.application?.notification, }, { type: SettingType.NUMBER, title: 'TTL:', description: 'Time in seconds of the validity of the token', placeholder: '888', key: 'ttl', value: this.application?.ttl, checker: (value: CheckerParameterType) => this.checkTTL(value), require: true, }, ]; this.updateButtonDisabled = undefined; this.dataUpdate = {}; } /** * Check the redirection have a good form */ checkRedirect(value: CheckerParameterType): string | undefined { if (!isString(value)) { return 'must be a string'; } if (value.length <= 5) { return 'This redirect is too small.'; } return undefined; } updateButtonDisabled: number | undefined = undefined; dataUpdate: object = {} onEditState(value: number) { console.log(`changeState : ${JSON.stringify(value, null, 2)}`); this.updateButtonDisabled = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } onEditValues(value: any) { console.log(`onDeltaValues : ${JSON.stringify(value, null, 2)}`); this.dataUpdate = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } updateState: string | boolean = undefined; /** * Request the creation of a new application. */ onUpdateApplication(): void { this.updateState = AsyncActionState.LOADING; let self = this; this.applicationService.update(this.id, this.dataUpdate) .then( (data: ApplicationModel) => { self.updateState = AsyncActionState.DONE; console.log(`Get new application data: ${JSON.stringify(data, null, 2)}`); self.application = data; self.configureEditInput() setTimeout(() => { this.updateState = undefined; }, 3000); } ).catch( (error: any) => { self.updateState = AsyncActionState.FAIL; setTimeout(() => { self.updateState = undefined; }, 3000); } ); } createToken(): void { let self = this; this.applicationTokenService .create(this.id, this.dataCreateApplication['name'], this.dataCreateApplication['ttl']) .then((response: ApplicationTokenModel) => { //console.log(`??? get fullllllll response: ${JSON.stringify(response, null, 4)}`); self.tokens.push(response); response.token = `"${response.id}:${response.token}"` self.cdr.detectChanges(); }) .catch((error: any) => { console.log(`??? get ERROR response: ${JSON.stringify(error, null, 4)}`); }); } onRemoveApplicationToken(_event: any, token: ApplicationTokenModel) { this.confirmDeleteComment = `Delete the application token ID: [${this.application.id}/${token.id}] ${token.name}`; this.confirmDeleteApplicationToken = token; this.popInService.open('popin-delete-confirm'); } removeApplicationConfirm(token: ApplicationTokenModel) { let self = this; this.applicationTokenService.remove(self.application.id, token.id) .then( () => { const index = self.tokens.indexOf(token, 0); if (index > -1) { self.tokens.splice(index, 1); self.cdr.detectChanges(); } } ).catch( (error: any) => { self.notificationService.errorRaw(`Fail to delete application token: [${self.application.id}/${token.id}] : ${token.name} ==> ${error}`) } ); } confirmDeleteComment: string = undefined; confirmDeleteApplicationToken: ApplicationTokenModel = undefined; deleteConfirmed() { if (this.confirmDeleteApplicationToken !== undefined) { this.removeApplicationConfirm(this.confirmDeleteApplicationToken); this.confirmDeleteComment = undefined; this.confirmDeleteApplicationToken = undefined; } } createTokenMenu: SettingsItem[] = [] // this permit to clear the input menu... configureInput() { this.createTokenMenu = [ { type: SettingType.STRING, title: 'Token name:', placeholder: 'Enter the token name / decription', key: 'name', value: '', checker: (value: CheckerParameterType) => this.checkName(value), require: true, }, { type: SettingType.NUMBER, title: 'Token TTL:', placeholder: 'Enter the Time To Lead (in day)', key: 'ttl', value: '', checker: (value: CheckerParameterType) => this.checkTTL(value), require: true, }, ]; } public createTokenDisabled: number = undefined; public dataCreateApplication: object = {}; /** * Check the application name is available */ checkName(value: CheckerParameterType): string | undefined { if (!isString(value)) { return 'Must be a String'; } if (value.length < 3) { return 'This name is too small >=3.'; } return undefined; } /** * Check the redirection have a good form */ checkTTL(value: CheckerParameterType): string | undefined { if (!isNumber(value)) { return 'Must be a number'; } const tokenTTL = Number(value); if (tokenTTL < 1) { return 'Minimum one day'; } return undefined; } onCreateValueState(value: number) { console.log(`changeState : ${JSON.stringify(value, null, 2)}`); this.createTokenDisabled = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } onCreateValueDeltaValues(value: any) { console.log(`onDeltaValues : ${JSON.stringify(value, null, 2)}`); this.dataCreateApplication = value; // we do not change the main ref ==> notify angular that something have change and need to be re-render??? this.cdr.detectChanges(); } }