Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the User data only if the validation passes #3345

Merged
merged 2 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/ops_controller/ops_rbac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def rbac_edit_save_or_add(what, rbac_suffix = what)
when :user
record = @edit[:user_id] ? User.find_by(:id => @edit[:user_id]) : User.new
validated = rbac_user_validate?
rbac_user_set_record_vars(record)
rbac_user_set_record_vars(record) if validated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgalis, change looks good, but i am curious why is invalid record being updated, shouldn't it skip save if record is not valid at https://github.com/lgalis/manageiq-ui-classic/blob/62358a3c6baef44122cb963fab2e576678aac7ba/app/controllers/ops_controller/ops_rbac.rb#L736

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rbac_user_set_record_vars updates the user's HABTM miq_groups link directly, without save being called for the User, this is how Rails' has_many and has_and_belongs_to_many associations work.
I also do not think there's a reason to set the record values if the UI validation is not successful. ( The validation for the Role doesn ot have this issue because the miq_user_role's features are not assigned directly).

when :group then
record = @edit[:group_id] ? MiqGroup.find_by(:id => @edit[:group_id]) : MiqGroup.new
validated = rbac_group_validate?
Expand Down
19 changes: 19 additions & 0 deletions spec/controllers/ops_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@
expect(flash_messages.first[:message]).to eq("A User must be assigned to a Group")
expect(flash_messages.first[:level]).to eq(:error)
end

it 'does not update the user without validation' do
user1 = FactoryGirl.create(:user, :name => "User1", :userid => "User1", :miq_groups => [group], :email => "[email protected]")

session[:edit] = {:key => "rbac_user_edit__#{user1.id}",
:new => {:name => 'test8',
:userid => 'test8',
:email => '[email protected]',
:group => nil,
:password => 'test8',
:verify => 'test8'}}
expect(controller).to receive(:render_flash)
post :rbac_user_edit, :params => {:button => 'save', :id => user1.id}
flash_messages = assigns(:flash_array)
expect(flash_messages.first[:message]).to eq("A User must be assigned to a Group")
expect(flash_messages.first[:level]).to eq(:error)
expect(user1.miq_groups).to eq([group])
expect(user1.name).to eq('User1')
end
end

context "#db_backup" do
Expand Down