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

Differentiate between Django's default model permissions and custom permissions #155

Closed
whitews opened this issue Jul 23, 2013 · 1 comment

Comments

@whitews
Copy link

whitews commented Jul 23, 2013

Hi,

I believe this came up before in issue #42 and I think it would be useful to have a mechanism to exclude the default permissions in BaseObjectPermissionsForm.

Unfortunately, as you've indicated in the responses to that issue, there is no real distinction between the Permission instances. I am currently doing this by creating my own Form and overriding get_obj_perms_field_choices() to filter the permissions using Model._meta like so:

class UserProjectPermissionForm(UserObjectPermissionsForm):
    def get_obj_perms_field_choices(self):
        choices = super(UserProjectPermissionForm, self).get_obj_perms_field_choices()
        return list(set(choices).intersection(Project._meta.permissions))

Of course, I'd like to avoid using a private attribute, but as far as I can tell using _meta is the only way to access the custom permissions. I realize it's just shifting to problem over to guardian, but what about adding an optional init argument to BaseObjectPermissionsForm to set a property like "exclude_default" and then use it in get_obj_perms_field_choices() to filter these permissions using something similar:

def get_obj_perms_field_choices(self):
        """
        Returns choices for object permissions management field. Default:
        list of tuples ``(codename, name)`` for each ``Permission`` instance
        for the managed object.
        """
        choices = [(p.codename, p.name) for p in get_perms_for_model(self.obj)]

        if self.exclude_default:
            choices = list(set(choices).intersection(self.obj._meta.permissions))

        return choices

Does this sound reasonable, or would there be a better way to implement it?

Thanks!

@brianmay brianmay modified the milestone: v1.2 Nov 13, 2015
@brianmay
Copy link
Contributor

I think what you are asking is to be able to distinguish global permissions and per object permissions. So I am going to connect this to #380 and close this report.

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

No branches or pull requests

2 participants