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

Commit

Permalink
User access control for role checkboxes
Browse files Browse the repository at this point in the history
Adds current user control for the role checkboxes in both orgs and
spaces. Does this by switching the currentUserAccess functiona based
on what tab you're on and passing it down all the way to the UI through
props.
  • Loading branch information
Marco Segreto committed Jul 11, 2016
1 parent 20647bf commit 2caa987
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions static_src/components/user_list.jsx
Expand Up @@ -117,6 +117,7 @@ export default class UserList extends React.Component {
<td column="Permissions" key={ `${user.guid}-role` }>
<UserRoleListControl
initialUserType={ this.state.userType }
initialCurrentUserAccess={ this.state.currentUserAccess }
onAddPermissions={ this.props.onAddPermissions }
onRemovePermissions={ this.props.onRemovePermissions }
user={ user }
Expand Down
11 changes: 9 additions & 2 deletions static_src/components/user_role_control.jsx
Expand Up @@ -6,13 +6,17 @@ export default class UserRoleControl extends React.Component {
super(props);
this.props = props;
this.state = {
checked: props.initialValue
checked: props.initialValue,
enableControl: props.initialEnableControl
};
this._handleChange = this._handleChange.bind(this);
}

componentWillReceiveProps(nextProps) {
this.setState({checked: nextProps.initialValue});
this.setState({
checked: nextProps.initialValue,
enableControl: nextProps.initialEnableControl
});
}

_handleChange(ev) {
Expand All @@ -27,6 +31,7 @@ export default class UserRoleControl extends React.Component {
onChange={ this._handleChange }
name={ this.props.roleKey }
checked={ this.state.checked }
disabled={ !this.state.enableControl }
id={ this.props.roleKey + this.props.userId }
/>
{ this.props.roleName }
Expand All @@ -39,9 +44,11 @@ UserRoleControl.propTypes = {
roleName: React.PropTypes.string.isRequired,
roleKey: React.PropTypes.string.isRequired,
initialValue: React.PropTypes.bool,
initialEnableControl: React.PropTypes.bool,
onChange: React.PropTypes.func
};
UserRoleControl.defaultProps = {
initialValue: false,
initialEnableControl: false,
onChange: function() { }
}
11 changes: 8 additions & 3 deletions static_src/components/user_role_list_control.jsx
Expand Up @@ -35,7 +35,8 @@ export default class UserRoleListControl extends React.Component {
this.props = props;
this.state = {
user: props.user,
userType: props.initialUserType
userType: props.initialUserType,
currentUserAccess: props.initialCurrentUserAccess
};
this._onChange = this._onChange.bind(this);
this.checkRole = this.checkRole.bind(this);
Expand All @@ -44,7 +45,8 @@ export default class UserRoleListControl extends React.Component {
componentWillReceiveProps(nextProps) {
this.setState({
user: nextProps.user,
userType: nextProps.initialUserType
userType: nextProps.initialUserType,
currentUserAccess: nextProps.initialCurrentUserAccess
});
}

Expand Down Expand Up @@ -81,6 +83,7 @@ export default class UserRoleListControl extends React.Component {
roleName={ role.label }
roleKey={ role.key }
initialValue={ this.checkRole(role.key) }
initialEnableControl={ this.state.currentUserAccess }
onChange={ this._onChange.bind(this, role.key) }
userId={ this.props.user.guid }
/>
Expand All @@ -93,11 +96,13 @@ export default class UserRoleListControl extends React.Component {
UserRoleListControl.propTypes = {
user: React.PropTypes.object.isRequired,
initialUserType: React.PropTypes.string,
initialCurrentUserAccess: React.PropTypes.bool,
onRemovePermissions: React.PropTypes.func,
onAddPermissions: React.PropTypes.func
onAddPermissions: React.PropTypes.func,
};
UserRoleListControl.defaultProps = {
initialUserType: 'space_users',
initialCurrentUserAccess: false,
onRemovePermissions: function defaultRemove() { },
onAddPermissions: function defaultAdd() { }
};
15 changes: 10 additions & 5 deletions static_src/components/users.jsx
Expand Up @@ -18,15 +18,19 @@ const TAB_ORG_NAME = 'org_users';
function stateSetter(currentState) {
let users = [];
const currentTab = UserStore.currentlyViewedType;
let currentUserAccess: false;

if (currentTab === TAB_SPACE_NAME) {
users = UserStore.getAllInSpace(currentState.currentSpaceGuid);
currentUserAccess = UserStore.currentUserHasSpaceRole('space_manager');
} else {
users = UserStore.getAllInOrg(currentState.currentOrgGuid);
currentUserAccess = UserStore.currentUserHasOrgRole('org_manager');
}

return {
error: UserStore.getError(),
currentUserAccess: UserStore.currentUserHasOrgRole('org_manager'),
currentUserAccess: currentUserAccess,
currentTab,
loading: UserStore.fetching,
users
Expand Down Expand Up @@ -131,6 +135,11 @@ export default class Users extends React.Component {
render() {
let removeHandler;
let errorMessage;

if (this.state.currentTab === TAB_ORG_NAME) {
removeHandler = this.handleRemove;
}

let content = (<UserList
initialUsers={ this.state.users }
initialUserType= { this.state.currentTab }
Expand All @@ -140,10 +149,6 @@ export default class Users extends React.Component {
onRemovePermissions={ this.handleRemovePermissions }
/>);

if (this.state.currentTab === TAB_ORG_NAME) {
removeHandler = this.handleRemove;
}

if (this.state.loading) {
content = <Loading text="Loading users" />;
}
Expand Down

0 comments on commit 2caa987

Please sign in to comment.