2018-07-06 09:38:25 +02:00
|
|
|
import { Directive, Input, ElementRef, TemplateRef, ViewContainerRef, OnInit } from '@angular/core';
|
2018-07-12 14:11:31 +02:00
|
|
|
|
2018-07-06 09:38:25 +02:00
|
|
|
import { OperatorService } from 'app/core/services/operator.service';
|
2018-07-12 14:11:31 +02:00
|
|
|
import { OpenSlidesComponent } from '../../openslides.component';
|
2018-07-06 09:38:25 +02:00
|
|
|
import { Group } from 'app/core/models/users/group';
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Directive to check if the {@link OperatorService} has the correct permissions to access certain functions
|
|
|
|
*
|
|
|
|
* Successor of os-perms in OpenSlides 2.2
|
|
|
|
* @example <div *appOsPerms=".." ..> ... < /div>
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
@Directive({
|
|
|
|
selector: '[appOsPerms]'
|
|
|
|
})
|
2018-07-12 14:11:31 +02:00
|
|
|
export class OsPermsDirective extends OpenSlidesComponent {
|
|
|
|
/**
|
|
|
|
* Holds the {@link OperatorService} permissions
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
private userPermissions: string[];
|
2018-07-12 14:11:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the required permissions the access a feature
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
private permissions;
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Constructs the direcctive once. Observes the operator for it's groups so the directvice can perform changes
|
|
|
|
* dynamically
|
|
|
|
*
|
|
|
|
* @param template inner part of the HTML container
|
|
|
|
* @param viewContainer outer part of the HTML container (for example a `<div>`)
|
|
|
|
* @param operator OperatorService
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
constructor(
|
|
|
|
private template: TemplateRef<any>,
|
2018-07-12 14:11:31 +02:00
|
|
|
private viewContainer: ViewContainerRef,
|
2018-07-06 09:38:25 +02:00
|
|
|
private operator: OperatorService
|
|
|
|
) {
|
|
|
|
super();
|
|
|
|
this.userPermissions = [];
|
|
|
|
|
|
|
|
// observe groups of operator, so the directive can actively react to changes
|
|
|
|
this.operator.getObservable().subscribe(content => {
|
|
|
|
console.log('os-perms did monitor changes in observer: ', content);
|
|
|
|
if (content instanceof Group && this.permissions !== '') {
|
|
|
|
console.log('content was a Group');
|
|
|
|
this.userPermissions = [...this.userPermissions, ...content.permissions];
|
|
|
|
this.updateView();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Comes directly from the view.
|
|
|
|
* The value defines the requires permissions.
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
@Input()
|
|
|
|
set appOsPerms(value) {
|
|
|
|
this.permissions = value;
|
|
|
|
this.readUserPermissions();
|
|
|
|
this.updateView();
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Updates the local `userPermissions[]` by the permissions found in the operators groups
|
|
|
|
* Will just set, but not remove them.
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
private readUserPermissions(): void {
|
|
|
|
const opGroups = this.operator.getGroups();
|
|
|
|
console.log('operator Groups: ', opGroups);
|
|
|
|
opGroups.forEach(group => {
|
|
|
|
this.userPermissions = [...this.userPermissions, ...group.permissions];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Shows or hides certain content in the view.
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
private updateView(): void {
|
|
|
|
if (this.checkPermissions()) {
|
|
|
|
// will just render the page normally
|
|
|
|
this.viewContainer.createEmbeddedView(this.template);
|
|
|
|
} else {
|
|
|
|
// will remove the content of the container
|
|
|
|
this.viewContainer.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 14:11:31 +02:00
|
|
|
/**
|
|
|
|
* Compare the required permissions with the users permissions.
|
|
|
|
* Returns true if the users permissions fit.
|
|
|
|
*/
|
2018-07-06 09:38:25 +02:00
|
|
|
private checkPermissions(): boolean {
|
|
|
|
let isPermitted = false;
|
|
|
|
if (this.userPermissions && this.permissions) {
|
|
|
|
this.permissions.forEach(perm => {
|
|
|
|
isPermitted = this.userPermissions.find(userPerm => userPerm === perm) ? true : false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return isPermitted;
|
|
|
|
}
|
|
|
|
}
|