X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fmodules%2Fcreate-group-modal%2Fcreate-group-modal.component.ts;fp=otf-frontend%2Fclient%2Fsrc%2Fapp%2Fshared%2Fmodules%2Fcreate-group-modal%2Fcreate-group-modal.component.ts;h=2d0befc11f2977e9f4b6d7b1a87667d71a1df46d;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-frontend/client/src/app/shared/modules/create-group-modal/create-group-modal.component.ts b/otf-frontend/client/src/app/shared/modules/create-group-modal/create-group-modal.component.ts new file mode 100644 index 0000000..2d0befc --- /dev/null +++ b/otf-frontend/client/src/app/shared/modules/create-group-modal/create-group-modal.component.ts @@ -0,0 +1,131 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +import { Component, OnInit, Inject } from '@angular/core'; +import { MatDialogRef, MAT_DIALOG_DATA, MatSnackBar, MatDialog } from '@angular/material'; +import { GroupService } from 'app/shared/services/group.service'; +import { CookieService } from 'ngx-cookie-service'; +import { UserService } from 'app/shared/services/user.service'; +import { AlertSnackbarComponent } from '../alert-snackbar/alert-snackbar.component'; +import { AlertModalComponent } from 'app/shared/modules/alert-modal/alert-modal.component'; + + +@Component({ + selector: 'app-create-group-modal', + templateUrl: './create-group-modal.component.pug', + styleUrls: ['./create-group-modal.component.scss'] +}) +export class CreateGroupModalComponent implements OnInit { + + constructor(public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public input_data, private userService: UserService, private groupService: GroupService, private cookieService: CookieService, private snack: MatSnackBar, private modal: MatDialog) { } + + public groups; + public newGroup; + public user; + + ngOnInit() { + this.newGroup = {}; + this.user = {}; + this.groups = []; + this.newGroup.groupName = ''; + this.newGroup.parentGroupId = null; + this.user._id = this.userService.getId(); + this.newGroup.ownerId = this.user["_id"]; + //filter list of groups by the Admin permssion from the user + //Also add group onto active dropdown list when this dialog is closed + this.groupService.find({ + $limit: -1 + + }).subscribe((list) => { + //console.log(list); + for(let i in list){ + //console.log(this.user._id + " " + list[i]); + if(this.checkIsAdmin(list[i], this.user._id)){ + this.groups.push(list[i]); + } + } + + }); + + } + + checkIsAdmin(group, userId){ + if(group.members){ + let memberIndex = group.members.findIndex(function(member){return member.userId.toString() == userId.toString()}); + if(memberIndex >= 0){ + if(group.members[memberIndex].roles.includes("admin")){ + return true; + } + } + } + return false; + } + + close(){ + + this.dialogRef.close(null); + } + + createGroup(){ + + //console.log(this.newGroup); + if(this.newGroup.parentGroupId == "None"){ + this.newGroup.parentGroupId = null; + } + + this.newGroup.roles = [{ + roleName: "admin", + permissions: ["management", "write", "delete", "read", "execute"] + }, + { + roleName: "user", + permissions: ["read"] + }, + { + roleName: "developer", + permissions: ["write", "delete", "read", "execute"] + }]; + this.newGroup.members = [{ + userId: this.user._id, + roles: ["admin"] + }]; + this.groupService.create(this.newGroup).subscribe(res => { + + let snackMessage = 'The group ' + this.newGroup.groupName + " has been created!"; + this.snack.openFromComponent(AlertSnackbarComponent, { + duration: 1500, + data: { + message: snackMessage + } + }); + if(res){ + this.dialogRef.close(res) + }else{ + this.close(); + } + }, (error) => { + this.modal.open(AlertModalComponent, { + width: "250px", + data: { + type: "alert", + message: error + } + }); + }); + + } + +}