--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+import { Component, OnInit } from '@angular/core';\r
+import { routerLeftTransition } from 'app/router.animations';\r
+import { ActivatedRoute } from '@angular/router';\r
+import { GroupService } from 'app/shared/services/group.service';\r
+import { CookieService } from 'ngx-cookie-service';\r
+import { MatTableDataSource } from '@angular/material/table';\r
+import { MatDialog } from '@angular/material';\r
+import {AlertModalComponent} from '../../../shared/modules/alert-modal/alert-modal.component';\r
+import { NEXT } from '@angular/core/src/render3/interfaces/view';\r
+import { convertPropertyBindingBuiltins } from '@angular/compiler/src/compiler_util/expression_converter';\r
+import { extractDirectiveDef } from '@angular/core/src/render3/definition';\r
+import { take } from 'rxjs/operators';\r
+\r
+\r
+\r
+\r
+\r
+@Component({\r
+ selector: 'app-manage-group-roles',\r
+ templateUrl: './manage-group-roles.component.pug',\r
+ styleUrls: ['./manage-group-roles.component.scss'],\r
+ animations: [routerLeftTransition()]\r
+})\r
+\r
+export class ManageGroupRolesComponent implements OnInit {\r
+\r
+\r
+ public selectedGroup;\r
+ public groupRoles;\r
+ public roleName = null;\r
+ public loading = false;\r
+ public groupPermissions;\r
+ public element = {};\r
+ public groupName;\r
+\r
+ public dataSource;\r
+ public displayedColumns;\r
+ public readPermission = false;\r
+ public writePermission = false;\r
+ public executePermission = false;\r
+ public deletePermission = false;\r
+ public managementPermission = false;\r
+\r
+ \r
+\r
+\r
+\r
+ constructor(\r
+ public _groups: GroupService,\r
+ private cookie: CookieService,\r
+ private modal: MatDialog,\r
+ private ResponseMatDialog: MatDialog\r
+ \r
+ ) { \r
+\r
+ \r
+ }\r
+\r
+ ngOnInit() {\r
+\r
+ this.setComponentData(this._groups.getGroup());\r
+ this._groups.groupChange().subscribe(group => {\r
+ this.setComponentData(group);\r
+\r
+ });\r
+\r
+\r
+ }\r
+\r
+ setComponentData(group) {\r
+ if(!group){\r
+ return;\r
+ }\r
+ this.groupName = group.groupName;\r
+ this.loading = true;\r
+ this.dataSource = new MatTableDataSource();\r
+ this._groups.find({ \r
+ $limit: -1,\r
+ _id: group['_id'], \r
+ $select: ['_id', 'roles']\r
+ }).subscribe((res) => {\r
+\r
+ this.selectedGroup = res[0];\r
+ this.groupRoles = res[0].roles;\r
+\r
+ //If current group does not have any roles\r
+ if ( (this.groupRoles == null) || (this.groupRoles.length < 1))\r
+ {\r
+ this.groupRoles = [\r
+ {roleName: "admin", permissions: "read, write, execute, delete, management"},\r
+ {roleName: "user", permissions: "read"},\r
+ {roleName: "developer", permissions: "read, write, execute, delete"}\r
+ ];\r
+ \r
+ }\r
+\r
+\r
+ for (let i = 0; i < this.groupRoles.length; i++){\r
+ this.groupRoles[i].readPermission = false;\r
+ this.groupRoles[i].writePermission = false;\r
+ this.groupRoles[i].executePermission = false;\r
+ this.groupRoles[i].deletePermission = false;\r
+ this.groupRoles[i].managementPermission = false;\r
+ if (this.groupRoles[i].permissions.includes('read')){\r
+ this.groupRoles[i].readPermission = true;\r
+ }\r
+ if (this.groupRoles[i].permissions.includes('write')){\r
+ this.groupRoles[i].writePermission = true;\r
+ }\r
+ if (this.groupRoles[i].permissions.includes('execute')){\r
+ this.groupRoles[i].executePermission = true;\r
+ }\r
+ if (this.groupRoles[i].permissions.includes('delete')){\r
+ this.groupRoles[i].deletePermission = true;\r
+ }\r
+ if (this.groupRoles[i].permissions.includes('management')){\r
+ this.groupRoles[i].managementPermission = true;\r
+ }\r
+ }\r
+ \r
+ this.dataSource.data = this.groupRoles;\r
+ this.loading = false;\r
+ this.update();\r
+\r
+ \r
+\r
+\r
+ })\r
+\r
+ this.displayedColumns = ['roleName', 'read', 'write', 'execute', 'delete', 'management', 'actions']\r
+\r
+ }\r
+\r
+\r
+ async create(){\r
+ \r
+ for (let i = 0; i < this.groupRoles.length; i++){\r
+ if (this.groupRoles[i].roleName == this.roleName){\r
+ this.sendFeedbackAlert('warning', 'Please do not add a duplicate role name.');\r
+ return;\r
+ }\r
+ }\r
+ \r
+ this.groupRoles.push({roleName: this.roleName, readPermission: true, writePermission: false, executePermission: false, deletePermission: false, managementPermission: false});\r
+ await this.update();\r
+ this.setComponentData(this._groups.getGroup());\r
+ \r
+\r
+ }\r
+\r
+ async update(){\r
+ \r
+ \r
+ for (let i = 0; i < this.groupRoles.length; i++) {\r
+ this.groupRoles[i].permissions = [];\r
+ if(this.groupRoles[i].readPermission){\r
+ this.groupRoles[i].permissions.push('read');\r
+ }\r
+ if(this.groupRoles[i].writePermission){\r
+ this.groupRoles[i].permissions.push('write');\r
+ }\r
+ if(this.groupRoles[i].executePermission){\r
+ this.groupRoles[i].permissions.push('execute');\r
+ }\r
+ if(this.groupRoles[i].deletePermission){\r
+ this.groupRoles[i].permissions.push('delete');\r
+ }\r
+ if(this.groupRoles[i].managementPermission){\r
+ this.groupRoles[i].permissions.push('management');\r
+ }\r
+\r
+ }\r
+ \r
+ this.groupPermissions = this.groupRoles.map(({ roleName, permissions }) => ({roleName, permissions}));\r
+ \r
+ let groupPatch = {\r
+ '_id': this.selectedGroup._id,\r
+ 'roles': this.groupPermissions\r
+ };\r
+ //console.log(groupPatch);\r
+ await this._groups.patch(groupPatch).pipe(take(1)).toPromise();\r
+\r
+ \r
+\r
+ }\r
+\r
+ async deleteRole(element){\r
+\r
+ for (let i = 0; i < this.groupRoles.length; i++){\r
+ if (this.groupRoles[i].roleName == element.roleName){\r
+ this.groupRoles.splice(i, 1);\r
+ break;\r
+ }\r
+ }\r
+ await this.update();\r
+ this.setComponentData(this._groups.getGroup());\r
+\r
+\r
+\r
+\r
+ }\r
+\r
+ public sendFeedbackAlert(type: string, message: string) {\r
+ this.ResponseMatDialog.open(AlertModalComponent, {\r
+ width: '250px',\r
+ data: {\r
+ type: type,\r
+ message: message\r
+ }\r
+ });\r
+ }\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+\r
+ \r
+\r
+\r
+\r
+\r
+\r
+\r
+}\r