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

Commit

Permalink
centralize rolemap info and add apiKey
Browse files Browse the repository at this point in the history
The data to how to map a role to various things was spread out.
This PR moves them all into one object.

Also, with moving into one object, we have the apiKey field.
This field is now used
  • Loading branch information
James C. Scott committed Jun 14, 2017
1 parent 9d67802 commit 0ed527b
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 57 deletions.
9 changes: 5 additions & 4 deletions static_src/actions/user_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const userActions = {
});
},

addUserRoles(roles, userGuid, entityGuid, entityType) {
addUserRoles(roles, apiKey, userGuid, entityGuid, entityType) {
const apiMethodMap = {
org: cfApi.putOrgUserPermissions,
space: cfApi.putSpaceUserPermissions
Expand All @@ -91,7 +91,7 @@ const userActions = {
return api(
userGuid,
entityGuid,
roles
apiKey
).then(() => {
userActions.addedUserRoles(
roles,
Expand All @@ -113,7 +113,7 @@ const userActions = {
});
},

deleteUserRoles(roles, userGuid, entityGuid, entityType) {
deleteUserRoles(roles, apiKey, userGuid, entityGuid, entityType) {
const apiMethodMap = {
org: cfApi.deleteOrgUserPermissions,
space: cfApi.deleteSpaceUserPermissions
Expand All @@ -131,7 +131,8 @@ const userActions = {
return api(
userGuid,
entityGuid,
roles
roles,
apiKey
).catch((err) => {
window.console.error(err);
});
Expand Down
42 changes: 33 additions & 9 deletions static_src/components/user_role_list_control.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,40 @@ import ElasticLine from './elastic_line.jsx';
import ElasticLineItem from './elastic_line_item.jsx';
import UserRoleControl from './user_role_control.jsx';

// roleMapping is a centralized relation of roles to machine-readable fields.
// The root contains which level of users are we referring to.
// Currently in CF, there are only two levels. space_user and org_users.
// Each root node contains an array objects to specify various fields.
// Each object will contain the following 3 fields:
//
// 1) key
// 'key' is useful for two reasons. One: When the app checks for the roles in the
// space via https://apidocs.cloudfoundry.org/263/spaces/retrieving_the_roles_of_all_users_in_the_space.html
// or in the
// org via https://apidocs.cloudfoundry.org/263/organizations/retrieving_the_roles_of_all_users_in_the_organization.html
// it will compare the roles returned with the value of 'key'.
// the second reason is because 'key is used in the rendering of the user list.
// the HTML element ID set as the 'key' + 'userguid'.
//
// 2) apiKey
// 'apiKey' is needed because the API to associate/dissociate roles does not use
// the same key as 'key'.
// For example to associate a developer to a space:
// https://apidocs.cloudfoundry.org/263/spaces/associate_developer_with_the_space.html
// It uses 'developers' instead of 'space_developer'.
//
// 3) label
// 'label' is a human-readable version of the role.
const roleMapping = {
space_users: [
{ key: 'space_developer', label: 'Space Developer' },
{ key: 'space_manager', label: 'Space Manager' },
{ key: 'space_auditor', label: 'Space Auditor' }
{ key: 'space_developer', apiKey: 'developers', label: 'Space Developer' },
{ key: 'space_manager', apiKey: 'managers', label: 'Space Manager' },
{ key: 'space_auditor', apiKey: 'auditors', label: 'Space Auditor' }
],
org_users: [
{ key: 'org_manager', label: 'Org Manager' },
{ key: 'billing_manager', label: 'Billing Manager' },
{ key: 'org_auditor', label: 'Org Auditor' }
{ key: 'org_manager', apiKey: 'managers', label: 'Org Manager' },
{ key: 'billing_manager', apiKey: 'billing_managers', label: 'Billing Manager' },
{ key: 'org_auditor', apiKey: 'auditors', label: 'Org Auditor' }
]

};
Expand Down Expand Up @@ -50,11 +74,11 @@ export default class UserRoleListControl extends React.Component {
return (this.roles().indexOf(roleKey) > -1);
}

_onChange(roleKey, checked) {
_onChange(roleKey, apiKey, checked) {
const handler = (!checked) ? this.props.onRemovePermissions :
this.props.onAddPermissions;

handler(roleKey, this.props.user.guid);
handler(roleKey, apiKey, this.props.user.guid);
}

roles() {
Expand All @@ -81,7 +105,7 @@ export default class UserRoleListControl extends React.Component {
roleKey={ role.key }
initialValue={ this.checkRole(role.key) }
initialEnableControl={ this.props.currentUserAccess }
onChange={ this._onChange.bind(this, role.key) }
onChange={ this._onChange.bind(this, role.key, role.apiKey) }
userId={ this.props.user.guid }
/>
</ElasticLineItem>
Expand Down
6 changes: 4 additions & 2 deletions static_src/components/users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ export default class Users extends React.Component {
userActions.deleteUser(userGuid, this.state.currentOrgGuid);
}

handleAddPermissions(roleKey, userGuid) {
handleAddPermissions(roleKey, apiKey, userGuid) {
userActions.addUserRoles(roleKey,
apiKey,
userGuid,
this.entityGuid,
this.entityType);
}

handleRemovePermissions(roleKey, userGuid) {
handleRemovePermissions(roleKey, apiKey, userGuid) {
userActions.deleteUserRoles(roleKey,
apiKey,
userGuid,
this.entityGuid,
this.entityType);
Expand Down
23 changes: 1 addition & 22 deletions static_src/stores/user_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ import BaseStore from './base_store.js';
import cfApi from '../util/cf_api.js';
import { userActionTypes } from '../constants.js';

// TODO why is this role mapping needed?
const resourceToRole = {
space: {
managers: 'space_manager',
developers: 'space_developer',
auditors: 'space_auditor'
},
org: {
managers: 'org_manager',
billing_managers: 'billing_manager',
auditors: 'org_auditor'
}
};

export class UserStore extends BaseStore {
constructor() {
super();
Expand Down Expand Up @@ -164,6 +150,7 @@ export class UserStore extends BaseStore {
const orgPermissionsReq = cfApi.deleteOrgUserPermissions(
action.userGuid,
action.orgGuid,
'users',
'users');

orgPermissionsReq.then(() => {
Expand Down Expand Up @@ -289,14 +276,6 @@ export class UserStore extends BaseStore {
return this._error;
}

getResourceToRole(resource, entityType) {
if (entityType !== 'space' && entityType !== 'org') {
throw new Error(`unknown resource type ${entityType}`);
}
const role = resourceToRole[entityType][resource] || resource;
return role;
}

get currentlyViewedType() {
return this._currentViewedType;
}
Expand Down
Loading

0 comments on commit 0ed527b

Please sign in to comment.