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

Permission pk field useless #44

Open
sean-dooher opened this issue Mar 3, 2018 · 0 comments
Open

Permission pk field useless #44

sean-dooher opened this issue Mar 3, 2018 · 0 comments

Comments

@sean-dooher
Copy link

Currently when making custom permissions, the "pk" field is useless as there is no way to actually retrieve the instance of the object you are checking against as the BasePermission class has no referent to a model type.

I ran into this issue when trying to write the following permission class to see if a user owns a model:

class IsOwner(BasePermission):
    def has_permission(self, user, action, pk):
        try:
            # have to hard code Job here because BasePermission has no reference to the Model it is checking
            job = Job.objects.get(pk=pk) 
        except Job.DoesNotExist:
            return False

        if action == "SUBSCRIBE":
            return user == job.owner
        return False

While this works for the Job model, I want to make this more general as I have other models with Owner that I want to use the same permission on. As I see it now, there's no way to generically use this field to do anything at all.

I think this was just a design oversight and I figure this can be solved pretty easily through just passing a reference to the Model of the binding to the permission class upon construction that can then be accessed from the has_permission method. I'll submit a PR with a fix once I have a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant