2016-05-09 17:04:56 +02:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-12-27 16:09:47 +01:00
|
|
|
import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO";
|
2017-07-03 19:17:49 +02:00
|
|
|
import {BehaviorSubject} from "rxjs/BehaviorSubject";
|
2016-12-26 23:36:38 +01:00
|
|
|
import {UserService} from "./user.service";
|
2016-04-30 18:01:54 +02:00
|
|
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
2016-12-26 23:36:38 +01:00
|
|
|
import {Cookie} from "ng2-cookies";
|
2017-06-04 15:25:08 +02:00
|
|
|
import {Config} from "../../../../common/config/public/Config";
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
declare module ServerInject {
|
2017-06-10 22:32:56 +02:00
|
|
|
export let user: UserDTO;
|
2016-04-09 18:06:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-13 11:28:29 +01:00
|
|
|
@Injectable()
|
2016-05-09 17:04:56 +02:00
|
|
|
export class AuthenticationService {
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
public user: BehaviorSubject<UserDTO>;
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
constructor(private _userService: UserService) {
|
2017-07-03 19:17:49 +02:00
|
|
|
this.user = new BehaviorSubject(null);
|
2016-05-09 17:04:56 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
//picking up session..
|
|
|
|
if (this.isAuthenticated() == false && Cookie.get('pigallery2-session') != null) {
|
|
|
|
if (typeof ServerInject !== "undefined" && typeof ServerInject.user !== "undefined") {
|
2017-07-03 19:17:49 +02:00
|
|
|
this.user.next(ServerInject.user);
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
|
|
|
this.getSessionUser();
|
|
|
|
} else {
|
2017-07-03 19:17:49 +02:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
this.user.next(<UserDTO>{name: "", password: "", role: UserRoles.Admin});
|
|
|
|
}
|
2016-03-20 20:06:14 +01:00
|
|
|
}
|
2016-05-09 17:04:56 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
private async getSessionUser(): Promise<void> {
|
|
|
|
try {
|
|
|
|
this.user.next(await this._userService.getSessionUser());
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2016-05-04 22:20:54 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-05-04 22:20:54 +02:00
|
|
|
|
2017-07-03 19:17:49 +02:00
|
|
|
|
|
|
|
public async login(credential: LoginCredential): Promise<UserDTO> {
|
2017-07-09 12:03:17 +02:00
|
|
|
const user = await this._userService.login(credential);
|
|
|
|
this.user.next(user);
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async shareLogin(password: string): Promise<UserDTO> {
|
|
|
|
const user = await this._userService.shareLogin(password);
|
|
|
|
this.user.next(user);
|
|
|
|
return user;
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2016-05-09 17:04:56 +02:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
public isAuthenticated(): boolean {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return true;
|
2016-05-01 21:30:43 +02:00
|
|
|
}
|
2017-07-03 19:17:49 +02:00
|
|
|
return !!(this.user.value && this.user.value != null);
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-03-13 11:28:29 +01:00
|
|
|
|
2017-07-09 12:03:17 +02:00
|
|
|
public isAuthorized(role: UserRoles) {
|
|
|
|
return this.user.value && this.user.value.role >= role;
|
|
|
|
}
|
2017-06-10 22:32:56 +02:00
|
|
|
|
|
|
|
public logout() {
|
|
|
|
this._userService.logout();
|
2017-07-03 19:17:49 +02:00
|
|
|
this.user.next(null);
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
2016-05-16 23:15:03 +02:00
|
|
|
|
2016-03-13 11:28:29 +01:00
|
|
|
|
|
|
|
}
|