|
| 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] |
0 commit comments