Skip to content

refactor: introduce declarative permission pattern for authorization#50

Merged
ivan-borovets merged 2 commits intoivan-borovets:masterfrom
ppravdin:feature/declarative-authorization-pattern
Jul 25, 2025
Merged

refactor: introduce declarative permission pattern for authorization#50
ivan-borovets merged 2 commits intoivan-borovets:masterfrom
ppravdin:feature/declarative-authorization-pattern

Conversation

@ppravdin
Copy link
Copy Markdown
Contributor

Replace procedural authorization logic with a declarative Permission/Policy pattern. This improves code clarity, maintainability, and testability.

Key changes:

  • Add Permission abstraction and concrete implementations (IsSelf, IsSuperior, etc.)
  • Add composite permissions (AnyOf, AllOf) for complex authorization rules
  • Extend AuthorizationService with declarative authorize() method
  • Refactor ChangePasswordInteractor and GrantAdminInteractor to use new pattern

The old procedural methods are retained for backward compatibility. This change demonstrates Clean Architecture principles by improving the application layer abstractions without affecting other layers.

Replace procedural authorization logic with a declarative Permission/Policy pattern.
This improves code clarity, maintainability, and testability.

Key changes:
- Add Permission abstraction and concrete implementations (IsSelf, IsSuperior, etc.)
- Add composite permissions (AnyOf, AllOf) for complex authorization rules
- Extend AuthorizationService with declarative authorize() method
- Refactor ChangePasswordInteractor and GrantAdminInteractor to use new pattern

The old procedural methods are retained for backward compatibility.
This change demonstrates Clean Architecture principles by improving
the application layer abstractions without affecting other layers.
@ivan-borovets
Copy link
Copy Markdown
Owner

Thanks for the idea!
Moving to a declarative permission model definitely helps express authorization logic in a cleaner and more extensible way.

I did hesitate a bit over adding another abstraction layer — it can raise the entry barrier for new developers — but in the end, the gain in clarity felt worth it.

To support that direction, a few adjustments were made:

  • Dropped TYPE_CHECKING since Python 3.12 handles forward references by default.
  • Introduced typed context dataclasses instead of **kwargs, to make permission contracts explicit and type-safe.
  • Used generics to tie permissions to their expected context types.

Let me know what you think about the current version.

@ivan-borovets ivan-borovets force-pushed the feature/declarative-authorization-pattern branch from 5b601d3 to 71d437c Compare July 25, 2025 17:27
@ivan-borovets
Copy link
Copy Markdown
Owner

Proceeding with this for now. Always open to feedback if anything needs to be revisited.

@ivan-borovets ivan-borovets merged this pull request into ivan-borovets:master Jul 25, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants