Skip to main content

bitwarden_policies/policies/
organization_user_notification.rs

1use bitwarden_organizations::OrganizationUserType;
2use serde::{Deserialize, Serialize};
3
4use crate::{PolicyDefinition, PolicyType, policy_type::PolicyDataType};
5
6/// Organization User Notification policy.
7pub struct OrganizationUserNotificationPolicy;
8
9impl PolicyDefinition 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#[bitwarden_ffi::wasm_record]
31pub struct OrganizationUserNotificationPolicyData {
32    /// The banner header text.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub header: Option<String>,
35
36    /// The banner description text.
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub description: Option<String>,
39
40    /// The label for the banner's call-to-action button.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub button_text: Option<String>,
43
44    /// Whether the banner is shown after every login rather than once.
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub show_after_every_login: Option<bool>,
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn round_trips() {
55        let data = OrganizationUserNotificationPolicyData {
56            header: Some("Heads up".to_string()),
57            description: Some("Please rotate your credentials".to_string()),
58            button_text: Some("Got it".to_string()),
59            show_after_every_login: Some(false),
60        };
61        let json = serde_json::to_string(&data).unwrap();
62        assert_eq!(
63            serde_json::from_str::<OrganizationUserNotificationPolicyData>(&json).unwrap(),
64            data
65        );
66        assert!(json.contains("showAfterEveryLogin"));
67        assert!(json.contains("buttonText"));
68    }
69
70    #[test]
71    fn empty_serializes_to_empty_object() {
72        assert_eq!(
73            serde_json::to_string(&OrganizationUserNotificationPolicyData::default()).unwrap(),
74            "{}"
75        );
76    }
77}