init
This commit is contained in:
commit
70c87bfad5
10 changed files with 4165 additions and 0 deletions
44
src/lib.rs
Normal file
44
src/lib.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
#![feature(const_vec_string_slice)]
|
||||
use data_encoding::HEXUPPER;
|
||||
use rand::RngCore;
|
||||
|
||||
pub mod csrf;
|
||||
pub mod profile_pic;
|
||||
mod session;
|
||||
mod user;
|
||||
pub use session::Session;
|
||||
pub use session::Sessions;
|
||||
pub use user::APIUser;
|
||||
pub use user::AdminUser;
|
||||
pub use user::MaybeUser;
|
||||
pub use user::User;
|
||||
pub use user::UserAuth;
|
||||
pub use user::UserRole;
|
||||
|
||||
/// A macro to check if a user has admin privileges.
|
||||
///
|
||||
/// This macro checks whether the provided user has admin privileges by calling the `is_admin` method on it.
|
||||
/// If the user is not an admin, it returns a `Forbidden` error with a message indicating the restriction.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `$u` - The user to check.
|
||||
///
|
||||
/// # Returns
|
||||
/// The macro does not return a value directly but controls the flow of execution. If the user is not an admin,
|
||||
/// it returns a `Forbidden` error immediately and prevents further execution.
|
||||
#[macro_export]
|
||||
macro_rules! check_admin {
|
||||
($u:ident) => {
|
||||
if !$u.is_admin() {
|
||||
return Err($crate::request::api::api_error("Forbidden"));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn gen_random(token_length: usize) -> String {
|
||||
let mut token_bytes = vec![0u8; token_length];
|
||||
|
||||
rand::thread_rng().fill_bytes(&mut token_bytes);
|
||||
|
||||
HEXUPPER.encode(&token_bytes)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue