pub struct UserId(/* private fields */);Expand description
User identifier value object for type-safe authentication and authorization
This value object provides type-safe user authentication with authorization management, identity validation, and comprehensive security features. It implements Domain-Driven Design (DDD) value object patterns with multiple user ID format support.
§Key Features
- Type Safety: Strongly-typed user identifiers that cannot be confused with other string types
- Authentication: Comprehensive user authentication with format validation
- Authorization: User-specific permission checking and access control
- Cross-Platform: Consistent representation across languages and storage systems
- Security Features: Audit trails, user classification, and domain management
- Serialization: Full serialization support for storage and API integration
§Benefits Over Raw Strings
- Type Safety:
UserIdcannot be confused with other string types - Domain Semantics: Clear intent in function signatures and authentication business logic
- User Validation: Comprehensive validation rules and format checking
- Future Evolution: Extensible for user-specific methods and security features
§Security Benefits
- Type Safety: Cannot be confused with other string types in security contexts
- Validation: Format checking and constraint enforcement for reliable authentication
- Audit Trails: Clear user action tracking and accountability
- Authorization: User-specific permission checking and access control
§User ID Formats
- Email:
user@domain.com(most common, normalized to lowercase) - Username:
username123(alphanumeric with underscores and hyphens) - UUID:
550e8400-e29b-41d4-a716-446655440000(standard UUID format) - System:
system-backup(automatically prefixed system accounts) - API:
api-webhook(automatically prefixed API accounts)
§Use Cases
- User Authentication: Authenticate users with various ID formats
- Authorization Management: User-specific permission checking and access control
- Identity Validation: Comprehensive format validation and constraint enforcement
- Audit Trails: Track user actions with proper identification
§Usage Examples
§Cross-Language Mapping
- Rust:
UserIdnewtype wrapper with full validation - Go:
UserIDstruct with equivalent interface - JSON: String representation for API compatibility
- Database: TEXT column with validation constraints
Implementations§
Source§impl UserId
impl UserId
Sourcepub fn new(user_id: String) -> Result<Self, PipelineError>
pub fn new(user_id: String) -> Result<Self, PipelineError>
Creates a new user ID with format validation
§Purpose
Creates a type-safe user identifier with comprehensive format validation. Supports email, username, UUID, and system account formats.
§Why
Type-safe user IDs provide:
- Prevention of authentication errors from invalid formats
- Clear API contracts for authentication systems
- Audit trail support with validated identities
- Domain-based authorization capabilities
§Arguments
user_id- User identifier string (email, username, UUID, or system)
§Returns
Ok(UserId)- Valid user IDErr(PipelineError::InvalidConfiguration)- Invalid format
§Errors
Returns error when:
- User ID is empty
- User ID exceeds 256 characters
- Contains invalid characters
§Examples
Sourcepub fn parse(user_id: &str) -> Result<Self, PipelineError>
pub fn parse(user_id: &str) -> Result<Self, PipelineError>
Creates a user ID from a string slice
Sourcepub fn is_username(&self) -> bool
pub fn is_username(&self) -> bool
Checks if this is a username format user ID
Sourcepub fn email_domain(&self) -> Option<&str>
pub fn email_domain(&self) -> Option<&str>
Gets the domain from email format user ID
Sourcepub fn email_local(&self) -> Option<&str>
pub fn email_local(&self) -> Option<&str>
Gets the local part from email format user ID
Sourcepub fn belongs_to_domain(&self, domain: &str) -> bool
pub fn belongs_to_domain(&self, domain: &str) -> bool
Checks if user belongs to a specific domain
§Purpose
Validates user’s email domain for domain-based authorization. Used for multi-tenant access control and organization filtering.
§Why
Domain-based authorization enables:
- Multi-tenant access control
- Organization-level permissions
- Domain-specific feature access
- Email-based user grouping
§Arguments
domain- Domain to check (case-insensitive)
§Returns
true if user’s email domain matches (case-insensitive), false
otherwise
§Examples
Sourcepub fn is_system_user(&self) -> bool
pub fn is_system_user(&self) -> bool
Checks if this is a system user account
§Purpose
Identifies service accounts and system users for special handling. System users typically have elevated permissions and different audit requirements.
§Why
System user detection enables:
- Service account identification
- Automated process authentication
- Different authorization rules
- Separate audit logging
§Returns
true if user ID starts with: system-, service-, bot-, or api-
§Examples
Sourcepub fn is_admin_user(&self) -> bool
pub fn is_admin_user(&self) -> bool
Checks if this is an admin user (contains ‘admin’ or ends with ‘-admin’)
Sourcepub fn validate(&self) -> Result<(), PipelineError>
pub fn validate(&self) -> Result<(), PipelineError>
Validates the user ID
Source§impl UserId
Predefined user ID builders
impl UserId
Predefined user ID builders
Sourcepub fn email(email: &str) -> Result<Self, PipelineError>
pub fn email(email: &str) -> Result<Self, PipelineError>
Creates an email-based user ID
Sourcepub fn username(username: &str) -> Result<Self, PipelineError>
pub fn username(username: &str) -> Result<Self, PipelineError>
Creates a username-based user ID
Sourcepub fn uuid(uuid: &str) -> Result<Self, PipelineError>
pub fn uuid(uuid: &str) -> Result<Self, PipelineError>
Creates a UUID-based user ID
Sourcepub fn system(name: &str) -> Result<Self, PipelineError>
pub fn system(name: &str) -> Result<Self, PipelineError>
Creates a system user ID
Sourcepub fn api(name: &str) -> Result<Self, PipelineError>
pub fn api(name: &str) -> Result<Self, PipelineError>
Creates an API user ID
Trait Implementations§
Source§impl Ord for UserId
impl Ord for UserId
Source§impl PartialOrd for UserId
impl PartialOrd for UserId
impl Eq for UserId
impl StructuralPartialEq for UserId
Auto Trait Implementations§
impl Freeze for UserId
impl RefUnwindSafe for UserId
impl Send for UserId
impl Sync for UserId
impl Unpin for UserId
impl UnwindSafe for UserId
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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