typedb_driver/user/
user_manager.rs1use std::collections::HashMap;
20
21use crate::{
22 common::{address::Address, Result},
23 connection::server_connection::ServerConnection,
24 error::ConnectionError,
25 User,
26};
27
28#[derive(Debug)]
30pub struct UserManager {
31 server_connections: HashMap<Address, ServerConnection>,
32}
33
34impl UserManager {
35 pub fn new(server_connections: HashMap<Address, ServerConnection>) -> Self {
36 Self { server_connections }
37 }
38
39 #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
40 pub async fn get_current_user(&self) -> Result<Option<User>> {
41 let (_, connection) = self
42 .server_connections
43 .iter()
44 .next()
45 .expect("Unexpected condition: the server connection collection is empty");
46 self.get(connection.username()).await
47 }
48
49 #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
61 pub async fn contains(&self, username: impl Into<String>) -> Result<bool> {
62 let username = username.into();
63 let mut error_buffer = Vec::with_capacity(self.server_connections.len());
64 for (server_id, server_connection) in self.server_connections.iter() {
65 match server_connection.contains_user(username.clone()).await {
66 Ok(res) => return Ok(res),
67 Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
68 }
69 }
70 Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
71 }
72
73 #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
85 pub async fn get(&self, username: impl Into<String>) -> Result<Option<User>> {
86 let uname = username.into();
87 let mut error_buffer = Vec::with_capacity(self.server_connections.len());
88 for (server_id, server_connection) in self.server_connections.iter() {
89 match server_connection.get_user(uname.clone()).await {
90 Ok(res) => {
91 return Ok(res.map(|u_info| User {
92 name: u_info.name,
93 password: u_info.password,
94 server_connections: self.server_connections.clone(),
95 }))
96 }
97 Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
98 }
99 }
100 Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
101 }
102
103 #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
111 pub async fn all(&self) -> Result<Vec<User>> {
112 let mut error_buffer = Vec::with_capacity(self.server_connections.len());
113 for (server_id, server_connection) in self.server_connections.iter() {
114 match server_connection.all_users().await {
115 Ok(res) => {
116 return Ok(res
117 .iter()
118 .map(|u_info| User {
119 name: u_info.name.clone(),
120 password: u_info.password.clone(),
121 server_connections: self.server_connections.clone(),
122 })
123 .collect())
124 }
125 Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
126 }
127 }
128 Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
129 }
130
131 #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
144 pub async fn create(&self, username: impl Into<String>, password: impl Into<String>) -> Result {
145 let uname = username.into();
146 let passwd = password.into();
147 let mut error_buffer = Vec::with_capacity(self.server_connections.len());
148 for (server_id, server_connection) in self.server_connections.iter() {
149 match server_connection.create_user(uname.clone(), passwd.clone()).await {
150 Ok(res) => return Ok(res),
151 Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
152 }
153 }
154 Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
155 }
156}