All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
src.app.core.services.login.service.ts Maven / Gradle / Ivy
/**
* Copyright 2017-2023 Enedis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable, BehaviorSubject} from 'rxjs';
import { tap, delay } from 'rxjs/operators';
import { environment } from '@env/environment';
import { User, Authorization } from '@model';
import { intersection, contains, isNullOrBlankString } from '@shared/tools';
@Injectable({
providedIn: 'root'
})
export class LoginService {
private url = '/api/v1/user';
private loginUrl = this.url + '/login';
private NO_USER = new User('');
private user$: BehaviorSubject = new BehaviorSubject(this.NO_USER);
constructor(
private http: HttpClient,
private router: Router
) { }
initLogin(url?: string) {
this.currentUser(true).pipe(
tap(user => this.setUser(user))
).subscribe(
() => this.navigateAfterLogin(url),
() => {
const nextUrl = this.nullifyLoginUrl(url);
const queryParams: Object = isNullOrBlankString(nextUrl) ? {} : { queryParams: { url: nextUrl } };
this.router.navigate(['login'], queryParams);
}
);
}
login(username: string, password: string): Observable {
if (isNullOrBlankString(username) && isNullOrBlankString(password)) {
return this.currentUser().pipe(
tap(user => this.setUser(user))
);
}
const body = new URLSearchParams();
body.set('username', username);
body.set('password', password);
const options = {
headers: new HttpHeaders()
.set('Content-Type', 'application/x-www-form-urlencoded')
.set('no-intercept-error', '')
};
return this.http.post(environment.backend + this.loginUrl, body.toString(), options)
.pipe(
tap(user => this.setUser(user))
);
}
navigateAfterLogin(url?: string) {
const nextUrl = this.nullifyLoginUrl(url);
if (this.isAuthenticated()) {
const user: User = this.user$.getValue();
this.router.navigateByUrl(nextUrl ? nextUrl : this.defaultForwardUrl(user));
} else {
this.router.navigateByUrl('/login');
}
}
logout() {
this.http.post(environment.backend + this.url + '/logout', null).pipe(
tap(() => this.setUser(this.NO_USER)),
delay(500)
).subscribe(
() => {
this.router.navigateByUrl('/login');
}
);
}
getUser(): Observable {
return this.user$;
}
isAuthenticated(): boolean {
const user: User = this.user$.getValue();
return this.NO_USER !== user;
}
hasAuthorization(authorization: Array | Authorization = [], u: User = null): boolean {
const user: User = u || this.user$.getValue();
const auth = [].concat(authorization);
if (user != this.NO_USER) {
return auth.length == 0 || intersection(user.authorizations, auth).length > 0;
}
return false;
}
isLoginUrl(url: string): boolean {
return url.includes(this.loginUrl);
}
private setUser(user: User) {
this.user$.next(user);
}
private currentUser(skipInterceptor: boolean = false): Observable {
const options = {
headers: { 'no-intercept-error': ''}
};
return this.http.get(environment.backend + this.url, skipInterceptor ? options : {});
}
private defaultForwardUrl(user: User): string {
const authorizations = user.authorizations;
if (authorizations) {
if (contains(authorizations, Authorization.SCENARIO_READ)) return '/scenario';
if (contains(authorizations, Authorization.CAMPAIGN_READ)) return '/campaign';
if (contains(authorizations, Authorization.ENVIRONMENT_ACCESS)) return '/targets';
if (contains(authorizations, Authorization.GLOBAL_VAR_READ)) return '/variable';
if (contains(authorizations, Authorization.DATASET_READ)) return '/dataset';
if (contains(authorizations, Authorization.ADMIN_ACCESS)) return '/';
}
return '/login';
}
private nullifyLoginUrl(url: string): string {
return url && url !== '/login' ? url : null;
}
}