Skip to content

Commit 573ff65

Browse files
authored
[FC-0099] feat: add admin configuration for CasbinRule model (#138)
1 parent c5ce1d1 commit 573ff65

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Change Log
1414
Unreleased
1515
**********
1616

17+
* Register ``CasbinRule`` model in the Django admin.
18+
* Register ``ExtendedCasbinRule`` model in the Django admin as an inline model of ``CasbinRule``.
19+
1720
0.15.0 - 2025-11-11
1821
********************
1922

openedx_authz/admin.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Admin configuration for openedx_authz."""
2+
3+
from casbin_adapter.models import CasbinRule
4+
from django import forms
5+
from django.contrib import admin
6+
7+
from openedx_authz.models import ExtendedCasbinRule
8+
9+
10+
class CasbinRuleForm(forms.ModelForm):
11+
"""Custom form for CasbinRule to make v3, v4, v5 fields optional."""
12+
13+
class Meta:
14+
"""Meta class for CasbinRuleForm."""
15+
16+
model = CasbinRule
17+
fields = "__all__"
18+
19+
def __init__(self, *args, **kwargs):
20+
"""Initialize CasbinRuleForm."""
21+
super().__init__(*args, **kwargs)
22+
# Make v2, v3, v4, v5 optional in the form
23+
# These fields are not always required depending on the policy type
24+
self.fields["v2"].required = False
25+
self.fields["v3"].required = False
26+
self.fields["v4"].required = False
27+
self.fields["v5"].required = False
28+
29+
30+
class ExtendedCasbinRuleInline(admin.StackedInline):
31+
"""Inline admin for ExtendedCasbinRule to display additional metadata."""
32+
33+
model = ExtendedCasbinRule
34+
extra = 0
35+
fields = ("casbin_rule_key", "scope", "subject", "description", "metadata", "created_at", "updated_at")
36+
readonly_fields = ("casbin_rule_key", "scope", "subject", "created_at", "updated_at")
37+
can_delete = False
38+
39+
40+
@admin.register(CasbinRule)
41+
class CasbinRuleAdmin(admin.ModelAdmin):
42+
"""Admin for CasbinRule to display additional metadata."""
43+
44+
form = CasbinRuleForm
45+
list_display = ("id", "ptype", "v0", "v1", "v2", "v3", "v4", "v5")
46+
search_fields = ("ptype", "v0", "v1", "v2", "v3", "v4", "v5")
47+
list_filter = ("ptype",)
48+
# TODO: In a future, possibly we should only show an inline for the rules that
49+
# have an extended rule, and show the subject and scope information in detail.
50+
inlines = [ExtendedCasbinRuleInline]

openedx_authz/models/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def get_registry(cls) -> dict[str, type["BaseRegistryModel"]]:
4444
"""
4545
return cls._registry
4646

47+
4748
class ScopeManager(models.Manager):
4849
"""Custom manager for Scope model that handles polymorphic behavior."""
4950

0 commit comments

Comments
 (0)