1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 import { Component, OnInit } from '@angular/core';
\r
18 import { routerLeftTransition } from 'app/router.animations';
\r
19 import { ActivatedRoute } from '@angular/router';
\r
20 import { GroupService } from 'app/shared/services/group.service';
\r
21 import { CookieService } from 'ngx-cookie-service';
\r
22 import { MatTableDataSource } from '@angular/material/table';
\r
23 import { MatDialog } from '@angular/material';
\r
24 import {AlertModalComponent} from '../../../shared/modules/alert-modal/alert-modal.component';
\r
25 import { NEXT } from '@angular/core/src/render3/interfaces/view';
\r
26 import { convertPropertyBindingBuiltins } from '@angular/compiler/src/compiler_util/expression_converter';
\r
27 import { extractDirectiveDef } from '@angular/core/src/render3/definition';
\r
28 import { take } from 'rxjs/operators';
\r
35 selector: 'app-manage-group-roles',
\r
36 templateUrl: './manage-group-roles.component.pug',
\r
37 styleUrls: ['./manage-group-roles.component.scss'],
\r
38 animations: [routerLeftTransition()]
\r
41 export class ManageGroupRolesComponent implements OnInit {
\r
44 public selectedGroup;
\r
46 public roleName = null;
\r
47 public loading = false;
\r
48 public groupPermissions;
\r
49 public element = {};
\r
53 public displayedColumns;
\r
54 public readPermission = false;
\r
55 public writePermission = false;
\r
56 public executePermission = false;
\r
57 public deletePermission = false;
\r
58 public managementPermission = false;
\r
65 public _groups: GroupService,
\r
66 private cookie: CookieService,
\r
67 private modal: MatDialog,
\r
68 private ResponseMatDialog: MatDialog
\r
77 this.setComponentData(this._groups.getGroup());
\r
78 this._groups.groupChange().subscribe(group => {
\r
79 this.setComponentData(group);
\r
86 setComponentData(group) {
\r
90 this.groupName = group.groupName;
\r
91 this.loading = true;
\r
92 this.dataSource = new MatTableDataSource();
\r
93 this._groups.find({
\r
96 $select: ['_id', 'roles']
\r
97 }).subscribe((res) => {
\r
99 this.selectedGroup = res[0];
\r
100 this.groupRoles = res[0].roles;
\r
102 //If current group does not have any roles
\r
103 if ( (this.groupRoles == null) || (this.groupRoles.length < 1))
\r
105 this.groupRoles = [
\r
106 {roleName: "admin", permissions: "read, write, execute, delete, management"},
\r
107 {roleName: "user", permissions: "read"},
\r
108 {roleName: "developer", permissions: "read, write, execute, delete"}
\r
114 for (let i = 0; i < this.groupRoles.length; i++){
\r
115 this.groupRoles[i].readPermission = false;
\r
116 this.groupRoles[i].writePermission = false;
\r
117 this.groupRoles[i].executePermission = false;
\r
118 this.groupRoles[i].deletePermission = false;
\r
119 this.groupRoles[i].managementPermission = false;
\r
120 if (this.groupRoles[i].permissions.includes('read')){
\r
121 this.groupRoles[i].readPermission = true;
\r
123 if (this.groupRoles[i].permissions.includes('write')){
\r
124 this.groupRoles[i].writePermission = true;
\r
126 if (this.groupRoles[i].permissions.includes('execute')){
\r
127 this.groupRoles[i].executePermission = true;
\r
129 if (this.groupRoles[i].permissions.includes('delete')){
\r
130 this.groupRoles[i].deletePermission = true;
\r
132 if (this.groupRoles[i].permissions.includes('management')){
\r
133 this.groupRoles[i].managementPermission = true;
\r
137 this.dataSource.data = this.groupRoles;
\r
138 this.loading = false;
\r
146 this.displayedColumns = ['roleName', 'read', 'write', 'execute', 'delete', 'management', 'actions']
\r
153 for (let i = 0; i < this.groupRoles.length; i++){
\r
154 if (this.groupRoles[i].roleName == this.roleName){
\r
155 this.sendFeedbackAlert('warning', 'Please do not add a duplicate role name.');
\r
160 this.groupRoles.push({roleName: this.roleName, readPermission: true, writePermission: false, executePermission: false, deletePermission: false, managementPermission: false});
\r
161 await this.update();
\r
162 this.setComponentData(this._groups.getGroup());
\r
170 for (let i = 0; i < this.groupRoles.length; i++) {
\r
171 this.groupRoles[i].permissions = [];
\r
172 if(this.groupRoles[i].readPermission){
\r
173 this.groupRoles[i].permissions.push('read');
\r
175 if(this.groupRoles[i].writePermission){
\r
176 this.groupRoles[i].permissions.push('write');
\r
178 if(this.groupRoles[i].executePermission){
\r
179 this.groupRoles[i].permissions.push('execute');
\r
181 if(this.groupRoles[i].deletePermission){
\r
182 this.groupRoles[i].permissions.push('delete');
\r
184 if(this.groupRoles[i].managementPermission){
\r
185 this.groupRoles[i].permissions.push('management');
\r
190 this.groupPermissions = this.groupRoles.map(({ roleName, permissions }) => ({roleName, permissions}));
\r
193 '_id': this.selectedGroup._id,
\r
194 'roles': this.groupPermissions
\r
196 //console.log(groupPatch);
\r
197 await this._groups.patch(groupPatch).pipe(take(1)).toPromise();
\r
203 async deleteRole(element){
\r
205 for (let i = 0; i < this.groupRoles.length; i++){
\r
206 if (this.groupRoles[i].roleName == element.roleName){
\r
207 this.groupRoles.splice(i, 1);
\r
211 await this.update();
\r
212 this.setComponentData(this._groups.getGroup());
\r
219 public sendFeedbackAlert(type: string, message: string) {
\r
220 this.ResponseMatDialog.open(AlertModalComponent, {
\r