From 8ed77eae3b394fe3f408fbed882ff9ab95ffedfd Mon Sep 17 00:00:00 2001 From: Andrew Watkins Date: Fri, 10 Mar 2017 13:30:17 -0800 Subject: [PATCH] Introduce update permissions for users --- ui/src/admin/actions/index.js | 12 +++++++++++- ui/src/admin/apis/index.js | 14 ++++++++++++++ ui/src/admin/components/AdminTabs.js | 3 +++ ui/src/admin/components/UserRow.js | 8 +++++++- ui/src/admin/components/UsersTable.js | 3 +++ ui/src/admin/containers/AdminPage.js | 9 +++++++++ 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/ui/src/admin/actions/index.js b/ui/src/admin/actions/index.js index b4ee6a7cfa..419c021a7a 100644 --- a/ui/src/admin/actions/index.js +++ b/ui/src/admin/actions/index.js @@ -8,10 +8,11 @@ import { deleteRole as deleteRoleAJAX, updateRoleUsers as updateRoleUsersAJAX, updateRolePermissions as updateRolePermissionsAJAX, + updateUserPermissions as updateUserPermissionsAJAX, } from 'src/admin/apis' + import {killQuery as killQueryProxy} from 'shared/apis/metaQuery' import {publishNotification} from 'src/shared/actions/notifications'; - import {ADMIN_NOTIFICATION_DELAY} from 'shared/constants' export const loadUsers = ({users}) => ({ @@ -206,3 +207,12 @@ export const updateRolePermissionsAsync = (role, permissions) => async (dispatch dispatch(publishNotification('error', `Failed to updated role: ${error.data.message}`)) } } + +export const updateUserPermissionsAsync = (user, permissions) => async (dispatch) => { + try { + await updateUserPermissionsAJAX(user.links.self, permissions) + dispatch(publishNotification('success', 'User permissions updated')) + } catch (error) { + dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`)) + } +} diff --git a/ui/src/admin/apis/index.js b/ui/src/admin/apis/index.js index 788e51e223..74b265cdae 100644 --- a/ui/src/admin/apis/index.js +++ b/ui/src/admin/apis/index.js @@ -124,3 +124,17 @@ export const updateRolePermissions = async (url, permissions) => { console.error(error) } } + +export const updateUserPermissions = async (url, permissions) => { + try { + await AJAX({ + method: 'PATCH', + url, + data: { + permissions, + }, + }) + } catch (error) { + console.error(error) + } +} diff --git a/ui/src/admin/components/AdminTabs.js b/ui/src/admin/components/AdminTabs.js index b8aaae8d68..cfd3374355 100644 --- a/ui/src/admin/components/AdminTabs.js +++ b/ui/src/admin/components/AdminTabs.js @@ -25,6 +25,7 @@ const AdminTabs = ({ onFilterUsers, onUpdateRoleUsers, onUpdateRolePermissions, + onUpdateUserPermissions, }) => { let tabs = [ { @@ -42,6 +43,7 @@ const AdminTabs = ({ onEdit={onEditUser} onDelete={onDeleteUser} onFilter={onFilterUsers} + onUpdatePermissions={onUpdateUserPermissions} /> ), }, @@ -124,6 +126,7 @@ AdminTabs.propTypes = { onUpdateRoleUsers: func.isRequired, onUpdateRolePermissions: func.isRequired, hasRoles: bool.isRequired, + onUpdateUserPermissions: func, } export default AdminTabs diff --git a/ui/src/admin/components/UserRow.js b/ui/src/admin/components/UserRow.js index d8aa090e07..9674456860 100644 --- a/ui/src/admin/components/UserRow.js +++ b/ui/src/admin/components/UserRow.js @@ -19,7 +19,12 @@ const UserRow = ({ onSave, onCancel, onDelete, + onUpdatePermissions, }) => { + const handleUpdatePermissions = (allowed) => { + onUpdatePermissions(user, [{scope: 'all', allowed}]) + } + if (isEditing) { return ( @@ -55,7 +60,7 @@ const UserRow = ({ items={allPermissions} selectedItems={_.get(permissions, ['0', 'allowed'], [])} label={permissions && permissions.length ? '' : 'Select Permissions'} - onApply={() => '//TODO'} + onApply={handleUpdatePermissions} /> : null } @@ -93,6 +98,7 @@ UserRow.propTypes = { onEdit: func, onSave: func, onDelete: func.isRequired, + onUpdatePermissions: func, } export default UserRow diff --git a/ui/src/admin/components/UsersTable.js b/ui/src/admin/components/UsersTable.js index 584f63019e..f19a7cca6e 100644 --- a/ui/src/admin/components/UsersTable.js +++ b/ui/src/admin/components/UsersTable.js @@ -16,6 +16,7 @@ const UsersTable = ({ onCancel, onDelete, onFilter, + onUpdatePermissions, }) => (
@@ -45,6 +46,7 @@ const UsersTable = ({ allRoles={allRoles} hasRoles={hasRoles} allPermissions={permissions} + onUpdatePermissions={onUpdatePermissions} />) : } @@ -83,6 +85,7 @@ UsersTable.propTypes = { allRoles: arrayOf(shape()), permissions: arrayOf(string), hasRoles: bool.isRequired, + onUpdatePermissions: func, } export default UsersTable diff --git a/ui/src/admin/containers/AdminPage.js b/ui/src/admin/containers/AdminPage.js index fd60a37551..4caea6828f 100644 --- a/ui/src/admin/containers/AdminPage.js +++ b/ui/src/admin/containers/AdminPage.js @@ -17,6 +17,7 @@ import { deleteRoleAsync, updateRoleUsersAsync, updateRolePermissionsAsync, + updateUserPermissionsAsync, filterUsers as filterUsersAction, filterRoles as filterRolesAction, } from 'src/admin/actions' @@ -47,6 +48,7 @@ class AdminPage extends Component { this.handleDeleteUser = ::this.handleDeleteUser this.handleUpdateRoleUsers = ::this.handleUpdateRoleUsers this.handleUpdateRolePermissions = ::this.handleUpdateRolePermissions + this.handleUpdateUserPermissions = ::this.handleUpdateUserPermissions } componentDidMount() { @@ -124,6 +126,10 @@ class AdminPage extends Component { this.props.updateRolePermissions(role, permissions) } + handleUpdateUserPermissions(user, permissions) { + this.props.updateUserPermissions(user, permissions) + } + render() { const {users, roles, source, permissions, filterUsers, filterRoles} = this.props const hasRoles = !!source.links.roles @@ -167,6 +173,7 @@ class AdminPage extends Component { onFilterRoles={filterRoles} onUpdateRoleUsers={this.handleUpdateRoleUsers} onUpdateRolePermissions={this.handleUpdateRolePermissions} + onUpdateUserPermissions={this.handleUpdateUserPermissions} /> : Loading... } @@ -213,6 +220,7 @@ AdminPage.propTypes = { filterUsers: func, updateRoleUsers: func, updateRolePermissions: func, + updateUserPermissions: func, } const mapStateToProps = ({admin: {users, roles, permissions}}) => ({ @@ -239,6 +247,7 @@ const mapDispatchToProps = (dispatch) => ({ filterRoles: bindActionCreators(filterRolesAction, dispatch), updateRoleUsers: bindActionCreators(updateRoleUsersAsync, dispatch), updateRolePermissions: bindActionCreators(updateRolePermissionsAsync, dispatch), + updateUserPermissions: bindActionCreators(updateUserPermissionsAsync, dispatch), }) export default connect(mapStateToProps, mapDispatchToProps)(AdminPage)