Files
MyAiDesk/nomifun-tauri/crates/backend/nomifun-db/src/models/user.rs
T
freedak f7a720204a Update: 将子项目从 submodule 转为完整内容
- 移除 GovAI, nomifun-tauri, 算力盒子 的 submodule 引用
- 添加所有子项目的完整源代码
- 保留原始 .git 为 .git.bak 备份
2026-07-04 19:20:46 +08:00

62 lines
1.5 KiB
Rust

use nomifun_common::TimestampMs;
use serde::{Deserialize, Serialize};
/// User role enum for RBAC
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum UserRole {
#[default]
User,
Admin,
}
impl UserRole {
pub fn as_str(&self) -> &'static str {
match self {
UserRole::User => "user",
UserRole::Admin => "admin",
}
}
pub fn from_str(s: &str) -> Self {
match s.to_lowercase().as_str() {
"admin" => UserRole::Admin,
_ => UserRole::User,
}
}
pub fn is_admin(&self) -> bool {
matches!(self, UserRole::Admin)
}
}
/// Row mapping for the `users` table.
///
/// All fields match the SQLite column names and types exactly.
/// Optional fields correspond to nullable columns.
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
pub struct User {
pub id: String,
pub username: String,
pub email: Option<String>,
pub password_hash: String,
pub role: String, // 'admin' or 'user'
pub avatar_path: Option<String>,
pub jwt_secret: Option<String>,
pub created_at: TimestampMs,
pub updated_at: TimestampMs,
pub last_login: Option<TimestampMs>,
}
impl User {
/// Get the parsed role
pub fn role(&self) -> UserRole {
UserRole::from_str(&self.role)
}
/// Check if user is admin
pub fn is_admin(&self) -> bool {
self.role().is_admin()
}
}