-
Notifications
You must be signed in to change notification settings - Fork 81
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
Fixes #1544: Org admins cannot remove themselves through the URL #1742
Conversation
7128d83
to
c4f7d02
Compare
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.
The functionality looks good. But there a three smaller things that could be improved in the code.
@@ -290,6 +291,15 @@ class OrganizationMembersRemove(mixins.OrganizationMixin, | |||
permission_required = update_permissions('org.users.remove') | |||
permission_denied_message = error_messages.ORG_USERS_REMOVE | |||
|
|||
def admin_is_deleting_themselves(self): | |||
organization = Organization.objects.get(slug=self.kwargs['slug']) |
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.
If you check if user == member_to_remove
first and return False
if not, you can skip the DB queries because you don't need them.
member_to_remove = self.kwargs['username'] | ||
user = self.request.user.username | ||
user_is_admin = OrganizationRole.objects.get( | ||
organization=organization, |
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.
If you make this organization__slug=self.kwargs['slug']
you can save the DB query from line 295
if self.admin_is_deleting_themselves(): | ||
messages.add_message(self.request, messages.ERROR, | ||
_("Administrators cannot remove themselves.")) | ||
return redirect('organization:members_edit', |
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.
You can use Python's Dictionary Unpacking here and make this one line only:
return redirect('organization:members_edit', **self.kwargs)
c4f7d02
to
3d75218
Compare
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.
Yay 🎉
f9f8b91
to
59ba9d3
Compare
59ba9d3
to
855df28
Compare
Proposed changes in this pull request
This PR fixes #1544 Organization admins can remove themselves through the URL.
Why this problem occurred? This happened because we don't have a permission action that refers to this user case: an administrator deleting themselves.
Since it's difficult to define this very conditional action in the permission policies, I proposed to simply add a final check inside the
OrganizationMembersRemove
class before posting the member to be deleted. To do so:admin_is_not_deleting_themselves
that checks if the current user is an administrator and if thecurrent_user.username == member_to_remove.username
.get()
function, I callself.admin_is_not_deleting_themselves()
to check if the current user is an administrator trying to remove themselves.admin_is_not_deleting_themselves() == True
--> behaves as before and we post the object to be removed.admin_is_not_deleting_themselves == False
--> the member cannot be removed and so we redirect to the admin/member edit page and we display an error message.Here you can see a silent short video demo of how this currently works.
When should this PR be merged
Risks
Follow-up actions
Checklist (for reviewing)
General
migration
label if a new migration is added.Functionality
Code
Tests
Security
Documentation