add inactivity page

This commit is contained in:
Peter Maquiran
2021-08-27 13:39:52 +01:00
parent 6aec4a1d5f
commit 230ea00341
20 changed files with 344 additions and 210 deletions
+2 -1
View File
@@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if(window.location.pathname != '' && !this.localstoreService.get('UserData', false)) {
if(window.location.pathname != '' && !SessionStore.exist) {
this.router.navigate(['/']);
return false
} else {
+16
View File
@@ -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();
});
});
+35
View File
@@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | 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
}
}
}
+2 -1
View File
@@ -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<boolean | UrlTree> | Promise<boolean | UrlTree> | 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 {
+12
View File
@@ -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]
},
];
+8 -5
View File
@@ -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() {
+30
View File
@@ -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
}
@@ -145,7 +145,7 @@ export class GabineteDigitalPage implements OnInit, DoCheck {
} */
}
closeAllDesktopComponent(){
closeAllDesktopComponent() {
this.desktopComponent = {
showEventList: false,
showExpediente: false
+38 -5
View File
@@ -2,7 +2,40 @@
<div class="main-wrapper">
<div class="main-content d-flex flex-column height-100 width" >
<div class="wrapper" *ngIf="SessionStore.user.LoginPreference == 'Password' ">
<div class="bg-1 d-flex justify-center align-center">
<div class="bg-2 d-flex justify-center align-center">
<div class="bg-3 d-flex justify-center align-center">
<div class="bg-4 d-flex justify-center align-center">
<div class="div-logo">
<img src='assets/images/fullLogo.png' alt='logo'>
</div>
</div>
</div>
</div>
</div>
<h3 class="center text-white">Inicie a sessão</h3>
<form class="form">
<p class="form-label text-white">Email</p>
<ion-item class="form-input">
<ion-input type="text" [(ngModel)]="username" name="input-username"></ion-input>
</ion-item>
<p class="form-label text-white">Palavra-passe</p>
<ion-item class="form-input">
<ion-input (keyup.enter)="Login()" type="password" [(ngModel)]="password" name="input-password" ></ion-input>
</ion-item>
<div class="d-flex pt-25">
<button class="btn-ok btn-login" fill="clear" expand="block" shape="round" (click)="Login()">Iniciar</button>
</div>
</form>
<div class="msg-bottom d-flex">
<p class="msg-bottom-p">Uma iniciativa do Gabinete do Presidente da República</p>
</div>
</div>
<div class="main-content d-flex flex-column height-100 width" *ngIf="!SessionStore.hasPin || SessionStore.hasPin" >
<div class="div-top-header">
@@ -13,8 +46,8 @@
</div>
<div class=" align-center justify-center d-flex flex-column width-100">
<div *ngIf="hasPin" class="title">Digite o PIN</div>
<div *ngIf="!hasPin" class="title">Digite o novo PIN</div>
<div *ngIf="SessionStore.hasPin" class="title">Digite o PIN</div>
<div *ngIf="!SessionStore.hasPin" class="title">Digite o novo PIN</div>
<div class="terminal">
@@ -43,7 +76,7 @@
</div>
<div *ngIf="hasPin" class="voltar d-flex align-center justify-center pt-25 clear" (click)="loginPreference = 'none'">
<div *ngIf="SessionStore.hasPin" class="voltar d-flex align-center justify-center pt-25 clear" >
Entrar com senha
</div>
@@ -57,7 +90,7 @@
</div>
</div>
</div>
</ion-content>
+10 -44
View File
@@ -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']);
+1 -51
View File
@@ -1,7 +1,7 @@
<ion-content class="text-white">
<div class="main-wrapper">
<div class="wrapper" *ngIf="!hasSession && !setPin || loginPreference == 'none' && !setPin ">
<div class="wrapper">
<div class="bg-1 d-flex justify-center align-center">
<div class="bg-2 d-flex justify-center align-center">
@@ -33,56 +33,6 @@
</div>
</div>
<!-- <div class="main-content height-100" *ngIf="setPin || hasSession && loginPreference == 'pin' ">
<div class=" align-center justify-center">
<div class="div-top-header">
<div class="div-logo">
<img src='assets/images/logo-bg.png' alt='logo'>
</div>
</div>
<ion-title *ngIf="hasPin" class="title">Digite o PIN</ion-title>
<ion-title *ngIf="!hasPin" class="title">Digite o novo PIN</ion-title>
<div class="d-flex pt-25 align-center justify-center">
<div class="dot" [class.dot-active]="code.length >= 1"></div>
<div class="dot" [class.dot-active]="code.length >= 2"></div>
<div class="dot" [class.dot-active]="code.length >= 3"></div>
<div class="dot"[class.dot-active]="code.length >= 4"></div>
</div>
<div class="terminal">
<div class="d-flex">
<div class="circle" (click)="setCode('1')">1</div> <div class="circle" (click)="setCode('2')">2</div> <div class="circle" (click)="setCode('3')">3</div>
</div>
<div class="d-flex">
<div class="circle" (click)="setCode('4')">4</div> <div class="circle" (click)="setCode('5')">5</div> <div class="circle" (click)="setCode('6')">6</div>
</div>
<div class="d-flex">
<div class="circle" (click)="setCode('7')">7</div> <div class="circle" (click)="setCode('8')">8</div> <div class="circle" (click)="setCode('9')">9</div>
</div>
<div class="d-flex align-center justify-center">
<div class="circle" (click)="setCode('0')">0</div>
</div>
</div>
<div *ngIf="hasPin" class="voltar d-flex align-center justify-center pt-25 clear" (click)="loginPreference = 'none'">
Entrar com senha
</div>
<div id="clear" class="cy-clear voltar d-flex align-center justify-center pt-25 clear" (click)="clearCode()">
Limpar
</div>
</div>
</div> -->
</div>
</ion-content>
+7 -87
View File
@@ -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']);
}
}
+14 -13
View File
@@ -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<User>(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);
@@ -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();
});
});
+35
View File
@@ -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
}
}
}
@@ -120,7 +120,6 @@ export class ProfileComponent implements OnInit {
this.router.navigateByUrl('/', { replaceUrl: true });
},100)
}
LoginPreferenceMethod(type: string) {
+1 -1
View File
@@ -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({
+6
View File
@@ -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()
+16
View File
@@ -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();
});
});
+94
View File
@@ -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()