1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Pre-defined hierarchical role system for access control.
//!
//! This module provides a built-in role system with four levels arranged in a hierarchy
//! where higher roles inherit access from lower roles. The hierarchy is:
//!
//! **Admin > Moderator > Reporter > User**
//!
//! # Role Hierarchy
//!
//! When using `AccessPolicy::require_role_or_supervisor()`, higher roles automatically
//! inherit access from lower roles:
//!
//! ```rust
//! use axum_gate::prelude::Role;
//! use axum_gate::authz::AccessPolicy;
//! use axum_gate::prelude::Group;
//!
//! // Allow User role and all supervisor roles (Reporter, Moderator, Admin)
//! let policy = AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::User);
//!
//! // Allow only Moderator role and supervisor roles (Admin)
//! let policy = AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::Moderator);
//!
//! // Allow only Admin role (no supervisors above Admin)
//! let policy = AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::Admin);
//! ```
//!
//! # Using Roles with Gates
//!
//! ```rust
//! use axum_gate::prelude::*;
//! use axum_gate::authz::AccessPolicy;
//! use axum_gate::codecs::jwt::{JsonWebToken, JwtClaims};
//! use axum_gate::accounts::Account;
//! use std::sync::Arc;
//!
//! # let jwt_codec = Arc::new(JsonWebToken::<JwtClaims<Account<Role, Group>>>::default());
//! // Exact role match (only Admin)
//! let admin_gate = Gate::cookie("my-app", Arc::clone(&jwt_codec))
//! .with_policy(AccessPolicy::<Role, Group>::require_role(Role::Admin));
//!
//! // Multiple specific roles
//! let staff_gate = Gate::cookie("my-app", Arc::clone(&jwt_codec))
//! .with_policy(
//! AccessPolicy::<Role, Group>::require_role(Role::Admin)
//! .or_require_role(Role::Moderator)
//! );
//!
//! // Hierarchical access (User + all supervisors)
//! let user_gate = Gate::cookie("my-app", jwt_codec)
//! .with_policy(AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::User));
//! ```
//!
//! # Creating Custom Roles
//!
//! Create your own role hierarchy by implementing the required traits:
//!
//! ```rust
//! use serde::{Deserialize, Serialize};
//! use axum_gate::authz::AccessHierarchy;
//!
//! #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
//! enum CompanyRole {
//! #[default]
//! Employee, // Lowest privilege
//! TeamLead,
//! Manager,
//! Director, // Highest privilege
//! }
//!
//! impl std::fmt::Display for CompanyRole {
//! fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//! match self {
//! CompanyRole::Employee => write!(f, "Employee"),
//! CompanyRole::TeamLead => write!(f, "TeamLead"),
//! CompanyRole::Manager => write!(f, "Manager"),
//! CompanyRole::Director => write!(f, "Director"),
//! }
//! }
//! }
//!
//! impl AccessHierarchy for CompanyRole {}
//! ```
use crateAccessHierarchy;
use crateCommaSeparatedValue;
use ;
use FromStr;
/// Pre-defined roles with hierarchical access control.
///
/// These roles are arranged in a hierarchy where higher roles automatically
/// inherit access from lower roles when using `AccessPolicy::require_role_or_supervisor()`.
///
/// **Hierarchy (highest to lowest):**
/// - `Admin` - Full system access
/// - `Moderator` - Content moderation and user management
/// - `Reporter` - Read access with reporting capabilities
/// - `User` - Basic user access
///
/** Updated example imports to reflect current public modules */
/// # Example Usage
/// ```rust
/// use axum_gate::prelude::{Role, Group};
/// use axum_gate::authz::AccessPolicy;
///
/// // Grant access to Moderators and all supervisor roles (Admin)
/// let policy = AccessPolicy::<Role, Group>::require_role_or_supervisor(Role::Moderator);
///
/// // Grant access to specific roles only
/// let policy = AccessPolicy::<Role, Group>::require_role(Role::Admin)
/// .or_require_role(Role::Moderator);
/// ```