a06673b
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[sea_orm(rs_type = "String", db_type = "String(StringLen::N(8))")]
pub enum AccessKeyScope {
#[sea_orm(string_value = "read")]
Read,
#[sea_orm(string_value = "write")]
Write,
#[sea_orm(string_value = "repo")]
Repo,
#[sea_orm(string_value = "admin")]
Admin,
#[sea_orm(string_value = "all")]
All,
}
impl AccessKeyScope {
pub const fn allows(self, want_write: bool) -> bool {
matches!(
(self, want_write),
(Self::Admin | Self::All | Self::Repo | Self::Write, _) | (Self::Read, false)
)
}
}
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "user_access_key")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(unique_key = "user_name")]
pub user_id: Uuid,
#[sea_orm(belongs_to, from = "user_id", to = "uid")]
pub user: BelongsTo<crate::user::users::Entity>,
#[sea_orm(unique_key = "user_name")]
pub name: String,
#[sea_orm(unique)]
pub token_hash: String,
pub token_salt: String,
pub token_last_eight: String,
pub scope: AccessKeyScope,
pub version: i32,
pub created_at: OffsetDateTime,
pub updated_at: OffsetDateTime,
}
impl ActiveModelBehavior for ActiveModel {}