Fix sorting of permissions if plugins are available

This commit is contained in:
FinnStutzenstein 2019-05-20 10:31:17 +02:00
parent 0f03eb1446
commit ba73d50886
3 changed files with 24 additions and 11 deletions

View File

@ -22,7 +22,7 @@ interface Permission {
/** /**
* Set rules to define the shape of an app permission * Set rules to define the shape of an app permission
*/ */
interface AppPermission { export interface AppPermissions {
name: string; name: string;
permissions: Permission[]; permissions: Permission[];
} }
@ -39,7 +39,7 @@ export class GroupRepositoryService extends BaseRepository<ViewGroup, Group> {
/** /**
* holds sorted permissions per app. * holds sorted permissions per app.
*/ */
public appPermissions: AppPermission[] = []; public appPermissions: AppPermissions[] = [];
/** /**
* Constructor calls the parent constructor * Constructor calls the parent constructor
@ -107,6 +107,7 @@ export class GroupRepositoryService extends BaseRepository<ViewGroup, Group> {
*/ */
private sortPermsPerApp(): void { private sortPermsPerApp(): void {
this.constants.get<any>('permissions').subscribe(perms => { this.constants.get<any>('permissions').subscribe(perms => {
let pluginCounter = 0;
for (const perm of perms) { for (const perm of perms) {
// extract the apps name // extract the apps name
const permApp = perm.value.split('.')[0]; const permApp = perm.value.split('.')[0];
@ -135,12 +136,20 @@ export class GroupRepositoryService extends BaseRepository<ViewGroup, Group> {
break; break;
default: default:
// plugins // plugins
const displayName = `${permApp.charAt(0).toUpperCase}${permApp.slice(1)}`; const displayName = `${permApp.charAt(0).toUpperCase()}${permApp.slice(1)}`;
// check if the plugin exists as app // check if the plugin exists as app. The appPermissions array might have empty
// entries, so pay attention in the findIndex below.
const result = this.appPermissions.findIndex(app => { const result = this.appPermissions.findIndex(app => {
return app.name === displayName; return app ? app.name === displayName : false;
}); });
const pluginId = result === -1 ? this.appPermissions.length : result; let pluginId: number;
if (result >= 0) {
pluginId = result;
} else {
// Ensure plugins to be behind the 7 core apps.
pluginId = pluginCounter + 7;
pluginCounter++;
}
this.addAppPerm(pluginId, perm, displayName); this.addAppPerm(pluginId, perm, displayName);
break; break;
} }
@ -156,8 +165,8 @@ export class GroupRepositoryService extends BaseRepository<ViewGroup, Group> {
* order, we could simply reverse the whole permissions. * order, we could simply reverse the whole permissions.
*/ */
private sortPermsByPower(): void { private sortPermsByPower(): void {
this.appPermissions.forEach((app: AppPermission, index: number) => { this.appPermissions.forEach((app: AppPermissions) => {
if (index === 5) { if (app.name === 'Users') {
app.permissions.reverse(); app.permissions.reverse();
} else { } else {
const see = []; const see = [];

View File

@ -27,7 +27,7 @@
<span translate>All your changes are saved immediately.</span> <span translate>All your changes are saved immediately.</span>
</div> </div>
<mat-accordion *ngFor="let app of repo.appPermissions"> <mat-accordion *ngFor="let app of appPermissions">
<mat-expansion-panel class="mat-elevation-z0" [expanded]=true> <mat-expansion-panel class="mat-elevation-z0" [expanded]=true>
<mat-expansion-panel-header> <mat-expansion-panel-header>
<mat-panel-title> <mat-panel-title>

View File

@ -4,7 +4,7 @@ import { TranslateService } from '@ngx-translate/core';
import { MatTableDataSource, MatSnackBar } from '@angular/material'; import { MatTableDataSource, MatSnackBar } from '@angular/material';
import { FormGroup, FormControl, Validators } from '@angular/forms'; import { FormGroup, FormControl, Validators } from '@angular/forms';
import { GroupRepositoryService } from 'app/core/repositories/users/group-repository.service'; import { GroupRepositoryService, AppPermissions } from 'app/core/repositories/users/group-repository.service';
import { ViewGroup } from '../../models/view-group'; import { ViewGroup } from '../../models/view-group';
import { Group } from 'app/shared/models/users/group'; import { Group } from 'app/shared/models/users/group';
import { BaseViewComponent } from '../../../base/base-view'; import { BaseViewComponent } from '../../../base/base-view';
@ -47,6 +47,10 @@ export class GroupListComponent extends BaseViewComponent implements OnInit {
@ViewChild('groupForm') @ViewChild('groupForm')
public groupForm: FormGroup; public groupForm: FormGroup;
public get appPermissions(): AppPermissions[] {
return this.repo.appPermissions;
}
/** /**
* Constructor * Constructor
* *
@ -60,7 +64,7 @@ export class GroupListComponent extends BaseViewComponent implements OnInit {
titleService: Title, titleService: Title,
protected translate: TranslateService, // protected required for ng-translate-extract protected translate: TranslateService, // protected required for ng-translate-extract
matSnackBar: MatSnackBar, matSnackBar: MatSnackBar,
public repo: GroupRepositoryService, private repo: GroupRepositoryService,
private promptService: PromptService private promptService: PromptService
) { ) {
super(titleService, translate, matSnackBar); super(titleService, translate, matSnackBar);