diff --git a/src/app/guards/auth.guard.ts b/src/app/guards/auth.guard.ts index d8b99e5bd..1a39ff1f9 100644 --- a/src/app/guards/auth.guard.ts +++ b/src/app/guards/auth.guard.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Observable } from 'rxjs'; import { LocalstoreService } from '../store/localstore.service'; +import { SessionStore } from '../store/session.service'; @Injectable({ providedIn: 'root' @@ -16,7 +17,7 @@ export class AuthGuard implements CanActivate { route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { - if(window.location.pathname != '' && !this.localstoreService.get('UserData', false)) { + if(window.location.pathname != '' && !SessionStore.exist) { this.router.navigate(['/']); return false } else { diff --git a/src/app/guards/inactivity.guard.spec.ts b/src/app/guards/inactivity.guard.spec.ts new file mode 100644 index 000000000..5c99bfeef --- /dev/null +++ b/src/app/guards/inactivity.guard.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { InactivityGuard } from './inactivity.guard'; + +describe('InactivityGuard', () => { + let guard: InactivityGuard; + + beforeEach(() => { + TestBed.configureTestingModule({}); + guard = TestBed.inject(InactivityGuard); + }); + + it('should be created', () => { + expect(guard).toBeTruthy(); + }); +}); diff --git a/src/app/guards/inactivity.guard.ts b/src/app/guards/inactivity.guard.ts new file mode 100644 index 000000000..cca15d62a --- /dev/null +++ b/src/app/guards/inactivity.guard.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; +import { Observable } from 'rxjs'; +import { SessionStore } from '../store/session.service'; + +@Injectable({ + providedIn: 'root' +}) +export class InactivityGuard implements CanActivate { + + constructor( + private router:Router + ){} + + canActivate( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { + + alert(SessionStore.exist+ ' '+SessionStore.user.Inativity+ ' '+ SessionStore.hasPin) + if(SessionStore.exist && SessionStore.user.Inativity && !SessionStore.hasPin) { + alert('stay set pin') + return true + + } else if(SessionStore.exist && SessionStore.user.Inativity) { + alert('stay cofirm') + return true + } else { + alert('goint') + this.router.navigate(['/home/events']); + return false + } + + } + +} diff --git a/src/app/guards/login.guard.ts b/src/app/guards/login.guard.ts index 38a3d3a1c..848df6137 100644 --- a/src/app/guards/login.guard.ts +++ b/src/app/guards/login.guard.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Observable } from 'rxjs'; import { LocalstoreService } from '../store/localstore.service'; +import { SessionStore } from '../store/session.service'; @Injectable({ providedIn: 'root' @@ -15,7 +16,7 @@ export class LoginGuard implements CanActivate { route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { - if(window.location.pathname == '/' && this.localstoreService.get('UserData', null) != null ) { + if(window.location.pathname == '/' && SessionStore.exist ) { this.router.navigate(['/home/events']); return false } else { diff --git a/src/app/home/home-routing.module.ts b/src/app/home/home-routing.module.ts index b7d8f0457..014635651 100644 --- a/src/app/home/home-routing.module.ts +++ b/src/app/home/home-routing.module.ts @@ -1,6 +1,7 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { AuthGuard } from '../guards/auth.guard'; +import { InactivityGuard } from '../guards/inactivity.guard'; import { UserDataResolver } from '../resolvers/userData.resolver'; import { HomePage } from './home.page'; @@ -379,6 +380,17 @@ const routes: Routes = [ loadChildren: ()=> import('../pages/inactivity/inactivity.module').then(m => m.InactivityPageModule) }, ], + canActivate: [InactivityGuard] + }, + { + path: 'pin', + children: [ + { + path:'', + loadChildren: ()=> import('../pages/inactivity/inactivity.module').then(m => m.InactivityPageModule) + }, + ], + canActivate: [InactivityGuard] }, ]; diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index f8485409d..05d0709f0 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -14,6 +14,7 @@ import { TotalDocumentStore } from '../store/total-document.service'; import { connection } from '../services/socket/synchro.service'; import { DespachoService } from '../Rules/despacho.service'; import { ExpedienteGdStore } from '../store/expedientegd-store.service'; +import { InativityService } from '../services/inativity.service'; // import { NetworkService } from '../services/network.service'; @@ -81,7 +82,8 @@ export class HomePage implements OnInit { // private network: NetworkService, public p: PermissionService, public documentCounterService: DocumentCounterService, - private despachoRule: DespachoService) { + private despachoRule: DespachoService, + private inativityService: InativityService) { this.router.events.subscribe((val) => { document.querySelectorAll('ion-modal').forEach((e: any) => e.remove()) @@ -89,6 +91,10 @@ export class HomePage implements OnInit { this.updateList() + window['inactivity/function'] = () => { + this.router.navigate(['/inactivity']); + } + // if (typeof Worker !== 'undefined') { // // Create a new @@ -102,11 +108,8 @@ export class HomePage implements OnInit { // // You should add a fallback so that your program still executes correctly. // } - { - //this.folderId = this.navParams.get('folderId'); - } - } + refreshing() { diff --git a/src/app/models/user.model.ts b/src/app/models/user.model.ts index e24bf1e2e..4f97c400b 100644 --- a/src/app/models/user.model.ts +++ b/src/app/models/user.model.ts @@ -30,4 +30,34 @@ export class User { }[] UserName: string Profile: any; +} + + +export class UserSession { + BasicAuthKey: string; + UserId: number; + Authorization: string; + Email: string + FullName: string + OwnerCalendars: { + CalendarId: string + CalendarName: "Oficial" | "Pessoal"; + CalendarRoleId: string; + Id: number; + }[] + RoleDescription: string + RoleID: number + SharedCalendars: { + CalendarId: string + CalendarName: "Oficial" | "Pessoal"; + CalendarRoleId: string; + Id: number; + OwnerUserId: string; + TypeShare: number; + }[] + UserName: string + Profile: any; + LoginPreference: 'None' | 'Password' | 'Pin' | null; + PIN: string + Inativity: boolean } \ No newline at end of file diff --git a/src/app/pages/gabinete-digital/gabinete-digital.page.ts b/src/app/pages/gabinete-digital/gabinete-digital.page.ts index 651c0a7a4..c1c40bd29 100644 --- a/src/app/pages/gabinete-digital/gabinete-digital.page.ts +++ b/src/app/pages/gabinete-digital/gabinete-digital.page.ts @@ -145,7 +145,7 @@ export class GabineteDigitalPage implements OnInit, DoCheck { } */ } - closeAllDesktopComponent(){ + closeAllDesktopComponent() { this.desktopComponent = { showEventList: false, showExpediente: false diff --git a/src/app/pages/inactivity/inactivity.page.html b/src/app/pages/inactivity/inactivity.page.html index 0c6dab43a..16f8fb80a 100644 --- a/src/app/pages/inactivity/inactivity.page.html +++ b/src/app/pages/inactivity/inactivity.page.html @@ -2,7 +2,40 @@
-
+ +
+ +
+
+
+
+ +
+
+
+
+

Inicie a sessão

+
+

Email

+ + + +

Palavra-passe

+ + + +
+ +
+
+
+

Uma iniciativa do Gabinete do Presidente da República

+
+
+ +
@@ -13,8 +46,8 @@
-
Digite o PIN
-
Digite o novo PIN
+
Digite o PIN
+
Digite o novo PIN
@@ -43,7 +76,7 @@
-
+
Entrar com senha
@@ -57,7 +90,7 @@
-
+
diff --git a/src/app/pages/inactivity/inactivity.page.ts b/src/app/pages/inactivity/inactivity.page.ts index ccafd3efd..28c522474 100644 --- a/src/app/pages/inactivity/inactivity.page.ts +++ b/src/app/pages/inactivity/inactivity.page.ts @@ -6,8 +6,8 @@ import { ToastService } from 'src/app/services/toast.service'; import { environment } from 'src/environments/environment'; import { AlertController } from '@ionic/angular'; import { NotificationsService } from 'src/app/services/notifications.service'; -import crypto from 'crypto-js' -import { LocalstoreService } from 'src/app/store/localstore.service'; +import { SessionStore } from 'src/app/store/session.service'; + @Component({ selector: 'app-inactivity', @@ -16,35 +16,22 @@ import { LocalstoreService } from 'src/app/store/localstore.service'; }) export class InactivityPage implements OnInit { - logstatus: boolean; username: string = environment.defaultuser; password: string = environment.defaultuserpwd; userattempt: UserForm; code = [] - - hasPin: boolean - loginPreference: string - hasSession = false setPin = false + SessionStore = SessionStore constructor( private notificatinsservice: NotificationsService, private router: Router, private authService: AuthService, private toastService: ToastService, - public alertController: AlertController, - private localstoreService: LocalstoreService - ) { - } + public alertController: AlertController + ) {} - ngOnInit() { - - let userData = this.localstoreService.get('UserData', {}) - - const loginPreference = userData?.loginPreference - const pin = userData?.PIN - - } + ngOnInit() {} async presentAlert(message: string) { const alert = await this.alertController.create({ @@ -62,11 +49,6 @@ export class InactivityPage implements OnInit { this.notificatinsservice.getAndpostToken(this.username); } - async Login() { - - - } - setCode(code: string) { if(this.code.length < 4) { @@ -75,10 +57,7 @@ export class InactivityPage implements OnInit { if(this.code.length == 4) { - const code = this.code.join('') - const encrypted = crypto.SHA1(code) - - if(!this.hasPin) { + if(!SessionStore.hasPin) { // console.log('storePin') this.storePin() } else { @@ -95,17 +74,11 @@ export class InactivityPage implements OnInit { pinLogin() { const code = this.code.join('') - const encrypted = crypto.SHA1(code) - let userData = this.localstoreService.get('UserData', {}) - const pin = userData?.PIN + if( SessionStore.validatePin(code)) { - //if( encrypted == pin) { - - if( encrypted == this.localstoreService.get('UserData', false)) { - - //this.toastService.successMessage() this.router.navigate(['/home/events']); + this.clearCode() } else { this.toastService.badRequest('Pin incorreto') this.code = [] @@ -116,15 +89,8 @@ export class InactivityPage implements OnInit { storePin() { const code = this.code.join('') - const encrypted = crypto.SHA1(code).toString() - let userData: Object = this.localstoreService.get('UserData', {}) - userData['PIN'] = encrypted - userData['loginPreference'] = 'none' - - this.localstoreService.set('UserData', userData) - - this.localstoreService.set('PIN', encrypted) + SessionStore.setPin(code) this.router.navigate(['/home/events']); diff --git a/src/app/pages/login/login.page.html b/src/app/pages/login/login.page.html index 489193583..f802c4aed 100644 --- a/src/app/pages/login/login.page.html +++ b/src/app/pages/login/login.page.html @@ -1,7 +1,7 @@
-
+
@@ -33,56 +33,6 @@
- -
- diff --git a/src/app/pages/login/login.page.ts b/src/app/pages/login/login.page.ts index 2f376d9ef..d573ebad0 100644 --- a/src/app/pages/login/login.page.ts +++ b/src/app/pages/login/login.page.ts @@ -8,6 +8,7 @@ import { AlertController } from '@ionic/angular'; import { NotificationsService } from 'src/app/services/notifications.service'; import crypto from 'crypto-js' import { LocalstoreService } from 'src/app/store/localstore.service'; +import { SessionStore } from 'src/app/store/session.service'; @Component({ selector: 'app-login', @@ -24,8 +25,8 @@ export class LoginPage implements OnInit { hasPin: boolean loginPreference: string - hasSession = false - setPin = false + + sessionStore = SessionStore constructor( private notificatinsservice: NotificationsService, @@ -38,20 +39,11 @@ export class LoginPage implements OnInit { } ngOnInit() { - // clear local storage - window.localStorage.clear(); - // App has session - if(!this.localstoreService.get('UserData', false)) { - this.hasSession = false - } else { - this.hasSession = true - // this.router.navigate(['/home/events']); - } - let userData = this.localstoreService.get('UserData', {}) + let userData = this.sessionStore.user - const loginPreference = userData?.loginPreference + const loginPreference = userData?.LoginPreference const pin = userData?.PIN if (pin) { @@ -130,18 +122,9 @@ export class LoginPage implements OnInit { if (attempt) { this.authService.loginChat(this.userattempt); this.getToken(); - this.hasSession = true - this.hasPin = false - this.setPin = true + + this.router.navigate(['/pin']); - // if(!this.hasPin || this.hasPin) { - // } else { - // this.router.navigate(['/home/events']); - // } - - this.code = ['1','1','1','1'] - this.storePin() - this.router.navigate(['/home/events']); } } else { @@ -153,67 +136,4 @@ export class LoginPage implements OnInit { } } - setCode(code: string) { - - if(this.code.length < 4) { - this.code.push(code) - } - - if(this.code.length == 4) { - - const code = this.code.join('') - const encrypted = crypto.SHA1(code) - - if(!this.hasPin) { - // console.log('storePin') - this.storePin() - } else { - // console.log('pinLogin') - this.pinLogin() - } - } - } - - clearCode() { - this.code =[] - } - - pinLogin() { - - const code = this.code.join('') - const encrypted = crypto.SHA1(code) - - let userData = this.localstoreService.get('UserData', {}) - const pin = userData?.PIN - - //if( encrypted == pin) { - - if( encrypted == this.localstoreService.get('UserData', false)) { - - //this.toastService.successMessage() - this.router.navigate(['/home/events']); - } else { - this.toastService.badRequest('Pin incorreto') - this.code = [] - } - - } - - storePin() { - - const code = this.code.join('') - const encrypted = crypto.SHA1(code) - let userData: Object = this.localstoreService.get('UserData', {}) - - userData['PIN'] = encrypted - userData['loginPreference'] = 'none' - - - this.localstoreService.set('UserData', userData) - this.localstoreService.set('PIN', encrypted) - - this.router.navigate(['/home/events']); - - } - } diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 711943234..3a2268f50 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { StorageService } from './storage.service'; import { Router } from '@angular/router'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; -import { User, UserForm } from '../models/user.model'; +import { User, UserForm, UserSession } from '../models/user.model'; import { environment } from 'src/environments/environment'; import { HttpService } from './http.service'; import { BehaviorSubject, Observable } from 'rxjs'; @@ -12,6 +12,7 @@ import { LocalstoreService } from '../store/localstore.service'; import { ToastService } from './toast.service'; import { UserStore } from 'src/app/store/user.service' import { SHA1, SHA256, AES, enc } from 'crypto-js' +import { SessionStore } from '../store/session.service'; @Injectable({ providedIn: 'root' @@ -37,8 +38,8 @@ export class AuthService { this.headers = new HttpHeaders(); - if (this.localstoreService.get('user', null) != null) { - this.ValidatedUser = this.localstoreService.get('user',{}); + if (SessionStore.exist) { + this.ValidatedUser = SessionStore.user } if (localStorage.getItem("userChat") != null) { @@ -63,22 +64,22 @@ export class AuthService { try { response = await this.http.post(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise(); + const session: UserSession = Object.assign(new UserSession(), response) - console.log(response); if (response) { - if( response.RoleID == 100000014) { - response.Profile = 'PR' - } else if(response.RoleID == 100000011) { - response.Profile = 'MDGPR' + if( session.RoleID == 100000014) { + session.Profile = 'PR' + } else if(session.RoleID == 100000011) { + session.Profile = 'MDGPR' } - response.BasicAuthKey = user.BasicAuthKey + session.BasicAuthKey = user.BasicAuthKey this.ValidatedUser = response; - // console.log('response', response) - - this.localstoreService.set('user', response) - this.userStore.reset(response) + console.log('session', session) + + this.userStore.reset(session) + SessionStore.reset(session) this.storageService.store(AuthConnstants.USER, response); diff --git a/src/app/services/inativity.service.spec.ts b/src/app/services/inativity.service.spec.ts new file mode 100644 index 000000000..a6d04afed --- /dev/null +++ b/src/app/services/inativity.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { InativityService } from './inativity.service'; + +describe('InativityService', () => { + let service: InativityService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(InativityService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/inativity.service.ts b/src/app/services/inativity.service.ts new file mode 100644 index 000000000..e34c7be82 --- /dev/null +++ b/src/app/services/inativity.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { SessionStore } from '../store/session.service'; + +@Injectable({ + providedIn: 'root' +}) +export class InativityService { + + constructor( + private router: Router, + ) { + + var t; + window.onload = resetTimer; + window.onmousemove = resetTimer; + window.onmousedown = resetTimer; // catches touchscreen presses as well + window.ontouchstart = resetTimer; // catches touchscreen swipes as well + window.onclick = resetTimer; // catches touchpad clicks as well + window.onkeydown = resetTimer; + window.addEventListener('scroll', resetTimer, true); // improved; see comments + + function userIsNotActive() { + // your function for too long inactivity goes here + SessionStore.setInativity(false) + // alert('go out') + window['inactivity/function']() + } + + function resetTimer() { + clearTimeout(t); + t = setTimeout(userIsNotActive, 60000); // time is in milliseconds + } + } +} diff --git a/src/app/shared/headers/header-no-search/profile/profile.page.ts b/src/app/shared/headers/header-no-search/profile/profile.page.ts index 4ac9bb5ed..32b0e9ba9 100644 --- a/src/app/shared/headers/header-no-search/profile/profile.page.ts +++ b/src/app/shared/headers/header-no-search/profile/profile.page.ts @@ -120,7 +120,6 @@ export class ProfileComponent implements OnInit { this.router.navigateByUrl('/', { replaceUrl: true }); },100) - } LoginPreferenceMethod(type: string) { diff --git a/src/app/store/expedientegd-store.service.ts b/src/app/store/expedientegd-store.service.ts index 678d2df80..7b1161bfb 100644 --- a/src/app/store/expedientegd-store.service.ts +++ b/src/app/store/expedientegd-store.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { localstoreService } from './localstore.service' -import { AES, enc, SHA1 } from 'crypto-js' +import { SHA1 } from 'crypto-js' import { ExpedienteTask } from '../models/dailyworktask.model'; @Injectable({ diff --git a/src/app/store/localstore.service.ts b/src/app/store/localstore.service.ts index 921c7a385..0b37af954 100644 --- a/src/app/store/localstore.service.ts +++ b/src/app/store/localstore.service.ts @@ -55,6 +55,12 @@ export class LocalstoreService { localStorage.setItem(keyName, encoded) } + delete(keyName) { + + keyName = this.getKey(keyName) + localStorage.removeItem(keyName) + } + } export const localstoreService = new LocalstoreService() diff --git a/src/app/store/session.service.spec.ts b/src/app/store/session.service.spec.ts new file mode 100644 index 000000000..4238e142b --- /dev/null +++ b/src/app/store/session.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { SessionService } from './session.service'; + +describe('SessionService', () => { + let service: SessionService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(SessionService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/store/session.service.ts b/src/app/store/session.service.ts new file mode 100644 index 000000000..ca62aec5f --- /dev/null +++ b/src/app/store/session.service.ts @@ -0,0 +1,94 @@ +import { Injectable } from '@angular/core'; +import { localstoreService } from './localstore.service' +import { SHA1 } from 'crypto-js' +import { UserSession } from '../models/user.model'; + + +@Injectable({ + providedIn: 'root' +}) +export class SessionService { + + // main data + private _user = new UserSession() + // local storage keyName + private keyName: string; + private _needTovalidateUser = false + + constructor() { + + this.keyName = (SHA1(this.constructor.name)).toString() + let restore = localstoreService.get(this.keyName, {}) + this._user = restore.user || new UserSession() + + } + + get user(): UserSession { + return this._user || new UserSession() + } + + get exist() { + let restore = localstoreService.get(this.keyName, {}) + let user: UserSession = restore.user + if(user) { + if(user.Profile) { + return true + } + } + + return false + } + + setLoginPreference(loginPreference: 'None' | 'Password' | 'Pin' | null) { + this._user.LoginPreference = loginPreference + this.save() + } + + setPin(pin: string) { + this._user.PIN = SHA1(pin).toString() + this.save() + } + + validatePin(pin: string) { + return this._user.PIN == SHA1(pin).toString() + } + + needTovalidateUser() { + return this._user.Inativity + } + + setInativity(value: boolean) { + this._user.Inativity = value + this.save() + } + + get hasPin() { + + if(!this._user.PIN) { + return false + } + return this._user.PIN.length >= 2 + } + + reset(user) { + this._user = user + + this.setInativity(true) + this.save() + } + + delete() { + localstoreService.delete(this.keyName) + } + + private save() { + + localstoreService.set(this.keyName, { + user: this._user + }) + + } +} + + +export const SessionStore = new SessionService() \ No newline at end of file