Introduce update permissions for users

pull/10616/head
Andrew Watkins 2017-03-10 13:30:17 -08:00
parent f0ab1cdebb
commit 8ed77eae3b
6 changed files with 47 additions and 2 deletions

View File

@ -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}`))
}
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -19,7 +19,12 @@ const UserRow = ({
onSave,
onCancel,
onDelete,
onUpdatePermissions,
}) => {
const handleUpdatePermissions = (allowed) => {
onUpdatePermissions(user, [{scope: 'all', allowed}])
}
if (isEditing) {
return (
<tr className="admin-table--edit-row">
@ -55,7 +60,7 @@ const UserRow = ({
items={allPermissions}
selectedItems={_.get(permissions, ['0', 'allowed'], [])}
label={permissions && permissions.length ? '' : 'Select Permissions'}
onApply={() => '//TODO'}
onApply={handleUpdatePermissions}
/> : null
}
</td>
@ -93,6 +98,7 @@ UserRow.propTypes = {
onEdit: func,
onSave: func,
onDelete: func.isRequired,
onUpdatePermissions: func,
}
export default UserRow

View File

@ -16,6 +16,7 @@ const UsersTable = ({
onCancel,
onDelete,
onFilter,
onUpdatePermissions,
}) => (
<div className="panel panel-info">
<FilterBar type="users" onFilter={onFilter} isEditing={isEditing} onClickCreate={onClickCreate} />
@ -45,6 +46,7 @@ const UsersTable = ({
allRoles={allRoles}
hasRoles={hasRoles}
allPermissions={permissions}
onUpdatePermissions={onUpdatePermissions}
/>) :
<EmptyRow tableName={'Users'} />
}
@ -83,6 +85,7 @@ UsersTable.propTypes = {
allRoles: arrayOf(shape()),
permissions: arrayOf(string),
hasRoles: bool.isRequired,
onUpdatePermissions: func,
}
export default UsersTable

View File

@ -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}
/> :
<span>Loading...</span>
}
@ -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)