From 76f4d0106aaa3bbe51caa7b9dce706d39831a804 Mon Sep 17 00:00:00 2001 From: Alberto-Audrix Date: Wed, 26 Aug 2026 14:04:38 +0700 Subject: [PATCH] update user_access table to match django --- backend/database/erd_schema.sql | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/backend/database/erd_schema.sql b/backend/database/erd_schema.sql index 82a3e6e..25d9b9f 100644 --- a/backend/database/erd_schema.sql +++ b/backend/database/erd_schema.sql @@ -3,7 +3,10 @@ -- Source: ERD_dashboard.png (updated 2026-08-20) -- -- user_access.status: active | inactive | superadmin --- (Django syncs is_active/is_staff/is_superuser from status for auth internals) +-- (Django syncs is_active/is_staff/is_superuser from status on save: +-- inactive => all false; superadmin => is_active/is_superuser true, +-- is_staff false; active => is_active true, is_superuser false, is_staff +-- left untouched so operators can be marked staff) -- user_access → sites → kandang → cycles -- cycles → flock (flock.cycle_id, required; cycles has no flock_id) -- flock → iot_panel @@ -14,18 +17,39 @@ -- ----------------------------------------------------------------------------- -- 1. User Access +-- Django AbstractBaseUser + PermissionsMixin. `status` is the app's role +-- (active | inactive | superadmin); Django auth flags are synced from it on +-- save (is_staff stays untouched for active users, superadmin => is_staff +-- false, is_superuser true). -- ----------------------------------------------------------------------------- CREATE TABLE IF NOT EXISTS user_access ( user_id SERIAL PRIMARY KEY, user_name VARCHAR(30) NOT NULL UNIQUE, - password_hash VARCHAR(255) NOT NULL, - status VARCHAR(30) NOT NULL, + password VARCHAR(128) NOT NULL, + last_login TIMESTAMP WITH TIME ZONE, + is_superuser BOOLEAN NOT NULL DEFAULT FALSE, + is_staff BOOLEAN NOT NULL DEFAULT FALSE, + is_active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + status VARCHAR(30) NOT NULL ); CREATE INDEX IF NOT EXISTS idx_user_access_status ON user_access (status); +-- Django PermissionsMixin many-to-many tables. +CREATE TABLE IF NOT EXISTS user_access_groups ( + id SERIAL PRIMARY KEY, + user_id INTEGER NOT NULL REFERENCES user_access (user_id) ON DELETE CASCADE, + group_id INTEGER NOT NULL REFERENCES auth_group (id) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS user_access_user_permissions ( + id SERIAL PRIMARY KEY, + user_id INTEGER NOT NULL REFERENCES user_access (user_id) ON DELETE CASCADE, + permission_id INTEGER NOT NULL REFERENCES auth_permission (id) ON DELETE CASCADE +); + -- ----------------------------------------------------------------------------- -- 2. Sites -- -----------------------------------------------------------------------------