Skip to content

Commit 81e8cd0

Browse files
committed
Add permissions support
1 parent 43587b5 commit 81e8cd0

File tree

6 files changed

+137
-65
lines changed

6 files changed

+137
-65
lines changed

src/components/catalog/CatalogForm.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import GenericInput from '../common/GenericInput';
55
import CatalogFormActions from './CatalogFormActions';
66

77
const CatalogForm = ({ inputList, formObject, collections,
8-
onSave, onClose, onChange, saving, errors }) => {
8+
onSave, onClose, onChange, saving, errors, permissions }) => {
99
inputList = inputList || [];
1010
return (
1111
<Form>
@@ -19,11 +19,12 @@ const CatalogForm = ({ inputList, formObject, collections,
1919
name={input.name}
2020
value={formObject[input.name]}
2121
label={input.displayName}
22+
permissions={permissions}
2223
onChange={onChange}/>
2324
);
2425
})}
2526
<CatalogFormActions
26-
permissions={formObject.objpermissions}
27+
permissions={permissions}
2728
onSave={onSave}
2829
onClose={onClose}
2930
/>
@@ -39,7 +40,8 @@ CatalogForm.propTypes = {
3940
onClose: PropTypes.func.isRequired,
4041
saving: PropTypes.bool,
4142
errors: PropTypes.object,
42-
collections: PropTypes.object
43+
collections: PropTypes.object,
44+
permissions: PropTypes.string
4345
};
4446

4547
export default CatalogForm;

src/components/catalog/CatalogFormActions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { Button } from 'antd';
55
const CatalogFormActions = ({ permissions, onClose, onSave }) => {
66
return (
77
<div style={{textAlign: 'right'}}>
8-
<Button style={{background: '#000', color: '#fff', marginRight: '7px'}} onClick={onClose}>Закрыть</Button>
9-
<Button type="primary" onClick={onSave}>Сохранить</Button>
8+
<Button style={{background: '#000', color: '#fff', marginRight: '7px'}} onClick={onClose}>Close</Button>
9+
{permissions.includes('U') && <Button type="primary" onClick={onSave}>Save</Button>}
1010
</div>
1111
);
1212
};

src/components/catalog/CatalogPage.js

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,76 +26,95 @@ class CatalogPage extends React.Component {
2626
{
2727
key: 'action',
2828
width: "100px",
29+
className: 'text-right',
2930
onCellClick: (record, event) => {
3031
if (
31-
event.target.matches('.anticon-edit') ||
32-
(event.target.firstElementChild &&
33-
event.target.firstElementChild.matches('.anticon-edit'))
32+
(event.target.matches('#view') ||
33+
(event.target.firstElementChild && event.target.firstElementChild.matches('#view'))) ||
34+
(event.target.matches('#edit') ||
35+
(event.target.firstElementChild && event.target.firstElementChild.matches('#edit')))
3436
) {
35-
this.edit(record);
36-
37-
} else if (
38-
event.target.matches('.anticon-delete') ||
39-
(event.target.firstElementChild &&
40-
event.target.firstElementChild.matches('.anticon-delete'))
37+
this.open(record);
38+
}
39+
else if (
40+
event.target.matches('#delete') ||
41+
(event.target.firstElementChild && event.target.firstElementChild.matches('#delete'))
4142
) {
4243
this.remove(record);
4344
}
4445
},
4546
render: (text, record) => (
4647
<span>
47-
<Button type="primary" icon="edit" style={{margin: "0 5px"}}/>
48-
<Button type="danger" icon="delete" style={{margin: "0 5px"}}/>
48+
{this.state.catalog.objpermissions.includes('U') &&
49+
<Button type="primary" id="edit" icon="edit" style={{margin: "0 5px"}}/>
50+
}
51+
52+
{!this.state.catalog.objpermissions.includes('U') &&
53+
this.state.catalog.objpermissions.includes('R') &&
54+
<Button type="primary" id="view" icon="search" style={{margin: "0 5px"}}/>
55+
}
56+
57+
{this.state.catalog.objpermissions.includes('D') &&
58+
<Button type="danger" icon="delete" style={{margin: "0 5px"}}/>
59+
}
4960
</span>
5061
)
5162
}
5263
];
5364

5465
this.state = {
55-
catalog: {},
66+
catalog: {
67+
name: '',
68+
objpermissions: ''
69+
},
5670
extent: []
5771
};
5872
}
5973

6074
componentDidMount() {
6175
const name = this.props.match.params.name;
6276

63-
let promise = CatalogApi.getCatalogInfo(name);
64-
promise = promise.then((response) => {
65-
this.setState({catalog: response.data});
66-
return this.loadCatalogExtent(name, promise);
67-
});
77+
CatalogApi.getCatalogInfo(name)
78+
.then((response) => {
79+
this.setState((state) => ({catalog: response.data}));
80+
81+
// Load catalog data only if we have permission for that
82+
if (response.data.objpermissions.includes('R')) {
83+
return this.loadCatalogExtent(name);
84+
}
85+
})
86+
.catch((error) => {
87+
const summary = (error && error.response && error.response.data && error.response.data.summary);
6888

69-
promise.catch((error) => {
7089
notification.error({
71-
message: 'An error has occurred: ' + error.summary
90+
message: 'An error has occurred: ' + summary
7291
});
7392
});
7493
}
7594

76-
loadCatalogExtent = (name, promise) => {
77-
if (promise) {
78-
promise = promise.then(() => {
79-
return CatalogApi.getCatalogExtent(name);
95+
loadCatalogExtent = (name) => {
96+
return CatalogApi.getCatalogExtent(name)
97+
.then((response) => {
98+
this.setState({extent: response.data.children});
99+
})
100+
.catch((error) => {
101+
const summary = (error && error.response && error.response.data && error.response.data.summary);
102+
103+
notification.error({
104+
message: 'An error has occurred: ' + summary
105+
});
80106
});
81-
}
82-
else {
83-
promise = CatalogApi.getCatalogExtent(name);
84-
}
85107

86-
promise = promise.then((response) => {
87-
this.setState({extent: response.data.children});
88-
});
89108
};
90109

91110
add = () => {
92111
const path = this.props.location.pathname;
93-
this.props.history.replace(`${path}/object`);
112+
this.props.history.push(`${path}/object`);
94113
};
95114

96-
edit = (record) => {
115+
open = (record) => {
97116
const path = this.props.location.pathname;
98-
this.props.history.replace(`${path}/object/${record._id}`);
117+
this.props.history.push(`${path}/object/${record._id}`);
99118
};
100119

101120
remove = (record) => {
@@ -128,29 +147,40 @@ class CatalogPage extends React.Component {
128147

129148
<div style={{background: '#fff', padding: 24, minHeight: 280}}>
130149

131-
<div className="clearfix" style={{width: "100%"}}>
150+
<div className="clearfix" style={{width: "100%", marginBottom: '20px'}}>
132151
<h2 style={{display: "inline"}}>
133152
{this.state.catalog.name || '...'}
134153
</h2>
135-
<Button type="primary"
136-
style={{float: 'right', marginBottom: 8}}
137-
size="small"
138-
onClick={this.add}
139-
className="clearfix"
140-
>
141-
Add
142-
</Button>
154+
155+
{(this.state.catalog.objpermissions.includes('C')) &&
156+
<Button type="primary"
157+
style={{float: 'right', marginBottom: 8}}
158+
size="small"
159+
onClick={this.add}
160+
className="clearfix"
161+
>
162+
Add
163+
</Button>
164+
}
165+
143166
</div>
144167

145-
<Table
146-
rowKey="_id"
147-
dataSource={this.state.extent}
148-
columns={this.columns}
149-
showHeader={false}
150-
bordered
151-
size="middle"
152-
pagination={{showSizeChanger: true}}
153-
/>
168+
{this.state.catalog.objpermissions.includes('R') &&
169+
<Table
170+
rowKey="_id"
171+
dataSource={this.state.extent}
172+
columns={this.columns}
173+
showHeader={false}
174+
size="middle"
175+
pagination={{showSizeChanger: true}}
176+
/>
177+
}
178+
179+
{this.state.catalog.name && !this.state.catalog.objpermissions.includes('R') &&
180+
<div style={{textAlign: 'center', padding: '20px'}}>
181+
<h3>You have no permissions to view this catalog.</h3>
182+
</div>
183+
}
154184
</div>
155185
</div>
156186
);

src/components/catalog/ManageCatalogPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export class ManageCatalogPage extends React.Component {
192192
collections={this.state.collections}
193193
errors={this.state.errors}
194194
saving={this.state.saving}
195+
permissions={this.state.catalog.objpermissions || ''}
195196
/>
196197
</div>
197198
}

src/components/common/GenericInput.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import moment from 'moment';
66

77
const GenericInput = ({ category, collection = [], collectionType,
88
type, name, label, onChange, placeholder,
9-
value, error }) => {
9+
value, error, permissions }) => {
1010

1111
function onChangeDate(name) {
1212
return function(date, dateString) {
@@ -64,27 +64,53 @@ const GenericInput = ({ category, collection = [], collectionType,
6464

6565
switch (type) {
6666
case '%Library.String':
67-
input = <Input value={value} onChange={onChangeString(name)} name={name}/>;
67+
if (permissions.includes('U')) {
68+
input = <Input value={value} onChange={onChangeString(name)} name={name}/>;
69+
}
70+
else {
71+
input = <Input value={value} readOnly name={name}/>;
72+
}
6873
break;
6974
case '%Library.Boolean':
70-
input = <Checkbox checked={value} onChange={onChangeBoolean(name)} name={name}>{label}</Checkbox>;
75+
if (permissions.includes('U')) {
76+
input = <Checkbox checked={value} onChange={onChangeBoolean(name)} name={name}>{label}</Checkbox>;
77+
}
78+
else {
79+
input = <Checkbox checked={value} readOnly name={name}>{label}</Checkbox>;
80+
}
7181
break;
7282
case '%Library.Date':
73-
input = (<DatePicker format={dateFormat} value={getMomentValue(value, dateFormat)}
74-
name={name} onChange={onChangeDate(name)}/>);
83+
if (permissions.includes('U')) {
84+
input = (<DatePicker format={dateFormat} value={getMomentValue(value, dateFormat)}
85+
name={name} onChange={onChangeDate(name)}/>);
86+
}
87+
else {
88+
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
89+
}
7590
break;
7691
case '%Library.TimeStamp':
77-
input = (<DatePicker format={datetimeFormat} value={getMomentValue(value, moment.ISO_8601)}
78-
showTime name={name} onChange={onChangeTimestamp(name)}/>);
92+
if (permissions.includes('U')) {
93+
input = (<DatePicker format={datetimeFormat} value={getMomentValue(value, moment.ISO_8601)}
94+
showTime name={name} onChange={onChangeTimestamp(name)}/>);
95+
}
96+
else {
97+
input = <Input value={getMomentValue(value, moment.ISO_8601)} name={name} readOnly style={{ width: '200px' }}/>;
98+
}
7999
break;
80100
case '%Library.Integer':
81101
case '%Library.Numeric':
82-
input = <InputNumber name={name} value={value} onChange={onChangeNumber(name)}/>;
102+
if (permissions.includes('U')) {
103+
input = <InputNumber name={name} value={value} onChange={onChangeNumber(name)}/>;
104+
}
105+
else {
106+
input = <Input value={value} name={name} readOnly style={{ width: '200px' }}/>;
107+
}
83108
break;
84109
default:
85110
if (category === 'form') {
86111

87112
if (!collectionType) {
113+
if (permissions.includes('U')) {
88114
let options = collection.map(item => {
89115
return <Select.Option key={item._id} value={item._id}>{item.displayName}</Select.Option>;
90116
});
@@ -95,7 +121,7 @@ const GenericInput = ({ category, collection = [], collectionType,
95121
}
96122

97123
input = (
98-
<Select showSearch style={{ width: 200 }}
124+
<Select showSearch style={{width: 200}}
99125
value={value}
100126
name={name}
101127
optionFilterProp="children"
@@ -104,6 +130,10 @@ const GenericInput = ({ category, collection = [], collectionType,
104130
{options}
105131
</Select>
106132
);
133+
}
134+
else {
135+
input = <Input name={name} value={value.name} readOnly style={{ width: '200px' }}/>;
136+
}
107137
}
108138

109139
}
@@ -135,6 +165,7 @@ GenericInput.propTypes = {
135165
label: PropTypes.string.isRequired,
136166
onChange: PropTypes.func.isRequired,
137167
placeholder: PropTypes.string,
168+
permissions: PropTypes.string,
138169
value: PropTypes.any,
139170
error: PropTypes.string
140171
};

src/styles/styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
margin-bottom: 12px;
1010
}
1111

12+
.text-center {
13+
text-align: center;
14+
}
15+
16+
.text-right {
17+
text-align: right;
18+
}
19+
1220
td.clickable-cell {
1321
cursor: pointer;
1422
}

0 commit comments

Comments
 (0)