Skip to main content

AccountInsertService

Struct AccountInsertService 

Source
pub struct AccountInsertService<R, G>
where R: AccessHierarchy + Eq, G: Eq,
{ /* 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>
where R: AccessHierarchy + Eq, G: Eq + Clone,

Source

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.
Source

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]);
Source

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")]);
Source

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);
Source

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:

  1. Creates an Account with the specified details
  2. Stores the account in the account repository
  3. Hashes the password using Argon2
  4. 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 data
  • secret_repository - Repository for storing password hashes
§Returns
  • Ok(Some(Account)) - Account successfully created
  • Ok(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>

§

impl<R, G> Send for AccountInsertService<R, G>
where R: Send, G: Send,

§

impl<R, G> Sync for AccountInsertService<R, G>
where R: Sync, G: Sync,

§

impl<R, G> Unpin for AccountInsertService<R, G>
where R: Unpin, G: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool