kar-cw/src/service/session.ts
2024-04-07 09:06:05 +02:00

195 lines
4.3 KiB
TypeScript

/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { Injectable, Output, EventEmitter, Inject } from '@angular/core';
import { Router } from '@angular/router';
import { EnvironmentService } from '../model/environment';
import { isNullOrUndefined } from '../utils';
export enum UserRoles222 {
admin = 'admin',
user = 'user',
guest = 'guest',
}
@Injectable({
providedIn: 'root',
})
export class SessionService {
private tokenJwt?: string;
public userLogin?: string;
public userId?: string;
public right: any = {};
@Output() change: EventEmitter<boolean> = new EventEmitter();
private environment;//: EnvironmentService
constructor(
//@Inject('ENVIRONMENT') private environment: EnvironmentService,
) { }
/**
* @brief Create a new session.
*
* @param userId -
* @param userLogin -
* @param tokenJwt -
*/
create({
userId,
userLogin,
tokenJwt,
}: {
userId: string;
userLogin: string;
tokenJwt: string;
}) {
console.log(
`Session Create: userId=${userId} userLogin=${userLogin} tokenJwt = ${tokenJwt}`
);
this.tokenJwt = tokenJwt;
this.userId = userId;
this.userLogin = userLogin;
this.right = this.parseToken(tokenJwt);
console.log(`Retrieve right: ${JSON.stringify(this.right, null, 4)}`);
this.change.emit(true);
}
b64_to_utf8(str: string): string {
return decodeURIComponent(window.atob(str));
}
parseToken(token: string): any {
const cut = token.split('.');
const decoded = this.b64_to_utf8(cut[1]);
const jsonModel = JSON.parse(decoded);
if (isNullOrUndefined(jsonModel.right)) {
return {};
}
if (isNullOrUndefined(jsonModel.right[this.environment.applName])) {
return {};
}
return jsonModel.right[this.environment.applName];
}
/**
* @brief destroy the current session.
*/
destroy() {
console.log('Session REMOVE');
this.tokenJwt = undefined;
this.userId = undefined;
this.userLogin = undefined;
this.right = {};
this.change.emit(false);
}
getToken(): string | undefined {
return this.tokenJwt;
}
islogged() {
return this.userId !== null;
}
hasRight(type: UserRoles222): boolean {
if (type === UserRoles222.admin) {
if (isNullOrUndefined(this.right.ADMIN)) {
return false;
}
return this.right.ADMIN;
}
if (type === UserRoles222.user) {
if (isNullOrUndefined(this.right.USER)) {
return false;
}
return this.right.USER;
}
if (type === UserRoles222.guest) {
// all the other ... maybe unneeded
return true;
}
return false;
}
hasNotRight(rightType: UserRoles222) {
return !this.hasRight(rightType);
}
getLogin() {
return this.userLogin;
}
getAvatar() {
return 'assets/images/avatar_generic.svg';
/* This is not ready:
if(this.userAvatar === false) {
return 'assets/images/avatar_generic.svg';
}
return this.userId;
*/
}
}
@Injectable({
providedIn: 'root',
})
export class OnlyUsersGuard {
constructor(private sessionService: SessionService, private router: Router) { }
canActivate() {
console.log('OnlyLoggedInUsers');
if (this.sessionService.hasRight(UserRoles222.user) || this.sessionService.hasRight(UserRoles222.admin)) {
return true;
} else {
this.router.navigateByUrl('/forbidden');
return false;
}
return true;
}
}
@Injectable({
providedIn: 'root',
})
export class OnlyUsersGuardHome {
constructor(private sessionService: SessionService, private router: Router) { }
canActivate() {
if (this.sessionService.hasRight(UserRoles222.user) || this.sessionService.hasRight(UserRoles222.admin)) {
return true;
} else {
this.router.navigateByUrl('/unregistered');
return false;
}
return true;
}
}
@Injectable({
providedIn: 'root',
})
export class OnlyUnregisteredGuardHome {
constructor(private sessionService: SessionService, private router: Router) { }
canActivate() {
if (!this.sessionService.islogged()) {
return true;
} else {
this.router.navigateByUrl('/home');
return false;
}
return true;
}
}
@Injectable({
providedIn: 'root',
})
export class OnlyAdminGuard {
constructor(private sessionService: SessionService, private router: Router) { }
canActivate() {
if (this.sessionService.hasRight(UserRoles222.user)) {
return true;
} else {
this.router.navigateByUrl('/forbidden');
return false;
}
return true;
}
}