Bind permissions to roles and scope operator access by branch.

Replace permission_user with permission_role, enforce RBAC on admin routes, and limit operators to their assigned branches.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-24 18:24:43 +05:00
parent 427d5b4e07
commit 1fd0abbcb2
57 changed files with 1281 additions and 657 deletions

View File

@@ -16,7 +16,7 @@ DROP TABLE IF EXISTS card_types;
DROP TABLE IF EXISTS branch_user;
DROP TABLE IF EXISTS branches;
DROP TABLE IF EXISTS provinces;
DROP TABLE IF EXISTS permission_user;
DROP TABLE IF EXISTS permission_role;
DROP TABLE IF EXISTS role_user;
DROP TABLE IF EXISTS permissions;
DROP TABLE IF EXISTS roles;

View File

@@ -1,4 +1,3 @@
-- 1. Identity
CREATE TABLE IF NOT EXISTS users (
id bigserial PRIMARY KEY,
email character varying(250) NOT NULL DEFAULT '',
@@ -68,15 +67,15 @@ CREATE TABLE IF NOT EXISTS role_user (
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
);
CREATE TABLE IF NOT EXISTS permission_user (
CREATE TABLE IF NOT EXISTS permission_role (
id bigserial PRIMARY KEY,
permission_id bigint REFERENCES permissions(id),
user_id bigint REFERENCES users(id),
role_id bigint REFERENCES roles(id),
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
CONSTRAINT permission_role_permission_id_role_id_key UNIQUE (permission_id, role_id)
);
-- 2. Geography
CREATE TABLE IF NOT EXISTS provinces (
id bigserial PRIMARY KEY,
name json NOT NULL DEFAULT '{}'::json,
@@ -117,7 +116,6 @@ CREATE TABLE IF NOT EXISTS branch_user (
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
);
-- 3. Catalogs
CREATE TABLE IF NOT EXISTS card_types (
id bigserial PRIMARY KEY,
name json NOT NULL DEFAULT '{}'::json,
@@ -158,7 +156,6 @@ CREATE TABLE IF NOT EXISTS loan_order_required_docs (
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
);
-- 4. Cards
CREATE TABLE IF NOT EXISTS cards (
id bigserial PRIMARY KEY,
user_id bigint,
@@ -247,7 +244,6 @@ CREATE TABLE IF NOT EXISTS card_pin_orders (
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- 5. Loans
CREATE TABLE IF NOT EXISTS loan_orders (
id bigserial PRIMARY KEY,
unique_id character varying(255),
@@ -361,7 +357,6 @@ CREATE TABLE IF NOT EXISTS loan_paid_off_letters (
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- 6. Payments (Visa / Master student transfers)
CREATE TABLE IF NOT EXISTS visa_master_payment_orders (
id bigserial PRIMARY KEY,
unique_id character varying(255),
@@ -396,7 +391,6 @@ CREATE TABLE IF NOT EXISTS visa_master_payment_orders (
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- 7. Logs
CREATE TABLE IF NOT EXISTS activity_logs (
id bigserial PRIMARY KEY,
user_id bigint,