pub struct AccountInsertService<R, G>{ /* private fields */ }Expand description
Service for creating new user accounts with their associated authentication secrets.
This service provides an ergonomic builder pattern for creating accounts with roles, groups, and permissions, then storing both the account data and authentication secrets in their respective repositories.
§Basic Usage
use axum_gate::accounts::AccountInsertService;
use axum_gate::prelude::{Role, Group};
use axum_gate::repositories::memory::{MemoryAccountRepository, MemorySecretRepository};
use std::sync::Arc;
let account_repo = Arc::new(MemoryAccountRepository::<Role, Group>::default());
let secret_repo = Arc::new(MemorySecretRepository::new_with_argon2_hasher().unwrap());
let account = AccountInsertService::insert("user@example.com", "secure_password")
.with_roles(vec![Role::User])
.with_groups(vec![Group::new("staff")])
.into_repositories(account_repo, secret_repo)
.await
.unwrap()
.unwrap();
println!("Created account: {}", account.user_id);Implementations§
Source§impl<R, G> AccountInsertService<R, G>
impl<R, G> AccountInsertService<R, G>
Sourcepub fn insert(user_id: &str, secret: &str) -> Self
pub fn insert(user_id: &str, secret: &str) -> Self
Creates a new account insertion builder with the specified credentials.
This is the starting point for creating a new account. The user ID should be unique within your application (typically an email or username), and the secret will be hashed before storage using Argon2.
§Arguments
user_id- Unique identifier for the user (e.g., email or username)secret- Plain text password that will be securely hashed
§Example
use axum_gate::accounts::AccountInsertService;
use axum_gate::prelude::{Role, Group};
let builder = AccountInsertService::<Role, Group>::insert("admin@example.com", "strong_password");
// Continue with .with_roles(), .with_groups(), etc.Sourcepub fn with_roles(self, roles: Vec<R>) -> Self
pub fn with_roles(self, roles: Vec<R>) -> Self
Adds roles to the account being created.
Roles determine what actions the user can perform. Use the pre-defined
Role enum or create your own custom role type.
§Example
use axum_gate::accounts::AccountInsertService;
use axum_gate::prelude::{Role, Group};
let builder = AccountInsertService::<Role, Group>::insert("user@example.com", "password")
.with_roles(vec![Role::User, Role::Reporter]);Sourcepub fn with_groups(self, groups: Vec<G>) -> Self
pub fn with_groups(self, groups: Vec<G>) -> Self
Adds groups to the account being created.
Groups provide organizational structure for users, such as department or team membership. They offer another dimension of access control.
§Example
use axum_gate::accounts::AccountInsertService;
use axum_gate::prelude::{Role, Group};
let builder = AccountInsertService::<Role, Group>::insert("user@example.com", "password")
.with_groups(vec![Group::new("engineering"), Group::new("backend-team")]);Sourcepub fn with_permissions(self, permissions: Permissions) -> Self
pub fn with_permissions(self, permissions: Permissions) -> Self
Adds custom permissions to the account being created.
This method allows you to set specific permissions using the zero-synchronization permission system. Permissions are stored as a compressed bitmap for efficiency.
§Arguments
permissions- A Permissions set containing the permission names
§Example
use axum_gate::accounts::AccountInsertService;
use axum_gate::permissions::Permissions;
use axum_gate::prelude::{Role, Group};
let permissions: Permissions = [
"read:api",
"write:api",
"manage:users"
].into_iter().collect();
let builder = AccountInsertService::<Role, Group>::insert("admin@example.com", "password")
.with_permissions(permissions);Sourcepub async fn into_repositories<AccRepo, SecRepo>(
self,
account_repository: Arc<AccRepo>,
secret_repository: Arc<SecRepo>,
) -> Result<Option<Account<R, G>>>where
AccRepo: AccountRepository<R, G>,
SecRepo: SecretRepository,
pub async fn into_repositories<AccRepo, SecRepo>(
self,
account_repository: Arc<AccRepo>,
secret_repository: Arc<SecRepo>,
) -> Result<Option<Account<R, G>>>where
AccRepo: AccountRepository<R, G>,
SecRepo: SecretRepository,
Creates the account and secret, storing them in the provided repositories.
This method consumes the builder and performs the actual account creation:
- Creates an
Accountwith the specified details - Stores the account in the account repository
- Hashes the password using Argon2
- Creates and stores the secret in the secret repository
Both operations must succeed for the account to be considered created.
§Arguments
account_repository- Repository for storing account datasecret_repository- Repository for storing password hashes
§Returns
Ok(Some(Account))- Account successfully createdOk(None)- Account creation failed (repository returned None)Err(...)- Error during creation process
§Example
use axum_gate::accounts::AccountInsertService;
use axum_gate::prelude::{Role, Group};
use axum_gate::repositories::memory::{MemoryAccountRepository, MemorySecretRepository};
use std::sync::Arc;
let account_repo = Arc::new(MemoryAccountRepository::<Role, Group>::default());
let secret_repo = Arc::new(MemorySecretRepository::new_with_argon2_hasher().unwrap());
let result = AccountInsertService::insert("user@example.com", "password")
.with_roles(vec![Role::User])
.into_repositories(account_repo, secret_repo)
.await?;
match result {
Some(account) => println!("Created account: {}", account.user_id),
None => println!("Account creation failed"),
}Auto Trait Implementations§
impl<R, G> Freeze for AccountInsertService<R, G>
impl<R, G> RefUnwindSafe for AccountInsertService<R, G>where
R: RefUnwindSafe,
G: RefUnwindSafe,
impl<R, G> Send for AccountInsertService<R, G>
impl<R, G> Sync for AccountInsertService<R, G>
impl<R, G> Unpin for AccountInsertService<R, G>
impl<R, G> UnsafeUnpin for AccountInsertService<R, G>
impl<R, G> UnwindSafe for AccountInsertService<R, G>where
R: UnwindSafe,
G: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more