Commit 2341f24c authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

Permission editor: Editing parent object ID


Signed-off-by: Matija Obreza's avatarMatija Obreza <matija.obreza@croptrust.org>
parent d9b20617
......@@ -9,6 +9,7 @@
"cancel": "Cancel",
"create": "Create",
"delete": "Delete",
"close": "Close",
"deleteData": "Delete data",
"download": "Download",
"edit": "Edit",
......@@ -89,8 +90,11 @@
"class": "Class",
"objectID": "Object ID",
"owner": "Owner",
"inheritsPermissions": "inheritsPermissions",
"inheritsPermissions": "Inherits permissions",
"effectivePermissions": "Effective permissions",
"sid": "Sid"
"sid": "Sid",
"objectIdentityId": "ACL OID ID",
"parentObjectIdentityId": "Parent OID ID",
"updateParentOID": "Update Parent OID ID"
}
}
import * as _ from 'lodash';
import {SubmissionError} from 'redux-form';
import { log } from 'utilities/debug';
import SidPermissions from 'model/acl/SidPermissions';
import PermissionService from 'service/genesys/PermissionService';
......@@ -25,3 +27,20 @@ export const autocomplete = (term: string) => (dispatch, getState) => {
log('Error', error);
});
};
export const updateInheriting = (id: number, inheriting: boolean) => (dispatch, getState) => {
return PermissionService.updateInheriting(id, inheriting)
.catch((error) => {
log('Error', error);
});
};
export const updateParentObject = (id: number, parentId: number) => (dispatch, getState) => {
return PermissionService.updateParentObject(id, parentId)
.catch((error) => {
const data = _.get(error, 'response.data');
throw new SubmissionError({ title: 'Error', _error: data.error });
});
};
export const PERMISSION_TABLE_FORM = 'Form/PERMISSION_TABLE_FORM';
export const UPDATE_PARENT_ID_FORM = 'Form/UPDATE_PARENT_ID_FORM';
......@@ -12,6 +12,8 @@ const URL_AC_USER = `/api/v1/permission/autocompleteuser`;
const URL_AC_SID = `/api/v1/permission/autocomplete`;
const URL_AC_ROLE = `/api/v1/permission/autocompleterole`;
const URL_PERMISSIONS = UrlTemplate.parse(`/api/v1/permission/permissions/{clazz}/{id}`);
const URL_UPDATE_OBJECT_IDENTITY = UrlTemplate.parse(`/api/v1/permission/update-inheriting/{inheriting}/{id}`);
const URL_UPDATE_PARENT_OBJECT = UrlTemplate.parse(`/api/v1/permission/update-parent/{id}/{parentId}`);
/*
* Defined in Swagger as 'permission'
......@@ -144,6 +146,43 @@ class PermissionService {
}).then(({ data }) => data as AclObjectIdentity);
}
/**
* updateObjectIdentity at /api/v1/permission/update-inheriting/{inheriting}/{id}
*
* @param id id
* @param inheriting inheriting
*/
public static updateInheriting(id: number, inheriting: boolean): Promise<AclObjectIdentity> {
const apiUrl = URL_UPDATE_OBJECT_IDENTITY.expand({id, inheriting});
// console.log(`Fetching from ${apiUrl}`);
const content = { /* No content in request body */ };
return axiosBackend({
url: apiUrl,
method: 'POST',
...content,
}).then(({ data }) => data as AclObjectIdentity);
}
/**
* updateParentObject at /api/v1/permission/update-parent/{id}/{parentId}
*
* @param id id
* @param parentId parentId
*/
public static updateParentObject(id: number, parentId: number): Promise<AclObjectIdentity> {
const apiUrl = URL_UPDATE_PARENT_OBJECT.expand({id, parentId});
// console.log(`Fetching from ${apiUrl}`);
const content = { /* No content in request body */ };
return axiosBackend({
url: apiUrl,
method: 'POST',
...content,
}).then(({ data }) => data as AclObjectIdentity);
}
}
......
......@@ -54,12 +54,13 @@ interface IItemProps extends React.ClassAttributes<any> {
numeric?: boolean;
t: any;
// children: any;
style?: any;
}
class PropertiesItem1 extends React.Component<IItemProps & WithStyles<'propertiesContainer' | 'propertiesRow' | 'propertiesValue' | 'propertiesValueSmall' | 'propertiesValueNumeric'>, any> {
public render() {
const { keepEmpty, small, numeric, title, classes, children, t } = this.props;
const { keepEmpty, small, numeric, title, classes, children, t, style } = this.props;
if (!keepEmpty && ! children) {
return null;
}
......@@ -69,7 +70,8 @@ class PropertiesItem1 extends React.Component<IItemProps & WithStyles<'propertie
<Grid item xs={ small && numeric ? 8 : 6 } md={ small ? 12 : (numeric ? 9 : 3) } className="font-bold p-halfrem" style={ { fontSize: '1rem', fontFamily: 'Roboto, sans-serif' } }>
{ title && typeof title === 'string' ? t(title) : title }
</Grid>
<Grid item xs={ small && numeric ? 4 : 6 } md={ small ? 12 : (numeric ? 3 : 9) } className={ `${small ? classes.propertiesValueSmall : ''} ${numeric && !small ? classes.propertiesValueNumeric : ''} ${classes.propertiesValue}` }>
<Grid item xs={ small && numeric ? 4 : 6 } md={ small ? 12 : (numeric ? 3 : 9) } style={ style }
className={ style ? `${style}` : `${small ? classes.propertiesValueSmall : ''} ${numeric && !small ? classes.propertiesValueNumeric : ''} ${classes.propertiesValue}` }>
{ children }
</Grid>
</Grid>
......
import * as React from 'react';
import {translate} from 'react-i18next';
import { Field, reduxForm } from 'redux-form';
// Utils
import Validators from 'utilities/Validators';
// UI
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableRow from '@material-ui/core/TableRow';
import { UPDATE_PARENT_ID_FORM } from 'constants/permission';
import {TextField} from 'ui/common/text-field';
const styles = {
/* tslint:disable */
head: {
fontSize: '1rem',
fontFamily: 'Roboto, sans-serif',
fontWeight: 'bold' as 'bold',
},
tableRow: {
display: 'flex' as 'flex',
justifyContent: 'space-between',
borderBottom: '1px solid rgba(224, 224, 224, 1)',
height: 'auto',
'& td': {
padding: '0px 10px 0px 0px',
borderBottom: 'none',
},
},
/* tslint:enable */
};
class ChangeParentForm extends React.Component<any, any> {
public render() {
const { t, error, invalid, handleSubmit, onCancel, classes } = this.props;
return (
<form onSubmit={ handleSubmit }>
<div className={ classes.head }>{ t('common:permissions.updateParentOID') }</div>
<Table>
<TableBody>
<TableRow className={ classes.tableRow }>
<TableCell >
<Field
name="id"
label={ t(`common:permissions.parentObjectIdentityId`) }
component={ TextField }
validate={ [ Validators.decimalNumber, Validators.required ] }
/>
{ error && <div style={ { color: 'red' } }>{ error }</div> }
</TableCell>
<TableCell style={ { margin: '1rem 0' } } >
<Button type="submit" disabled={ invalid || error }>{ t(`common:action.save`) }</Button>
<Button onClick={ onCancel }>{ t(`common:action.cancel`) }</Button>
</TableCell>
</TableRow>
</TableBody>
</Table>
</form>
);
}
}
export default translate()(reduxForm({
form: UPDATE_PARENT_ID_FORM,
enableReinitialize: true,
})(withStyles(styles)(ChangeParentForm)));
......@@ -2,21 +2,27 @@ import * as React from 'react';
import {translate} from 'react-i18next';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import {SubmissionError, change} from 'redux-form';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogTitle from '@material-ui/core/DialogTitle';
import {withStyles} from '@material-ui/core/styles';
import Switch from '@material-ui/core/Switch';
import ChangeParentForm from './ChangeParentForm';
import { permissions, addPermission, autocomplete } from 'actions/permission';
import { permissions, addPermission, autocomplete, updateInheriting, updateParentObject } from 'actions/permission';
import SidPermissions from 'model/acl/SidPermissions';
import Loading from 'ui/common/Loading';
import Authorize from 'ui/common/authorized/Authorize';
import { Properties, PropertiesItem } from 'ui/common/Properties';
import PermissionsTableForm from 'ui/common/permission/PermissionsTableForm';
import AclObjectIdentity from 'model/acl/AclObjectIdentity';
import AclEntry from 'model/acl/AclEntry';
import {UPDATE_PARENT_ID_FORM} from 'constants/permission';
/*tslint:disable*/
const styles = (theme) => ({
permButton: {
......@@ -34,6 +40,9 @@ interface IPermissionsProps extends React.ClassAttributes<any> {
permissions: (clazz: string, id: number) => Promise<AclObjectIdentity>;
autocomplete: (term: string) => Promise<{[key: string]: number}>;
addPermission: (clazz: string, id: number, sidPermissions: SidPermissions) => Promise<AclObjectIdentity>;
updateInheriting: (id: number, inheriting: boolean) => Promise<AclObjectIdentity>;
updateParentObject: (id: number, parentId: number) => Promise<AclObjectIdentity>;
change: any;
classes: any;
variant?: 'text' | 'flat' | 'outlined' | 'contained' | 'contained' | 'fab' | 'extendedFab';
t: any;
......@@ -87,6 +96,32 @@ class Permissions extends React.Component<IPermissionsProps, any> {
}
}
protected handleSwitch = (event, checked) => {
const {updateInheriting} = this.props;
updateInheriting(this.state.aclObjectIdentity.id, checked).then((aclObjectIdentity) => {
this.setState({ aclObjectIdentity });
});
}
protected updateParentObject = (value) => {
const { updateParentObject } = this.props;
return updateParentObject(this.state.aclObjectIdentity.id, value.id)
.then((aclObjectIdentity) => {
this.setState({ aclObjectIdentity });
})
.catch((error) => {
throw new SubmissionError({ ...error.errors });
});
}
protected handleCancel = () => {
const {aclObjectIdentity} = this.state;
const parentId = typeof aclObjectIdentity.parentObject === 'number'
? aclObjectIdentity.parentObject : (aclObjectIdentity.parentObject === null ? '' : aclObjectIdentity.parentObject.id);
this.props.change(UPDATE_PARENT_ID_FORM, 'id', parentId);
}
public render() {
const {autocomplete, variant = 'contained', t } = this.props;
const {aclObjectIdentity, autocompleteObj} = this.state;
......@@ -110,7 +145,21 @@ class Permissions extends React.Component<IPermissionsProps, any> {
<PropertiesItem title={ t('common:permissions.class') }>{ aclObjectIdentity.aclClass.aclClass }</PropertiesItem>
<PropertiesItem title={ t('common:permissions.objectID') }>{ aclObjectIdentity.objectIdIdentity }</PropertiesItem>
<PropertiesItem title={ t('common:permissions.owner') }>{ aclObjectIdentity.ownerSid.sid }</PropertiesItem>
<PropertiesItem title={ t('common:permissions.inheritsPermissions') }>{ `${aclObjectIdentity.entriesInheriting}` }</PropertiesItem>
<PropertiesItem title={ t('common:permissions.objectIdentityId') }>{ `${aclObjectIdentity.id}` }</PropertiesItem>
{
aclObjectIdentity.parentObject &&
<PropertiesItem title={ t('common:permissions.parentObjectIdentityId') }>
{ `${ typeof aclObjectIdentity.parentObject === 'object' ? aclObjectIdentity.parentObject.id : aclObjectIdentity.parentObject }` }
</PropertiesItem>
}
{
<PropertiesItem title={ t('common:permissions.inheritsPermissions') } style={ { padding: '0 14px', display: 'flex', alignItems: 'center' } }>
<span>{ t(`${ aclObjectIdentity.entriesInheriting ? `common:label.yes` : `common:label.no` }`) }</span>
<Authorize role="ROLE_ADMINISTRATOR">
<Switch style={ { maxHeight: '35px' } } checked={ aclObjectIdentity.entriesInheriting } onChange={ this.handleSwitch }/>
</Authorize>
</PropertiesItem>
}
</Properties>
<PermissionsTableForm
......@@ -127,13 +176,20 @@ class Permissions extends React.Component<IPermissionsProps, any> {
autocompleteObj={ autocompleteObj }
inherits={ aclObjectIdentity.entriesInheriting }
/>
<Authorize role="ROLE_ADMINISTRATOR">
<ChangeParentForm
onSubmit={ this.updateParentObject }
onCancel={ this.handleCancel }
initialValues={ typeof aclObjectIdentity.parentObject === 'number' ?
{ id: aclObjectIdentity.parentObject } : aclObjectIdentity.parentObject }
/>
</Authorize>
</div>
}
</DialogContent>
<DialogActions>
<Button onClick={ this.hide }>
{ t('common:action.close') }
</Button>
<Button onClick={ this.hide }>{ t('common:action.close') }</Button>
</DialogActions>
</Dialog>
}
......@@ -146,6 +202,9 @@ const mapDispatchToProps = (dispatch) => bindActionCreators({
permissions,
addPermission,
autocomplete,
updateInheriting,
updateParentObject,
change,
}, dispatch);
export default translate()(connect(null, mapDispatchToProps)(withStyles(styles)<any>(Permissions)));
......@@ -111,7 +111,7 @@ class PermissionsTableForm extends React.Component<IPermissionsTableFormProps, a
name={ `${entry}.create` }
component={ ReduxCheckbox }
onChange={ this.save(fields, index, isNewEntry) }
disabled={ owner || inherits }
disabled={ !(!owner && !inherits) }
/>
</TableCell>
<TableCell>
......@@ -119,7 +119,7 @@ class PermissionsTableForm extends React.Component<IPermissionsTableFormProps, a
name={ `${entry}.read` }
component={ ReduxCheckbox }
onChange={ this.save(fields, index, isNewEntry) }
disabled={ owner || inherits }
disabled={ !(!owner && !inherits) }
/>
</TableCell>
<TableCell>
......@@ -127,7 +127,7 @@ class PermissionsTableForm extends React.Component<IPermissionsTableFormProps, a
name={ `${entry}.write` }
component={ ReduxCheckbox }
onChange={ this.save(fields, index, isNewEntry) }
disabled={ owner || inherits }
disabled={ !(!owner && !inherits) }
/>
</TableCell>
<TableCell>
......@@ -135,7 +135,7 @@ class PermissionsTableForm extends React.Component<IPermissionsTableFormProps, a
name={ `${entry}.delete` }
component={ ReduxCheckbox }
onChange={ this.save(fields, index, isNewEntry) }
disabled={ owner || inherits }
disabled={ !(!owner && !inherits) }
/>
</TableCell>
<TableCell>
......@@ -143,7 +143,7 @@ class PermissionsTableForm extends React.Component<IPermissionsTableFormProps, a
name={ `${entry}.manage` }
component={ ReduxCheckbox }
onChange={ this.save(fields, index, isNewEntry) }
disabled={ owner || inherits }
disabled={ !(!owner && !inherits) }
/>
</TableCell>
<TableCell>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment