OpenSlides/client/src/app/site/users/services/group-repository.service.ts

149 lines
5.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { BaseRepository } from '../../base/base-repository';
2018-11-27 22:44:37 +01:00
import { CollectionStringModelMapperService } from '../../../core/services/collectionStringModelMapper.service';
import { ConstantsService } from '../../../core/services/constants.service';
2018-11-27 22:44:37 +01:00
import { DataSendService } from '../../../core/services/data-send.service';
import { DataStoreService } from '../../../core/services/data-store.service';
import { Group } from '../../../shared/models/users/group';
2018-10-26 11:19:05 +02:00
import { Identifiable } from '../../../shared/models/base/identifiable';
2018-11-27 22:44:37 +01:00
import { ViewGroup } from '../models/view-group';
/**
* Set rules to define the shape of an app permission
*/
interface AppPermission {
name: string;
permissions: string[];
}
/**
* Repository service for Groups
*
* Documentation partially provided in {@link BaseRepository}
*/
@Injectable({
providedIn: 'root'
})
export class GroupRepositoryService extends BaseRepository<ViewGroup, Group> {
/**
* holds sorted permissions per app.
*/
public appPermissions: AppPermission[] = [];
/**
* Constructor calls the parent constructor
* @param DS The DataStore
* @param mapperService Maps collection strings to classes
* @param dataSend sending changed objects
* @param constants reading out the OpenSlides constants
*/
public constructor(
DS: DataStoreService,
mapperService: CollectionStringModelMapperService,
private dataSend: DataSendService,
private constants: ConstantsService
) {
super(DS, mapperService, Group);
this.sortPermsPerApp();
}
/**
* Add an entry to appPermissions
*
* @param appId number that indicates the app
* @param perm certain permission as string
* @param appName Indicates the header in the Permission Matrix
*/
private addAppPerm(appId: number, perm: string, appName: string): void {
if (!this.appPermissions[appId]) {
this.appPermissions[appId] = {
name: appName,
permissions: []
};
}
this.appPermissions[appId].permissions.push(perm);
}
/**
* read the constants, add them to an array of apps
*/
private sortPermsPerApp(): void {
this.constants.get('permissions').subscribe(perms => {
perms.forEach(perm => {
// extract the apps name
const permApp = perm.value.split('.')[0];
switch (permApp) {
case 'core':
if (perm.value.indexOf('projector') > -1) {
this.addAppPerm(0, perm, 'Projector');
} else {
this.addAppPerm(6, perm, 'General');
}
break;
case 'agenda':
this.addAppPerm(1, perm, 'Agenda');
break;
case 'motions':
this.addAppPerm(2, perm, 'Motions');
break;
case 'assignments':
this.addAppPerm(3, perm, 'Assignments');
break;
case 'mediafiles':
this.addAppPerm(4, perm, 'Mediafiles');
break;
case 'users':
this.addAppPerm(5, perm, 'Users');
break;
default:
// plugins
const displayName = `${permApp.charAt(0).toUpperCase}${permApp.slice(1)}`;
// check if the plugin exists as app
const result = this.appPermissions.findIndex(app => {
return app.name === displayName;
});
const pluginId = result === -1 ? this.appPermissions.length : result;
this.addAppPerm(pluginId, perm, displayName);
break;
}
});
});
}
/**
* creates and saves a new user
*
* @param groupData form value. Usually not yet a real user
*/
2018-10-26 11:19:05 +02:00
public async create(groupData: Partial<Group>): Promise<Identifiable> {
const newGroup = new Group();
newGroup.patchValues(groupData);
2018-10-26 11:19:05 +02:00
return await this.dataSend.createModel(newGroup);
}
/**
* Updates the given Group with the new permission
*
* @param permission the new permission
* @param viewGroup the selected Group
*/
2018-10-26 11:19:05 +02:00
public async update(groupData: Partial<Group>, viewGroup: ViewGroup): Promise<void> {
const updateGroup = new Group();
updateGroup.patchValues(viewGroup.group);
updateGroup.patchValues(groupData);
2018-10-26 11:19:05 +02:00
await this.dataSend.updateModel(updateGroup);
}
/**
* Deletes a given group
*/
2018-10-26 11:19:05 +02:00
public async delete(viewGroup: ViewGroup): Promise<void> {
await this.dataSend.deleteModel(viewGroup.group);
}
public createViewModel(group: Group): ViewGroup {
return new ViewGroup(group);
}
}