File
tools
|
tools: Tool[]
|
Type : Tool[]
|
import { Component, OnInit, Input } from '@angular/core';
import { UtilService } from 'src/app/core/services/util-service/util.service';
import { LabelService } from 'src/app/core/services/label-service/label-service.service';
import { KeycloakService } from 'keycloak-angular';
import { Tool } from 'src/app/core/interfaces/interfaces.component';
interface ToolsCollection {
id: string;
title: string;
tools: Tool[];
restricted: boolean;
}
@Component({
selector: 'div[tools-collection-main]',
templateUrl: './tools-collection-main.component.html',
styleUrls: ['./tools-collection-main.component.scss']
})
export class ToolsCollectionMainComponent implements OnInit {
@Input() entity;
isLoggedIn: any;
userRoles: String[] = [];
data: ToolsCollection;
constructor(private util: UtilService, private keycloakService: KeycloakService, private labels: LabelService ) { }
ngOnInit() {
this.keycloakService.isLoggedIn().then(res => {
this.isLoggedIn = res;
if (this.isLoggedIn) {
this.keycloakService.getUserRoles(true).map(role => this.userRoles.push(role));
this.userRoles = this.userRoles.map(role => this.parseRole(role.toString()));
}
this.data = this.parseToolsCollection(this.entity);
});
}
parseToolsCollection(json: any): ToolsCollection {
return {
id: this.util.extract(json, 'Id'),
title: this.util.extract(json, 'Title'),
tools: this.util.extract(json, 'Tools') || [],
restricted: this.isRestricted(json, this.util.extract(json, 'OpenOrRestrictedToolCollection'))
};
}
parseRole(role: string): String {
if (role.includes('[')) {
role = role.slice(0, role.indexOf('['));
}
return role.trim();
}
isRestricted(json: any, openOrRestricted: string): boolean {
let restricted = false;
if (openOrRestricted && openOrRestricted.startsWith('Role based tool')) {
restricted = true;
const requiredRoles = this.util.extract(json, 'Roles') || [];
requiredRoles.forEach(requiredRole => {
if (this.userRoles.includes(requiredRole)) {
restricted = false;
}
});
}
else if (openOrRestricted && openOrRestricted.startsWith('Login required tool')) {
restricted = true;
if (this.isLoggedIn) {
restricted = false;
}
}
return restricted;
}
}