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 */
19use std::sync::Arc;
20
21use crate::{
22    common::Result,
23    connection::server::{server_manager::ServerManager, server_routing::ServerRouting},
24    info::UserInfo,
25};
26
27/// A TypeDB server user, identified by a username.
28#[derive(Clone, Debug)]
29pub struct User {
30    name: String,
31    password: Option<String>,
32    server_manager: Arc<ServerManager>,
33}
34
35impl User {
36    pub(crate) fn new(name: String, password: Option<String>, server_manager: Arc<ServerManager>) -> Self {
37        Self { name, password, server_manager }
38    }
39
40    pub(crate) fn from_info(user_info: UserInfo, server_manager: Arc<ServerManager>) -> Self {
41        Self::new(user_info.name, user_info.password, server_manager)
42    }
43
44    /// Retrieves the username as a string.
45    pub fn name(&self) -> &str {
46        self.name.as_str()
47    }
48
49    // TODO: We don't actually need to expose it?
50    /// Retrieves the password as a string, if accessible.
51    pub fn password(&self) -> Option<&str> {
52        self.password.as_ref().map(|value| value.as_str())
53    }
54
55    /// Updates the user's password.
56    ///
57    /// # Arguments
58    ///
59    /// * `password` — The new password
60    ///
61    /// # Examples
62    ///
63    /// ```rust
64    #[cfg_attr(feature = "sync", doc = "user.update_password(password);")]
65    #[cfg_attr(not(feature = "sync"), doc = "user.update_password(password).await;")]
66    /// ```
67    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
68    pub async fn update_password(&self, password: impl Into<String>) -> Result<()> {
69        let password = password.into();
70        self.server_manager
71            .execute(ServerRouting::Auto, |server_connection| {
72                let name = self.name.clone();
73                let password = password.clone();
74                async move { server_connection.update_password(name, password).await }
75            })
76            .await
77    }
78
79    /// Deletes this user.
80    ///
81    /// # Examples
82    ///
83    /// ```rust
84    #[cfg_attr(feature = "sync", doc = "user.delete();")]
85    #[cfg_attr(not(feature = "sync"), doc = "user.delete().await;")]
86    /// ```
87    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
88    pub async fn delete(self) -> Result {
89        self.server_manager
90            .execute(ServerRouting::Auto, |server_connection| {
91                let name = self.name.clone();
92                async move { server_connection.delete_user(name).await }
93            })
94            .await
95    }
96}