OpenSlides/client/src/app/core/ui-services/login-data.service.ts

124 lines
3.5 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { ConfigService } from './config.service';
2019-03-08 09:19:05 +01:00
import { StorageService } from '../core-services/storage.service';
/**
2019-03-08 09:19:05 +01:00
* The login data send by the server.
*/
export interface LoginData {
privacy_policy: string;
legal_notice: string;
theme: string;
}
const LOGIN_DATA_STORAGE_KEY = 'LoginData';
/**
* This service holds the privacy policy, the legal notice and the OpenSlides theme, so
* they are available even if the user is not logged in.
*/
@Injectable({
providedIn: 'root'
})
2019-02-08 17:24:32 +01:00
export class LoginDataService {
/**
* Holds the privacy policy
*/
2019-03-08 09:19:05 +01:00
private readonly _privacy_policy = new BehaviorSubject<string>('');
/**
* Returns an observable for the privacy policy
*/
public get privacy_policy(): Observable<string> {
return this._privacy_policy.asObservable();
}
/**
* Holds the legal notice
*/
2019-03-08 09:19:05 +01:00
private readonly _legal_notice = new BehaviorSubject<string>('');
/**
* Returns an observable for the legal notice
*/
public get legal_notice(): Observable<string> {
return this._legal_notice.asObservable();
}
2019-03-08 09:19:05 +01:00
/**
* Holds the theme
*/
private readonly _theme = new BehaviorSubject<string>('');
/**
* Returns an observable for the theme
*/
public get theme(): Observable<string> {
return this._theme.asObservable();
}
/**
* Constructs this service. The config service is needed to update the privacy
* policy and legal notice, when their config values change.
* @param configService
*/
2019-03-08 09:19:05 +01:00
public constructor(private configService: ConfigService, private storageService: StorageService) {
2019-01-31 08:20:15 +01:00
this.configService.get<string>('general_event_privacy_policy').subscribe(value => {
2019-03-08 09:19:05 +01:00
this._privacy_policy.next(value);
this.storeLoginData();
});
2019-01-31 08:20:15 +01:00
this.configService.get<string>('general_event_legal_notice').subscribe(value => {
2019-03-08 09:19:05 +01:00
this._legal_notice.next(value);
this.storeLoginData();
});
2019-03-08 09:19:05 +01:00
configService.get<string>('openslides_theme').subscribe(value => {
this._theme.next(value);
this.storeLoginData();
});
this.loadLoginData();
}
/**
* Load the login data from the storage. If it there, set it.
*/
private async loadLoginData(): Promise<void> {
const loginData = await this.storageService.get<LoginData | null>(LOGIN_DATA_STORAGE_KEY);
if (loginData) {
this.setLoginData(loginData);
}
}
/**
2019-03-08 09:19:05 +01:00
* Setter for the login data
*
* @param loginData the login data
*/
2019-03-08 09:19:05 +01:00
public setLoginData(loginData: LoginData): void {
this._privacy_policy.next(loginData.privacy_policy);
this._legal_notice.next(loginData.legal_notice);
this._theme.next(loginData.theme);
this.storeLoginData(loginData);
}
/**
2019-03-08 09:19:05 +01:00
* Saves the login data in the storage.
*
* @param loginData If given, this data is used. If it's null, the current values
* from the behaviour subject are taken.
*/
2019-03-08 09:19:05 +01:00
private storeLoginData(loginData?: LoginData): void {
if (!loginData) {
loginData = {
privacy_policy: this._privacy_policy.getValue(),
legal_notice: this._legal_notice.getValue(),
theme: this._theme.getValue()
};
}
this.storageService.set(LOGIN_DATA_STORAGE_KEY, loginData);
}
}