Skip to content

Commit 3c22295

Browse files
Minor fixes
1 parent 9928380 commit 3c22295

File tree

13 files changed

+121
-94
lines changed

13 files changed

+121
-94
lines changed

app/controllers/ops_controller/ops_rbac.rb

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ def rbac_role_add
9898
rbac_edit_reset('new', 'role', MiqUserRole)
9999
end
100100

101-
def rbac_role_add_react
102-
assert_privileges("rbac_role_add")
103-
@hide_bottom_bar = true
104-
if params[:id] != 'new'
105-
rbac_edit_reset_react('edit', 'role', MiqUserRole)
106-
else
107-
rbac_edit_reset_react('new', 'role', MiqUserRole)
108-
end
109-
end
110-
111101
def rbac_role_copy
112102
assert_privileges("rbac_role_copy")
113103
@hide_bottom_bar = true
@@ -124,14 +114,25 @@ def rbac_role_edit
124114
end
125115
end
126116

127-
def rbac_role_edit_get
117+
def rbac_role_save
118+
@hide_bottom_bar = true
119+
if params[:id] != 'new'
120+
assert_privileges("rbac_role_edit")
121+
rbac_edit_reset_react('edit', 'role', MiqUserRole)
122+
else
123+
assert_privileges("rbac_role_add")
124+
rbac_edit_reset_react('new', 'role', MiqUserRole)
125+
end
126+
end
127+
128+
def rbac_role_get_values
128129
assert_privileges("rbac_role_edit")
129130
unless params[:id]
130131
obj = find_checked_items
131132
@_params[:id] = obj[0]
132133
end
133134
@hide_bottom_bar = true
134-
role = MiqUserRole.find_by(id: params[:id])
135+
role = MiqUserRole.find_by(:id => params[:id])
135136
render :json => {
136137
:name => role.name,
137138
:vm_restriction => role[:settings] && role[:settings][:restrictions][:vms],
@@ -682,8 +683,7 @@ def rbac_edit_reset(operation, what, klass)
682683
def rbac_edit_reset_react(operation, what, klass)
683684
key = what.to_sym
684685
if operation != "new"
685-
record = MiqUserRole.find_by(id: params[:id])
686-
record.miq_product_features = [MiqProductFeature.find_by(:identifier => MiqProductFeature.feature_root)]
686+
record = MiqUserRole.find_by(:id => params[:id])
687687
end
688688

689689
case operation
@@ -718,16 +718,18 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
718718
case key
719719
when :user
720720
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
721+
721722
record = @edit[:user_id] ? User.find_by(:id => @edit[:user_id]) : User.new
722723
validated = rbac_user_validate?
723724
rbac_user_set_record_vars(record)
724-
when :group then
725+
when :group
726+
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
727+
725728
record = @edit[:group_id] ? MiqGroup.find_by(:id => @edit[:group_id]) : MiqGroup.new
726729
validated = rbac_group_validate?
727730
rbac_group_set_record_description_role(record) # Set new Description, Role and Tenant for a new Group
728731
rbac_group_set_record_vars(record) if validated
729-
when :role then
730-
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
732+
when :role
731733
record = @edit[:role_id] ? MiqUserRole.find_by(:id => @edit[:role_id]) : MiqUserRole.new
732734
validated = rbac_role_validate?
733735
rbac_role_set_record_vars(record)
@@ -1393,25 +1395,25 @@ def rbac_role_add_parent(node)
13931395

13941396
# Set role record variables to new values
13951397
def rbac_role_set_record_vars(role)
1396-
role.name = @edit[:new][:name]
1398+
role.name = params[:name]
13971399
role.settings ||= {}
13981400
role.settings[:restrictions] ||= {}
1399-
if @edit[:new][:vm_restriction] == :none
1401+
if params[:vm_restriction] == :none
14001402
role.settings[:restrictions].delete(:vms)
14011403
else
1402-
role.settings[:restrictions][:vms] = @edit[:new][:vm_restriction]
1404+
role.settings[:restrictions][:vms] = params[:vm_restriction]
14031405
end
1404-
if @edit[:new][:service_template_restriction] == :none
1406+
if params[:service_template_restriction] == :none
14051407
role.settings[:restrictions].delete(:service_templates)
14061408
else
1407-
role.settings[:restrictions][:service_templates] = @edit[:new][:service_template_restriction]
1409+
role.settings[:restrictions][:service_templates] = params[:service_template_restriction]
14081410
end
14091411
role.settings = nil if role.settings[:restrictions].blank?
14101412
end
14111413

14121414
def populate_role_features(role)
14131415
role.miq_product_features =
1414-
MiqProductFeature.find_all_by_identifier(rbac_compact_features(@edit[:new][:features]))
1416+
MiqProductFeature.find_all_by_identifier(rbac_compact_features(params[:features]))
14151417
User.current_user.reload
14161418
end
14171419

app/helpers/ops_helper/role_rbac_details_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def rbac_role_info_view(role, rbac_menu_tree)
2222
})
2323
end
2424

25-
def rbac_role_product_features(role, rbac_menu_tree)
25+
def rbac_role_product_features(rbac_menu_tree)
2626
cells = row_data(_("Product Features (Read Only)"), {:input => 'component', :component => 'TREE_VIEW_REDUX', :props => rbac_menu_tree.locals_for_render, :name => rbac_menu_tree.name})
2727
cells[:cells][:value][:props]
2828
end

app/javascript/components/rbac-role-form/index.jsx

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import React, { useState, useEffect } from 'react';
22
import {
3-
Folder20, Search20, RuleFilled20, Edit20
3+
Folder20, Search20, RuleFilled20, Edit20,
44
} from '@carbon/icons-react';
55
import MiqFormRenderer, { useFormApi } from '@@ddf';
66
import { FormSpy } from '@data-driven-forms/react-form-renderer';
77
import PropTypes from 'prop-types';
88
import { Loading, Button } from 'carbon-components-react';
99
import createSchema from './rbac-role-form.schema';
1010
import miqRedirectBack from '../../helpers/miq-redirect-back';
11+
import miqFlash from '../../helpers/miq-flash';
1112

1213
let idCounter = 0;
1314

1415
let modified = false;
1516
let features = new Set();
1617
const RbacRoleForm = (props) => {
1718
const {
18-
selectOptions, url, getURL, customProps, role, existingProductFeatures,
19+
selectOptions, url, customProps, role, existingProductFeatures,
1920
} = props;
2021

21-
const generateId = () => idCounter++;
22+
const generateId = () => {
23+
idCounter += 1;
24+
return idCounter;
25+
};
2226

2327
// necessary for older roles that do only have top level nodes as features
2428
const checkChildren = (productFeature, child) => {
2529
if (!child.children) {
2630
features.add(child.value);
2731
} else {
28-
for (let nextChild of child.children) {
32+
child.children.forEach((nextChild) => {
2933
checkChildren(productFeature, nextChild);
30-
}
34+
});
3135
}
3236
};
3337

@@ -38,15 +42,15 @@ const RbacRoleForm = (props) => {
3842
if (result === productFeature) {
3943
features.add(node.value);
4044
if (node.children) {
41-
for (let child of node.children) {
45+
node.children.forEach((child) => {
4246
checkChildren(productFeature, child);
43-
}
47+
});
4448
}
4549
}
4650
if (node.children) {
47-
for (let child of node.children) {
51+
node.children.forEach((child) => {
4852
findCheck(productFeature, child);
49-
}
53+
});
5054
}
5155
};
5256

@@ -103,6 +107,7 @@ const RbacRoleForm = (props) => {
103107
if (values.tree_dropdown === undefined) {
104108
values.tree_dropdown = formData.initialValues.miqProductFeatures || [];
105109
}
110+
106111
if (values) {
107112
if (values.name === formData.initialValues.name
108113
&& values.vm_restriction === formData.initialValues.vm_restriction
@@ -113,6 +118,10 @@ const RbacRoleForm = (props) => {
113118
modified = true;
114119
}
115120

121+
if (values.tree_dropdown.length === 0 || values.tree_dropdown.length === undefined) {
122+
errors.tree_dropdown = 'Required';
123+
}
124+
116125
if (values.name === '' || (values && values.tree_dropdown && values.tree_dropdown.length === 0)) {
117126
errors.valid = 'not_valid';
118127
}
@@ -123,15 +132,9 @@ const RbacRoleForm = (props) => {
123132
const isEdit = !!(role && role.id);
124133

125134
useEffect(() => {
126-
if (formData.isLoading) {
127-
http.post(url, formData.params)
128-
.then(() => {
129-
const confirmation = isEdit ? __('Role Edited') : __('Role Created');
130-
miqRedirectBack(sprintf(confirmation), 'success', '/ops/explorer');
131-
})
132-
.catch((error) => console.log('error: ', error));
133-
} else if (isEdit) {
134-
http.get(`${getURL}/${role.id}`).then((roleValues) => {
135+
if (isEdit) {
136+
miqSparkleOn();
137+
http.get(`rbac_role_get_values/${role.id}`).then((roleValues) => {
135138
if (roleValues) {
136139
const bsTree = JSON.parse(customProps.bs_tree);
137140
const nodes = bsTree.map(transformTree);
@@ -141,6 +144,7 @@ const RbacRoleForm = (props) => {
141144
setFormData({
142145
...formData, isLoading: false, initialValues: roleValues, nodes, checked: features,
143146
});
147+
miqSparkleOff();
144148
}
145149
});
146150
} else {
@@ -160,7 +164,6 @@ const RbacRoleForm = (props) => {
160164
});
161165
}
162166
initialValues.tree_dropdown = features;
163-
console.log("initial Values: ", initialValues);
164167
if (initialValues) {
165168
setFormData({
166169
...formData, isLoading: false, initialValues, nodes, checked: features,
@@ -196,11 +199,15 @@ const RbacRoleForm = (props) => {
196199
features: productFeatures,
197200
};
198201

199-
setFormData({
200-
...formData,
201-
isLoading: true,
202-
params,
203-
});
202+
http.post(url, params)
203+
.then(() => {
204+
const confirmation = sprintf(__('Role "%s" was saved'), params.name);
205+
miqRedirectBack(sprintf(confirmation), 'success', '/ops/explorer');
206+
})
207+
.catch((error) => {
208+
miqSparkleOff();
209+
miqFlash('error', error);
210+
});
204211
};
205212

206213
const onCancel = () => {
@@ -215,7 +222,7 @@ const RbacRoleForm = (props) => {
215222
const onReset = () => {
216223
features = new Set();
217224
idCounter = 0;
218-
http.get(`${getURL}/${role.id}`).then((roleValues) => {
225+
http.get(`rbac_role_get_values/${role.id}`).then((roleValues) => {
219226
if (roleValues) {
220227
if (roleValues.miqProductFeatures) {
221228
const bsTree = JSON.parse(customProps.bs_tree);
@@ -317,7 +324,6 @@ const FormTemplate = ({
317324
RbacRoleForm.propTypes = {
318325
selectOptions: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string.isRequired)).isRequired,
319326
url: PropTypes.string,
320-
getURL: PropTypes.string,
321327
customProps: PropTypes.oneOfType([PropTypes.string, PropTypes.object, PropTypes.array]).isRequired,
322328
role: PropTypes.shape({
323329
id: PropTypes.number,
@@ -334,7 +340,6 @@ RbacRoleForm.propTypes = {
334340

335341
RbacRoleForm.defaultProps = {
336342
url: '',
337-
getURL: '',
338343
role: undefined,
339344
existingProductFeatures: undefined,
340345
};
@@ -343,9 +348,8 @@ FormTemplate.propTypes = {
343348
formFields: PropTypes.arrayOf(
344349
PropTypes.shape({ selectOptions: PropTypes.arrayOf(PropTypes.string) }),
345350
PropTypes.shape({ url: PropTypes.string }),
346-
PropTypes.shape({ getURL: PropTypes.string }),
347-
PropTypes.shape({ customProps: PropTypes.object }),
348-
PropTypes.shape({ role: PropTypes.object }),
351+
PropTypes.shape({ customProps: PropTypes.objectOf(PropTypes.any) }),
352+
PropTypes.shape({ role: PropTypes.objectOf(PropTypes.any) }),
349353
),
350354
roleId: PropTypes.number,
351355
};

app/javascript/components/rbac-role-form/rbac-role-form.schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const createSchema = (selectOptions, customProps, formData) => ({
4848
className: 'checkbox-tree',
4949
nodes: formData.nodes,
5050
checked: formData.checked,
51+
isRequired: true,
5152
},
5253
],
5354
});

app/javascript/components/tree-view/checkbox_tree.jsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const carbonIcons = {
1818
};
1919

2020
const CheckboxTreeComponent = (props) => {
21-
const { label, nodes, checked } = props;
21+
const {
22+
label, nodes, checked, isRequired,
23+
} = props;
2224
const { input } = useFieldApi(props);
2325

2426
const [treeState, setTreeState] = useState({
@@ -32,8 +34,9 @@ const CheckboxTreeComponent = (props) => {
3234
}, [treeState.checked]);
3335

3436
return (
35-
<div>
36-
<p>{label}</p>
37+
<div className="checkbox-tree-wrapper">
38+
{isRequired ? <span className="checkbox-tree-required-tag">*</span> : undefined}
39+
<p className="checkbox-tree-title">{label}</p>
3740
<CheckboxTree
3841
icons={carbonIcons}
3942
nodes={nodes}
@@ -52,11 +55,13 @@ CheckboxTreeComponent.propTypes = {
5255
label: PropTypes.string.isRequired,
5356
nodes: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
5457
checked: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
58+
isRequired: PropTypes.bool,
5559
};
5660

5761
CheckboxTreeComponent.defaultProps = {
5862
nodes: {},
5963
checked: [],
64+
isRequired: false,
6065
};
6166

6267
export default CheckboxTreeComponent;

app/javascript/spec/rbac-role-form/__snapshots__/rbac-role-form.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ exports[`Rbac Role Form Component render add rbac role form 1`] = `
8787
"className": "checkbox-tree",
8888
"component": "checkbox-tree",
8989
"id": "tree_dropdown",
90+
"isRequired": true,
9091
"label": "Product Features (Editing)",
9192
"name": "tree_dropdown",
9293
"nodes": Array [],
@@ -187,6 +188,7 @@ exports[`Rbac Role Form Component render copy rbac role form 1`] = `
187188
"className": "checkbox-tree",
188189
"component": "checkbox-tree",
189190
"id": "tree_dropdown",
191+
"isRequired": true,
190192
"label": "Product Features (Editing)",
191193
"name": "tree_dropdown",
192194
"nodes": Array [],
@@ -287,6 +289,7 @@ exports[`Rbac Role Form Component render edit rbac role form 1`] = `
287289
"className": "checkbox-tree",
288290
"component": "checkbox-tree",
289291
"id": "tree_dropdown",
292+
"isRequired": true,
290293
"label": "Product Features (Editing)",
291294
"name": "tree_dropdown",
292295
"nodes": Array [],

app/javascript/spec/rbac-role-form/rbac-role-form.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Rbac Role Form Component', () => {
7171
role={RbacRoleEditData}
7272
/>);
7373

74-
fetchMock.get(`/ops/rbac_role_edit_get/${RbacRoleEditData.id}`, RbacRoleEditData);
74+
fetchMock.get(`/ops/rbac_role_get_values/${RbacRoleEditData.id}`, RbacRoleEditData);
7575
await new Promise((resolve) => {
7676
setImmediate(() => {
7777
wrapper.update();
@@ -96,4 +96,4 @@ describe('Rbac Role Form Component', () => {
9696
});
9797
});
9898
});
99-
});
99+
});

app/stylesheet/application-webpack.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
@import './no-records-found.scss';
2424
@import './notifications.scss';
2525
@import './quadicon.scss';
26-
@import './rbac-role-form.scss';
2726
@import './search-bar.scss';
2827
@import './settings.scss';
2928
@import './responsive_layout.scss';
@@ -33,3 +32,4 @@
3332
@import './widget.scss';
3433
@import './workflows.scss';
3534
@import './cloud-container-projects-dashboard.scss';
35+
@import './checkbox-tree.scss';

0 commit comments

Comments
 (0)