actix_grants_proc_macro/
lib.rs

1extern crate proc_macro;
2use proc_macro::TokenStream;
3use quote::ToTokens;
4use syn::{parse_macro_input, AttributeArgs, ItemFn};
5
6use crate::expand::HasPermissions;
7
8mod expand;
9
10const HAS_AUTHORITIES: &str = "has_permissions";
11const HAS_ANY_AUTHORITY: &str = "has_any_permission";
12
13const HAS_ROLES: &str = "has_roles";
14const HAS_ANY_ROLE: &str = "has_any_role";
15
16/// Macro to сheck that the user has all the specified permissions.
17/// Allow to add a conditional restriction based on handlers parameters.
18/// Add the `secure` attribute followed by the the boolean expression to validate based on parameters
19///
20/// Also you can use you own types instead of Strings, just add `type` attribute with path to type
21/// # Examples
22/// ```
23/// use actix_web_grants::proc_macro::has_permissions;
24/// use actix_web::HttpResponse;
25///
26/// // User should be ADMIN with OP_GET_SECRET permission
27/// #[has_permissions["ROLE_ADMIN", "OP_GET_SECRET"]]
28/// async fn macro_secured() -> HttpResponse {
29///     HttpResponse::Ok().body("some secured info")
30/// }
31///
32/// // User should be ADMIN with OP_GET_SECRET permission and the user.id param should be equal
33/// // to the path parameter {user_id}
34/// struct User {id: i32}
35/// #[has_permissions["ROLE_ADMIN", "OP_GET_SECRET", secure="user_id.into_inner() == user.id"]]
36/// async fn macro_secured_params(user_id: web::Path<i32>, user: web::Data<User>) -> HttpResponse {
37///     HttpResponse::Ok().body("some secured info with user_id path equal to user.id")
38///}
39///
40/// // User must have MyPermissionEnum::OP_GET_SECRET (you own enum example)
41/// #[has_permissions["OP_GET_SECRET", type = "MyPermissionEnum"]]
42/// async fn macro_enum_secured() -> HttpResponse {
43///     HttpResponse::Ok().body("some secured info")
44/// }
45///
46///```
47#[proc_macro_attribute]
48pub fn has_permissions(args: TokenStream, input: TokenStream) -> TokenStream {
49    check_permissions(HAS_AUTHORITIES, args, input)
50}
51
52/// Macro to сheck that the user has any of the specified permissions.
53///
54/// # Examples
55/// ```
56/// use actix_web_grants::proc_macro::has_any_permission;
57/// use actix_web::HttpResponse;
58///
59/// // User should be ADMIN or MANAGER
60/// #[has_any_permission["ROLE_ADMIN", "ROLE_MANAGER"]]
61/// async fn macro_secured() -> HttpResponse {
62///     HttpResponse::Ok().body("some secured info")
63/// }
64/// ```
65#[proc_macro_attribute]
66pub fn has_any_permission(args: TokenStream, input: TokenStream) -> TokenStream {
67    check_permissions(HAS_ANY_AUTHORITY, args, input)
68}
69
70/// Macro to сheck that the user has all the specified roles.
71/// Role - is permission with prefix "ROLE_".
72///
73/// # Examples
74/// ```
75/// use actix_web_grants::proc_macro::has_roles;
76/// use actix_web::HttpResponse;
77///
78/// // User should be ADMIN and MANAGER
79/// #[has_roles["ADMIN", "MANAGER"]]
80/// async fn macro_secured() -> HttpResponse {
81///     HttpResponse::Ok().body("some secured info")
82/// }
83/// ```
84#[proc_macro_attribute]
85pub fn has_roles(args: TokenStream, input: TokenStream) -> TokenStream {
86    check_permissions(HAS_ROLES, args, input)
87}
88
89/// Macro to сheck that the user has any the specified roles.
90/// Role - is permission with prefix "ROLE_".
91///
92/// # Examples
93/// ```
94/// use actix_web_grants::proc_macro::has_any_role;
95/// use actix_web::HttpResponse;
96///
97/// // User should be ADMIN or MANAGER
98/// #[has_any_role["ADMIN", "MANAGER"]]
99/// async fn macro_secured() -> HttpResponse {
100///     HttpResponse::Ok().body("some secured info")
101/// }
102/// ```
103#[proc_macro_attribute]
104pub fn has_any_role(args: TokenStream, input: TokenStream) -> TokenStream {
105    check_permissions(HAS_ANY_ROLE, args, input)
106}
107
108fn check_permissions(check_fn_name: &str, args: TokenStream, input: TokenStream) -> TokenStream {
109    let args = parse_macro_input!(args as AttributeArgs);
110    let func = parse_macro_input!(input as ItemFn);
111
112    match HasPermissions::new(check_fn_name, args, func) {
113        Ok(has_permissions) => has_permissions.into_token_stream().into(),
114        Err(err) => err.to_compile_error().into(),
115    }
116}