Skip to main content

babelforce_manager_sdk/resources/
users.rs

1use std::sync::Arc;
2
3use crate::error::{map_manager_err, ManagerError};
4use crate::gen::manager::apis::configuration::Configuration;
5use crate::gen::manager::apis::manager_api;
6use crate::gen::manager::apis::user_management_api as api;
7use crate::gen::manager::models;
8use crate::retry::{with_retry, RetryPolicy};
9
10/// User management — `/api/v2/users`.
11pub struct UsersResource {
12    pub(crate) cfg: Arc<Configuration>,
13    pub(crate) retry: RetryPolicy,
14}
15
16impl UsersResource {
17    /// Get the currently authenticated user.
18    ///
19    /// The underlying generated operation is marked deprecated upstream, but it remains part of the
20    /// spec and is wrapped here for parity with the TypeScript/Go SDKs.
21    #[allow(deprecated)]
22    pub async fn me(&self) -> Result<models::GenericItemResponse, ManagerError> {
23        with_retry(&self.retry, true, || manager_api::get_me(self.cfg.as_ref()))
24            .await
25            .map_err(map_manager_err)
26    }
27
28    /// Get a user by email.
29    pub async fn get_by_email(
30        &self,
31        email: &str,
32    ) -> Result<models::ManagedUserItemResponse, ManagerError> {
33        with_retry(&self.retry, true, || {
34            manager_api::get_user_by_email(self.cfg.as_ref(), email)
35        })
36        .await
37        .map_err(map_manager_err)
38    }
39
40    /// List all users.
41    pub async fn list_all(&self) -> Result<Vec<models::ManagedUser>, ManagerError> {
42        let resp = with_retry(&self.retry, true, || {
43            api::list_users(self.cfg.as_ref(), None)
44        })
45        .await
46        .map_err(map_manager_err)?;
47        Ok(resp.items)
48    }
49
50    /// Create a user.
51    pub async fn create(
52        &self,
53        user: models::CreateManagedUserRequest,
54    ) -> Result<models::ManagedUserItemResponse, ManagerError> {
55        with_retry(&self.retry, false, || {
56            api::create_user(self.cfg.as_ref(), Some(user.clone()))
57        })
58        .await
59        .map_err(map_manager_err)
60    }
61
62    /// Enable the given users (by email).
63    pub async fn enable(&self, emails: Vec<String>) -> Result<(), ManagerError> {
64        with_retry(&self.retry, false, || {
65            api::enable_users(self.cfg.as_ref(), Some(email_list(&emails)))
66        })
67        .await
68        .map_err(map_manager_err)?;
69        Ok(())
70    }
71
72    /// Disable the given users (by email).
73    pub async fn disable(&self, emails: Vec<String>) -> Result<(), ManagerError> {
74        with_retry(&self.retry, false, || {
75            api::disable_users(self.cfg.as_ref(), Some(email_list(&emails)))
76        })
77        .await
78        .map_err(map_manager_err)?;
79        Ok(())
80    }
81
82    /// Delete the given users (by email).
83    pub async fn delete(&self, emails: Vec<String>) -> Result<(), ManagerError> {
84        with_retry(&self.retry, false, || {
85            api::delete_users(self.cfg.as_ref(), Some(email_list(&emails)))
86        })
87        .await
88        .map_err(map_manager_err)?;
89        Ok(())
90    }
91
92    /// Trigger a password-reset email for the given users (by email).
93    pub async fn reset_passwords(&self, emails: Vec<String>) -> Result<(), ManagerError> {
94        with_retry(&self.retry, false, || {
95            api::reset_passwords(self.cfg.as_ref(), Some(email_list(&emails)))
96        })
97        .await
98        .map_err(map_manager_err)?;
99        Ok(())
100    }
101
102    /// List the role names that can be assigned to users.
103    pub async fn list_roles(&self) -> Result<Vec<models::AccountRole>, ManagerError> {
104        let resp = with_retry(&self.retry, true, || {
105            api::list_available_roles(self.cfg.as_ref())
106        })
107        .await
108        .map_err(map_manager_err)?;
109        Ok(resp.items)
110    }
111
112    /// Grant the given roles to the given users (by email).
113    pub async fn add_roles(
114        &self,
115        emails: Vec<String>,
116        roles: Vec<models::AccountRole>,
117    ) -> Result<(), ManagerError> {
118        with_retry(&self.retry, false, || {
119            api::add_roles(self.cfg.as_ref(), Some(role_binding(&emails, &roles)))
120        })
121        .await
122        .map_err(map_manager_err)?;
123        Ok(())
124    }
125
126    /// Revoke the given roles from the given users (by email).
127    pub async fn remove_roles(
128        &self,
129        emails: Vec<String>,
130        roles: Vec<models::AccountRole>,
131    ) -> Result<(), ManagerError> {
132        with_retry(&self.retry, false, || {
133            api::remove_roles(self.cfg.as_ref(), Some(role_binding(&emails, &roles)))
134        })
135        .await
136        .map_err(map_manager_err)?;
137        Ok(())
138    }
139}
140
141fn email_list(emails: &[String]) -> models::EmailListRequest {
142    models::EmailListRequest {
143        emails: emails.to_vec(),
144    }
145}
146
147fn role_binding(emails: &[String], roles: &[models::AccountRole]) -> models::EmailRoleBinding {
148    models::EmailRoleBinding {
149        emails: Some(emails.to_vec()),
150        roles: Some(roles.to_vec()),
151    }
152}