-
Notifications
You must be signed in to change notification settings - Fork 897
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 users' current_group on group deletion #16809
Update users' current_group on group deletion #16809
Conversation
86363a6
to
cccef2d
Compare
cccef2d
to
10d2205
Compare
@lpichler , @gtanzillo - please review |
@miq-bot add_label bug, gaprindashvili/yes, blocker |
@lpichler @gtanzillo as this involves a blocker issue, can this be reviewed asap. |
@JPrause I am working on it 👍 |
app/models/miq_group.rb
Outdated
end | ||
|
||
def reset_current_group_for_users | ||
User.where(:current_group_id => id).each {|u| u.change_current_group(id)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like we have method for it already User#current_group_by_description
spec/models/miq_group_spec.rb
Outdated
group2 = FactoryGirl.create(:miq_group) | ||
FactoryGirl.create(:user, :miq_groups => [group, group2], :current_group => group2) | ||
FactoryGirl.create(:user, :miq_groups => [group, group2], :current_group => group) | ||
expect { expect { group.destroy }.to raise_error(RuntimeError, /The group is the login group for at least one user in the group/) }.to_not change { MiqGroup.count } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't succeed because both users are members of another group and you new logic would switch to current_group to group2
for the second user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
discussed with @lgalis and LGTM 👍
app/models/user.rb
Outdated
def change_current_group(id) | ||
user_groups = miq_group_ids | ||
user_groups.delete(id) | ||
raise _("The user's loging group cannot be changed.") if user_groups.empty? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a typo in this error message?
It looks like this would be the case when the user's only group would be the one deleted.
What is a "loging" group? Even if that's supposed to be "login group" I'm still not sure what that means.
app/models/miq_group.rb
Outdated
def ensure_can_be_destroyed | ||
raise _("The login group cannot be deleted") if current_user_group? | ||
raise _("The group has users assigned that do not belong to any other group") if single_group_users? | ||
raise _("A tenant default group can not be deleted") if tenant_group? && referenced_by_tenant? | ||
raise _("A read only group cannot be deleted.") if system_group? | ||
reset_current_group_for_users if current_group_for_any_user? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a strange side effect of a method that seems to be checking if a group is allowed to be destroyed.
The way this is written it feels like this should be two separate steps:
- Ensure that the group in question is not the only group assigned to any user
- When (or just before) the group is actually being removed (not in this method), reassign the current group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the group in question is not the only group assigned to any user
Ah I see that this check is being done by the line above with if single_group_users?
, but I still feel like we could add another hook for this, maybe after_destroy
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did consider that, however, the group should not be destroyed if the current_group cannot be changed for any user that has this as the current_group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I read it the only reason the current_group would not be able to be changed is if a user had no other group to change to. This case is already covered by another assertion already in this method, so I don't see any reason this can't be moved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the changing of the current_group for the users that requires it to the after_destroy.
dd317da
to
105e7a6
Compare
105e7a6
to
e876972
Compare
e876972
to
2670de6
Compare
27caac8
to
ab7229b
Compare
ab7229b
to
d029675
Compare
…ave this group as the current_group
… use the current_group id.
Move reset_current_group to :after_destroy
d029675
to
a615f97
Compare
Checked commits lgalis/manageiq@2939ab3~...a615f97 with ruby 2.3.3, rubocop 0.52.0, haml-lint 0.20.0, and yamllint 1.10.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM
@carbonin Are you good with this PR at this point? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! 👍
…tion Update users' current_group on group deletion (cherry picked from commit abc4775) https://bugzilla.redhat.com/show_bug.cgi?id=1536035
Gaprindashvili backport details:
|
When deleting a group, update the current_group for users that have the deleted group as the current_group
Links
https://bugzilla.redhat.com/show_bug.cgi?id=1264967
Steps for Testing/QA
The user that had the current_group set to the new group can log in with the changes in this PR, as its new current_group is the second group it belonged to.