-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On branch prefix_perms Your branch is up to date with 'origin/prefix_perms'. Changes to be committed: modified: config/services.py modified: prefix/apis.py modified: prefix/models.py modified: prefix/selectors.py modified: prefix/services.py
- Loading branch information
1 parent
bce794b
commit bacb265
Showing
5 changed files
with
241 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,57 @@ | ||
# prefix/selectors.py | ||
|
||
"""Prefix Selectors | ||
def is_accessible_by(self, user): | ||
"""If no authorized_groups are specified, it's accessible by everyone""" | ||
if self.authorized_users.exists(): | ||
return self.authorized_users.filter(id=user.id).exists() | ||
return True | ||
Functions to query the database related to Prefixes | ||
""" | ||
|
||
from django.core.serializers import serialize | ||
from django.contrib.auth.models import Permission | ||
from django.contrib.auth.models import User | ||
from django.db import utils | ||
from prefix.models import Prefix | ||
|
||
def get_prefix_object(prefix_name:str) -> dict: | ||
"""Get Prefix Object | ||
Returns a serialized Prefix instance. If the Prefix is not public then | ||
a dictionary with users and the associated Prefix permisssions will also | ||
be included. | ||
""" | ||
|
||
prefix_instance = Prefix.objects.get(prefix=prefix_name) | ||
prefix_object = serialize('python', [prefix_instance])[0] | ||
if prefix_instance.public is False: | ||
prefix_permissions = get_prefix_permissions(prefix_name) | ||
prefix_object["user_permissions"] = prefix_permissions | ||
return prefix_object | ||
|
||
def get_prefix_permissions(prefix_name:str) -> dict: | ||
"""Get Prefix Permissions | ||
Returns a dictionary with users and the associated Prefix permisssions. | ||
""" | ||
|
||
users_permissions = {} | ||
perms = [] | ||
for perm in [ "view", "add", "change", "delete", "publish"]: | ||
codename = f"{perm}_{prefix_name}" | ||
try: | ||
perms.append(Permission.objects.get(codename__exact=codename)) | ||
except utils.IntegrityError: | ||
# The permissions doesn't exist. | ||
pass | ||
|
||
|
||
for perm in perms: | ||
users_with_perm = User.objects.filter(user_permissions=perm).prefetch_related('user_permissions') | ||
for user in users_with_perm: | ||
# Initialize the user entry in the dictionary if not already present | ||
if user.username not in users_permissions: | ||
users_permissions[user.username] = [] | ||
|
||
# Add the permission codename to the user's permissions list, avoiding duplicates | ||
if perm.codename not in users_permissions[user.username]: | ||
users_permissions[user.username].append(perm.codename) | ||
|
||
return users_permissions |
Oops, something went wrong.