Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ Added
be loaded together. Validation rejects unknown dependencies, dependency cycles, dependencies that
do not support the dependent permission's scopes, visible permissions that depend on hidden
ones, and roles that grant a permission without all of its dependencies.
* Permission dependencies are stored in a new definition table, in migration
``0015_authzpermissiondependency``, and rendered as one Casbin ``g2`` row each (ADR 0034).
``load_authz_schema`` adds and removes these rows with the ``p`` rows, keeps existing rows that
match, leaves ``g2`` rows that no schema declares in place, and lists the ``g2`` changes in its
report.
* The content library permissions now use schema version ``1.1`` and declare the dependencies that
``authz.policy`` held before the static schema, so new installations get the library ``g2`` rows
again. Installations that loaded ``authz.policy`` keep their rows unchanged. A schema file outside
this package that redefines a content library permission must remove that definition, or declare
the same ``depends_on`` in a ``1.1`` file; otherwise ``load_authz_schema`` reports a conflicting
permission definition.
* Added an optional ``hidden`` flag to permission categories and permissions in the authorization
schema. Hidden permissions, and the permissions of hidden categories, are left out of the
role and permission catalog, including each role's permission list, but keep their role grants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Status
******

**Draft**
**Accepted**

Context
*******
Expand Down
4 changes: 4 additions & 0 deletions docs/references/authorization-schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ Validation rejects a schema when:

The last rule applies to roles and role extensions in every file, whatever ``schema_version`` the file declares. If a ``1.1`` file adds ``depends_on`` to a permission, every role that grants that permission must also list its dependencies.

Only the file that defines a permission declares its dependencies. Another file that defines the same permission must declare the same ``depends_on``, which requires schema version ``1.1``. A ``1.0`` copy of a permission that declares dependencies is a conflicting definition.

Each direct dependency is stored as a Casbin ``g2`` row, such as ``g2, act^content_libraries.edit_library_content, act^content_libraries.view_library``. Loading the schema adds, keeps and removes these rows the same way as the ``p`` rows of role grants: an existing ``g2`` row that matches a dependency is kept as is, and a ``g2`` row that no schema declares is left in place. A permission check that a role passes for a permission also passes for the permissions it depends on, so a role stored only in the database still works when it lacks a dependency.

Roles
*****

Expand Down
28 changes: 19 additions & 9 deletions docs/references/compiled-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ permissions:
- lib
icon: Plus
hidden: false
depends_on: []
depends_on:
- content_libraries.edit_library_collection
content_libraries.delete_library:
applet: content_libraries
namespace: content_libraries
Expand All @@ -284,7 +285,8 @@ permissions:
- lib
icon: Delete
hidden: false
depends_on: []
depends_on:
- content_libraries.edit_library_content
content_libraries.delete_library_collection:
applet: content_libraries
namespace: content_libraries
Expand All @@ -296,7 +298,8 @@ permissions:
- lib
icon: Delete
hidden: false
depends_on: []
depends_on:
- content_libraries.edit_library_collection
content_libraries.edit_library_collection:
applet: content_libraries
namespace: content_libraries
Expand All @@ -308,7 +311,8 @@ permissions:
- lib
icon: EditOutline
hidden: false
depends_on: []
depends_on:
- content_libraries.view_library
content_libraries.edit_library_content:
applet: content_libraries
namespace: content_libraries
Expand All @@ -320,7 +324,8 @@ permissions:
- lib
icon: EditOutline
hidden: false
depends_on: []
depends_on:
- content_libraries.view_library
content_libraries.manage_library_tags:
applet: content_libraries
namespace: content_libraries
Expand All @@ -332,7 +337,8 @@ permissions:
- lib
icon: Settings
hidden: false
depends_on: []
depends_on:
- content_libraries.edit_library_content
content_libraries.manage_library_team:
applet: content_libraries
namespace: content_libraries
Expand All @@ -344,7 +350,8 @@ permissions:
- lib
icon: Settings
hidden: false
depends_on: []
depends_on:
- content_libraries.view_library_team
content_libraries.publish_library_content:
applet: content_libraries
namespace: content_libraries
Expand All @@ -356,7 +363,9 @@ permissions:
- lib
icon: DownloadDone
hidden: false
depends_on: []
depends_on:
- content_libraries.edit_library_content
- content_libraries.view_library
content_libraries.reuse_library_content:
applet: content_libraries
namespace: content_libraries
Expand All @@ -368,7 +377,8 @@ permissions:
- lib
icon: SpinnerIcon
hidden: false
depends_on: []
depends_on:
- content_libraries.view_library
content_libraries.view_library:
applet: content_libraries
namespace: content_libraries
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
schema_version: "1.0"
schema_version: "1.1"
priority: 100

# Content library permission and category definitions for the
Expand Down Expand Up @@ -41,6 +41,8 @@ permissions:
scopes:
- lib
icon: Settings
depends_on:
- content_libraries.edit_library_content
- namespace: content_libraries
name: delete_library
display_name: Delete
Expand All @@ -49,6 +51,8 @@ permissions:
scopes:
- lib
icon: Delete
depends_on:
- content_libraries.edit_library_content
- namespace: content_libraries
name: edit_library_content
display_name: Edit
Expand All @@ -57,6 +61,8 @@ permissions:
scopes:
- lib
icon: EditOutline
depends_on:
- content_libraries.view_library
- namespace: content_libraries
name: publish_library_content
display_name: Publish
Expand All @@ -65,6 +71,9 @@ permissions:
scopes:
- lib
icon: DownloadDone
depends_on:
- content_libraries.edit_library_content
- content_libraries.view_library
- namespace: content_libraries
name: reuse_library_content
display_name: Reuse
Expand All @@ -73,6 +82,8 @@ permissions:
scopes:
- lib
icon: SpinnerIcon
depends_on:
- content_libraries.view_library
- namespace: content_libraries
name: view_library_team
display_name: View
Expand All @@ -89,6 +100,8 @@ permissions:
scopes:
- lib
icon: Settings
depends_on:
- content_libraries.view_library_team
- namespace: content_libraries
name: create_library_collection
display_name: Create
Expand All @@ -97,6 +110,8 @@ permissions:
scopes:
- lib
icon: Plus
depends_on:
- content_libraries.edit_library_collection
- namespace: content_libraries
name: edit_library_collection
display_name: Edit
Expand All @@ -105,6 +120,8 @@ permissions:
scopes:
- lib
icon: EditOutline
depends_on:
- content_libraries.view_library
- namespace: content_libraries
name: delete_library_collection
display_name: Delete
Expand All @@ -113,3 +130,5 @@ permissions:
scopes:
- lib
icon: Delete
depends_on:
- content_libraries.edit_library_collection
54 changes: 53 additions & 1 deletion src/openedx_authz/engine/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@
from openedx_authz.engine.enforcer import AuthzEnforcer
from openedx_authz.models.engine import PolicyCacheControl

__all__ = ["PolicyRow", "GroupingRow", "PolicyStore", "policy_write_lock", "serialized_policy_update"]
__all__ = [
"PolicyRow",
"GroupingRow",
"ActionGroupingRow",
"PolicyStore",
"policy_write_lock",
"serialized_policy_update",
]


@dataclass(frozen=True)
Expand Down Expand Up @@ -142,6 +149,38 @@ def from_policy(cls, values: list[str]) -> "GroupingRow":
return cls(subject, role, scope)


@dataclass(frozen=True)
class ActionGroupingRow:
"""A single Casbin ``g2`` row: an action that implies another (ADR 0034 §4).

Fields follow the ``g2`` shape: ``[action, implied_action]``, both in the internal ``act^``
form. A permission dependency renders as one row whose ``action`` is the dependent
permission and whose ``implied_action`` is the permission it depends on.
"""

PTYPE: ClassVar[str] = "g2"

action: str
implied_action: str

def as_policy(self) -> list[str]:
"""Return the enforcer arg form: ``[action, implied_action]``."""
return [self.action, self.implied_action]

@classmethod
def from_policy(cls, values: list[str]) -> "ActionGroupingRow":
"""Build from a stored ``g2`` row (``[action, implied_action]``)."""
return cls(values[0], values[1])

@classmethod
def from_dependency(cls, permission_id: str, depends_on_id: str) -> "ActionGroupingRow":
"""Build the ``g2`` row for one direct dependency, applying the same ``act^`` form as ``p`` rows."""
return cls(
action=f"{ACTION_NAMESPACE}{SEP}{permission_id}",
implied_action=f"{ACTION_NAMESPACE}{SEP}{depends_on_id}",
)


class PolicyStore:
"""Casbin-facing facade exposing only the operations the schema pipeline needs.

Expand Down Expand Up @@ -205,6 +244,19 @@ def remove_grouping_row(self, row: GroupingRow) -> bool:
"""
return self._resolved_enforcer.remove_grouping_policy(*row.as_policy())

def get_action_grouping_rows(self) -> list[ActionGroupingRow]:
"""Return every stored ``g2`` row as an :class:`ActionGroupingRow`."""
rows = self._resolved_enforcer.get_named_grouping_policy(ActionGroupingRow.PTYPE)
return [ActionGroupingRow.from_policy(row) for row in rows]

def add_action_grouping_row(self, row: ActionGroupingRow) -> bool:
"""Add one ``g2`` row. Returns the enforcer's add result (True if added)."""
return self._resolved_enforcer.add_named_grouping_policy(ActionGroupingRow.PTYPE, *row.as_policy())

def remove_action_grouping_row(self, row: ActionGroupingRow) -> bool:
"""Remove one ``g2`` row. Returns the enforcer's remove result (True if removed)."""
return self._resolved_enforcer.remove_named_grouping_policy(ActionGroupingRow.PTYPE, *row.as_policy())

def reload(self) -> None:
"""Reload the enforcer's in-memory model from the database (ADR 0018 §8).

Expand Down
Loading