Added IsSuperAdmin directive

This commit is contained in:
FinnStutzenstein 2019-07-30 09:01:59 +02:00
parent 60098af22d
commit f876d1c1fa
8 changed files with 86 additions and 13 deletions

View File

@ -85,6 +85,10 @@ export class OperatorService implements OnAfterAppsLoaded {
return !this.user || this.user.id === 0; return !this.user || this.user.id === 0;
} }
public get isSuperAdmin(): boolean {
return this.isInGroupIdsNonAdminCheck(2);
}
/** /**
* Save, if guests are enabled. * Save, if guests are enabled.
*/ */
@ -358,10 +362,6 @@ export class OperatorService implements OnAfterAppsLoaded {
return groupIds.some(id => this.user.groups_id.includes(id)); return groupIds.some(id => this.user.groups_id.includes(id));
} }
public isSuperAdmin(): boolean {
return this.isInGroupIdsNonAdminCheck(2);
}
/** /**
* Update the operators permissions and publish the operator afterwards. * Update the operators permissions and publish the operator afterwards.
* Saves the current WhoAmI to storage with the updated permissions * Saves the current WhoAmI to storage with the updated permissions
@ -383,7 +383,7 @@ export class OperatorService implements OnAfterAppsLoaded {
this.permissions = defaultGroup.permissions; this.permissions = defaultGroup.permissions;
} }
} else { } else {
const permissionSet = new Set(); const permissionSet = new Set<string>();
this.DS.getMany(Group, this.user.groups_id).forEach(group => { this.DS.getMany(Group, this.user.groups_id).forEach(group => {
group.permissions.forEach(permission => { group.permissions.forEach(permission => {
permissionSet.add(permission); permissionSet.add(permission);

View File

@ -0,0 +1,6 @@
describe('IsSuperAdminDirective', () => {
it('should create an instance', () => {
// const directive = new IsSuperAdminDirective();
// expect(directive).toBeTruthy();
});
});

View File

@ -0,0 +1,68 @@
import { Directive, OnDestroy, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';
import { Subscription } from 'rxjs';
import { OperatorService } from 'app/core/core-services/operator.service';
/**
* Directive to check if the operator is a superadmin
*
* @example <div *osIsSuperadmin ..> ... < /div>
*/
@Directive({
selector: '[osIsSuperAdmin]'
})
export class IsSuperAdminDirective implements OnInit, OnDestroy {
/**
* Holds the value of the last is superadmin check. Therefore one can check, if the
* permission has changes, to save unnecessary view updates, if not.
*/
private lastIsSuperAdminCheckResult = false;
private operatorSubscription: Subscription | null;
/**
* Constructs the directive once. Observes the operator for it's groups so the
* directive 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
*/
public constructor(
private template: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private operator: OperatorService
) {}
public ngOnInit(): void {
// observe groups of operator, so the directive can actively react to changes
this.operatorSubscription = this.operator.getUserObservable().subscribe(() => {
this.updateView();
});
}
public ngOnDestroy(): void {
if (this.operatorSubscription) {
this.operatorSubscription.unsubscribe();
}
}
/**
* Shows or hides certain content in the view.
*/
private updateView(): void {
const isSuperadmin = this.operator.isSuperAdmin;
const superADminChanged = isSuperadmin !== this.lastIsSuperAdminCheckResult;
if (isSuperadmin && superADminChanged) {
// clean up and add the template
this.viewContainer.clear();
this.viewContainer.createEmbeddedView(this.template);
} else if (!isSuperadmin) {
// will remove the content of the container
this.viewContainer.clear();
}
this.lastIsSuperAdminCheckResult = isSuperadmin;
}
}

View File

@ -52,6 +52,7 @@ import { EditorModule } from '@tinymce/tinymce-angular';
// directives // directives
import { PermsDirective } from './directives/perms.directive'; import { PermsDirective } from './directives/perms.directive';
import { IsSuperAdminDirective } from './directives/is-super-admin.directive';
import { DomChangeDirective } from './directives/dom-change.directive'; import { DomChangeDirective } from './directives/dom-change.directive';
import { AutofocusDirective } from './directives/autofocus.directive'; import { AutofocusDirective } from './directives/autofocus.directive';
@ -195,6 +196,7 @@ import { AttachmentControlComponent } from './components/attachment-control/atta
TranslateModule, TranslateModule,
OpenSlidesTranslateModule, OpenSlidesTranslateModule,
PermsDirective, PermsDirective,
IsSuperAdminDirective,
DomChangeDirective, DomChangeDirective,
AutofocusDirective, AutofocusDirective,
HeadBarComponent, HeadBarComponent,
@ -235,6 +237,7 @@ import { AttachmentControlComponent } from './components/attachment-control/atta
], ],
declarations: [ declarations: [
PermsDirective, PermsDirective,
IsSuperAdminDirective,
DomChangeDirective, DomChangeDirective,
AutofocusDirective, AutofocusDirective,
HeadBarComponent, HeadBarComponent,

View File

@ -19,7 +19,7 @@
</button> </button>
</div> </div>
<div *osPerms="'users.can_manage'"> <div *osIsSuperAdmin>
<button <button
type="button" type="button"
mat-button mat-button

View File

@ -5,7 +5,7 @@
<!-- Menu --> <!-- Menu -->
<div class="menu-slot"> <div class="menu-slot">
<!-- Hidden for everyone but the superadmin --> <!-- Hidden for everyone but the superadmin -->
<button *ngIf="isSuperAdmin" type="button" mat-icon-button [matMenuTriggerFor]="historyMenu"> <button *osIsSuperAdmin type="button" mat-icon-button [matMenuTriggerFor]="historyMenu">
<mat-icon>more_vert</mat-icon> <mat-icon>more_vert</mat-icon>
</button> </button>
</div> </div>

View File

@ -65,10 +65,6 @@ export class HistoryListComponent extends BaseViewComponent implements OnInit {
return this.modelSelectForm.controls.model.value; return this.modelSelectForm.controls.model.value;
} }
public get isSuperAdmin(): boolean {
return this.operator.isSuperAdmin();
}
/** /**
* Constructor for the history list component * Constructor for the history list component
* *
@ -190,7 +186,7 @@ export class HistoryListComponent extends BaseViewComponent implements OnInit {
* @param history Represents the selected element * @param history Represents the selected element
*/ */
public async onClickRow(history: History): Promise<void> { public async onClickRow(history: History): Promise<void> {
if (!this.isSuperAdmin) { if (!this.operator.isSuperAdmin) {
return; return;
} }

View File

@ -50,7 +50,7 @@ class NotifyWebsocketClientMessage(BaseWebsocketClientMessage):
} }
# Define a required permission for a notify message here. If the emitting user does not # Define a required permission for a notify message here. If the emitting user does not
# have this permission, he will get an error message in response. # have this permission, he will get an error message in response.
notify_permissions: Dict[str, str] = {"swCheckForUpdate": "users.can_manage"} notify_permissions: Dict[str, str] = {"swCheckForUpdate": "superadmin"}
async def receive_content( async def receive_content(
self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str self, consumer: "ProtocollAsyncJsonWebsocketConsumer", content: Any, id: str