-
Notifications
You must be signed in to change notification settings - Fork 6
[FC-0099] feat: add model to be used as backreference to maintain rules consistent #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
2f7521d
feat: add initial extended model for consistency and additional storage
mariajgrimaldi 6a6752d
feat: integration tests for consistency mechanism
mariajgrimaldi 59bca69
feat: add model to be used as backreference to maintain rules up to date
mariajgrimaldi 3c1bd7c
refactor!: use registry pattern to extend base models
mariajgrimaldi f65d318
refactor: consider when deleting extended model so the casbin rule is…
mariajgrimaldi ca33863
test: implement integration tests for main rest API use cases
mariajgrimaldi 0616a7a
refactor: load policies into enforcer before running tests
mariajgrimaldi 4549c87
refactor: get adapter from enforcer as singleton
mariajgrimaldi 17a79b0
refactor: run make format
mariajgrimaldi d1c3b76
refactor: address quality issues for ruff format
mariajgrimaldi 2db6b9e
refactor: add no_pii to models
mariajgrimaldi c0f3b7f
refactor: address quality issues
mariajgrimaldi 8b9c7e9
refactor: regenerate migrations
mariajgrimaldi 0e56bc9
refactor: fix issues when rebasing
mariajgrimaldi bec5f23
refactor: drop unused variables from data classes
mariajgrimaldi a376a34
refactor: drop failing test
mariajgrimaldi 6003a71
refactor: use swappable dependency to avoid wrong generation for scope
mariajgrimaldi 38d3c24
refactor: drop wrong imports after changes
mariajgrimaldi ad64d80
refactor: address integration test failure
mariajgrimaldi f34923e
refactor: consider double namespace instead
mariajgrimaldi ef2e6d5
refactor: address quality issues
mariajgrimaldi c6d8d57
test: add minimal unittest suite for extended casbin model
mariajgrimaldi c384808
refactor: drop /tests package in favor of openedx_authz/tests
mariajgrimaldi 2f11250
refactor: address quality issues
mariajgrimaldi 0f827b5
docs: update docs for next release
mariajgrimaldi 9be04f5
test: add test for missing coverage
mariajgrimaldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| """ | ||
| Signal handlers for the authorization framework. | ||
|
|
||
| These handlers ensure proper cleanup and consistency when models are deleted. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from casbin_adapter.models import CasbinRule | ||
| from django.db.models.signals import post_delete | ||
| from django.dispatch import receiver | ||
|
|
||
| from openedx_authz.models.core import ExtendedCasbinRule | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=ExtendedCasbinRule) | ||
| def delete_casbin_rule_on_extended_rule_deletion(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| Delete the companion CasbinRule after its ExtendedCasbinRule disappears. | ||
|
|
||
| The handler keeps authorization data symmetric with three common flows: | ||
|
|
||
| - Direct ExtendedCasbinRule deletes (API/UI) trigger removal of the linked CasbinRule. | ||
| - Cascades from `Scope` or `Subject` deletions clear their ExtendedCasbinRule rows and, | ||
| via this handler, the matching CasbinRule entries. | ||
| - Cascades initiated from the CasbinRule side (enforcer cleanups) leave the query as a | ||
| no-op because the row is already gone. | ||
|
|
||
| Running on ``post_delete`` ensures database cascades complete before the cleanup runs, so | ||
| enforcer-driven deletions no longer raise false errors. | ||
|
|
||
| Args: | ||
| sender: The model class (ExtendedCasbinRule). | ||
| instance: The ExtendedCasbinRule instance being deleted. | ||
| **kwargs: Additional keyword arguments from the signal. | ||
| """ | ||
| try: | ||
| # Rely on delete() being idempotent; returns 0 rows if the CasbinRule was | ||
| # already removed (for example, because it triggered this signal). | ||
| CasbinRule.objects.filter(id=instance.casbin_rule_id).delete() | ||
| except Exception as exc: # pylint: disable=broad-exception-caught | ||
| # Log but don't raise - we don't want to break the deletion of | ||
| # ExtendedCasbinRule if something goes wrong while deleting the CasbinRule. | ||
| logger.exception( | ||
| "Error deleting CasbinRule %s during ExtendedCasbinRule cleanup", | ||
| instance.casbin_rule_id, | ||
| exc_info=exc, | ||
| ) |
This file contains hidden or 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,78 @@ | ||
| # Generated by Django 4.2.24 on 2025-10-24 11:19 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ("casbin_adapter", "0001_initial"), | ||
| ("openedx_authz", "0001_add_casbin_dependency"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Scope", | ||
| fields=[ | ||
| ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="Subject", | ||
| fields=[ | ||
| ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ], | ||
| options={ | ||
| "abstract": False, | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="ExtendedCasbinRule", | ||
| fields=[ | ||
| ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ("casbin_rule_key", models.CharField(max_length=255, unique=True)), | ||
| ("description", models.TextField(blank=True, null=True)), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("updated_at", models.DateTimeField(auto_now=True)), | ||
| ("metadata", models.JSONField(blank=True, null=True)), | ||
| ( | ||
| "casbin_rule", | ||
| models.OneToOneField( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="extended_rule", | ||
| to="casbin_adapter.casbinrule", | ||
| ), | ||
| ), | ||
| ( | ||
| "scope", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="casbin_rules", | ||
| to="openedx_authz.scope", | ||
| ), | ||
| ), | ||
| ( | ||
| "subject", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="casbin_rules", | ||
| to="openedx_authz.subject", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name": "Extended Casbin Rule", | ||
| "verbose_name_plural": "Extended Casbin Rules", | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or 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,42 @@ | ||
| # Generated by Django 4.2.24 on 2025-10-24 11:19 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ("openedx_authz", "0002_initial"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="UserSubject", | ||
| fields=[ | ||
| ( | ||
| "subject_ptr", | ||
| models.OneToOneField( | ||
| auto_created=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| parent_link=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| to="openedx_authz.subject", | ||
| ), | ||
| ), | ||
| ( | ||
| "user", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="authz_subjects", | ||
| to=settings.AUTH_USER_MODEL, | ||
| ), | ||
| ), | ||
| ], | ||
| bases=("openedx_authz.subject",), | ||
| ), | ||
| ] |
This file contains hidden or 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,43 @@ | ||
| # Generated by Django 4.2.24 on 2025-10-24 11:19 | ||
| # Custom migration - DO NOT REGENERATE | ||
| # This migration conditionally depends on the content library model based on settings | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("openedx_authz", "0003_usersubject"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="ContentLibraryScope", | ||
| fields=[ | ||
| ( | ||
| "scope_ptr", | ||
| models.OneToOneField( | ||
| auto_created=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| parent_link=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| to="openedx_authz.scope", | ||
| ), | ||
| ), | ||
| ( | ||
| "content_library", | ||
| models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="authz_scopes", | ||
| to=settings.OPENEDX_AUTHZ_CONTENT_LIBRARY_MODEL, | ||
| ), | ||
| ), | ||
| ], | ||
| bases=("openedx_authz.scope",), | ||
| ), | ||
| ] |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been thinking the entire day that is the 10th.