Skip to main content

typedb_driver/user/
user_manager.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    User,
23    common::Result,
24    connection::server::{server_manager::ServerManager, server_routing::ServerRouting},
25};
26
27/// Provides access to all user management methods.
28#[derive(Debug)]
29pub struct UserManager {
30    server_manager: Arc<ServerManager>,
31}
32
33impl UserManager {
34    pub(crate) fn new(server_manager: Arc<ServerManager>) -> Self {
35        Self { server_manager }
36    }
37
38    /// Checks if a user with the given name exists.
39    ///
40    /// # Arguments
41    ///
42    /// * `username` — The username to be checked
43    ///
44    /// # Examples
45    ///
46    /// ```rust
47    #[cfg_attr(feature = "sync", doc = "driver.users().contains(username);")]
48    #[cfg_attr(not(feature = "sync"), doc = "driver.users().contains(username).await;")]
49    /// ```
50    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
51    pub async fn contains(&self, username: impl Into<String>) -> Result<bool> {
52        let username = username.into();
53        self.server_manager
54            .execute(ServerRouting::Auto, move |server_connection| {
55                let username = username.clone();
56                async move { server_connection.contains_user(username).await }
57            })
58            .await
59    }
60
61    /// Retrieves a user with the given name.
62    ///
63    /// # Arguments
64    ///
65    /// * `username` — The name of the user to retrieve
66    ///
67    /// # Examples
68    ///
69    /// ```rust
70    #[cfg_attr(feature = "sync", doc = "driver.users().get(username);")]
71    #[cfg_attr(not(feature = "sync"), doc = "driver.users().get(username).await;")]
72    /// ```
73    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
74    pub async fn get(&self, username: impl Into<String>) -> Result<Option<User>> {
75        let username = username.into();
76        self.server_manager
77            .execute(ServerRouting::Auto, |server_connection| {
78                let username = username.clone();
79                let server_manager = self.server_manager.clone();
80                async move {
81                    let user_info = server_connection.get_user(username).await?;
82                    Ok(user_info.map(|user_info| User::from_info(user_info, server_manager)))
83                }
84            })
85            .await
86    }
87
88    /// Returns the user of the current connection.
89    ///
90    /// # Examples
91    ///
92    /// ```rust
93    #[cfg_attr(feature = "sync", doc = "driver.users().get_current();")]
94    #[cfg_attr(not(feature = "sync"), doc = "driver.users().get_current().await;")]
95    /// ```
96    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
97    pub async fn get_current(&self) -> Result<Option<User>> {
98        self.get(self.server_manager.username()?).await
99    }
100
101    /// Retrieves all users which exist on the TypeDB server.
102    ///
103    /// # Examples
104    ///
105    /// ```rust
106    #[cfg_attr(feature = "sync", doc = "driver.users().all();")]
107    #[cfg_attr(not(feature = "sync"), doc = "driver.users().all().await;")]
108    /// ```
109    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
110    pub async fn all(&self) -> Result<Vec<User>> {
111        self.server_manager
112            .execute(ServerRouting::Auto, |server_connection| {
113                let server_manager = self.server_manager.clone();
114                async move {
115                    let user_infos = server_connection.all_users().await?;
116                    Ok(user_infos
117                        .into_iter()
118                        .map(|user_info| User::from_info(user_info, server_manager.clone()))
119                        .collect())
120                }
121            })
122            .await
123    }
124
125    /// Creates a user with the given name &amp; password.
126    ///
127    /// # Arguments
128    ///
129    /// * `username` — The name of the user to be created
130    /// * `password` — The password of the user to be created
131    ///
132    /// # Examples
133    ///
134    /// ```rust
135    #[cfg_attr(feature = "sync", doc = "driver.users().create(username, password);")]
136    #[cfg_attr(not(feature = "sync"), doc = "driver.users().create(username, password).await;")]
137    /// ```
138    #[cfg_attr(feature = "sync", maybe_async::must_be_sync)]
139    pub async fn create(&self, username: impl Into<String>, password: impl Into<String>) -> Result {
140        let username = username.into();
141        let password = password.into();
142        self.server_manager
143            .execute(ServerRouting::Auto, move |server_connection| {
144                let username = username.clone();
145                let password = password.clone();
146                async move { server_connection.create_user(username, password).await }
147            })
148            .await
149    }
150}