Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable removal of org user role if user has others #2427

Merged
merged 7 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import { CfPermissionCell, ICellPermissionList } from '../cf-permission-cell';
})
export class CfOrgPermissionCellComponent extends CfPermissionCell<OrgUserRoleNames> {
constructor(
store: Store<AppState>,
public cfUserService: CfUserService,
public store: Store<AppState>,
cfUserService: CfUserService,
private userPerms: CurrentUserPermissionsService,
confirmDialog: ConfirmationDialogService
) {
super(store, confirmDialog);
super(store, confirmDialog, cfUserService);
this.chipsConfig$ = combineLatest(
this.rowSubject.asObservable(),
this.configSubject.asObservable().pipe(switchMap(config => config.org$))
Expand Down Expand Up @@ -90,5 +90,4 @@ export class CfOrgPermissionCellComponent extends CfPermissionCell<OrgUserRoleNa
public canRemovePermission = (cfGuid: string, orgGuid: string, spaceGuid: string) =>
this.userPerms.can(CurrentUserPermissions.ORGANIZATION_CHANGE_ROLES, cfGuid, orgGuid)


}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Input } from '@angular/core';
import { Store } from '@ngrx/store';
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
import { first, map } from 'rxjs/operators';
import { filter, map, switchMap, first } from 'rxjs/operators';

import { IUserRole } from '../../../../../features/cloud-foundry/cf.helpers';
import { AppState } from '../../../../../store/app-state';
import { selectSessionData } from '../../../../../store/reducers/auth.reducer';
import { APIResource } from '../../../../../store/types/api.types';
import { CfUser } from '../../../../../store/types/user.types';
import { UserRoleLabels } from '../../../../../store/types/users-roles.types';
import { CfUserService } from '../../../../data-services/cf-user.service';
import { AppChip } from '../../../chips/chips.component';
import { ConfirmationDialogConfig } from '../../../confirmation-dialog.config';
import { ConfirmationDialogService } from '../../../confirmation-dialog.service';
Expand All @@ -29,12 +31,14 @@ interface ICellPermissionUpdates {
[key: string]: Observable<boolean>;
}

export abstract class CfPermissionCell<T> extends TableCellCustom<APIResource<CfUser>> {
export abstract class CfPermissionCell<T> extends TableCellCustom<APIResource<CfUser>> {
userEntity: BehaviorSubject<CfUser> = new BehaviorSubject(null);

@Input('row')
set row(row: APIResource<CfUser>) {
this.rowSubject.next(row);
this.guid = row.metadata.guid;
this.userEntity.next(row.entity);
}

@Input('config')
Expand All @@ -48,7 +52,11 @@ export abstract class CfPermissionCell<T> extends TableCellCustom<APIResource<Cf
protected rowSubject = new BehaviorSubject<APIResource<CfUser>>(null);
protected configSubject = new BehaviorSubject<any>(null);

constructor(public store: Store<AppState>, private confirmDialog: ConfirmationDialogService) {
constructor(
public store: Store<AppState>,
private confirmDialog: ConfirmationDialogService,
public cfUserService: CfUserService
) {
super();
}

Expand All @@ -64,6 +72,18 @@ export abstract class CfPermissionCell<T> extends TableCellCustom<APIResource<Cf
};
chipConfig.hideClearButton$ = this.canRemovePermission(perm.cfGuid, perm.orgGuid, perm.spaceGuid).pipe(
map(can => !can),
switchMap(can => {
if (!can) {
if (perm.string === UserRoleLabels.org.short.users) {
// If there are other roles than Org User, disable clear button
return this.userEntity.pipe(
filter(p => !!p),
map((entity: CfUser) => this.cfUserService.hasRolesInOrg(entity, perm.orgGuid)),
);
}
}
return observableOf(can);
})
);
return chipConfig;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ import { CfPermissionCell, ICellPermissionList } from '../cf-permission-cell';
export class CfSpacePermissionCellComponent extends CfPermissionCell<SpaceUserRoleNames> {

constructor(
store: Store<AppState>,
public cfUserService: CfUserService,
public store: Store<AppState>,
cfUserService: CfUserService,
private userPerms: CurrentUserPermissionsService,
confirmDialog: ConfirmationDialogService
) {
super(store, confirmDialog);
super(store, confirmDialog, cfUserService);
this.chipsConfig$ = combineLatest(
this.rowSubject.asObservable(),
this.configSubject.asObservable().pipe(switchMap(config => config.org$)),
Expand Down
42 changes: 42 additions & 0 deletions src/frontend/app/shared/data-services/cf-user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
createUserRoleInSpace,
IUserPermissionInOrg,
IUserPermissionInSpace,
OrgUserRoleNames,
SpaceUserRoleNames,
UserRoleInOrg,
UserRoleInSpace,
} from '../../store/types/user.types';
Expand Down Expand Up @@ -157,6 +159,46 @@ export class CfUserService {
return res;
}

/**
* Helper to determine if user has roles other than Org User
*/
hasRolesInOrg(user: CfUser, orgGuid: string, excludeOrgUser = true): boolean {

const orgRoles = this.getOrgRolesFromUser(user).filter(o => o.orgGuid === orgGuid);
const spaceRoles = this.getSpaceRolesFromUser(user).filter(o => o.orgGuid === orgGuid);

for (const roleKey in orgRoles) {
if (!orgRoles.hasOwnProperty(roleKey)) {
continue;
}

const permissions = orgRoles[roleKey].permissions;
if (
permissions[OrgUserRoleNames.MANAGER] ||
permissions[OrgUserRoleNames.BILLING_MANAGERS] ||
permissions[OrgUserRoleNames.AUDITOR]
) {
return true;
}
if (!excludeOrgUser && permissions[OrgUserRoleNames.USER]) {
return true;
}
}

for (const roleKey in spaceRoles) {
if (!spaceRoles.hasOwnProperty(roleKey)) {
continue;
}

const permissions = spaceRoles[roleKey].permissions;
if (permissions[SpaceUserRoleNames.MANAGER] ||
permissions[SpaceUserRoleNames.AUDITOR] ||
permissions[SpaceUserRoleNames.DEVELOPER]) {
return true;
}
}
}

getUserRoleInOrg = (
userGuid: string,
orgGuid: string,
Expand Down