Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

feat(permission): add methods for role management and audit RPT logic #132

Merged
merged 6 commits into from
Nov 30, 2018

Conversation

rohitkrai03
Copy link
Contributor

As part of the work for 890 and 889 we need

  • A way to assign user with a specific role.
  • Get all the users that are assigned a particular role in a space.
  • Call the Audit RPT API if the current token is not valid RPT or if it does not contain permission information for a particular resource. The Audit endpoint will then return an RPT token with required permission information of a user for a particular resource.

This PR adds the functionality into PermissionService -

  • assignRole - Assign a specific role to a one or more users in a resource such as space.
  • getUsersByRole - Get all the users who have a speicific role.
  • auditRPT - Call the Audit RPT API to get a new RPT token if the token if not an RPT or if the token doesn't contain permission info for a particular resource.

Also,

  • The logic for getDecodedToken is changed to include a check for valid RPT. If the token is not valid (In case of accessing the resource for the first time when instead of RPT the localStorage will be having auth_token).

  • Logic for getPermission will also include a call to auditRPT if current token does not include permission information for the required resource. This case may arise when the scope of user has been changed and the RPT is not updated with correct permissions.

  • Added more tests to include all the new scenarios.

@rohitkrai03 rohitkrai03 changed the title feat(permission): add methods for role management and audit RPT logic [WIP] feat(permission): add methods for role management and audit RPT logic Nov 25, 2018
@rohitkrai03 rohitkrai03 removed the request for review from christianvogt November 25, 2018 14:15
@rohitkrai03 rohitkrai03 changed the title [WIP] feat(permission): add methods for role management and audit RPT logic feat(permission): add methods for role management and audit RPT logic Nov 28, 2018
getUsersByRole(resourceId: string, roleName: string): Observable<UserRoleData[]> {
const url = `${this.authApi}resources/${resourceId}/roles/${roleName}`;
return this.http
.get(url, { headers: this.headers })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should parameterize the get function instead of adding type info to the map call.
.get<{ data: UserRoleData[] }>(url, { headers: this.headers })
Then your map becomes:
map(res => res.data)

const url = `${this.authApi}token/audit`;
const params = new HttpParams().set('resource_id', resourceId);
return this.http
.post(url, '', { headers: this.headers, params })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add type parameters to the the post call instead of in the map.

return permissions ? permissions.scopes : [];
getAllScopes(resourceId: string): Observable<string[]> {
return this.getPermission(resourceId).pipe(
map((permission: Permission) => permission.scopes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permission may be undefined here.

return permissions ? permissions.scopes.includes(scope) : false;
hasScope(resourceId: string, scope: string): Observable<boolean> {
return this.getPermission(resourceId).pipe(
map((permission: Permission) => permission.scopes.includes(scope))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

permission may be undefined here.

private getDecodedToken(): any {
const token = localStorage.getItem('auth_token');
return token ? this.jwtHelper.decodeToken(token) : '';
assignRole(resourceId: string, roleName: string, userIDs: Array<string>): Observable<any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this return Observable<any> and not an observable of a specific value or undefined | null ?

providers: [
PermissionService
PermissionService,
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need interceptors for these tests.

useClass: AuthInterceptor,
multi: true
},
{ provide: WIT_API_PROXY, useValue: 'http://wit.example.com/'},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the WIT_API_PROXY or SSO_API_URL for these tests.

@@ -1,36 +1,183 @@
import { TestBed } from '@angular/core/testing';
import { PermissionService, Permission } from './permission.service';
import { TestBed, async } from '@angular/core/testing';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async import is unused.

@@ -47,7 +47,7 @@ export class PermissionService {
*/
hasScope(resourceId: string, scope: string): Observable<boolean> {
return this.getPermission(resourceId).pipe(
map((permission: Permission) => permission.scopes.includes(scope))
map((permission: Permission | undefined) => permission.scopes.includes(scope))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a problem because you use permission without a null check. The old code use to return false if permission was null.

@christianvogt
Copy link
Contributor

@rohitkrai03 whenever a test performs assertions in a callback (ie. a subscribe), you need to ensure that the callback is executed. Otherwise the test can still pass even though your assertions weren't executed. Most tests have been implemented using done to notify when the test completes.

@christianvogt christianvogt merged commit abc6eb3 into fabric8-ui:master Nov 30, 2018
@fabric8cd
Copy link

🎉 This PR is included in version 2.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants