diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index bdb69864..b5946f2b 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -7,6 +7,7 @@ use chrono::{NaiveDateTime, Utc}; use derive_more::{AsRef, Deref, Display, From}; use diesel::prelude::*; use num_traits::FromPrimitive; +use serde::{Deserialize, Serialize}; use serde_json::Value; use crate::{ @@ -57,6 +58,43 @@ pub struct Membership { pub atype: i32, pub reset_password_key: Option, pub external_id: Option, + pub permissions: Option, +} + +#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[serde(default, rename_all = "camelCase")] +pub struct OrganizationUserPermissions { + pub access_event_logs: bool, + pub access_import_export: bool, + pub access_reports: bool, + pub create_new_collections: bool, + pub edit_any_collection: bool, + pub delete_any_collection: bool, + pub manage_groups: bool, + pub manage_policies: bool, + pub manage_sso: bool, + pub manage_users: bool, + pub manage_reset_password: bool, + pub manage_scim: bool, +} + +impl OrganizationUserPermissions { + pub fn from_payload_map(payload: HashMap) -> serde_json::Result { + let value = Value::Object(payload.into_iter().collect()); + serde_json::from_value(value) + } + + pub fn from_db_json(raw: Option<&str>) -> serde_json::Result> { + raw.map(serde_json::from_str).transpose() + } + + pub fn to_db_json(&self) -> serde_json::Result { + serde_json::to_string(self) + } + + pub fn has_manage_all_collections(&self) -> bool { + self.edit_any_collection && self.delete_any_collection && self.create_new_collections + } } #[derive(Identifiable, Queryable, Insertable, AsChangeset)] @@ -274,6 +312,7 @@ impl Membership { atype: MembershipType::User as i32, reset_password_key: None, external_id: None, + permissions: None, } } diff --git a/src/db/schema.rs b/src/db/schema.rs index af342186..dc407f62 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -242,6 +242,7 @@ table! { atype -> Integer, reset_password_key -> Nullable, external_id -> Nullable, + permissions -> Nullable, } }