feat: Add role-based access control to database schema
- Add role column to users table with admin/user constraint - Add added_by_admin_id column to ebooks table for tracking - Add constraint to ensure only admins can manage folders - Update all SQL queries to include role field - Regenerate database models with new schema
This commit is contained in:
@@ -9,6 +9,7 @@ CREATE TABLE users (
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
first_name VARCHAR(255),
|
||||
last_name VARCHAR(255),
|
||||
role VARCHAR(20) NOT NULL DEFAULT 'user' CHECK (role IN ('admin', 'user')),
|
||||
theme VARCHAR(50) DEFAULT 'tokyo-night',
|
||||
scan_frequency_minutes INTEGER DEFAULT 60,
|
||||
auto_scan_enabled BOOLEAN DEFAULT true,
|
||||
@@ -34,6 +35,7 @@ CREATE TABLE ebooks (
|
||||
date_published DATE,
|
||||
publisher VARCHAR(255),
|
||||
contributors TEXT,
|
||||
added_by_admin_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
||||
);
|
||||
@@ -66,7 +68,10 @@ CREATE TABLE user_ebook_folders (
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
folder_path VARCHAR(500) NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(user_id, folder_path)
|
||||
UNIQUE(user_id, folder_path),
|
||||
CONSTRAINT admin_only_folder CHECK (EXISTS (
|
||||
SELECT 1 FROM users WHERE id = user_id AND role = 'admin'
|
||||
))
|
||||
);
|
||||
|
||||
-- Create indexes for better query performance
|
||||
@@ -74,6 +79,7 @@ CREATE INDEX idx_users_email ON users(email);
|
||||
CREATE INDEX idx_users_username ON users(username);
|
||||
CREATE INDEX idx_ebooks_title ON ebooks(title);
|
||||
CREATE INDEX idx_ebooks_author ON ebooks(author);
|
||||
CREATE INDEX idx_ebooks_added_by_admin_id ON ebooks(added_by_admin_id);
|
||||
CREATE INDEX idx_reading_progress_ebook_id ON reading_progress(ebook_id);
|
||||
CREATE INDEX idx_reading_progress_user_id ON reading_progress(user_id);
|
||||
CREATE INDEX idx_ebook_ratings_ebook_id ON ebook_ratings(ebook_id);
|
||||
@@ -81,4 +87,11 @@ CREATE INDEX idx_ebook_ratings_user_id ON ebook_ratings(user_id);
|
||||
CREATE INDEX idx_user_ebook_folders_user_id ON user_ebook_folders(user_id);
|
||||
|
||||
-- Add comment explaining the rating system
|
||||
COMMENT ON COLUMN ebook_ratings.rating IS 'Rating scale 1-10 (odd numbers = half-stars: 1,3,5,7,9 = 0.5,1.5,2.5,3.5,4.5 stars)';
|
||||
COMMENT ON COLUMN ebook_ratings.rating IS 'Rating scale 1-10 (odd numbers = half-stars: 1,3,5,7,9 = 0.5,1.5,2.5,3.5,4.5 stars)';
|
||||
|
||||
-- Role System Notes:
|
||||
-- - All users default to 'user' role
|
||||
-- - Admin users can: add/edit/delete folders, scan ebooks, modify ebook metadata, delete ebooks
|
||||
-- - Regular users can: view all ebooks, rate ebooks, track reading progress, manage their profile
|
||||
-- - To create first admin: UPDATE users SET role = 'admin' WHERE email = 'your-admin-email';
|
||||
-- - Only admins can manage folders and ebook metadata
|
||||
Reference in New Issue
Block a user