Skip to main content

actix_security_core/http/auth/
access.rs

1//! Access control types for method-level security.
2//!
3//! This module provides types used by the `#[has_access]` macro for
4//! method-level security checks.
5//!
6//! # Future Use
7//! This will be used when the codegen macros are fully implemented.
8
9#![allow(dead_code)]
10
11/// Access requirements for a protected resource.
12///
13/// Used by procedural macros to specify required roles and authorities.
14pub struct Access {
15    roles: Vec<&'static str>,
16    authorities: Vec<&'static str>,
17}
18
19impl Access {
20    pub fn new(roles: Vec<&'static str>, authorities: Vec<&'static str>) -> Self {
21        Access { roles, authorities }
22    }
23
24    pub fn has_role(&self, role: &str) -> bool {
25        self.roles.contains(&role)
26    }
27
28    pub fn has_authority(&self, authority: &str) -> bool {
29        self.authorities.contains(&authority)
30    }
31
32    pub fn has_any_role(&self, roles: &[&str]) -> bool {
33        roles.iter().any(|r| self.has_role(r))
34    }
35
36    pub fn has_any_authority(&self, authorities: &[&str]) -> bool {
37        authorities.iter().any(|a| self.has_authority(a))
38    }
39}
40
41/// Function type for custom access checks.
42pub type AccessFn = fn(access: &Access) -> bool;