UNPKG

armisa-models

Version:
253 lines (252 loc) 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.User = exports.UserColumn = exports.UserOption = void 0; const Login_1 = require("./Login/Login"); const TabData_1 = require("./Page/Tab/TabData"); class UserOption { constructor(key, form, value) { this.key = key; this.form = form; this.value = value.toString(); } static deserialize(userOption) { return new UserOption(userOption.key, userOption.form, userOption.value); } } exports.UserOption = UserOption; class UserColumn { constructor(key, form, width) { this.key = key; this.form = form; this.width = width; } static deserialize(userColumn) { return new UserColumn(userColumn.key, userColumn.form, userColumn.width); } } exports.UserColumn = UserColumn; class User { constructor(mainStateManager) { this.mainStateManager = mainStateManager; this.isInRole = (main, role) => { if (this.isThisUserAdmin) { return true; } else if (this.roles.includes(main.selfRole)) { return true; } else if (this.roles.includes((main.selfRole + '_' + main[role]).toLowerCase())) { return true; } return false; }; this._isThisUserAdmin = () => { if (this.roles.find(i => i === 'Admin'.toLowerCase())) { this.isThisUserAdmin = true; } else { this.isThisUserAdmin = false; } }; this.userLoginFormState = 'login'; this.headers = () => { if (this.accessToken) { return { Authorization: `bearer ${this.accessToken}`, }; } else { return undefined; } }; this.id = 0; this.accessToken = ''; this.userName = ''; this.firstName = ''; this.lastName = ''; this.picture = ''; this.smallPicture = ''; this.rememberMe = false; this.roles = []; this.userOptions = []; this.userColumns = []; this.isThisUserAdmin = false; this.LoginFactory = Login_1.Login.buildNew(this.mainStateManager); this.initializingUserBackground = false; this.loginDataLoad = () => { const userPropertyString = localStorage.getItem('userProperty'); if (userPropertyString) { this.rememberMe = true; const userProperty = new UserProperty(JSON.parse(userPropertyString)); if (userProperty && userProperty.accessToken) { this.accessToken = userProperty.accessToken; this.LoginFactory = Login_1.Login.buildNew(this.mainStateManager); // this.LoginFactory.get('user', 'TokenValidation', (response: AxiosResponse) => // this.validateToken(response, userProperty, this.rememberMe), // this.unauthorized // ); // this.LoginFactory.get<any, any>('user', 'tokenValidation') // .then(response => { // if (response.isSuccess) { // // this.mainStateManager.Usering.LoginFactory.loadingState = 'none'; // const userData: IUser = response.data; // userProperty.firstName = userData.firstName; // userProperty.lastName = userData.lastName; // userProperty.userName = userData.userName; // userProperty.id = userData.id; // userProperty.picture = userData.picture; // userProperty.smallPicture = userData.smallPicture; // userProperty.roles = userData.roles; // this.userLogin(userData, this.rememberMe); // } else { // localStorage.removeItem('userProperty'); // this.gotoLoginPage(); // } // }) // .catch(error => { // localStorage.removeItem('userProperty'); // this.gotoLoginPage(); // }) return; } } this.gotoLoginPage(); }; // validateToken = (response: AxiosResponse, userProperty: UserProperty, rememberMe: boolean) => { // if (response.status === 200 && response.data.isSuccess) { // const userData: IUser = response.data.data; // userProperty.firstName = userData.firstName; // userProperty.lastName = userData.lastName; // userProperty.userName = userData.userName; // userProperty.id = userData.id; // userProperty.picture = userData.picture; // userProperty.smallPicture = userData.smallPicture; // userProperty.roles = userData.roles; // this.userLogin(userData, rememberMe); // } else { // localStorage.removeItem('userProperty'); // this.gotoLoginPage(); // } // }; // unauthorized = (error: AxiosError) => { // localStorage.removeItem('userProperty'); // this.gotoLoginPage(); // }; this.logOut = () => { localStorage.removeItem('userProperty'); this.gotoLoginPage(); }; this.gotoLoginPage = () => { // this.mainStateManager.Usering.LoginFactory.loadingState = 'none'; this.LoginFactory.pageData = new TabData_1.TabPageData(this.mainStateManager); // this.mainStateManager.Eventing.trigger('userLoginLoadData'); }; this.userLogin = (response, rememberMe) => { this.id = response.id; this.userName = response.userName; this.accessToken = response.accessToken; this.firstName = response.firstName; this.lastName = response.lastName; this.smallPicture = response.smallPicture; this.roles = response.roles.map(i => i.toLowerCase()); // this.mainStateManager.ReportInfoing.userLogin(response.reportInfo); this._isThisUserAdmin(); this.userOptions = response.userOptions?.map(i => UserOption.deserialize(i)); this.userColumns = response.userColumns?.map(i => UserColumn.deserialize(i)); // this.mainStateManager.Dataing.deserialize(response.attachedData); this.rememberMe = rememberMe; if (rememberMe) { localStorage.setItem('userProperty', JSON.stringify({ accessToken: this.accessToken, })); } // this.mainStateManager.generateTheme(); // this.mainStateManager.Eventing.trigger('userLoginSuccess'); }; this.userChangeData = (response) => { this.userName = response.userName; this.accessToken = response.accessToken; this.firstName = response.firstName; this.lastName = response.lastName; this.smallPicture = response.smallPicture; this.roles = response.roles; this.userOptions = response.userOptions.map(i => UserOption.deserialize(i)); if (this.rememberMe) { localStorage.setItem('userProperty', JSON.stringify({ accessToken: this.accessToken, })); } // this.mainStateManager.Dataing.deserialize(response.attachedData); // this.mainStateManager.Eventing.trigger('loadLanguagesSuccess'); }; this.getFormUserOptions = (keyOfForm) => { return this.userOptions.filter(i => i.form === keyOfForm); }; } unMountUserPage() { this.initializingUserBackground = false; this.userBackgrounDiv = undefined; if (this.intervalForBackgroundChangePage) { clearInterval(this.intervalForBackgroundChangePage); } } // addUserOption = (key: string, pageKeys: string[], value: string, isSave?: boolean) => { // pageKeys.forEach((i: any) => { // const userOption = new UserOption(key, i, value); // this.userOptions.push(userOption); // if (isSave) { // this.LoginFactory.post('User', 'AddUserOption', userOption); // } // }); // this.updateUserOptionTabPage(pageKeys); // } // removeUserOption = (key: string, pageKeys: string[], isSave?: boolean) => { // this.userOptions = this.userOptions.filter(i => !(i.key === key && pageKeys.includes(i.form))); // if (isSave) { // pageKeys.forEach(i => { // const result = { key: key, form: i }; // this.LoginFactory.post('User', 'DeleteUserOption', result); // }); // } // this.updateUserOptionTabPage(pageKeys); // } // saveUserOption = (key: string, pageKeys: string[], value: string) => { // pageKeys.forEach((i: any) => { // const result: IUserOption = { key: key, form: i, value: value }; // this.LoginFactory.post('User', 'AddUserOption', result); // }); // } // public setUserOption = <V>(key: string, pageKey: string, value: V, defaultValue: V, isSave?: boolean) => { // const finded = this.userOptions.find(i => i.key === key && i.form === pageKey); // const valueString = (value as any).toString(); // if (finded) { // const defaultValueString = (defaultValue as any).toString(); // if (valueString === defaultValueString) { // this.removeUserOption(finded.key, [finded.form], true); // } else { // finded.value = valueString; // if (isSave) { // this.saveUserOption(finded.key, [finded.form], valueString); // } // } // } else { // this.addUserOption(key, [pageKey], valueString, isSave); // } // } updateUserOptionTabPage(pageKeys) { // this.mainStateManager.Tabing.tabs.forEach((page: any) => { // if (pageKeys.includes(page.pageKey)) { // page.userOptions = this.userOptions?.filter(i => i.form === page.pageKey); // } // }); } } exports.User = User; class UserProperty { constructor(values) { this.id = values.id; this.userName = values.userName; this.firstName = values.firstName; this.lastName = values.lastName; this.accessToken = values.accessToken; this.picture = values.picture; this.smallPicture = values.smallPicture; this.roles = values.roles; } }