1 import { Component, OnInit } from '@angular/core';
2 import { Role } from '@app/model/role';
3 import {UserService} from "@app/services/user.service";
4 import {ActivatedRoute} from "@angular/router";
5 import {filter, map, switchMap} from "rxjs/operators";
6 import {RoleTree} from "@app/model/role-tree";
7 import {RoleService} from "@app/services/role.service";
8 import {RoleTemplate} from "@app/model/role-template";
9 import {Observable} from "rxjs";
10 import {Util} from "@app/modules/shared/shared.module";
13 selector: 'app-manage-users-roles',
14 templateUrl: './manage-users-roles.component.html',
15 styleUrls: ['./manage-users-roles.component.scss']
17 export class ManageUsersRolesComponent implements OnInit {
19 baseRoles : Array<Role>
22 // Map of (resource, [roles])
23 templateRoleInstances: Map<string, Array<Role>>
24 templateRoles$: Observable<RoleTemplate[]>;
26 constructor(private route : ActivatedRoute, private userService : UserService, private roleService : RoleService) {
27 this.route.params.pipe(
28 map(params => params.userid), filter(userid => userid!=null), switchMap(userid => userService.userRoleTree(userid))).subscribe(roleTree => {
29 this.parseRoleTree(roleTree);
35 this.templateRoles$ = this.roleService.getTemplates();
38 private parseRoleTree(roleTree:RoleTree): void {
40 for(let rootRole of roleTree.root_roles) {
41 roleTable = this.recurseRoleTree(rootRole, roleTable, 0);
43 this.baseRoles = roleTable;
44 let templateMap : Map<string,Array<Role>> = new Map<string, Array<Role>>();
45 for (let rootRole of roleTree.root_roles) {
46 templateMap = this.recurseTemplates(rootRole, templateMap, 0);
48 this.templateRoleInstances = templateMap;
51 private recurseRoleTree(role:Role, roles : Array<Role>, level:number) : Array<Role> {
52 if (role.id=='guest') {
54 } else if (role.id=='registered-user') {
55 this.registered = role;
59 if (!role.template_instance && role.assignable) {
66 for(let childRole of role.children) {
67 roles = this.recurseRoleTree(childRole, roles, newLevel);
72 private recurseTemplates(role:Role, roles : Map<string, Array<Role>>, level:number) : Map<string, Array<Role>> {
74 if (role.template_instance && role.assignable) {
76 let roleList = roles.get(role.resource)
81 roles.set(role.resource, roleList);
83 for(let childRole of role.children) {
84 roles = this.recurseTemplates(childRole, roles, level+1);
89 getRoleContent(role:Role) : string {
90 let level = role.level
92 for(let _i=0; _i<level; _i++) {
93 result = result + " <i class='fas fa-angle-double-right'></i> ";
96 return result + role.name;
98 return "<strong>"+result+role.name+"</strong>"
102 changeBaseAssignment(role : Role, event) {
105 if (role.id==this.guest.id) {
107 for (let cRole of this.baseRoles) {
108 if (cRole.id != this.guest.id) {
109 cRole.assigned = false;
113 role.enabled = false;
116 this.guest.enabled = true;
117 for (let cRole of this.baseRoles) {
118 if (cRole.id == role.id) {
119 console.log("Value: " + cRole.assigned);
120 cLevel = cRole.level;
121 assignStatus = cRole.assigned;
123 this.guest.assigned = false;
125 if (!this.baseRoles.find(role=>role.assigned)) {
126 this.guest.assigned=true;
130 console.log("Level " + cLevel);
131 if (cLevel >= 0 && cLevel < cRole.level) {
133 cRole.assigned = true;
134 cRole.enabled = false;
136 cRole.enabled = true;
138 } else if (cLevel >= 0) {
146 changeInstAssignment(role : Role, event) {
147 console.log("Change " + role.id);
148 console.log("Assignment changed "+JSON.stringify(event));
149 console.log("Event target "+event.target);
152 getInstanceContent(template:RoleTemplate, roles:Array<Role>) : Role {
153 return roles.find(role=>role.model_id==template.id)