Skip to content

Commit 55e116c

Browse files
Convert user form to react
1 parent 3e0cd03 commit 55e116c

File tree

52 files changed

+1681
-519
lines changed

Some content is hidden

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

52 files changed

+1681
-519
lines changed

app/controllers/ops_controller.rb

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

599599
replace_explorer_trees(replace_trees, presenter)
600600
rebuild_toolbars(presenter)
601-
handle_bottom_cell(nodetype, presenter, locals)
601+
unless @hide_bottom_bar
602+
handle_bottom_cell(nodetype, presenter, locals)
603+
end
602604
x_active_tree_replace_cell(nodetype, presenter)
603605
extra_js_commands(presenter)
604606

app/controllers/ops_controller/ops_rbac.rb

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ def rbac_tags_edit
4949

5050
def rbac_user_add
5151
assert_privileges("rbac_user_add")
52+
@hide_bottom_bar = true
5253
rbac_edit_reset('new', 'user', User)
5354
end
5455

5556
def rbac_user_copy
5657
# get users id either from gtl check or detail id
5758
user_id = params[:miq_grid_checks].presence || params[:id]
5859
user = User.find(user_id)
60+
@hide_bottom_bar = true
5961
# check if it is allowed to copy the user
6062
if rbac_user_copy_restriction?(user)
6163
rbac_restricted_user_copy_flash(user)
@@ -65,16 +67,14 @@ def rbac_user_copy
6567
return
6668
end
6769
assert_privileges("rbac_user_copy")
70+
@hide_bottom_bar = true
6871
rbac_edit_reset('copy', 'user', User)
6972
end
7073

7174
def rbac_user_edit
7275
assert_privileges("rbac_user_edit")
73-
case params[:button]
74-
when 'cancel' then rbac_edit_cancel('user')
75-
when 'save', 'add' then rbac_edit_save_or_add('user')
76-
when 'reset', nil then rbac_edit_reset(params[:typ], 'user', User) # Reset or first time in
77-
end
76+
@hide_bottom_bar = true
77+
rbac_edit_reset(params[:typ], 'user', User)
7878
end
7979

8080
def rbac_group_add
@@ -183,13 +183,6 @@ def rbac_tenant_tags_edit
183183
end
184184
end
185185

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

@@ -658,10 +651,6 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
658651
return unless load_edit("rbac_#{what}_edit__#{id}", "replace_cell__explorer")
659652

660653
case key
661-
when :user
662-
record = @edit[:user_id] ? User.find_by(:id => @edit[:user_id]) : User.new
663-
validated = rbac_user_validate?
664-
rbac_user_set_record_vars(record)
665654
when :group then
666655
record = @edit[:group_id] ? MiqGroup.find_by(:id => @edit[:group_id]) : MiqGroup.new
667656
validated = rbac_group_validate?
@@ -674,7 +663,6 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
674663
end
675664

676665
if record.valid? && validated && record.save!
677-
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
678666
populate_role_features(record) if what == "role"
679667
self.current_user = record if what == 'user' && @edit[:current][:userid] == current_userid
680668
AuditEvent.success(build_saved_audit(record, @edit))
@@ -744,13 +732,10 @@ def rbac_field_changed(rec_type)
744732
return unless load_edit("rbac_#{rec_type}_edit__#{id}", "replace_cell__explorer")
745733

746734
case rec_type
747-
when "user" then rbac_user_get_form_vars
748735
when "group" then rbac_group_get_form_vars
749736
when "role" then rbac_role_get_form_vars
750737
end
751738

752-
@edit[:new][:group] = rbac_user_get_group_ids if rec_type == "user"
753-
754739
session[:changed] = changed = @edit[:new] != @edit[:current]
755740

756741
render :update do |page|
@@ -762,11 +747,6 @@ def rbac_field_changed(rec_type)
762747
page.replace(@refresh_div, :partial => @refresh_partial)
763748
end
764749
else
765-
# only do following for user (adding/editing a user)
766-
if x_node.split("-").first == "u" || x_node == "xx-u"
767-
page.replace("group_selected",
768-
:partial => "ops/rbac_group_selected")
769-
end
770750
# only do following for groups
771751
if @refresh_div
772752
page.replace(@refresh_div,
@@ -964,82 +944,16 @@ def build_rbac_feature_tree
964944
# Set form variables for user edit
965945
def rbac_user_set_form_vars
966946
copy = @sb[:typ] == "copy"
967-
# save a shadow copy of the record if record is being copied
968-
@user = copy ? @record.dup : @record
969-
@user.miq_groups = @record.miq_groups if copy
970947
@edit = {:new => {}, :current => {}}
971948
@edit[:user_id] = @record.id unless copy
972-
@edit[:key] = "rbac_user_edit__#{@edit[:user_id] || "new"}"
973-
# prefill form fields for edit and copy action
974-
@edit[:new].merge!(:name => @user.name,
975-
:email => @user.email,
976-
:group => @user.miq_groups ? @user.miq_groups.map(&:id).sort : nil)
977-
unless copy
978-
@edit[:new].merge!(:userid => @user.userid,
979-
:password => @user.password,
980-
:verify => @user.password)
981-
end
982-
# load all user groups, filter available for tenant
983-
@edit[:groups] = Rbac.filtered(MiqGroup.non_tenant_groups_in_my_region).sort_by { |g| g.description.downcase }.collect { |g| [g.description, g.id] }
984-
# store current state of the new users information
985-
@edit[:current] = copy_hash(@edit[:new])
949+
@edit[:new][:userid] = @record.userid
986950
@right_cell_text = if @edit[:user_id]
987951
_("Editing User \"%{name}\"") % {:name => @record.name}
988952
else
989953
_('Adding a new User')
990954
end
991955
end
992956

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