Skip to main content

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 */
19
20use crate::{common::Result, error::ConnectionError, Connection};
21
22#[derive(Clone, Debug)]
23/// User information
24pub struct User {
25    /// Returns the name of this user.
26    pub username: String,
27    /// Returns the number of seconds remaining till this user’s current password expires.
28    pub password_expiry_seconds: Option<i64>,
29}
30
31impl User {
32    /// Updates user password.
33    ///
34    /// # Arguments
35    ///
36    /// * `connection` -- an opened `Connection`
37    /// * `password_old` -- an old password
38    /// * `password_new` -- a new password
39    ///
40    /// # Examples
41    ///
42    /// ```rust
43    /// user.password_update(connection, "oldpassword", "nEwp@ssw0rd").await;
44    ///```
45    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
46    pub async fn password_update(
47        &self,
48        connection: &Connection,
49        password_old: impl Into<String>,
50        password_new: impl Into<String>,
51    ) -> Result {
52        let password_old = password_old.into();
53        let password_new = password_new.into();
54        let mut error_buffer = Vec::with_capacity(connection.server_count());
55        for (server_id, server_connection) in connection.connections() {
56            match server_connection
57                .update_user_password(self.username.clone(), password_old.clone(), password_new.clone())
58                .await
59            {
60                Ok(()) => return Ok(()),
61                Err(err) => error_buffer.push(format!("- {}: {}", server_id, err)),
62            }
63        }
64        Err(ConnectionError::CloudAllNodesFailed { errors: error_buffer.join("\n") })?
65    }
66}