paladin-ai 0.4.3

Enterprise AI orchestration framework with multi-agent coordination patterns
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
User Service

Business logic layer for user operations including registration, authentication,
and profile management. This service coordinates between domain entities and
infrastructure adapters.
*/

use crate::application::services::notification_orchestrator::NotificationService;
use crate::core::base::entity::message::Location;
use crate::core::platform::container::log::{LogDestination, LogEntryBuilder, LogLevel};
use crate::core::platform::container::user::{Email, User, UserError};
use argon2::password_hash::{SaltString, rand_core::OsRng};
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use async_trait::async_trait;
use paladin_ports::output::auth_port::AuthPort;
use paladin_ports::output::log_port::LogPort;
use paladin_ports::output::user_repository_port::UserRepositoryPort;
use std::sync::Arc;
use uuid::Uuid;

// Re-export trait + DTOs from paladin-core so existing consumers keep working.
pub use paladin_core::platform::manager::user_service::{
    UserAuthenticationResult, UserLoginRequest, UserProfileUpdateRequest, UserRegistrationRequest,
    UserServiceTrait,
};

/// Concrete implementation of UserService
pub struct UserService {
    user_repository: Arc<dyn UserRepositoryPort>,
    log_port: Arc<dyn LogPort>,
    notification_service: Arc<NotificationService>,
    argon2: Argon2<'static>,
    auth_port: Option<Arc<dyn AuthPort>>,
}

impl UserService {
    /// Creates a new UserService instance
    pub fn new(
        user_repository: Arc<dyn UserRepositoryPort>,
        log_port: Arc<dyn LogPort>,
        notification_service: Arc<NotificationService>,
    ) -> Self {
        Self {
            user_repository,
            log_port,
            notification_service,
            argon2: Argon2::default(),
            auth_port: None,
        }
    }

    /// Attaches an authentication provider so that successful logins issue a
    /// bearer token. Without it, `login_user` succeeds but returns no token.
    pub fn with_auth_port(mut self, auth_port: Arc<dyn AuthPort>) -> Self {
        self.auth_port = Some(auth_port);
        self
    }

    /// Hash password using Argon2
    pub fn hash_password(&self, password: &str) -> Result<String, UserError> {
        if password.len() < 8 {
            return Err(UserError::InvalidPassword(
                "Password must be at least 8 characters".to_string(),
            ));
        }
        if password.len() > 128 {
            return Err(UserError::InvalidPassword(
                "Password cannot exceed 128 characters".to_string(),
            ));
        }

        let salt = SaltString::generate(&mut OsRng);
        let password_hash = self
            .argon2
            .hash_password(password.as_bytes(), &salt)
            .map_err(|e| UserError::HashError(e.to_string()))?;

        Ok(password_hash.to_string())
    }

    /// Verify password against hash
    pub fn verify_password(&self, password: &str, hash: &str) -> Result<bool, UserError> {
        let parsed_hash =
            PasswordHash::new(hash).map_err(|e| UserError::HashError(e.to_string()))?;

        match self
            .argon2
            .verify_password(password.as_bytes(), &parsed_hash)
        {
            Ok(_) => Ok(true),
            Err(_) => Ok(false),
        }
    }

    /// Validate username
    fn validate_username(&self, username: &str) -> Result<(), UserError> {
        if username.trim().is_empty() {
            return Err(UserError::InvalidUsername(
                "Username cannot be empty".to_string(),
            ));
        }
        if username.len() < 3 {
            return Err(UserError::InvalidUsername(
                "Username must be at least 3 characters".to_string(),
            ));
        }
        if username.len() > 50 {
            return Err(UserError::InvalidUsername(
                "Username cannot exceed 50 characters".to_string(),
            ));
        }
        if !username
            .chars()
            .all(|c| c.is_alphanumeric() || c == '_' || c == '-')
        {
            return Err(UserError::InvalidUsername(
                "Username can only contain alphanumeric characters, underscores, and hyphens"
                    .to_string(),
            ));
        }
        Ok(())
    }

    /// Send welcome notification
    async fn send_welcome_notification(&self, user: &User) -> Result<(), UserError> {
        use crate::core::platform::container::notification::{
            NotificationChannel, NotificationContent, NotificationPriority, NotificationRecipient,
        };

        let recipient = NotificationRecipient::Email(user.email().value().to_string());
        let content = NotificationContent {
            title: "Welcome to paladin!".to_string(),
            body: format!("Hello {}, welcome to our platform!", user.username()),
            category: "welcome".to_string(),
            action_url: None,
            attachments: Vec::new(),
            template_id: Some("user_welcome".to_string()),
            template_variables: std::collections::HashMap::new(),
            metadata: std::collections::HashMap::new(),
        };

        let notification = self
            .notification_service
            .create_notification(
                recipient,
                content,
                NotificationChannel::Email,
                NotificationPriority::Normal,
            )
            .await
            .map_err(|e| {
                UserError::RepositoryError(format!("Failed to create welcome notification: {}", e))
            })?;

        self.notification_service
            .send_notification(notification.id)
            .await
            .map_err(|e| {
                UserError::RepositoryError(format!("Failed to send welcome notification: {}", e))
            })?;

        Ok(())
    }

    /// Log user action
    async fn log_action(&self, level: LogLevel, message: String, user_id: Option<Uuid>) {
        let enhanced_message = match user_id {
            Some(id) => format!("[User: {}] {}", id, message),
            None => message,
        };

        // Create a proper log entry using LogEntryBuilder
        let log_entry = LogEntryBuilder::new_entry(
            Location::service("user-service"),
            LogDestination::System,
            level,
            enhanced_message,
        );

        // Use write_entry instead of log
        if let Err(e) = self.log_port.write_entry(log_entry).await {
            eprintln!("Failed to log user action: {}", e);
        }
    }
}

#[async_trait]
impl UserServiceTrait for UserService {
    async fn register_user(&self, request: UserRegistrationRequest) -> Result<User, UserError> {
        // Validate input
        self.validate_username(&request.username)?;
        let email = Email::new(request.email)?;

        // Check if user already exists
        if self
            .user_repository
            .find_by_email(email.value())
            .await?
            .is_some()
        {
            return Err(UserError::EmailAlreadyExists(email.value().to_string()));
        }

        // Hash password
        let password_hash = self.hash_password(&request.password)?;

        // Create user
        let user = User::new_user(
            request.username.clone(),
            email,
            password_hash,
            request.profile,
        );

        // Save user
        let saved_user = self.user_repository.save(user).await?;

        // Log successful registration
        self.log_action(
            LogLevel::Info,
            format!("User registered successfully: {}", request.username),
            Some(saved_user.uuid),
        )
        .await;

        // Send welcome notification
        if let Err(e) = self.send_welcome_notification(&saved_user).await {
            self.log_action(
                LogLevel::Warn,
                format!("Failed to send welcome notification: {}", e),
                Some(saved_user.uuid),
            )
            .await;
        }

        Ok(saved_user)
    }

    async fn login_user(
        &self,
        request: UserLoginRequest,
    ) -> Result<UserAuthenticationResult, UserError> {
        // Find user by email
        let user = self
            .user_repository
            .find_by_email(&request.email)
            .await?
            .ok_or(UserError::AuthenticationFailed)?;

        // Check if user is active
        if !user.is_active() {
            self.log_action(
                LogLevel::Warn,
                format!("Login attempt for inactive user: {}", request.email),
                Some(user.uuid),
            )
            .await;
            return Err(UserError::UserNotActive);
        }

        // Verify password
        if !self.verify_password(&request.password, user.password_hash())? {
            self.log_action(
                LogLevel::Warn,
                format!("Failed login attempt for user: {}", request.email),
                Some(user.uuid),
            )
            .await;
            return Err(UserError::AuthenticationFailed);
        }

        // Log successful login
        self.log_action(
            LogLevel::Info,
            format!("User logged in successfully: {}", request.email),
            Some(user.uuid),
        )
        .await;

        // Issue a bearer token when an auth provider is configured.
        let (token, token_expires_at) = match &self.auth_port {
            Some(auth_port) => {
                let issued = auth_port
                    .issue_token(user.uuid, user.role())
                    .await
                    .map_err(|e| UserError::HashError(format!("token issuance failed: {e}")))?;
                (Some(issued.token), Some(issued.expires_at))
            }
            None => (None, None),
        };

        Ok(UserAuthenticationResult {
            user_id: user.uuid,
            username: user.username().to_string(),
            email: user.email().value().to_string(),
            is_verified: user.is_verified(),
            success: true,
            token,
            token_expires_at,
        })
    }

    async fn update_user_profile(
        &self,
        request: UserProfileUpdateRequest,
    ) -> Result<User, UserError> {
        // Get existing user
        let mut user = self
            .user_repository
            .find_by_id(request.user_id)
            .await?
            .ok_or(UserError::UserNotFound(request.user_id))?;

        // Update username if provided
        if let Some(new_username) = request.username {
            user.update_username(new_username)?;
        }

        // Update email if provided
        if let Some(new_email) = request.email {
            let email = Email::new(new_email)?;

            // Check if email is already taken by another user
            if let Some(existing_user) = self.user_repository.find_by_email(email.value()).await?
                && existing_user.uuid != user.uuid
            {
                return Err(UserError::EmailAlreadyExists(email.value().to_string()));
            }

            user.update_email(email)?;
        }

        // Update profile if provided
        if let Some(new_profile) = request.profile {
            user.update_profile(new_profile);
        }

        // Save updated user
        let updated_user = self.user_repository.update(user).await?;

        // Log update
        self.log_action(
            LogLevel::Info,
            "User profile updated successfully".to_string(),
            Some(updated_user.uuid),
        )
        .await;

        Ok(updated_user)
    }

    async fn get_user_by_id(&self, user_id: Uuid) -> Result<Option<User>, UserError> {
        self.user_repository.find_by_id(user_id).await
    }

    async fn get_user_by_email(&self, email: &str) -> Result<Option<User>, UserError> {
        self.user_repository.find_by_email(email).await
    }

    async fn delete_user(&self, user_id: Uuid) -> Result<(), UserError> {
        // Ensure the user exists so callers get a clear not-found error.
        if self.user_repository.find_by_id(user_id).await?.is_none() {
            return Err(UserError::UserNotFound(user_id));
        }

        self.user_repository.delete(user_id).await?;

        self.log_action(
            LogLevel::Info,
            "User account deleted".to_string(),
            Some(user_id),
        )
        .await;

        Ok(())
    }

    async fn list_users(&self) -> Result<Vec<User>, UserError> {
        // The repository exposes status-scoped queries; combine both active
        // states to enumerate every user without a dedicated `find_all` method.
        let mut users = self.user_repository.find_by_active_status(true).await?;
        let inactive = self.user_repository.find_by_active_status(false).await?;
        users.extend(inactive);
        Ok(users)
    }

    async fn activate_user(&self, user_id: Uuid) -> Result<(), UserError> {
        let mut user = self
            .user_repository
            .find_by_id(user_id)
            .await?
            .ok_or(UserError::UserNotFound(user_id))?;

        user.activate();
        self.user_repository.update(user).await?;

        self.log_action(
            LogLevel::Info,
            "User account activated".to_string(),
            Some(user_id),
        )
        .await;

        Ok(())
    }

    async fn deactivate_user(&self, user_id: Uuid) -> Result<(), UserError> {
        let mut user = self
            .user_repository
            .find_by_id(user_id)
            .await?
            .ok_or(UserError::UserNotFound(user_id))?;

        user.deactivate();
        self.user_repository.update(user).await?;

        self.log_action(
            LogLevel::Info,
            "User account deactivated".to_string(),
            Some(user_id),
        )
        .await;

        Ok(())
    }

    async fn verify_user(&self, user_id: Uuid) -> Result<(), UserError> {
        let mut user = self
            .user_repository
            .find_by_id(user_id)
            .await?
            .ok_or(UserError::UserNotFound(user_id))?;

        user.verify();
        self.user_repository.update(user).await?;

        self.log_action(
            LogLevel::Info,
            "User email verified".to_string(),
            Some(user_id),
        )
        .await;

        Ok(())
    }

    /// CLI support methods
    /// Find users by active status
    async fn find_by_active_status(&self, is_active: bool) -> Result<Vec<User>, UserError> {
        self.user_repository.find_by_active_status(is_active).await
    }

    /// Find users by verification status
    async fn find_by_verification_status(&self, is_verified: bool) -> Result<Vec<User>, UserError> {
        self.user_repository
            .find_by_verification_status(is_verified)
            .await
    }

    /// Count total users
    async fn count_users(&self) -> Result<u64, UserError> {
        self.user_repository.count_users().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::base::service::message_service::{MessageService, MessageServiceConfig};
    use crate::infrastructure::adapters::auth::InMemoryTokenAuthAdapter;
    use crate::infrastructure::adapters::logs::system_log_adapter::{
        SystemLogAdapter, SystemLogAdapterConfig,
    };
    use crate::infrastructure::repositories::sqlite_user_repository::SqliteUserRepository;
    use paladin_core::platform::container::notification::NotificationServiceConfig;

    async fn build_service(with_auth: bool) -> UserService {
        let repo = Arc::new(SqliteUserRepository::new("sqlite::memory:").await.unwrap());
        let log_port =
            Arc::new(SystemLogAdapter::new_for_test(SystemLogAdapterConfig::default()).unwrap());
        let message_service = Arc::new(MessageService::new(MessageServiceConfig::default()));
        let notification_service = Arc::new(NotificationService::new(
            NotificationServiceConfig::default(),
            message_service,
        ));
        let service = UserService::new(repo, log_port, notification_service);
        if with_auth {
            service.with_auth_port(Arc::new(InMemoryTokenAuthAdapter::new()))
        } else {
            service
        }
    }

    fn registration(username: &str, email: &str) -> UserRegistrationRequest {
        UserRegistrationRequest {
            username: username.to_string(),
            email: email.to_string(),
            password: "password123".to_string(),
            profile: None,
        }
    }

    #[tokio::test]
    async fn delete_user_removes_the_user() {
        let service = build_service(false).await;
        let user = service
            .register_user(registration("alice", "alice@example.com"))
            .await
            .unwrap();

        service.delete_user(user.uuid).await.unwrap();

        assert!(service.get_user_by_id(user.uuid).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn delete_unknown_user_is_not_found() {
        let service = build_service(false).await;
        let err = service.delete_user(Uuid::new_v4()).await.unwrap_err();
        assert!(matches!(err, UserError::UserNotFound(_)));
    }

    #[tokio::test]
    async fn list_users_returns_all_users() {
        let service = build_service(false).await;
        service
            .register_user(registration("bob", "bob@example.com"))
            .await
            .unwrap();
        service
            .register_user(registration("carol", "carol@example.com"))
            .await
            .unwrap();

        let users = service.list_users().await.unwrap();
        assert_eq!(users.len(), 2);
    }

    #[tokio::test]
    async fn login_issues_token_when_auth_port_configured() {
        let service = build_service(true).await;
        service
            .register_user(registration("dave", "dave@example.com"))
            .await
            .unwrap();

        let result = service
            .login_user(UserLoginRequest {
                email: "dave@example.com".to_string(),
                password: "password123".to_string(),
            })
            .await
            .unwrap();

        assert!(result.success);
        let token = result.token.expect("token should be issued");
        assert!(!token.is_empty());
        let expires_at = result.token_expires_at.expect("expiry should be set");
        assert!(expires_at > chrono::Utc::now());
    }

    #[tokio::test]
    async fn login_without_auth_port_has_no_token() {
        let service = build_service(false).await;
        service
            .register_user(registration("erin", "erin@example.com"))
            .await
            .unwrap();

        let result = service
            .login_user(UserLoginRequest {
                email: "erin@example.com".to_string(),
                password: "password123".to_string(),
            })
            .await
            .unwrap();

        assert!(result.success);
        assert!(result.token.is_none());
        assert!(result.token_expires_at.is_none());
    }
}