Files
doneit-web/src/app/store/session.service.ts
T

195 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-08-27 13:39:52 +01:00
import { Injectable } from '@angular/core';
import { localstoreService } from './localstore.service'
import { SHA1 } from 'crypto-js'
import { UserSession } from '../models/user.model';
@Injectable({
providedIn: 'root'
})
2021-08-27 17:11:05 +01:00
class SessionService {
2021-08-27 13:39:52 +01:00
// main data
private _user = new UserSession()
// local storage keyName
2022-02-08 14:22:32 +01:00
private keyName: string;
2021-08-27 13:39:52 +01:00
2022-12-19 15:48:40 +01:00
forceToLoginWithForceToLogInWithPassword = false
2023-01-25 15:06:19 +01:00
permission = {
Agenda: {
2023-09-28 09:02:15 +01:00
access: false
2023-01-25 15:06:19 +01:00
},
Gabinete: {
2023-09-28 09:02:15 +01:00
access: false,
pr_tasks: false,
2023-01-25 15:06:19 +01:00
md_tasks: false,
2023-09-28 09:02:15 +01:00
aprove_event: false
2023-01-25 15:06:19 +01:00
},
Actions: {
access : false,
create : false,
delete : false,
edit : false,
createPost : false,
deletePost : false,
editPost : false
},
Chat: {
access: false
}
}
2023-09-28 09:02:15 +01:00
hasPassLogin = false
2021-08-27 13:39:52 +01:00
constructor() {
2023-01-12 15:27:09 +01:00
this.keyName = (SHA1("SessionService")).toString()
2023-05-29 11:51:08 +01:00
let restore = this.getDataFromLocalStorage()
2021-08-27 13:39:52 +01:00
this._user = restore.user || new UserSession()
2023-09-28 09:02:15 +01:00
if(this._user.LoginPreference == 'Pin') {
this._user.Inactivity = false
}
2021-08-27 13:39:52 +01:00
}
2023-05-29 11:51:08 +01:00
getDataFromLocalStorage() {
return localstoreService.get(this.keyName, {})
}
2021-08-27 13:39:52 +01:00
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()
}
2021-08-30 10:24:46 +01:00
needToValidateUser() {
2021-08-27 17:11:05 +01:00
return this._user.Inactivity
2021-08-27 13:39:52 +01:00
}
2022-12-19 00:07:05 +01:00
isUserActive() {
return this._user.Inactivity
}
2021-08-27 13:39:52 +01:00
setInativity(value: boolean) {
2021-08-27 17:11:05 +01:00
this._user.Inactivity = value
2021-09-03 12:19:21 +01:00
this._user.UrlBeforeInactivity = ''
2021-08-27 17:11:05 +01:00
this.save()
}
setUrlBeforeInactivity(pathname: string) {
this._user.UrlBeforeInactivity = pathname
2021-08-27 13:39:52 +01:00
this.save()
}
get hasPin() {
if(!this._user.PIN) {
return false
}
return this._user.PIN.length >= 2
2022-02-08 14:22:32 +01:00
2021-08-27 13:39:52 +01:00
}
reset(user) {
this._user = user
this.setInativity(true)
this.save()
}
delete() {
localstoreService.delete(this.keyName)
2021-08-27 15:21:15 +01:00
this.reset(new UserSession())
2021-08-27 13:39:52 +01:00
}
2022-03-03 22:57:33 +01:00
save() {
2021-08-27 13:39:52 +01:00
localstoreService.set(this.keyName, {
user: this._user
})
}
2022-04-07 14:24:07 +01:00
get getInitials() {
let names = this._user.FullName.split(' ') || ' ',
initials = names[0].substring(0, 1).toUpperCase();
if (names.length > 1) {
initials += names[names.length - 1].substring(0, 1).toUpperCase();
}
2023-09-28 09:02:15 +01:00
2022-04-07 14:24:07 +01:00
return initials;
}
2022-04-07 14:40:37 +01:00
get getManagerInitials() {
2022-04-07 15:35:48 +01:00
let names = this._user.ManagerName.split(' ') || ' ',
2022-04-07 14:40:37 +01:00
initials = names[0].substring(0, 1).toUpperCase();
if (names.length > 1) {
initials += names[names.length - 1].substring(0, 1).toUpperCase();
}
2023-09-28 09:02:15 +01:00
2022-04-07 14:40:37 +01:00
return initials;
}
2023-02-03 19:37:47 +01:00
clearPermission() {
this.permission = {
Agenda: {
2023-09-28 09:02:15 +01:00
access: false
2023-02-03 19:37:47 +01:00
},
Gabinete: {
2023-09-28 09:02:15 +01:00
access: false,
pr_tasks: false,
2023-02-03 19:37:47 +01:00
md_tasks: false,
2023-09-28 09:02:15 +01:00
aprove_event: false
2023-02-03 19:37:47 +01:00
},
Actions: {
access : false,
create : false,
delete : false,
edit : false,
createPost : false,
deletePost : false,
editPost : false
},
Chat: {
access: false
}
}
}
setPermission() {}
2021-08-27 13:39:52 +01:00
}
2022-02-08 14:22:32 +01:00
export const SessionStore = new SessionService()