feat: Add user role entity and integrate with user model, including migration updates and a new Makefile target.
This commit is contained in:
8
Makefile
8
Makefile
@@ -9,6 +9,7 @@ help:
|
|||||||
@echo " make dev - Start development servers"
|
@echo " make dev - Start development servers"
|
||||||
@echo " make build - Build for development"
|
@echo " make build - Build for development"
|
||||||
@echo " make release - Build optimized production bundle"
|
@echo " make release - Build optimized production bundle"
|
||||||
|
@echo " make migrate - Run database migrations"
|
||||||
@echo " make lint - Run linters (Clippy + ESLint)"
|
@echo " make lint - Run linters (Clippy + ESLint)"
|
||||||
@echo " make typecheck - Type checking (Rust + TypeScript)"
|
@echo " make typecheck - Type checking (Rust + TypeScript)"
|
||||||
@echo " make test - Run all tests"
|
@echo " make test - Run all tests"
|
||||||
@@ -16,10 +17,13 @@ help:
|
|||||||
@echo " make clean - Clean build artifacts"
|
@echo " make clean - Clean build artifacts"
|
||||||
|
|
||||||
# Backend commands
|
# Backend commands
|
||||||
.PHONY: backend-dev backend-build backend-release backend-lint backend-test
|
.PHONY: backend-dev backend-build backend-release backend-lint backend-test migrate
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
cd backend && ./target/debug/zhealth migrate
|
||||||
|
|
||||||
backend-dev:
|
backend-dev:
|
||||||
cd backend && cargo run
|
cd backend && ./target/debug/zhealth serve
|
||||||
|
|
||||||
backend-build:
|
backend-build:
|
||||||
cd backend && cargo build
|
cd backend && cargo build
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use sea_orm::sea_query::SqliteQueryBuilder;
|
|||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::models::bio::{biomarker_entry, biomarker_type};
|
use crate::models::bio::{biomarker_entry, biomarker_type};
|
||||||
use crate::models::user::{session, user};
|
use crate::models::user::{role, session, user};
|
||||||
|
|
||||||
/// Connect to the SQLite database.
|
/// Connect to the SQLite database.
|
||||||
pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> {
|
pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> {
|
||||||
@@ -24,9 +24,10 @@ pub async fn connect(config: &Config) -> Result<DatabaseConnection, DbErr> {
|
|||||||
pub async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn run_migrations(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
let schema = Schema::new(DbBackend::Sqlite);
|
let schema = Schema::new(DbBackend::Sqlite);
|
||||||
|
|
||||||
// Create table statements
|
// Create table statements (order matters for foreign keys)
|
||||||
let statements = vec![
|
let statements = vec![
|
||||||
schema.create_table_from_entity(user::Entity),
|
schema.create_table_from_entity(role::Entity), // roles first
|
||||||
|
schema.create_table_from_entity(user::Entity), // users references roles
|
||||||
schema.create_table_from_entity(session::Entity),
|
schema.create_table_from_entity(session::Entity),
|
||||||
schema.create_table_from_entity(biomarker_type::Entity),
|
schema.create_table_from_entity(biomarker_type::Entity),
|
||||||
schema.create_table_from_entity(biomarker_entry::Entity),
|
schema.create_table_from_entity(biomarker_entry::Entity),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
//! User and session entities for authentication.
|
//! User and session entities for authentication.
|
||||||
|
|
||||||
|
pub mod role;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
|
pub use role::Entity as Role;
|
||||||
pub use session::Entity as Session;
|
pub use session::Entity as Session;
|
||||||
pub use user::Entity as User;
|
pub use user::Entity as User;
|
||||||
|
|||||||
33
backend/src/models/user/role.rs
Normal file
33
backend/src/models/user/role.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
//! Role entity for RBAC.
|
||||||
|
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
|
||||||
|
#[sea_orm(table_name = "roles")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
|
||||||
|
/// Role name: admin, user, reader
|
||||||
|
#[sea_orm(unique)]
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
/// Human-readable description
|
||||||
|
#[sea_orm(column_type = "Text", nullable)]
|
||||||
|
pub description: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {
|
||||||
|
#[sea_orm(has_many = "super::user::Entity")]
|
||||||
|
Users,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Related<super::user::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Users.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
@@ -18,6 +18,9 @@ pub struct Model {
|
|||||||
#[sea_orm(unique)]
|
#[sea_orm(unique)]
|
||||||
pub email: String,
|
pub email: String,
|
||||||
|
|
||||||
|
/// Foreign key to roles table
|
||||||
|
pub role_id: i32,
|
||||||
|
|
||||||
pub created_at: DateTime,
|
pub created_at: DateTime,
|
||||||
pub updated_at: DateTime,
|
pub updated_at: DateTime,
|
||||||
}
|
}
|
||||||
@@ -26,6 +29,13 @@ pub struct Model {
|
|||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
#[sea_orm(has_many = "super::session::Entity")]
|
#[sea_orm(has_many = "super::session::Entity")]
|
||||||
Sessions,
|
Sessions,
|
||||||
|
|
||||||
|
#[sea_orm(
|
||||||
|
belongs_to = "super::role::Entity",
|
||||||
|
from = "Column::RoleId",
|
||||||
|
to = "super::role::Column::Id"
|
||||||
|
)]
|
||||||
|
Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Related<super::session::Entity> for Entity {
|
impl Related<super::session::Entity> for Entity {
|
||||||
@@ -34,4 +44,11 @@ impl Related<super::session::Entity> for Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Related<super::role::Entity> for Entity {
|
||||||
|
fn to() -> RelationDef {
|
||||||
|
Relation::Role.def()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user