resource_share.rs 79 lignes · 2714 octets
use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// A share entry granting access to a resource for a given principal
/// (a user OR a team) with a specific permission level.
///
/// Exactly one of `shared_with_user_id` and `shared_with_team_id` is set —
/// enforced by the `chk_resource_shares_principal` CHECK constraint at the
/// DB level.
#[derive(Clone, Debug, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "resource_shares")]
pub struct Model {
    /// Primary key.
    #[sea_orm(primary_key)]
    pub id: Uuid,
    /// FK → resources.id
    pub resource_id: Uuid,
    /// The user the resource is shared with (`Some` for user-principal shares).
    #[sea_orm(nullable)]
    pub shared_with_user_id: Option<Uuid>,
    /// The team the resource is shared with (`Some` for team-principal shares).
    #[sea_orm(nullable)]
    pub shared_with_team_id: Option<Uuid>,
    /// RFC 0008: the remote identity the resource is shared with for
    /// cross-instance shares. Mutually exclusive with `shared_with_user_id`
    /// and `shared_with_team_id` once RFC 0009 lands the extended XOR.
    #[sea_orm(nullable)]
    pub shared_with_remote_identity_id: Option<Uuid>,
    /// Permission level: "read", "write", or "admin"
    pub permission_level: String,
    /// The user who created this share
    pub shared_by_user_id: Uuid,
    /// Timestamp when this share was created.
    pub created_at: DateTime<Utc>,
}

/// SeaORM relations for the ResourceShare entity.
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    /// The resource being shared.
    #[sea_orm(
        belongs_to = "super::resource::Entity",
        from = "Column::ResourceId",
        to = "super::resource::Column::Id",
        on_delete = "Cascade"
    )]
    Resource,
    /// The user the resource is shared with (user-principal shares).
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::SharedWithUserId",
        to = "super::user::Column::Id"
    )]
    SharedWithUser,
    /// The team the resource is shared with (team-principal shares).
    #[sea_orm(
        belongs_to = "super::team::Entity",
        from = "Column::SharedWithTeamId",
        to = "super::team::Column::Id"
    )]
    SharedWithTeam,
    /// The user who created the share.
    #[sea_orm(
        belongs_to = "super::user::Entity",
        from = "Column::SharedByUserId",
        to = "super::user::Column::Id"
    )]
    SharedByUser,
}

impl Related<super::resource::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Resource.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}