2018-07-06 09:38:25 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2018-07-12 14:11:31 +02:00
|
|
|
import { Observable, BehaviorSubject } from 'rxjs';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
2018-08-29 13:21:25 +02:00
|
|
|
import { tap, catchError } from 'rxjs/operators';
|
2018-07-12 14:11:31 +02:00
|
|
|
import { OpenSlidesComponent } from 'app/openslides.component';
|
2018-07-23 16:42:17 +02:00
|
|
|
import { Group } from 'app/shared/models/users/group';
|
2018-08-22 11:26:53 +02:00
|
|
|
import { User } from '../../shared/models/users/user';
|
2018-08-23 16:49:51 +02:00
|
|
|
import { environment } from 'environments/environment';
|
2018-09-13 14:40:04 +02:00
|
|
|
import { DataStoreService } from './data-store.service';
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-08-28 11:07:10 +02:00
|
|
|
/**
|
|
|
|
* Permissions on the client are just strings. This makes clear, that
|
|
|
|
* permissions instead of arbitrary strings should be given.
|
|
|
|
*/
|
|
|
|
export type Permission = string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Response format of the WHoAMI request.
|
|
|
|
*/
|
|
|
|
interface WhoAmIResponse {
|
|
|
|
user_id: number;
|
|
|
|
guest_enabled: boolean;
|
|
|
|
user: User;
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* The operator represents the user who is using OpenSlides.
|
|
|
|
*
|
|
|
|
* Changes in operator can be observed, directives do so on order to show
|
|
|
|
* or hide certain information.
|
|
|
|
*
|
|
|
|
* The operator is an {@link OpenSlidesComponent}.
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2018-07-12 14:11:31 +02:00
|
|
|
export class OperatorService extends OpenSlidesComponent {
|
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* The operator.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
private _user: User;
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Get the user that corresponds to operator.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public get user(): User {
|
2018-08-28 11:07:10 +02:00
|
|
|
return this._user;
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Sets the current operator.
|
|
|
|
*
|
|
|
|
* The permissions are updated and the new user published.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-29 13:21:25 +02:00
|
|
|
public set user(user: User) {
|
2018-08-28 11:07:10 +02:00
|
|
|
this._user = user;
|
|
|
|
this.updatePermissions();
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Save, if quests are enabled.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
public guestsEnabled: boolean;
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* The permissions of the operator. Updated via {@method updatePermissions}.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
private permissions: Permission[] = [];
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* The subject that can be observed by other instances using observing functions.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-09-07 13:12:59 +02:00
|
|
|
private operatorSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null);
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* @param http HttpClient
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-09-13 14:40:04 +02:00
|
|
|
public constructor(private http: HttpClient, private DS: DataStoreService) {
|
2018-08-28 11:07:10 +02:00
|
|
|
super();
|
2018-07-06 09:38:25 +02:00
|
|
|
|
2018-09-07 12:51:16 +02:00
|
|
|
this.DS.changeObservable.subscribe(newModel => {
|
2018-08-28 11:07:10 +02:00
|
|
|
if (this._user) {
|
|
|
|
if (newModel instanceof Group) {
|
|
|
|
this.updatePermissions();
|
|
|
|
}
|
2018-08-22 11:26:53 +02:00
|
|
|
|
2018-08-28 11:07:10 +02:00
|
|
|
if (newModel instanceof User && this._user.id === newModel.id) {
|
|
|
|
this._user = newModel;
|
|
|
|
this.updatePermissions();
|
|
|
|
}
|
|
|
|
} else if (newModel instanceof Group && newModel.id === 1) {
|
|
|
|
// Group 1 (default) for anonymous changed
|
|
|
|
this.updatePermissions();
|
2018-08-22 11:26:53 +02:00
|
|
|
}
|
2018-07-06 09:38:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Calls `/apps/users/whoami` to find out the real operator.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
public whoAmI(): Observable<WhoAmIResponse> {
|
|
|
|
return this.http.get<WhoAmIResponse>(environment.urlPrefix + '/users/whoami/').pipe(
|
|
|
|
tap((response: WhoAmIResponse) => {
|
|
|
|
if (response && response.user_id) {
|
2018-09-04 11:33:28 +02:00
|
|
|
this.user = new User(response.user);
|
2018-08-28 11:07:10 +02:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
catchError(this.handleError())
|
|
|
|
) as Observable<WhoAmIResponse>;
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Returns the operatorSubject as an observable.
|
2018-07-12 14:11:31 +02:00
|
|
|
*
|
|
|
|
* Services an components can use it to get informed when something changes in
|
|
|
|
* the operator
|
|
|
|
*/
|
2018-09-07 13:12:59 +02:00
|
|
|
public getObservable(): Observable<User> {
|
2018-07-06 09:38:25 +02:00
|
|
|
return this.operatorSubject.asObservable();
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Checks, if the operator has at least one of the given permissions.
|
2018-09-18 18:27:14 +02:00
|
|
|
* @param checkPerms The permissions to check, if at least one matches.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-09-18 18:27:14 +02:00
|
|
|
public hasPerms(...checkPerms: Permission[]): boolean {
|
|
|
|
return checkPerms.some(permission => {
|
|
|
|
return this.permissions.includes(permission);
|
2018-08-28 11:07:10 +02:00
|
|
|
});
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
2018-08-28 11:07:10 +02:00
|
|
|
* Update the operators permissions and publish the operator afterwards.
|
2018-07-12 14:11:31 +02:00
|
|
|
*/
|
2018-08-28 11:07:10 +02:00
|
|
|
private updatePermissions(): void {
|
|
|
|
this.permissions = [];
|
|
|
|
if (!this.user) {
|
2018-09-10 08:57:53 +02:00
|
|
|
const defaultGroup = this.DS.get<Group>('users/group', 1);
|
2018-08-28 11:07:10 +02:00
|
|
|
if (defaultGroup && defaultGroup.permissions instanceof Array) {
|
|
|
|
this.permissions = defaultGroup.permissions;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const permissionSet = new Set();
|
2018-09-13 14:40:04 +02:00
|
|
|
this.DS.getMany(Group, this.user.groups_id).forEach(group => {
|
2018-08-28 11:07:10 +02:00
|
|
|
group.permissions.forEach(permission => {
|
|
|
|
permissionSet.add(permission);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.permissions = Array.from(permissionSet.values());
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|
2018-08-28 11:07:10 +02:00
|
|
|
// publish changes in the operator.
|
|
|
|
this.operatorSubject.next(this.user);
|
2018-08-22 11:26:53 +02:00
|
|
|
}
|
2018-07-06 09:38:25 +02:00
|
|
|
}
|