Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

8.4 KB
Raw Blame
//! SQL query functions for entity access checks.
//!
//! Each module contains a single query function for checking access to a specific entity type.

#[cfg(not(test))]
use cached::proc_macro::cached;

use anyhow::Context;
use bot_id::BotIdStr;
use macro_user_id::{
    cowlike::CowLike,
    lowercased::Lowercase,
    user_id::{MacroUserId, MacroUserIdStr},
};
use model_entity::EntityType;
use sqlx::{Pool, Postgres};

#[cfg(feature = "explain_binary")]
use crate::domain::models::{AccessGrant, AccessLevel};
#[cfg(feature = "explain_binary")]
use models_entity_access_management::EntityAccessSourceType;

pub mod agent_session_access;
pub mod call_access;
pub mod call_channel;
pub mod channel_membership;
pub mod channel_role;
pub mod channel_users;
pub mod chat_access;
pub mod crm_company_access;
pub mod crm_contact_access;
pub mod crm_entity_users;
pub mod database_access;
pub mod database_row_access;
pub mod document_access;
pub mod foreign_entity_access;
pub mod form_access;
pub mod initiative_access;
pub mod pipeline_access;
pub mod project_access;
pub mod scheduled_action_access;
pub mod team_access;
pub mod thread_access;

#[cfg(test)]
mod test;
#[cfg(test)]
mod typed_owner_test;

/// Type safety for source ids for entity_access table
#[derive(Debug, Clone)]
pub struct SourceIds(pub Vec<String>);

/// Grabs the users source ids for the entity access table
/// NOTE: This could return an empty list in the event the user is not logged in and attempting to review a resource
#[tracing::instrument(skip(pool), err)]
#[cfg_attr(
    not(test),
    cached(
        time = 10,
        result = true,
        key = "String",
        convert = r#"{format!("{}", user_id.map(AsRef::as_ref).unwrap_or(""))}"#,
    )
)]
pub async fn get_user_source_ids(
    pool: &Pool<Postgres>,
    user_id: Option<&MacroUserId<Lowercase<'_>>>,
) -> anyhow::Result<SourceIds> {
    if let Some(user_id) = user_id {
        // Fetch source IDs first
        let source_ids = sqlx::query_scalar!(
            r#"
            SELECT cp.channel_id::text FROM comms_channel_participants cp
                WHERE cp.user_id = $1 AND cp.left_at IS NULL
            UNION ALL
            SELECT t.team_id::text FROM team_user t
                WHERE t.user_id = $1
            UNION ALL
            SELECT $1
            "#,
            user_id.as_ref()
        )
        .fetch_all(pool)
        .await?;

        let source_ids: Vec<String> = source_ids.into_iter().flatten().collect();

        Ok(SourceIds(source_ids))
    } else {
        Ok(SourceIds(vec![]))
    }
}

/// Grabs the source ids available to a bot operating in its owning team's scope.
///
/// A direct agent conversation is not among them, although the persona takes
/// part in it: what is shared there belongs to the person it is with.
#[tracing::instrument(skip(pool), err)]
#[cfg_attr(
    not(test),
    cached(
        time = 10,
        result = true,
        key = "String",
        convert = r#"{format!("{}:{}", bot_id, team_id)}"#,
    )
)]
pub async fn get_team_scope_source_ids(
    pool: &Pool<Postgres>,
    bot_id: &BotIdStr<'_>,
    team_id: &uuid::Uuid,
) -> anyhow::Result<SourceIds> {
    let source_ids = sqlx::query_scalar!(
        r#"
        WITH active_bot AS (
            SELECT id
            FROM bots
            WHERE id = $2
              AND team_id = $3
              AND deleted_at IS NULL
        )
        SELECT $1::text AS "source_id!"
        FROM active_bot
        UNION
        SELECT $3::text
        FROM active_bot
        UNION
        SELECT c.id::text
        FROM active_bot
        JOIN comms_channels c
          ON c.channel_type = 'team'
         AND c.team_id = $3
        UNION
        SELECT cp.channel_id::text
        FROM active_bot
        JOIN comms_channel_participants cp
          ON cp.user_id = $1
         AND cp.left_at IS NULL
        WHERE NOT EXISTS (
            SELECT 1
            FROM comms_channel_agents ca
            WHERE ca.channel_id = cp.channel_id
              AND ca.kind = 'direct'
        )
        "#,
        bot_id.as_ref(),
        bot_id.as_uuid(),
        team_id,
    )
    .fetch_all(pool)
    .await?;

    Ok(SourceIds(source_ids))
}

/// Grabs all user IDs with access to an entity via the entity_access table.
#[tracing::instrument(skip(pool), err)]
#[cfg_attr(
    not(test),
    cached(
        time = 10,
        result = true,
        key = "String",
        convert = r#"{format!("{}:{}", entity_type.as_ref(), entity_id)}"#
    )
)]
pub(in crate::outbound::pg_access_repo) async fn get_entity_users(
    pool: &Pool<Postgres>,
    entity_id: &uuid::Uuid,
    entity_type: EntityType,
) -> anyhow::Result<Vec<MacroUserIdStr<'static>>> {
    if entity_type == EntityType::ForeignEntity {
        return foreign_entity_access::get_foreign_entity_users(pool, entity_id).await;
    }
    // because we don't store entity_access per email we need to also grab the owner
    // of the email to append to the list, plus any primary that delegates the inbox
    // via macro_user_links (shared inbox)
    let mut email_owner: Vec<MacroUserIdStr> = if let EntityType::EmailThread = entity_type {
        let macro_ids = sqlx::query_scalar!(
            r#"
        SELECT l.macro_id AS "macro_id!"
        FROM email_threads et
        JOIN email_links l ON et.link_id = l.id
        WHERE et.id = $1
        UNION
        SELECT mul.primary_macro_id
        FROM email_threads et
        JOIN email_links l ON et.link_id = l.id
        JOIN macro_user_links mul ON mul.link_id = l.id
        WHERE et.id = $1
        "#,
            entity_id
        )
        .fetch_all(pool)
        .await?;

        macro_ids
            .into_iter()
            .map(|macro_id| {
                MacroUserIdStr::parse_from_str(macro_id.as_str())
                    .map(|u| u.into_owned())
                    .context("macro user id should be valid")
            })
            .collect::<anyhow::Result<Vec<_>>>()?
    } else {
        vec![]
    };

    let mut users: Vec<MacroUserIdStr<'static>> = sqlx::query_scalar!(
        r#"
    SELECT user_id FROM (
        -- Direct user grants
        SELECT source_id as user_id FROM entity_access
        WHERE entity_id = $1 AND entity_type = $2 AND source_type = 'user'

        UNION ALL

        -- Channel Members
        SELECT cp.user_id FROM comms_channel_participants cp
        WHERE cp.left_at IS NULL AND cp.channel_id IN (
            SELECT source_id::uuid FROM entity_access
            WHERE entity_id = $1 AND entity_type = $2 AND source_type = 'channel'
        )

        UNION ALL

        -- Team Members
        SELECT tu.user_id FROM team_user tu
        WHERE tu.team_id IN (
            SELECT source_id::uuid FROM entity_access
            WHERE entity_id = $1 AND entity_type = $2 AND source_type = 'team'
        )
    ) AS combined_users

    "#,
        entity_id,
        entity_type.as_ref(),
    )
    .fetch_all(pool)
    .await?
    .into_iter()
    .filter_map(|u| {
        u.and_then(|u| {
            MacroUserIdStr::parse_from_str(u.as_str())
                .ok()
                .map(|u| u.into_owned())
        })
    })
    .collect::<Vec<MacroUserIdStr<'static>>>();

    // add in email owner (if applicable)
    users.append(&mut email_owner);

    Ok(users
        .into_iter()
        .collect::<std::collections::HashSet<_>>()
        .into_iter()
        .collect())
}

#[cfg(feature = "explain_binary")]
#[tracing::instrument(skip(pool, source_ids), err)]
pub async fn list_entity_access_grants(
    pool: &Pool<Postgres>,
    entity_id: &uuid::Uuid,
    entity_type: EntityType,
    source_ids: &SourceIds,
) -> Result<Vec<AccessGrant>, sqlx::Error> {
    if source_ids.0.is_empty() {
        return Ok(vec![]);
    }

    let rows = sqlx::query!(
        r#"
        SELECT
            source_type AS "source_type!: EntityAccessSourceType",
            source_id,
            access_level AS "access_level!: AccessLevel",
            granted_from_project_id
        FROM entity_access
        WHERE entity_id = $1
          AND entity_type = $2
          AND source_id = ANY($3)
        "#,
        entity_id,
        entity_type.as_ref(),
        &source_ids.0,
    )
    .fetch_all(pool)
    .await?;

    Ok(rows
        .into_iter()
        .map(|row| AccessGrant::EntityAccess {
            source_type: row.source_type,
            source_id: row.source_id,
            access_level: row.access_level,
            granted_from_project_id: row.granted_from_project_id,
        })
        .collect())
}

pub mod external_chat_access;