From 42fe05d7cabccbb55bf79956d6a2ed861b14361a Mon Sep 17 00:00:00 2001 From: Barath Kumar R Date: Fri, 29 Jan 2021 16:02:34 +0530 Subject: [PATCH] Bug 1414 - Is not possible to remove all role-mappings of a user * Able remove the project role mapping Change-Id: I68ec168d18675434faf98b57a34183ac06cafb60 Signed-off-by: Barath Kumar R --- .../project-role/ProjectRoleComponent.html | 20 +- .../project-role/ProjectRoleComponent.ts | 402 +++++++++--------- src/models/MenuModel.ts | 2 +- src/models/UserModel.ts | 1 + src/services/AuthInterceptorService.ts | 10 +- 5 files changed, 237 insertions(+), 198 deletions(-) diff --git a/src/app/users/project-role/ProjectRoleComponent.html b/src/app/users/project-role/ProjectRoleComponent.html index c093f37..c85b768 100644 --- a/src/app/users/project-role/ProjectRoleComponent.html +++ b/src/app/users/project-role/ProjectRoleComponent.html @@ -29,7 +29,9 @@ Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.i {{'PAGE.USERS.ADDMAPPINGS' | translate}} - +
@@ -44,9 +46,19 @@ Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.i formControlName="role_name" id="roles_{{i}}" [ngClass]="{ 'is-invalid': submitted && params.controls.role_name.errors }">
-
- +
+
+
diff --git a/src/app/users/project-role/ProjectRoleComponent.ts b/src/app/users/project-role/ProjectRoleComponent.ts index f5bb772..600cecd 100644 --- a/src/app/users/project-role/ProjectRoleComponent.ts +++ b/src/app/users/project-role/ProjectRoleComponent.ts @@ -37,200 +37,222 @@ import { ProjectRoleMappings, UserDetail, UserRoleMap } from 'UserModel'; * @Component takes ProjectRole.html as template url */ @Component({ - templateUrl: './ProjectRoleComponent.html', - styleUrls: ['./ProjectRoleComponent.scss'] + templateUrl: './ProjectRoleComponent.html', + styleUrls: ['./ProjectRoleComponent.scss'] }) /** Exporting a class @exports ProjectRoleComponent */ export class ProjectRoleComponent implements OnInit { - /** To inject services @public */ - public injector: Injector; + /** To inject services @public */ + public injector: Injector; - /** Instance for active modal service @public */ - public activeModal: NgbActiveModal; - - /** FormGroup user Edit Account added to the form @ html @public */ - public projectRoleForm: FormGroup; - - /** Form submission Add */ - public submitted: boolean = false; - - /** Input contains Modal dialog component Instance @private */ - @Input() public userTitle: string; - - /** Input contains Modal dialog component Instance @private */ - @Input() public userID: string; - - /** Contains user details information @public */ - public userDetails: UserDetail; - - /** Project Role Mapping @public */ - public projectRoleMap: UserRoleMap = {}; - - /** Check the loading results @public */ - public isLoadingResults: boolean = false; - - /** Give the message for the loading @public */ - public message: string = 'PLEASEWAIT'; - - /** Contains project information @public */ - public projects: ProjectData[] = []; - - /** Contains roles information @public */ - public roles: RoleData[] = []; - - /** Instance of the rest service @private */ - private restService: RestService; - - /** FormBuilder instance added to the formBuilder @private */ - private formBuilder: FormBuilder; - - /** Controls the header form @private */ - private headers: HttpHeaders; - - /** Notifier service to popup notification @private */ - private notifierService: NotifierService; - - /** Contains tranlsate instance @private */ - private translateService: TranslateService; - - /** Project Role Form array @private */ - private projectRoleFormArray: FormArray; - - /** Holds all project details @private */ - private projectService: ProjectService; - - constructor(injector: Injector) { - this.injector = injector; - this.formBuilder = this.injector.get(FormBuilder); - this.restService = this.injector.get(RestService); - this.activeModal = this.injector.get(NgbActiveModal); - this.notifierService = this.injector.get(NotifierService); - this.translateService = this.injector.get(TranslateService); - this.projectService = this.injector.get(ProjectService); - this.initializeForm(); - } - - /** Generate primitive params @public */ - get projectRoleParamsBuilder(): FormGroup { - return this.formBuilder.group({ - project_name: [null, [Validators.required]], - role_name: [null, [Validators.required]] - }); - } - - /** Lifecyle Hooks the trigger before component is instantiate @public */ - public ngOnInit(): void { - this.headers = new HttpHeaders({ - 'Content-Type': 'application/json', - Accept: 'application/json', - 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' - }); - this.getProjects(); - this.generateData(); - } - - /** convenience getter for easy access to form fields */ - get f(): FormGroup['controls'] { return this.projectRoleForm.controls; } - - /** Initializing Form Action @public */ - public initializeForm(): void { - this.projectRoleForm = this.formBuilder.group({ - project_role_mappings: this.formBuilder.array([]) - }); - } - - /** Handle FormArray Controls @public */ - public getControls(): AbstractControl[] { - // tslint:disable-next-line:no-backbone-get-set-outside-model - return (this.projectRoleForm.get('project_role_mappings') as FormArray).controls; - } - - /** Fetching the data from server to Load in the smarttable @public */ - public generateData(): void { - if (this.userID !== '') { - this.isLoadingResults = true; - this.restService.getResource(environment.USERS_URL + '/' + this.userID).subscribe((userDetails: UserDetail) => { - this.userDetails = userDetails; - this.loadMapping(); - this.isLoadingResults = false; - }, (error: ERRORDATA) => { - this.isLoadingResults = false; - this.restService.handleError(error, 'get'); - }); + /** Instance for active modal service @public */ + public activeModal: NgbActiveModal; + + /** FormGroup user Edit Account added to the form @ html @public */ + public projectRoleForm: FormGroup; + + /** Form submission Add */ + public submitted: boolean = false; + + /** Input contains Modal dialog component Instance @private */ + @Input() public userTitle: string; + + /** Input contains Modal dialog component Instance @private */ + @Input() public userID: string; + + /** Contains user details information @public */ + public userDetails: UserDetail; + + /** Project Role Mapping @public */ + public projectRoleMap: UserRoleMap = {}; + + /** Check the loading results @public */ + public isLoadingResults: boolean = false; + + /** Give the message for the loading @public */ + public message: string = 'PLEASEWAIT'; + + /** Contains project information @public */ + public projects: ProjectData[] = []; + + /** Contains roles information @public */ + public roles: RoleData[] = []; + + /** Instance of the rest service @private */ + private restService: RestService; + + /** FormBuilder instance added to the formBuilder @private */ + private formBuilder: FormBuilder; + + /** Controls the header form @private */ + private headers: HttpHeaders; + + /** Notifier service to popup notification @private */ + private notifierService: NotifierService; + + /** Contains tranlsate instance @private */ + private translateService: TranslateService; + + /** Project Role Form array @private */ + private projectRoleFormArray: FormArray; + + /** Holds all project details @private */ + private projectService: ProjectService; + + constructor(injector: Injector) { + this.injector = injector; + this.formBuilder = this.injector.get(FormBuilder); + this.restService = this.injector.get(RestService); + this.activeModal = this.injector.get(NgbActiveModal); + this.notifierService = this.injector.get(NotifierService); + this.translateService = this.injector.get(TranslateService); + this.projectService = this.injector.get(ProjectService); + } + + /** Generate primitive params @public */ + get projectRoleParamsBuilder(): FormGroup { + return this.formBuilder.group({ + project_name: [null, [Validators.required]], + role_name: [null, [Validators.required]] + }); } - } - /** Fetching the projects information @public */ - public getProjects(): void { - this.isLoadingResults = true; - this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectData[]) => { - this.projects = projectsData; - this.getRoles(); - }, (error: ERRORDATA) => { - this.isLoadingResults = false; - this.restService.handleError(error, 'get'); - }); - } - - /** Fetching the Roles information @public */ - public getRoles(): void { - this.restService.getResource(environment.ROLES_URL).subscribe((rolesData: RoleData[]) => { - this.roles = rolesData; - this.isLoadingResults = false; - }, (error: ERRORDATA) => { - this.isLoadingResults = false; - this.restService.handleError(error, 'get'); - }); - } - - /** Set all roles and project values to the form @public */ - public loadMapping(): void { - this.userDetails.project_role_mappings.forEach((data: ProjectRoleMappings) => { - // tslint:disable-next-line:no-backbone-get-set-outside-model - this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; - this.projectRoleFormArray.push(this.projectRoleParamsBuilder); - }); - this.projectRoleForm.patchValue(this.userDetails); - } - - /** Remove project and roles from the list @public */ - public removeMapping(index: number): void { - this.projectRoleFormArray.removeAt(index); - } - - /** Submit project and roles @public */ - public addProjectRole(): void { - this.submitted = true; - const modalData: MODALCLOSERESPONSEDATA = { - message: 'Done' - }; - if (this.projectRoleForm.invalid) { return; } - const apiURLHeader: APIURLHEADER = { - url: environment.USERS_URL + '/' + this.userID - }; - this.projectRoleMap.project_role_mappings = []; - this.projectRoleForm.value.project_role_mappings.forEach((res: ProjectRoleMappings) => { - this.projectRoleMap.project_role_mappings.push({ project: res.project_name, role: res.role_name }); - }); - if (this.projectRoleMap.project_role_mappings.length !== 0) { - this.isLoadingResults = true; - this.restService.patchResource(apiURLHeader, this.projectRoleMap).subscribe((result: {}) => { - this.isLoadingResults = false; - this.activeModal.close(modalData); - this.projectService.setHeaderProjects(); - this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY')); - }, (error: ERRORDATA) => { - this.isLoadingResults = false; - this.restService.handleError(error, 'patch'); - }); - } else { - this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.EDITPROJECTROLEERROR')); + + /** convenience getter for easy access to form fields */ + get f(): FormGroup['controls'] { return this.projectRoleForm.controls; } + + /** Lifecyle Hooks the trigger before component is instantiate @public */ + public ngOnInit(): void { + this.headers = new HttpHeaders({ + 'Content-Type': 'application/json', + Accept: 'application/json', + 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' + }); + this.initializeForm(); + this.getProjects(); + this.generateData(); + } + + /** Initializing Form Action @public */ + public initializeForm(): void { + this.projectRoleForm = this.formBuilder.group({ + project_role_mappings: this.formBuilder.array([]) + }); + } + + /** Handle FormArray Controls @public */ + public getControls(): AbstractControl[] { + // tslint:disable-next-line:no-backbone-get-set-outside-model + return (this.projectRoleForm.get('project_role_mappings') as FormArray).controls; + } + + /** Fetching the data from server to Load in the smarttable @public */ + public generateData(): void { + if (this.userID !== '') { + this.isLoadingResults = true; + this.restService.getResource(environment.USERS_URL + '/' + this.userID).subscribe((userDetails: UserDetail): void => { + this.userDetails = userDetails; + this.loadMapping(); + this.isLoadingResults = false; + }, (error: ERRORDATA): void => { + this.isLoadingResults = false; + this.restService.handleError(error, 'get'); + }); + } + } + /** Fetching the projects information @public */ + public getProjects(): void { + this.isLoadingResults = true; + this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectData[]): void => { + this.projects = projectsData; + this.getRoles(); + }, (error: ERRORDATA): void => { + this.isLoadingResults = false; + this.restService.handleError(error, 'get'); + }); + } + + /** Fetching the Roles information @public */ + public getRoles(): void { + this.restService.getResource(environment.ROLES_URL).subscribe((rolesData: RoleData[]): void => { + this.roles = rolesData; + this.isLoadingResults = false; + }, (error: ERRORDATA): void => { + this.isLoadingResults = false; + this.restService.handleError(error, 'get'); + }); + } + + /** Set all roles and project values to the form @public */ + public loadMapping(): void { + this.userDetails.project_role_mappings.forEach((data: ProjectRoleMappings): void => { + // tslint:disable-next-line:no-backbone-get-set-outside-model + this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; + this.projectRoleFormArray.push(this.projectRoleParamsBuilder); + }); + this.projectRoleForm.patchValue(this.userDetails); + } + + /** Remove project and roles from the list @public */ + public removeMapping(index: number): void { + this.projectRoleFormArray.removeAt(index); + } + + /** Submit project and roles @public */ + public addProjectRole(): void { + this.submitted = true; + const modalData: MODALCLOSERESPONSEDATA = { + message: 'Done' + }; + if (this.projectRoleForm.invalid) { return; } + const apiURLHeader: APIURLHEADER = { + url: environment.USERS_URL + '/' + this.userID + }; + this.projectRoleMap.project_role_mappings = []; + this.projectRoleForm.value.project_role_mappings.forEach((res: ProjectRoleMappings): void => { + this.projectRoleMap.project_role_mappings.push({ project: res.project_name, role: res.role_name }); + }); + if (this.projectRoleMap.project_role_mappings.length !== 0) { + this.isLoadingResults = true; + this.restService.patchResource(apiURLHeader, this.projectRoleMap).subscribe((result: {}): void => { + this.isLoadingResults = false; + this.activeModal.close(modalData); + this.projectService.setHeaderProjects(); + this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY')); + }, (error: ERRORDATA): void => { + this.isLoadingResults = false; + this.restService.handleError(error, 'patch'); + }); + } else { + this.notifierService.notify('error', this.translateService.instant('PAGE.USERS.EDITPROJECTROLEERROR')); + } + } + + /** Add extra mapping and set empty project and roles @public */ + public addMapping(): void { + // tslint:disable-next-line:no-backbone-get-set-outside-model + this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; + this.projectRoleFormArray.push(this.projectRoleParamsBuilder); + } + + /** Remove project and roles for the user @public */ + public deleteProjectAndRoleMapping(getProjectRoles: ProjectRoleMappings): void { + const modalData: MODALCLOSERESPONSEDATA = { + message: 'Done' + }; + const removeProjectRole: UserRoleMap = { remove_project_role_mappings: [] }; + removeProjectRole.remove_project_role_mappings = [{ project: getProjectRoles.project_name, role: getProjectRoles.role_name }]; + const apiURLHeader: APIURLHEADER = { + url: environment.USERS_URL + '/' + this.userID + }; + this.isLoadingResults = true; + this.restService.patchResource(apiURLHeader, removeProjectRole).subscribe((result: {}): void => { + this.isLoadingResults = false; + this.activeModal.close(modalData); + this.projectService.setHeaderProjects(); + this.notifierService.notify('success', this.translateService.instant('PAGE.USERS.EDITEDSUCCESSFULLY')); + }, (error: ERRORDATA): void => { + this.isLoadingResults = false; + this.restService.handleError(error, 'patch'); + }); } - } - - /** Add extra mapping and set empty project and roles @public */ - public addMapping(): void { - // tslint:disable-next-line:no-backbone-get-set-outside-model - this.projectRoleFormArray = this.projectRoleForm.get('project_role_mappings') as FormArray; - this.projectRoleFormArray.push(this.projectRoleParamsBuilder); - } } diff --git a/src/models/MenuModel.ts b/src/models/MenuModel.ts index 0b49db8..deb9654 100644 --- a/src/models/MenuModel.ts +++ b/src/models/MenuModel.ts @@ -247,7 +247,7 @@ export const MENU_ITEMS: MENUITEMS[] = [ liClass: 'round-edge-bottom-3', anchorTagClass: 'link round-edge-bottom-3 individual', clickFunction: 'nosubmenu', - routerLink: '/roles', + routerLink: '/roles/details', routerLinkActive: ['parentactive'], routerLinkActiveOptions: true, icon: 'fas fa-user-tag', diff --git a/src/models/UserModel.ts b/src/models/UserModel.ts index 83dd08b..64626c8 100644 --- a/src/models/UserModel.ts +++ b/src/models/UserModel.ts @@ -42,6 +42,7 @@ export interface UserDetail { /** Interface for user role mappings */ export interface UserRoleMap { project_role_mappings?: ProjectRoleMappings[]; + remove_project_role_mappings?: ProjectRoleMappings[]; } /** Interface for Admin */ diff --git a/src/services/AuthInterceptorService.ts b/src/services/AuthInterceptorService.ts index 4a66482..8c5a9d7 100644 --- a/src/services/AuthInterceptorService.ts +++ b/src/services/AuthInterceptorService.ts @@ -93,8 +93,12 @@ export class AuthInterceptorService implements HttpInterceptor { }); } else { this.clonedReq = req.clone({ - setHeaders: { Authorization: 'Bearer ' + idToken, 'Content-Type': 'charset=UTF-8', - 'Cache-Control': 'no-cache', Pragma: 'no-cache' } + setHeaders: { + Authorization: 'Bearer ' + idToken, + 'Content-Type': 'charset=UTF-8', + 'Cache-Control': 'no-cache', + Pragma: 'no-cache' + } }); } } @@ -119,7 +123,7 @@ export class AuthInterceptorService implements HttpInterceptor { /** Method to handle 401, 403 & 502 error */ private handleError(err: HttpErrorResponse): void { - if (err.error.detail !== 'Access denied: lack of permissions.') { + if (err.error.detail !== 'Access denied: lack of permissions.' && err.error.detail !== 'You cannot remove system_admin role from admin user') { this.notifierService.hideAll(); this.authService.logoutResponse(); if (this.authService.handle401) { -- 2.25.1