systemprompt_users/services/
user_provider.rs1use async_trait::async_trait;
2use systemprompt_identifiers::UserId;
3use systemprompt_traits::{AuthProviderError, AuthResult, AuthUser, UserProvider};
4
5
6use crate::UserService;
7
8#[derive(Debug, Clone)]
9pub struct UserProviderImpl {
10 user_service: UserService,
11}
12
13impl UserProviderImpl {
14 pub const fn new(user_service: UserService) -> Self {
15 Self { user_service }
16 }
17}
18
19impl From<crate::User> for AuthUser {
20 fn from(user: crate::User) -> Self {
21 let is_active = user.is_active();
22 Self {
23 id: user.id,
24 name: user.name,
25 email: user.email,
26 roles: user.roles,
27 is_active,
28 }
29 }
30}
31
32#[async_trait]
33impl UserProvider for UserProviderImpl {
34 async fn find_by_id(&self, id: &UserId) -> AuthResult<Option<AuthUser>> {
35 self.user_service
36 .find_by_id(id)
37 .await
38 .map(|opt| opt.map(AuthUser::from))
39 .map_err(|e| AuthProviderError::Internal(e.to_string()))
40 }
41
42 async fn find_by_email(&self, email: &str) -> AuthResult<Option<AuthUser>> {
43 self.user_service
44 .find_by_email(email)
45 .await
46 .map(|opt| opt.map(AuthUser::from))
47 .map_err(|e| AuthProviderError::Internal(e.to_string()))
48 }
49
50 async fn find_by_name(&self, name: &str) -> AuthResult<Option<AuthUser>> {
51 self.user_service
52 .find_by_name(name)
53 .await
54 .map(|opt| opt.map(AuthUser::from))
55 .map_err(|e| AuthProviderError::Internal(e.to_string()))
56 }
57
58 async fn create_user(
59 &self,
60 name: &str,
61 email: &str,
62 full_name: Option<&str>,
63 ) -> AuthResult<AuthUser> {
64 self.user_service
65 .create(name, email, full_name, None)
66 .await
67 .map(AuthUser::from)
68 .map_err(|e| AuthProviderError::Internal(e.to_string()))
69 }
70
71 async fn create_anonymous(&self, fingerprint: &str) -> AuthResult<AuthUser> {
72 self.user_service
73 .create_anonymous(fingerprint)
74 .await
75 .map(AuthUser::from)
76 .map_err(|e| AuthProviderError::Internal(e.to_string()))
77 }
78
79 async fn assign_roles(&self, user_id: &UserId, roles: &[String]) -> AuthResult<()> {
80 self.user_service
81 .assign_roles(user_id, roles)
82 .await
83 .map(|_| ())
84 .map_err(|e| AuthProviderError::Internal(e.to_string()))
85 }
86}