Skip to main content

bitwarden_policies/policies/
organization_user_notification.rs

1use bitwarden_organizations::OrganizationUserType;
2use serde::{Deserialize, Serialize};
3
4use crate::{Policy, PolicyType, policy_type::PolicyDataType};
5
6/// Organization User Notification policy.
7pub struct OrganizationUserNotificationPolicy;
8
9impl Policy for OrganizationUserNotificationPolicy {
10    type Data = OrganizationUserNotificationPolicyData;
11
12    fn policy_type(&self) -> PolicyType {
13        PolicyType::OrganizationUserNotification
14    }
15
16    fn to_erased(&self, data: Self::Data) -> PolicyDataType {
17        PolicyDataType::OrganizationUserNotification(data)
18    }
19
20    fn exempt_roles(&self) -> &[OrganizationUserType] {
21        &[]
22    }
23}
24
25/// Configuration data for the Organization User Notification policy: an
26/// organization-configured banner shown to members in their vault.
27#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
28#[serde(rename_all = "camelCase")]
29#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
30#[cfg_attr(
31    feature = "wasm",
32    derive(tsify::Tsify),
33    tsify(into_wasm_abi, from_wasm_abi)
34)]
35pub struct OrganizationUserNotificationPolicyData {
36    /// The banner header text.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub header: Option<String>,
39
40    /// The banner description text.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub description: Option<String>,
43
44    /// The label for the banner's call-to-action button.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub button_text: Option<String>,
47
48    /// Whether the banner is shown after every login rather than once.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub show_after_every_login: Option<bool>,
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn round_trips() {
59        let data = OrganizationUserNotificationPolicyData {
60            header: Some("Heads up".to_string()),
61            description: Some("Please rotate your credentials".to_string()),
62            button_text: Some("Got it".to_string()),
63            show_after_every_login: Some(false),
64        };
65        let json = serde_json::to_string(&data).unwrap();
66        assert_eq!(
67            serde_json::from_str::<OrganizationUserNotificationPolicyData>(&json).unwrap(),
68            data
69        );
70        assert!(json.contains("showAfterEveryLogin"));
71        assert!(json.contains("buttonText"));
72    }
73
74    #[test]
75    fn empty_serializes_to_empty_object() {
76        assert_eq!(
77            serde_json::to_string(&OrganizationUserNotificationPolicyData::default()).unwrap(),
78            "{}"
79        );
80    }
81}