django / django/new-features

contrib/auth/backends: allow to extend/change the permissions for a user

Open
#54 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

Small refactor: extract a -> method to allow to extend/change the permissions for a user.

### Problem

I'd like to implement some hierarchy for the permissions which requires some post-work with the explicitly set permissions. Now I have to rewrite the whole `_get_permissions` method in a subclass, while I just need the explicitly set permissions to know what other permissions I should add there.

### Request or proposal

proposal

### Additional Details

_No response_

### Implementation Suggestions

At this code: https://github.com/django/django/blob/23529b662793cdf4725d5f8ff58f0df94b343365/django/contrib/auth/backends.py#L119

Instead of this:
```python
...

perms = perms.values_list("content_type__app_label", "codename").order_by()
setattr(
user_obj, perm_cache_name, {"%s.%s" % (ct, name) for ct, name in perms}
)

...
```
Extract the conversion to a new method:
```python
def _permission_strings(self, permission_queryset):
perms = permission_queryset.values_list("content_type__app_label", "codename").order_by()
return {f"{ct}.{name}" for ct, name in perms}

...

setattr(
user_obj, perm_cache_name, self._permission_strings(perms)
)

...
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.