Skip to content

Commit

Permalink
Add has space role in any space in any cf org
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-cox committed May 22, 2018
1 parent 6763413 commit b5ec5c2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
16 changes: 14 additions & 2 deletions src/frontend/app/core/current-user-permissions.checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../store/selectors/current-user-roles-permissions-selectors/role.selectors';
import { endpointsRegisteredEntitiesSelector } from '../store/selectors/endpoint.selectors';
import { APIResource } from '../store/types/api.types';
import { IOrgRoleState, ISpaceRoleState, ISpacesRoleState } from '../store/types/current-user-roles.types';
import { IOrgRoleState, ISpaceRoleState, ISpacesRoleState, IOrgsRoleState } from '../store/types/current-user-roles.types';
import { IFeatureFlag } from './cf-api.types';
import {
PermissionConfig,
Expand All @@ -38,13 +38,15 @@ export enum CHECKER_GROUPS {

export type IConfigGroup = PermissionConfig[];
export class CurrentUserPermissionsChecker {
static readonly ALL_ORGS = 'PERMISSIONS__ALL_ORGS_PLEASE';
static readonly ALL_SPACES = 'PERMISSIONS__ALL_SPACES_PLEASE';
constructor(private store: Store<AppState>) { }
public check(
type: PermissionTypes,
permission: PermissionValues,
endpointGuid?: string,
orgOrSpaceGuid?: string,
allSpacesWithinCfOrgs = false,
allSpacesWithinOrg = false
) {
if (type === PermissionTypes.STRATOS) {
Expand All @@ -69,6 +71,9 @@ export class CurrentUserPermissionsChecker {
filter(state => !!state),
map(state => {
const permissionString = permission as PermissionStrings;
if (allSpacesWithinCfOrgs) {
return this.checkAllSpacesInCfOrgs(state[PermissionTypes.ORGANIZATION], state[PermissionTypes.SPACE], permissionString);
}
if (allSpacesWithinOrg) {
const orgOrSpaceState = state[PermissionTypes.ORGANIZATION][orgOrSpaceGuid];
const spaceState = state[PermissionTypes.SPACE];
Expand Down Expand Up @@ -104,6 +109,12 @@ export class CurrentUserPermissionsChecker {
}
}

private checkAllSpacesInCfOrgs(orgStates: IOrgsRoleState, spaceStates: ISpacesRoleState, permission: PermissionStrings) {
return Object.values(orgStates).map(orgState => {
return this.checkAllSpacesInOrg(orgState, spaceStates, permission);
}).some(check => check);
}

private checkAllSpacesInOrg(orgState: IOrgRoleState, endpointSpaces: ISpacesRoleState, permission: PermissionStrings) {
return orgState.spaceGuids.map(spaceGuid => {
const space = endpointSpaces[spaceGuid];
Expand Down Expand Up @@ -175,11 +186,12 @@ export class CurrentUserPermissionsChecker {

public getCfCheck(config: PermissionConfig, endpointGuid?: string, orgOrSpaceGuid?: string, spaceGuid?: string): Observable<boolean> {
const { type, permission } = config;
const checkOrgsSpaces = orgOrSpaceGuid === CurrentUserPermissionsChecker.ALL_ORGS;
const checkAllSpaces = spaceGuid === CurrentUserPermissionsChecker.ALL_SPACES;
const actualGuid = type === PermissionTypes.SPACE && spaceGuid && !checkAllSpaces ? spaceGuid : orgOrSpaceGuid;
const cfPermissions = permission as PermissionStrings;
if (type === PermissionTypes.ENDPOINT || (endpointGuid && actualGuid)) {
return this.check(type, cfPermissions, endpointGuid, actualGuid, checkAllSpaces);
return this.check(type, cfPermissions, endpointGuid, actualGuid, checkOrgsSpaces, checkAllSpaces);
} else if (!actualGuid) {
const endpointGuids$ = this.getEndpointGuidObservable(endpointGuid);
return endpointGuids$.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ <h4>Space Commands</h4>
<app-cli-command name="List" msg="To list the spaces in the targeted org:" syntax="cf spaces">
</app-cli-command>

<!-- TODO: RC Can I do this action in a particular org or any space within that org -->
<app-cli-command *appUserPermission="permsSpaceEdit;endpointGuid:activeRouteCfOrgSpace.cfGuid;organizationGuid:activeRouteCfOrgSpace.orgGuid;spaceGuid:spaceGuid" name="Rename" msg="To rename a space:" syntax="cf rename-space {{context.spaceName || '[space name]'}} [name]">
<app-cli-command *appUserPermission="permsSpaceEdit;endpointGuid:activeRouteCfOrgSpace.cfGuid;organizationGuid:orgGuid;spaceGuid:spaceGuid" name="Rename" msg="To rename a space:" syntax="cf rename-space {{context.spaceName || '[space name]'}} [name]">
</app-cli-command>

<app-cli-command name="Information" msg="To fetch space information:" syntax="cf space {{context.spaceName || '[space name]'}}">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import { Component, OnInit, ViewEncapsulation, Optional } from '@angular/core';
import { AppState } from '../../../store/app-state';
import { Component, OnInit, Optional } from '@angular/core';
import { Store } from '@ngrx/store';
import { getPreviousRoutingState, RoutingEvent } from '../../../store/types/routing.type';
import { first, map, filter } from 'rxjs/operators';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
import { combineLatest } from 'rxjs/observable/combineLatest';
import { first, map } from 'rxjs/operators';

import { IOrganization, ISpace } from '../../../core/cf-api.types';
import { CurrentUserPermissionsChecker } from '../../../core/current-user-permissions.checker';
import { CurrentUserPermissions } from '../../../core/current-user-permissions.config';
import { CFAppCLIInfoContext } from '../../../shared/components/cli-info/cli-info.component';
import { IHeaderBreadcrumb } from '../../../shared/components/page-header/page-header.types';
import { RouterNav } from '../../../store/actions/router.actions';
import { ActivatedRoute } from '@angular/router';
import { EntityServiceFactory } from '../../../core/entity-service-factory.service';
import { AppState } from '../../../store/app-state';
import { APIResource, EntityInfo } from '../../../store/types/api.types';
import { endpointSchemaKey, entityFactory } from '../../../store/helpers/entity-factory';
import { GetAllEndpoints } from '../../../store/actions/endpoint.actions';
import { combineLatest } from 'rxjs/observable/combineLatest';
import { EndpointModel } from '../../../store/types/endpoint.types';
import { getPreviousRoutingState } from '../../../store/types/routing.type';
import { getFullEndpointApiUrl } from '../../endpoints/endpoint-helpers';
import { Observable } from 'rxjs/Observable';
import { IApp, IOrganization, ISpace } from '../../../core/cf-api.types';
import { IHeaderBreadcrumb } from '../../../shared/components/page-header/page-header.types';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { CFAppCLIInfoContext } from '../../../shared/components/cli-info/cli-info.component';
import { ActiveRouteCfOrgSpace } from '../cf-page.types';
import { getActiveRouteCfOrgSpaceProvider } from '../cf.helpers';
import { CloudFoundryEndpointService } from '../services/cloud-foundry-endpoint.service';
import { CloudFoundryOrganizationService } from '../services/cloud-foundry-organization.service';
import { EndpointModel } from '../../../store/types/endpoint.types';
import { CloudFoundrySpaceService } from '../services/cloud-foundry-space.service';
import { getActiveRouteCfOrgSpaceProvider } from '../cf.helpers';
import { CfUserService } from '../../../shared/data-services/cf-user.service';
import { CurrentUserPermissions } from '../../../core/current-user-permissions.config';
import { CurrentUserPermissionsChecker } from '../../../core/current-user-permissions.checker';

@Component({
selector: 'app-cli-info-cloud-foundry',
Expand All @@ -42,6 +38,7 @@ export class CliInfoCloudFoundryComponent implements OnInit {
permsOrgEdit = CurrentUserPermissions.ORGANIZATION_EDIT;
permsSpaceEdit = CurrentUserPermissions.SPACE_EDIT;

orgGuid: string;
spaceGuid: string;

cfEndpointEntityService: any;
Expand All @@ -68,9 +65,8 @@ export class CliInfoCloudFoundryComponent implements OnInit {
@Optional() private cfSpaceService: CloudFoundrySpaceService
) {
this.breadcrumbs$ = new BehaviorSubject<IHeaderBreadcrumb[]>([]);
this.orgGuid = activeRouteCfOrgSpace.orgGuid || CurrentUserPermissionsChecker.ALL_ORGS;
this.spaceGuid = activeRouteCfOrgSpace.spaceGuid || CurrentUserPermissionsChecker.ALL_SPACES;
// console.log('orgGuid: ', activeRouteCfOrgSpace.orgGuid);
// console.log('spaceGuid: ', this.spaceGuid);
}

ngOnInit() {
Expand Down

0 comments on commit b5ec5c2

Please sign in to comment.