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

Add a "Remove all space roles" button #1164

Merged
merged 9 commits into from
Jul 25, 2017
23 changes: 23 additions & 0 deletions static_src/actions/user_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ const userActions = {
.then((spaceUsers) => userActions.deleteUserOrDisplayNotice(spaceUsers, userGuid, orgGuid));
},

removeAllSpaceRoles(userGuid, spaceGuid) {
AppDispatcher.handleViewAction({
type: userActionTypes.USER_REMOVE_ALL_SPACE_ROLES,
userGuid,
spaceGuid
});

const spaceRoles = ['auditors', 'developers', 'managers'];
const spaceRemovalRequests = spaceRoles.map((role) =>
Promise.resolve(cfApi.deleteSpaceUserPermissions(userGuid, spaceGuid, role))
);
return Promise.all(spaceRemovalRequests)
.then((responses) => userActions.handleSpaceRolesRemoved(responses, userGuid));
},

handleSpaceRolesRemoved(responses, userGuid) {
AppDispatcher.handleViewAction({
type: userActionTypes.USER_REMOVED_ALL_SPACE_ROLES,
responses,
userGuid
});
},

deleteUser(userGuid, orgGuid) {
AppDispatcher.handleViewAction({
type: userActionTypes.USER_DELETE,
Expand Down
8 changes: 7 additions & 1 deletion static_src/components/user_list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export default class UserList extends React.Component {
}

render() {
let buttonText;
let content = <div><Loading text="Loading users" /></div>;

if (this.props.empty) {
Expand All @@ -147,12 +148,17 @@ export default class UserList extends React.Component {
if (this.props.onRemove) {
let button = <span></span>;
if (this.props.currentUserAccess) {
if (this.props.userType === 'org_users') {
buttonText = 'Remove User From Org';
} else if (this.props.userType === 'space_users') {
buttonText = 'Remove All Space Roles';
}
button = (
<Action
style="base"
clickHandler={ this._handleDelete.bind(this, user.guid) }
label="delete">
<span>Remove User From Org</span>
<span>{ buttonText }</span>
</Action>
);
}
Expand Down
11 changes: 11 additions & 0 deletions static_src/components/users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export default class Users extends React.Component {
userActions.deleteUser(userGuid, this.state.currentOrgGuid);
}

handleRemoveSpaceRoles(userGuid, ev) {
ev.preventDefault();
userActions.removeAllSpaceRoles(userGuid, this.entityGuid);
}

get entityType() {
return this.isOrganization ? ORG_ENTITY : SPACE_ENTITY;
}
Expand All @@ -124,6 +129,10 @@ export default class Users extends React.Component {
return this.state.currentType === ORG_NAME;
}

get isSpace() {
return this.state.currentType === SPACE_NAME;
}

get entityGuid() {
const entityGuid = this.isOrganization ?
this.state.currentOrgGuid : this.state.currentSpaceGuid;
Expand Down Expand Up @@ -186,6 +195,8 @@ export default class Users extends React.Component {

if (this.isOrganization) {
removeHandler = this.handleRemoveUser;
} else if (this.isSpace) {
removeHandler = this.handleRemoveSpaceRoles;
}

return (
Expand Down
4 changes: 4 additions & 0 deletions static_src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ const userActionTypes = keymirror({
USER_INVITE_ERROR: null,
// Action to dismiss an user list notification.
USER_LIST_NOTICE_DISMISSED: null,
// Action when all roles for space user are removed.
Copy link
Contributor

@jcscottiii jcscottiii Jul 25, 2017

Choose a reason for hiding this comment

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

oops i think i told you to put that comment on the wrong constant. i meant the one below should be the Action when all...

Copy link
Contributor

Choose a reason for hiding this comment

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

this first one should be Action to...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

USER_REMOVE_ALL_SPACE_ROLES: null,
// Action to removed all roles for space user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Action when all roles removed from space user

USER_REMOVED_ALL_SPACE_ROLES: null,
// Action to delete a user from an org.
USER_DELETE: null,
// Action when a user was deleted from an org on the server.
Expand Down
7 changes: 7 additions & 0 deletions static_src/stores/user_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ export class UserStore extends BaseStore {
break;
}

case userActionTypes.USER_REMOVED_ALL_SPACE_ROLES: {
Copy link
Contributor

Choose a reason for hiding this comment

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

only need one more test. that it changes the list of users upon removing the person.

Copy link
Contributor

Choose a reason for hiding this comment

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

there's similar tests for the store for the add or delete cases.

this.delete(action.userGuid, (changed) => {
if (changed) this.emitChange();
});
break;
}

case userActionTypes.ERROR_REMOVE_USER: {
this._error = action.error;
this.emitChange();
Expand Down
36 changes: 36 additions & 0 deletions static_src/test/unit/actions/user_actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,42 @@ describe('userActions', function() {
});
});

describe('removeAllSpaceRoles()', function() {
let userGuid;
let spaceGuid;
let user;
let spy;
let expectedParams;

beforeEach(function (done) {
spy = setupViewSpy(sandbox);
userGuid = 'user-guid';
spaceGuid = 'space-guid';
user = { guid: userGuid };
expectedParams = { userGuid, spaceGuid }

sandbox.stub(cfApi, 'deleteSpaceUserPermissions')
.returns(Promise.resolve({}));

sandbox.stub(userActions, 'handleSpaceRolesRemoved');

userActions.removeAllSpaceRoles(userGuid, spaceGuid).then(done, done.fail);
});

it('should dispatch a view event for USER_REMOVE_ALL_SPACE_ROLES',
function() {
assertAction(spy, userActionTypes.USER_REMOVE_ALL_SPACE_ROLES, expectedParams);
});

it(`should call cfApi.deleteSpaceUserPermissions`, function() {
expect(cfApi.deleteSpaceUserPermissions).toHaveBeenCalledThrice();
});

it(`should call userActions.handleSpaceRolesRemoved`, function() {
expect(userActions.handleSpaceRolesRemoved).toHaveBeenCalledOnce();
});
});

describe('deleteUserIfNoSpaceAssociation()', function() {
let userGuid;
let orgGuid;
Expand Down
49 changes: 49 additions & 0 deletions static_src/test/unit/stores/user_store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,55 @@ describe('UserStore', function () {
});
});

describe('on user spaces roles remove', function() {
let spaceGuid;
beforeEach(() => {
spaceGuid = 'space-guid';
});

it('should remove the user of the guid from the data', function() {
var expectedUserGuid = 'zxkvnakjdva',
expectedUser = { guid: expectedUserGuid };

UserStore._data.push(expectedUser);

userActions.removeAllSpaceRoles(expectedUserGuid, spaceGuid);

expect(UserStore.get(expectedUserGuid)).toBeFalsy();
});
});

describe('on user spaces roles removed', function() {
it('should remove the user of the guid from the data', function() {
var expectedUserGuid = 'zxkvnakjdva',
expectedUser = { guid: expectedUserGuid };

UserStore._data.push(expectedUser);

userActions.handleSpaceRolesRemoved(['random-response'], expectedUserGuid);

expect(UserStore.get(expectedUserGuid)).toBeFalsy();
});

it('should emit a change event if it deletes something', function() {
var spy = sandbox.spy(UserStore, 'emitChange'),
testUserGuid = 'qpweoiralkfdsj';

UserStore._data = Immutable.fromJS([{guid: testUserGuid}]);
userActions.handleSpaceRolesRemoved(['random-response'], testUserGuid);

expect(spy).toHaveBeenCalledOnce();
});

it('should not emit a change event if nothing deleted', function() {
var spy = sandbox.spy(UserStore, 'emitChange');

userActions.handleSpaceRolesRemoved(['random-response'], 'asdfljk');

expect(spy).not.toHaveBeenCalledOnce();
});
});

describe('on user deleted', function() {
it('should remove the user of the guid from the data', function() {
var expectedUserGuid = 'zxkvnakjdva',
Expand Down