typedb_driver/user/
user.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19use std::{collections::HashMap, sync::Arc};
20
21use crate::{
22    common::{address::Address, Result},
23    connection::server_connection::ServerConnection,
24    error::ConnectionError,
25};
26
27#[derive(Clone, Debug)]
28pub struct User {
29    pub name: String,
30    pub password: Option<String>,
31    pub server_connections: HashMap<Address, ServerConnection>,
32}
33
34impl User {
35    /// Update the user's password.
36    ///
37    /// # Arguments
38    ///
39    /// * `username` — The name of the user
40    /// * `password` — The new password
41    ///
42    /// # Examples
43    ///
44    /// ```rust
45    #[cfg_attr(feature = "sync", doc = "user.update_password(username, password);")]
46    #[cfg_attr(not(feature = "sync"), doc = "user.update_password(username, password).await;")]
47    /// user.update_password(username, password).await;
48    /// ```
49    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
50    pub async fn update_password(&self, password: impl Into<String>) -> Result<()> {
51        let password = password.into();
52        let mut error_buffer = Vec::with_capacity(self.server_connections.len());
53        for (server_id, server_connection) in self.server_connections.iter() {
54            match server_connection.update_password(self.name.clone(), password.clone()).await {
55                Ok(res) => return Ok(()),
56                Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
57            }
58        }
59        Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
60    }
61
62    /// Deletes this user
63    ///
64    /// * `username` — The name of the user to be deleted
65    ///
66    /// # Examples
67    ///
68    /// ```rust
69    #[cfg_attr(feature = "sync", doc = "user.delete();")]
70    #[cfg_attr(not(feature = "sync"), doc = "user.delete().await;")]
71    /// user.delete(username).await;
72    /// ```
73    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
74    pub async fn delete(self) -> Result {
75        let mut error_buffer = Vec::with_capacity(self.server_connections.len());
76        for (server_id, server_connection) in self.server_connections.iter() {
77            match server_connection.delete_user(self.name.clone()).await {
78                Ok(res) => return Ok(res),
79                Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
80            }
81        }
82        Err(ConnectionError::ServerConnectionFailedWithError { error: error_buffer.join("\n") })?
83    }
84}