-
Notifications
You must be signed in to change notification settings - Fork 22
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
add admin permission, move from is_staff to permission system #126
Conversation
To clarify, this PR creates one new permission that only excludes the ability to view the django admin panel. Is there a new role for admins who have full access or will that stay as a standard superuser? |
I can't imagine a scenario in which we want a distinction between "can access everything including django admin" and "superuser", so I would think that stays as a superuser. |
I can see the name being a source of confusion down the road so I think the name of the permission should change. Since this will be used by site moderators, "moderator" is more fitting. What do you think? |
was thinking the same, I agree "moderator" is a better name here |
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.
Looks good on first pass. Will do a second pass to actually test everything
home/models.py
Outdated
# to the prod db, which the django admin panel grants to a moderate | ||
# degree. | ||
permissions = [ | ||
("admin", "Can take any planetterp admin actions") |
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.
"moderator" is more fitting here
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've gone with mod
, as moderator
was quite long.
is_staff = value.pop("is_staff") | ||
is_planetterp_admin = value.pop("is_planetterp_admin") |
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.
What would be involved in having this permission have the user's is_staff=True
but is_superuser=False
? This would allow us to continue to use the is_staff check everywhere except in places where we only want superusers, in which case we'd use is_superuser
. This would also make it easier to search for users based on if they're staff or not.
EDIT: I realize that is_staff
will grants access to the django admin panel, so unless there's a way around that, this idea is moot.
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 you said, the issue here is the admin panel. is_staff is also a very stiff permission and would not allow us to split things into multiple permissions in the future if we wanted.
home/views/admin.py
Outdated
def test_func(self): | ||
return self.request.user.is_staff | ||
class Admin(PermissionRequiredMixin, View): | ||
permission_required = "home.admin" |
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.
Thoughts on making an enum to store the string value? I assume the string won't change, but I think it's more pleasant to deal with an enum than with a string. Also, this would give us more flexibility in naming/updating it.
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 doesn't work as well as you might hope because there's no easy way to use this enum value in templates. Currently django populates a global perms
variable and does some magic for perms checking:
{% if perms.home.admin %}
...
{% endif %}
I don't even know how this would work with a custom enum. In addition, if we did want to reference Permissinon
in templates, we would have to populate the template with our Permission
enum, which requires a custom context processor.
I'm not sure the added complexity is worthwhile here.
Second pass is good! I didn't encounter any errors and everything appears to work. I'm good to merge once the above things have been addressed. |
merge at your discretion, as I did not change the enum suggested above |
See inline comment for details:
I did try adding a
User.is_planetterp_admin
field first, but ran into issues withrequest.user.is_planetterp_admin
erroring whentype(user) is AnonymousUser
. This wasn't an issue withrequest.user.is_staff
becauseis_staff
is a builtin field onAbstractUser
, while we only setis_planetterp_admin
on our ownUser : AbstractUser
. Frankly, this seems like bad design by django.Not happy about using the django permissions system - it has its own warts1 - but it seemed the easiest and most flexible way to achieve this.
All current staff accounts are also superusers, which have every permission automatically, so there's no need to do anything on the migration side of things.
To assign this permission, use the django admin panel.
Footnotes
permissions are inherently tied to a model, so there's no way to declare a site-wide permission like "is an admin". The next best place for it to live is under the
User
model, which looks strange. Nothing breaks, but it's counterintuitive when adding permissions to users. ↩