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

Fix assign role for non-admin connected user #2562

Merged
merged 2 commits into from
Jun 29, 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 @@ -118,7 +118,8 @@ export class CfSpacePermissionCellComponent extends CfPermissionCell<SpaceUserRo
cellPermission.guid,
cellPermission.key,
true,
updateConnectedUser
updateConnectedUser,
cellPermission.orgGuid
));
}

Expand Down
30 changes: 19 additions & 11 deletions src/frontend/app/store/actions/users.actions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { RequestOptions, RequestMethod } from '@angular/http';
import { RequestOptions } from '@angular/http';

import {
cfUserSchemaKey,
endpointSchemaKey,
entityFactory,
EntitySchema,
organizationSchemaKey,
spaceSchemaKey,
endpointSchemaKey,
} from '../helpers/entity-factory';
import { createEntityRelationKey, EntityInlineParentAction, createEntityRelationPaginationKey } from '../helpers/entity-relations.types';
import { PaginatedAction, PaginationParam } from '../types/pagination.types';
import { CFStartAction, IRequestAction, RequestEntityLocation } from '../types/request.types';
import { getActions } from './action.helper';
import {
createEntityRelationKey,
createEntityRelationPaginationKey,
EntityInlineParentAction,
} from '../helpers/entity-relations.types';
import { PaginatedAction } from '../types/pagination.types';
import { CFStartAction, IRequestAction } from '../types/request.types';
import { OrgUserRoleNames, SpaceUserRoleNames } from '../types/user.types';
import { Action } from '@ngrx/store';
import { getActions } from './action.helper';

export const GET_ALL = '[Users] Get all';
export const GET_ALL_SUCCESS = '[Users] Get all success';
Expand Down Expand Up @@ -110,7 +113,8 @@ export class ChangeUserRole extends CFStartAction implements IRequestAction {
public permissionTypeKey: OrgUserRoleNames | SpaceUserRoleNames,
public entityGuid: string,
public isSpace = false,
public updateConnectedUser = false
public updateConnectedUser = false,
public orgGuid?: string
) {
super();
this.guid = entityGuid;
Expand Down Expand Up @@ -140,7 +144,8 @@ export class AddUserRole extends ChangeUserRole {
entityGuid: string,
permissionTypeKey: OrgUserRoleNames | SpaceUserRoleNames,
isSpace = false,
updateConnectedUser = false
updateConnectedUser = false,
orgGuid?: string
) {
super(
endpointGuid,
Expand All @@ -151,6 +156,7 @@ export class AddUserRole extends ChangeUserRole {
entityGuid,
isSpace,
updateConnectedUser,
orgGuid
);
}
}
Expand All @@ -162,7 +168,8 @@ export class RemoveUserRole extends ChangeUserRole {
entityGuid: string,
permissionTypeKey: OrgUserRoleNames | SpaceUserRoleNames,
isSpace = false,
updateConnectedUser = false
updateConnectedUser = false,
orgGuid?: string
) {
super(
endpointGuid,
Expand All @@ -172,7 +179,8 @@ export class RemoveUserRole extends ChangeUserRole {
permissionTypeKey,
entityGuid,
isSpace,
updateConnectedUser
updateConnectedUser,
orgGuid
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/app/store/effects/users-roles.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export class UsersRolesEffects {
const isSpace = !!change.spaceGuid;
const entityGuid = isSpace ? change.spaceGuid : change.orgGuid;
return change.add ?
new AddUserRole(cfGuid, change.userGuid, entityGuid, change.role, isSpace, updateConnectedUser) :
new RemoveUserRole(cfGuid, change.userGuid, entityGuid, change.role, isSpace, updateConnectedUser);
new AddUserRole(cfGuid, change.userGuid, entityGuid, change.role, isSpace, updateConnectedUser, change.orgGuid) :
new RemoveUserRole(cfGuid, change.userGuid, entityGuid, change.role, isSpace, updateConnectedUser, change.orgGuid);
}

private createActionObs(action: ChangeUserRole): Observable<boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ChangeUserRole } from '../../actions/users.actions';
import { ICfRolesState, ICurrentUserRolesState } from '../../types/current-user-roles.types';
import { APISuccessOrFailedAction } from '../../types/request.types';
import { OrgUserRoleNames, SpaceUserRoleNames } from '../../types/user.types';
import { defaultUserOrgRoleState } from './current-user-roles-org.reducer';
import { defaultUserSpaceRoleState } from './current-user-roles-space.reducer';

export function updateAfterRoleChange(
state: ICurrentUserRolesState,
Expand All @@ -17,12 +19,10 @@ export function updateAfterRoleChange(
const entityType = changePerm.isSpace ? 'spaces' : 'organizations';

const cf = state.cf[changePerm.endpointGuid];
const entity = cf[entityType][changePerm.entityGuid];

const entity = cf[entityType][changePerm.entityGuid] || createEmptyState(changePerm.isSpace, changePerm.orgGuid);
const permissionType = userRoleNameToPermissionName(changePerm.permissionTypeKey);
const currentValue = entity[permissionType];

if (currentValue === isAdd) {
if (entity && entity[permissionType] === isAdd) {
// No change, just return the state. Unlikely to happen
return state;
}
Expand All @@ -40,6 +40,15 @@ export function updateAfterRoleChange(
return spreadState(state, changePerm.endpointGuid, newCf);
}

function createEmptyState(isSpace: boolean, orgId?: string) {
return isSpace ? {
...defaultUserSpaceRoleState,
orgId
} : {
...defaultUserOrgRoleState,
};
}

function userRoleNameToPermissionName(roleName: OrgUserRoleNames | SpaceUserRoleNames): PermissionStrings {
switch (roleName) {
case OrgUserRoleNames.AUDITOR:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UserRelationTypes } from '../../actions/permissions.actions';
import { IOrgRoleState } from '../../types/current-user-roles.types';

const defaultOrgRoleStateState = {
export const defaultUserOrgRoleState = {
isManager: false,
isAuditor: false,
isBillingManager: false,
Expand All @@ -10,9 +10,9 @@ const defaultOrgRoleStateState = {
};

export const createOrgRoleStateState = () => ({
...defaultOrgRoleStateState,
...defaultUserOrgRoleState,
spaceGuids: [
...defaultOrgRoleStateState.spaceGuids
...defaultUserOrgRoleState.spaceGuids
]
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { GetCurrentUserRelationsComplete, UserRelationTypes } from '../../actions/permissions.actions';
import { ISpaceRoleState } from '../../types/current-user-roles.types';
import { APIResource } from '../../types/api.types';
import { ISpace } from '../../../core/cf-api.types';
import { UserRelationTypes } from '../../actions/permissions.actions';
import { APIResource } from '../../types/api.types';
import { ISpaceRoleState } from '../../types/current-user-roles.types';

const defaultState = {
export const defaultUserSpaceRoleState = {
orgId: null,
isManager: false,
isAuditor: false,
isDeveloper: false,
};

export function currentUserSpaceRoleReducer(
state: ISpaceRoleState = defaultState,
state: ISpaceRoleState = defaultUserSpaceRoleState,
relationType: UserRelationTypes,
userHasRelation: boolean,
space: APIResource<ISpace>
Expand All @@ -28,7 +28,7 @@ export function currentUserSpaceRoleReducer(
}

function addId(
state: ISpaceRoleState = defaultState,
state: ISpaceRoleState = defaultUserSpaceRoleState,
space: APIResource<ISpace>
) {
if (!state.orgId) {
Expand Down