Skip to main content

bitwarden_member_administration/
organization_users_management_client.rs

1use std::sync::Arc;
2
3use bitwarden_api_api::models::OrganizationUserBulkRequestModel;
4use bitwarden_core::{
5    ApiError, Client, FromClient, MissingFieldError, OrganizationId, client::ApiConfigurations,
6    key_management::KeySlotIds,
7};
8use bitwarden_crypto::KeyStore;
9use bitwarden_error::bitwarden_error;
10use bitwarden_organizations::OrganizationUserId;
11use thiserror::Error;
12
13use crate::OrganizationUserBulkResponse;
14
15/// Errors returned from [`OrganizationUsersManagementClient`] operations.
16#[bitwarden_error(flat)]
17#[derive(Debug, Error)]
18pub enum OrganizationUsersManagementError {
19    /// The request failed as a whole.
20    #[error(transparent)]
21    Api(#[from] ApiError),
22    /// A required field was missing from the server response.
23    #[error(transparent)]
24    MissingField(#[from] MissingFieldError),
25}
26
27/// Client for administering the members of an organization.
28#[bitwarden_ffi::wasm_object]
29#[derive(FromClient)]
30pub struct OrganizationUsersManagementClient {
31    pub(crate) key_store: KeyStore<KeySlotIds>,
32    pub(crate) api_configurations: Arc<ApiConfigurations>,
33}
34
35#[bitwarden_ffi::wasm_export]
36impl OrganizationUsersManagementClient {
37    /// Sends invites to the given staged members, promoting them to invited and consuming a seat.
38    ///
39    /// Returns an `Err` if the entire request fails. Otherwise returns `Ok` containing success or
40    /// failure information for each member.
41    pub async fn send_staged_invites(
42        &self,
43        organization_id: OrganizationId,
44        organization_user_ids: Vec<OrganizationUserId>,
45    ) -> Result<Vec<OrganizationUserBulkResponse>, OrganizationUsersManagementError> {
46        let response = self
47            .api_configurations
48            .api_client
49            .organization_users_api()
50            .send_invite_to_staged_users(
51                organization_id.into(),
52                Some(bulk_request(organization_user_ids)),
53            )
54            .await?;
55
56        // A missing list is treated as empty, matching how the clients parse list responses.
57        response
58            .data
59            .unwrap_or_default()
60            .into_iter()
61            .map(|row| OrganizationUserBulkResponse::try_from(row).map_err(Into::into))
62            .collect()
63    }
64
65    /// Re-sends the invitation email to the given invited members.
66    ///
67    /// Returns an `Err` if the entire request fails. Otherwise returns `Ok` containing success or
68    /// failure information for each member.
69    pub async fn bulk_reinvite(
70        &self,
71        organization_id: OrganizationId,
72        organization_user_ids: Vec<OrganizationUserId>,
73    ) -> Result<Vec<OrganizationUserBulkResponse>, OrganizationUsersManagementError> {
74        let response = self
75            .api_configurations
76            .api_client
77            .organization_users_api()
78            .bulk_reinvite(
79                organization_id.into(),
80                Some(bulk_request(organization_user_ids)),
81            )
82            .await?;
83
84        // A missing list is treated as empty, matching how the clients parse list responses.
85        response
86            .data
87            .unwrap_or_default()
88            .into_iter()
89            .map(|row| OrganizationUserBulkResponse::try_from(row).map_err(Into::into))
90            .collect()
91    }
92
93    /// Re-sends the invitation email to a single invited member.
94    pub async fn reinvite(
95        &self,
96        organization_id: OrganizationId,
97        organization_user_id: OrganizationUserId,
98    ) -> Result<(), OrganizationUsersManagementError> {
99        self.api_configurations
100            .api_client
101            .organization_users_api()
102            .reinvite(organization_id.into(), organization_user_id.into())
103            .await?;
104
105        Ok(())
106    }
107}
108
109/// Builds the request body shared by the bulk member endpoints.
110fn bulk_request(
111    organization_user_ids: Vec<OrganizationUserId>,
112) -> OrganizationUserBulkRequestModel {
113    OrganizationUserBulkRequestModel::new(
114        organization_user_ids.into_iter().map(Into::into).collect(),
115    )
116}
117
118/// Extension trait exposing [`OrganizationUsersManagementClient`] on [`Client`].
119pub trait OrganizationUsersManagementClientExt {
120    /// Organization member administration operations.
121    fn organization_users_management(&self) -> OrganizationUsersManagementClient;
122}
123
124impl OrganizationUsersManagementClientExt for Client {
125    fn organization_users_management(&self) -> OrganizationUsersManagementClient {
126        OrganizationUsersManagementClient::from_client(self)
127    }
128}