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

capture or provide way to capture change_permission events #805

Open
shivam-coditation opened this issue Oct 11, 2023 · 3 comments
Open

Comments

@shivam-coditation
Copy link

So, there is a need to notify end user about the permission changed and for that there should be a way to capture permissions that are changed(provide or revoked)

@iamMHZ
Copy link

iamMHZ commented Oct 11, 2023

@shivam-coditation
One approach is using Django signals.

@shivam-coditation
Copy link
Author

shivam-coditation commented Oct 12, 2023

@shivam-coditation One approach is using Django signals.

I tried it, but django_guardian do not provide explicit signals so I tried with Middleware so getting final list of object_level_permissions a user have and what I required is which permission is changed(provided or revoked)

@iamMHZ
Copy link

iamMHZ commented Oct 12, 2023

@shivam-coditation

One simple example using signals:


def capture_user_permission_changes(sender, instance, **kwargs):
    print(instance)
    pass


class YourAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'yourapp'

    def ready(self):
        from django.db.models import signals
        from guardian.models import UserObjectPermission

        signals.post_save.connect(capture_user_permission_changes, sender=UserObjectPermission)




Screenshot from 2023-10-12 16-31-42

If you wanna capture permission changes at the API level I guess you can do it in serializers with a little bit more work.
Also, I guess two other options are available:

  • Using Proxy models in Django.
  • Customizing object permissions models in Guardian.

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

2 participants