-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert DAB RBAC to using its own concrete permission model (#3)
* Initial add of permission model * Convert DAB RBAC to using its own concrete permission model * Flake8 fixes * black
- Loading branch information
1 parent
8f5646a
commit a71608f
Showing
16 changed files
with
162 additions
and
158 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import logging | ||
|
||
from django.apps import apps as global_apps | ||
from django.contrib.contenttypes.management import create_contenttypes | ||
from django.db import DEFAULT_DB_ALIAS, router | ||
|
||
from ansible_base.rbac import permission_registry | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): | ||
""" | ||
This is modified from the django auth. | ||
This will create DABPermission entries | ||
this will only create permissions for registered models | ||
""" | ||
if not app_config.models_module: | ||
return | ||
|
||
# Ensure that contenttypes are created for this app. Needed if | ||
# 'ansible_base.rbac' is in INSTALLED_APPS before | ||
# 'django.contrib.contenttypes'. | ||
create_contenttypes( | ||
app_config, | ||
verbosity=verbosity, | ||
interactive=interactive, | ||
using=using, | ||
apps=apps, | ||
**kwargs, | ||
) | ||
|
||
app_label = app_config.label | ||
try: | ||
app_config = apps.get_app_config(app_label) | ||
ContentType = apps.get_model("contenttypes", "ContentType") | ||
Permission = apps.get_model("dab_rbac", "DABPermission") | ||
except LookupError: | ||
return | ||
|
||
if not router.allow_migrate_model(using, Permission): | ||
return | ||
|
||
# This will hold the permissions we're looking for as (content_type, (codename, name)) | ||
searched_perms = [] | ||
# The codenames and ctypes that should exist. | ||
ctypes = set() | ||
for klass in app_config.get_models(): | ||
if not permission_registry.is_registered(klass): | ||
continue | ||
# Force looking up the content types in the current database | ||
# before creating foreign keys to them. | ||
ctype = ContentType.objects.db_manager(using).get_for_model(klass, for_concrete_model=False) | ||
|
||
ctypes.add(ctype) | ||
|
||
for action in klass._meta.default_permissions: | ||
searched_perms.append( | ||
( | ||
ctype, | ||
( | ||
f"{action}_{klass._meta.model_name}", | ||
f"Can {action} {klass._meta.verbose_name_raw}", | ||
), | ||
) | ||
) | ||
for codename, name in klass._meta.permissions: | ||
searched_perms.append((ctype, (codename, name))) | ||
|
||
# Find all the Permissions that have a content_type for a model we're | ||
# looking for. We don't need to check for codenames since we already have | ||
# a list of the ones we're going to create. | ||
all_perms = set(Permission.objects.using(using).filter(content_type__in=ctypes).values_list("content_type", "codename")) | ||
|
||
perms = [] | ||
for ct, (codename, name) in searched_perms: | ||
if (ct.pk, codename) not in all_perms: | ||
permission = Permission() | ||
permission._state.db = using | ||
permission.codename = codename | ||
permission.name = name | ||
permission.content_type = ct | ||
perms.append(permission) | ||
|
||
Permission.objects.using(using).bulk_create(perms) | ||
for perm in perms: | ||
logger.debug("Adding permission '%s'" % perm) |
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
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
Oops, something went wrong.