Skip to content

Commit 2a03fb0

Browse files
Convert user form to react
1 parent c2bd3bf commit 2a03fb0

File tree

54 files changed

+1585
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1585
-520
lines changed

app/controllers/ops_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ def replace_right_cell(options = {})
583583

584584
replace_explorer_trees(replace_trees, presenter)
585585
rebuild_toolbars(presenter)
586-
handle_bottom_cell(nodetype, presenter, locals)
586+
unless @hide_bottom_bar
587+
handle_bottom_cell(nodetype, presenter, locals)
588+
end
587589
x_active_tree_replace_cell(nodetype, presenter)
588590
extra_js_commands(presenter)
589591

app/controllers/ops_controller/ops_rbac.rb

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ def rbac_tags_edit
5151

5252
def rbac_user_add
5353
assert_privileges("rbac_user_add")
54+
@hide_bottom_bar = true
5455
rbac_edit_reset('new', 'user', User)
5556
end
5657

5758
def rbac_user_copy
5859
# get users id either from gtl check or detail id
5960
user_id = params[:miq_grid_checks].presence || params[:id]
60-
user = User.find(user_id)
61+
user = User.find(user_id)
62+
6163
# check if it is allowed to copy the user
6264
if rbac_user_copy_restriction?(user)
6365
rbac_restricted_user_copy_flash(user)
@@ -66,17 +68,17 @@ def rbac_user_copy
6668
javascript_flash
6769
return
6870
end
71+
72+
@copy_user_id = user_id
6973
assert_privileges("rbac_user_copy")
74+
@hide_bottom_bar = true
7075
rbac_edit_reset('copy', 'user', User)
7176
end
7277

7378
def rbac_user_edit
7479
assert_privileges("rbac_user_edit")
75-
case params[:button]
76-
when 'cancel' then rbac_edit_cancel('user')
77-
when 'save', 'add' then rbac_edit_save_or_add('user')
78-
when 'reset', nil then rbac_edit_reset(params[:typ], 'user', User) # Reset or first time in
79-
end
80+
@hide_bottom_bar = true
81+
rbac_edit_reset(params[:typ], 'user', User)
8082
end
8183

8284
def rbac_group_add
@@ -185,13 +187,6 @@ def rbac_tenant_tags_edit
185187
end
186188
end
187189

188-
# AJAX driven routines to check for changes in ANY field on the form
189-
def rbac_user_field_changed
190-
assert_privileges(params[:id] == "new" ? "rbac_user_add" : "rbac_user_edit")
191-
192-
rbac_field_changed("user")
193-
end
194-
195190
def rbac_group_field_changed
196191
assert_privileges(params[:id] == "new" ? "rbac_group_add" : "rbac_group_edit")
197192

@@ -660,10 +655,6 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
660655
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
661656

662657
case key
663-
when :user
664-
record = @edit[:user_id] ? User.find_by(:id => @edit[:user_id]) : User.new
665-
validated = rbac_user_validate?
666-
rbac_user_set_record_vars(record)
667658
when :group then
668659
record = @edit[:group_id] ? MiqGroup.find_by(:id => @edit[:group_id]) : MiqGroup.new
669660
validated = rbac_group_validate?
@@ -676,7 +667,6 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
676667
end
677668

678669
if record.valid? && validated && record.save!
679-
record.update!(:miq_groups => Rbac.filtered(MiqGroup.where(:id => rbac_user_get_group_ids))) if key == :user # only set miq_groups if everything is valid
680670
populate_role_features(record) if what == "role"
681671
self.current_user = record if what == 'user' && @edit[:current][:userid] == current_userid
682672
AuditEvent.success(build_saved_audit(record, @edit))
@@ -746,13 +736,10 @@ def rbac_field_changed(rec_type)
746736
return unless load_edit("rbac_#{rec_type}_edit__#{id}", "replace_cell__explorer")
747737

748738
case rec_type
749-
when "user" then rbac_user_get_form_vars
750739
when "group" then rbac_group_get_form_vars
751740
when "role" then rbac_role_get_form_vars
752741
end
753742

754-
@edit[:new][:group] = rbac_user_get_group_ids if rec_type == "user"
755-
756743
session[:changed] = changed = @edit[:new] != @edit[:current]
757744

758745
render :update do |page|
@@ -764,11 +751,6 @@ def rbac_field_changed(rec_type)
764751
page.replace(@refresh_div, :partial => @refresh_partial)
765752
end
766753
else
767-
# only do following for user (adding/editing a user)
768-
if x_node.split("-").first == "u" || x_node == "xx-u"
769-
page.replace("group_selected",
770-
:partial => "ops/rbac_group_selected")
771-
end
772754
# only do following for groups
773755
if @refresh_div
774756
page.replace(@refresh_div,
@@ -966,82 +948,16 @@ def build_rbac_feature_tree
966948
# Set form variables for user edit
967949
def rbac_user_set_form_vars
968950
copy = @sb[:typ] == "copy"
969-
# save a shadow copy of the record if record is being copied
970-
@user = copy ? @record.dup : @record
971-
@user.miq_groups = @record.miq_groups if copy
972951
@edit = {:new => {}, :current => {}}
973952
@edit[:user_id] = @record.id unless copy
974-
@edit[:key] = "rbac_user_edit__#{@edit[:user_id] || "new"}"
975-
# prefill form fields for edit and copy action
976-
@edit[:new].merge!(:name => @user.name,
977-
:email => @user.email,
978-
:group => @user.miq_groups ? @user.miq_groups.map(&:id).sort : nil)
979-
unless copy
980-
@edit[:new].merge!(:userid => @user.userid,
981-
:password => @user.password,
982-
:verify => @user.password)
983-
end
984-
# load all user groups, filter available for tenant
985-
@edit[:groups] = Rbac.filtered(MiqGroup.non_tenant_groups_in_my_region).sort_by { |g| g.description.downcase }.collect { |g| [g.description, g.id] }
986-
# store current state of the new users information
987-
@edit[:current] = copy_hash(@edit[:new])
953+
@edit[:new][:userid] = @record.userid
988954
@right_cell_text = if @edit[:user_id]
989955
_("Editing User \"%{name}\"") % {:name => @record.name}
990956
else
991957
_('Adding a new User')
992958
end
993959
end
994960

995-
# Get variables from user edit form
996-
def rbac_user_get_form_vars
997-
copy_params_if_present(@edit[:new], params, %i[name password verify])
998-
999-
@edit[:new][:userid] = params[:userid].strip.presence if params[:userid]
1000-
@edit[:new][:email] = params[:email].strip.presence if params[:email]
1001-
@edit[:new][:group] = params[:chosen_group] if params[:chosen_group]
1002-
end
1003-
1004-
# Set user record variables to new values
1005-
def rbac_user_set_record_vars(user)
1006-
user.name = @edit[:new][:name]
1007-
user.userid = @edit[:new][:userid]
1008-
user.email = @edit[:new][:email]
1009-
user.password = @edit[:new][:password] if @edit[:new][:password]
1010-
end
1011-
1012-
# Get array of group ids
1013-
def rbac_user_get_group_ids
1014-
case @edit[:new][:group]
1015-
when 'null', nil
1016-
[]
1017-
when String
1018-
@edit[:new][:group].split(',').delete_if(&:blank?).map(&:to_i).sort
1019-
when Array
1020-
@edit[:new][:group].map(&:to_i).sort
1021-
end
1022-
end
1023-
1024-
# Validate some of the user fields
1025-
def rbac_user_validate?
1026-
valid = true
1027-
if @edit[:new][:password] != @edit[:new][:verify]
1028-
add_flash(_("Password/Verify Password do not match"), :error)
1029-
valid = false
1030-
end
1031-
1032-
new_group_ids = rbac_user_get_group_ids
1033-
new_groups = new_group_ids.present? && MiqGroup.find(new_group_ids).present? ? MiqGroup.find(new_group_ids) : []
1034-
1035-
if new_group_ids.blank?
1036-
add_flash(_("A User must be assigned to a Group"), :error)
1037-
valid = false
1038-
elsif Rbac.filtered(new_groups).count != new_group_ids.count
1039-
add_flash(_("A User must be assigned to an allowed Group"), :error)
1040-
valid = false
1041-
end
1042-
valid
1043-
end
1044-
1045961
def valid_tenant?(tenant_id)
1046962
Rbac.filtered(Tenant.in_my_region.where(:id => tenant_id)).present?
1047963
end

app/javascript/components/async-credentials/edit-password-field.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useFieldApi, componentTypes } from '@@ddf';
1010

1111
const EditPasswordField = ({ componentClass, ...props }) => {
1212
const {
13-
labelText, validateOnMount, isDisabled, editMode, setEditMode, buttonLabel, input, meta, ...rest
13+
labelText, validateOnMount, isDisabled, editMode, setEditMode, buttonLabel, input, meta, icon, kind, ...rest
1414
} = useFieldApi(prepareProps(props));
1515

1616
const invalid = (meta.touched || validateOnMount) && meta.error;
@@ -58,7 +58,14 @@ const EditPasswordField = ({ componentClass, ...props }) => {
5858
{ field }
5959
</div>
6060
<div className="bx--col-sm-1 bx--col-md-1 bx--col-lg-1">
61-
<Button hasIconOnly kind="secondary" size="field" onClick={setEditMode} iconDescription={buttonLabel} renderIcon={EditOff16} />
61+
<Button
62+
hasIconOnly
63+
kind={kind || 'secondary'}
64+
size="field"
65+
onClick={setEditMode}
66+
iconDescription={buttonLabel}
67+
renderIcon={icon || EditOff16}
68+
/>
6269
</div>
6370
</div>
6471
</div>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable jsx-a11y/label-has-associated-control */
2+
import React from 'react';
3+
import PropTypes from 'prop-types';
4+
5+
const SelectedGroupsList = ({ groups }) => {
6+
const selectedGroups = [];
7+
// console.log(groups);
8+
groups.sort();
9+
if (groups) {
10+
groups.forEach((group) => {
11+
selectedGroups.push(
12+
<div key={group}>
13+
<i className="ff ff-group" />
14+
<p className="group-name" id={group}>
15+
{group}
16+
</p>
17+
<br />
18+
</div>
19+
);
20+
});
21+
}
22+
23+
return (
24+
<div>
25+
<label id="selected-groups-label">
26+
{__('Selected Groups')}
27+
</label>
28+
<div id="selected-groups">
29+
{selectedGroups}
30+
</div>
31+
</div>
32+
);
33+
};
34+
35+
SelectedGroupsList.propTypes = {
36+
groups: PropTypes.arrayOf(PropTypes.string),
37+
};
38+
39+
SelectedGroupsList.defaultProps = {
40+
groups: [],
41+
};
42+
43+
export default SelectedGroupsList;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const areGroupsEqual = (initialValues, selectedGroups = []) => {
2+
selectedGroups.sort();
3+
initialValues.groups.sort();
4+
if (selectedGroups.length !== initialValues.groups.length) {
5+
return false;
6+
}
7+
return selectedGroups.every((group, index) => group === initialValues.groups[index]);
8+
};
9+
10+
export const passwordValidation = (initialValues, id, editMode, values, setState, selectedGroups) => {
11+
if (values.groups === undefined) {
12+
if (selectedGroups.length > 0) {
13+
setState((state) => ({
14+
...state,
15+
selectedGroups: [],
16+
}));
17+
}
18+
}
19+
const errors = {};
20+
const groupIds = [];
21+
if (values.groups) {
22+
values.groups.forEach((group) => {
23+
if (group.value) {
24+
groupIds.push(group.value);
25+
} else {
26+
groupIds.push(group);
27+
}
28+
});
29+
}
30+
if (!editMode && !!id) {
31+
values.password = undefined;
32+
values.confirmPassword = undefined;
33+
if (values.name === initialValues.name
34+
&& values.userid === initialValues.userid
35+
&& values.email === initialValues.email
36+
&& areGroupsEqual(initialValues, groupIds)) {
37+
errors.confirmPassword = '';
38+
}
39+
}
40+
41+
if (values.password === undefined) {
42+
if (!!id && editMode) {
43+
errors.password = 'Required';
44+
}
45+
}
46+
47+
if (editMode && values.password !== values.confirmPassword) {
48+
errors.confirmPassword = 'Password/Verify Password do not match';
49+
}
50+
if (!!id === false && values.password !== values.confirmPassword) {
51+
errors.confirmPassword = 'Password/Verify Password do not match';
52+
}
53+
return errors;
54+
};

0 commit comments

Comments
 (0)