ch mg
This commit is contained in:
@@ -1,3 +1,57 @@
|
||||
-- RBAC: permission = modul (bir permission her modul üçin), rol = permission-lar + scope
|
||||
-- (own | branch | all). Her ulanyjynyň bir roly bar (users.role_id).
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id bigserial PRIMARY KEY,
|
||||
code character varying(255) NOT NULL DEFAULT '',
|
||||
name character varying(255) NOT NULL,
|
||||
scope character varying(16) NOT NULL,
|
||||
is_super boolean NOT NULL DEFAULT false,
|
||||
is_system boolean NOT NULL DEFAULT false,
|
||||
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
deleted_at TIMESTAMPTZ,
|
||||
CONSTRAINT roles_scope_check CHECK (scope IN ('own', 'branch', 'all'))
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS roles_code_uq ON roles (code) WHERE deleted_at IS NULL;
|
||||
|
||||
-- Katalog kodda (internal/identity/rbac/perm); app başlanda şu sanawy sinhronlaýar.
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
code character varying(64) PRIMARY KEY,
|
||||
name character varying(255) NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
INSERT INTO permissions (code, name) VALUES
|
||||
('dashboard', 'Dashboard'),
|
||||
('cards', 'Cards'),
|
||||
('loans', 'Loans'),
|
||||
('users', 'Users'),
|
||||
('roles', 'Roles'),
|
||||
('branches', 'Branches'),
|
||||
('provinces', 'Provinces'),
|
||||
('currencies', 'Currencies'),
|
||||
('payment-settings', 'Payment settings'),
|
||||
('backups', 'Backups'),
|
||||
('activity-logs', 'Activity logs')
|
||||
ON CONFLICT (code) DO NOTHING;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS role_permissions (
|
||||
role_id bigint NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
|
||||
permission_code character varying(64) NOT NULL REFERENCES permissions(code) ON DELETE CASCADE,
|
||||
PRIMARY KEY (role_id, permission_code)
|
||||
);
|
||||
|
||||
INSERT INTO roles (code, name, scope, is_super, is_system) VALUES
|
||||
('superadmin', 'Superadmin', 'all', true, true),
|
||||
('user', 'User', 'own', false, true)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO role_permissions (role_id, permission_code)
|
||||
SELECT r.id, x.code
|
||||
FROM roles r CROSS JOIN (VALUES ('cards'), ('loans')) x(code)
|
||||
WHERE r.code = 'user'
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id bigserial PRIMARY KEY,
|
||||
email character varying(250) NOT NULL DEFAULT '',
|
||||
@@ -10,7 +64,7 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
avatar character varying(250) NOT NULL DEFAULT '',
|
||||
social_media json NOT NULL DEFAULT '{}'::json,
|
||||
light_mode integer NOT NULL DEFAULT 1,
|
||||
role character varying(250) NOT NULL DEFAULT 'user',
|
||||
role_id bigint NOT NULL REFERENCES roles(id),
|
||||
active boolean NOT NULL DEFAULT true,
|
||||
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
@@ -38,53 +92,6 @@ CREATE TABLE IF NOT EXISTS registration_verifications (
|
||||
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id bigserial PRIMARY KEY,
|
||||
code character varying(255) NOT NULL DEFAULT '',
|
||||
name character varying(255) NOT NULL,
|
||||
guard_name character varying(255) NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
INSERT INTO roles (code, name, guard_name)
|
||||
SELECT 'user', 'User', 'web'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'user');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
id bigserial PRIMARY KEY,
|
||||
code character varying(255) NOT NULL DEFAULT '',
|
||||
name character varying(255) NOT NULL,
|
||||
guard_name character varying(255) NOT NULL,
|
||||
module character varying(255) NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
updated_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') NOT NULL,
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS role_user (
|
||||
id bigserial PRIMARY KEY,
|
||||
role_id bigint REFERENCES roles(id),
|
||||
user_id bigint REFERENCES users(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,
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS permission_role (
|
||||
id bigserial PRIMARY KEY,
|
||||
permission_id bigint REFERENCES permissions(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,
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS permission_role_permission_id_role_id_idx
|
||||
ON permission_role (permission_id, role_id)
|
||||
WHERE deleted_at IS NULL;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS provinces (
|
||||
id bigserial PRIMARY KEY,
|
||||
name json NOT NULL DEFAULT '{}'::json,
|
||||
@@ -630,18 +637,12 @@ CREATE TABLE IF NOT EXISTS visa_master_payment_logs (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS visa_master_payment_logs_order_id_idx ON visa_master_payment_logs (order_id);
|
||||
|
||||
-- Indexes for hot filter/join columns: role_user, permission_role and
|
||||
-- branch_user are queried on every authenticated request (RBAC role and
|
||||
-- branch resolution), and every order table below is list-filtered by
|
||||
-- user_id/branch_id. Postgres does not index foreign key columns
|
||||
-- automatically, so without these every one of those lookups was a full
|
||||
-- table scan.
|
||||
CREATE INDEX IF NOT EXISTS idx_role_user_user_id ON role_user(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_permission_role_role_id ON permission_role(role_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_code ON roles(code);
|
||||
CREATE INDEX IF NOT EXISTS idx_permissions_code ON permissions(code);
|
||||
CREATE INDEX IF NOT EXISTS idx_permissions_module ON permissions(module);
|
||||
CREATE INDEX IF NOT EXISTS idx_permissions_guard_name ON permissions(guard_name);
|
||||
-- Indexes for hot filter/join columns: users.role_id and branch_user are
|
||||
-- queried on every authenticated request (RBAC role and branch resolution),
|
||||
-- and every order table below is list-filtered by user_id/branch_id.
|
||||
-- Postgres does not index foreign key columns automatically, so without
|
||||
-- these every one of those lookups was a full table scan.
|
||||
CREATE INDEX IF NOT EXISTS idx_users_role_id ON users(role_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_branch_user_user_id ON branch_user(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_branch_user_branch_id ON branch_user(branch_id);
|
||||
|
||||
@@ -676,3 +677,35 @@ CREATE INDEX IF NOT EXISTS idx_currencies_from ON currencies(currency_from);
|
||||
CREATE INDEX IF NOT EXISTS idx_currencies_to ON currencies(currency_to);
|
||||
CREATE INDEX IF NOT EXISTS idx_currencies_created_by ON currencies(created_by);
|
||||
CREATE INDEX IF NOT EXISTS idx_currencies_updated_by ON currencies(updated_by);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- RBAC cache wersiýasy: trigger-ler bilen artdyrylýar; app cache-i şony görüp arassalaýar
|
||||
-- ---------------------------------------------------------------------------
|
||||
CREATE TABLE IF NOT EXISTS rbac_version (
|
||||
id int PRIMARY KEY DEFAULT 1 CHECK (id = 1),
|
||||
version bigint NOT NULL DEFAULT 0
|
||||
);
|
||||
INSERT INTO rbac_version (id, version) VALUES (1, 0) ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
CREATE OR REPLACE FUNCTION rbac_bump_version() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
UPDATE rbac_version SET version = version + 1 WHERE id = 1;
|
||||
RETURN NULL;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE OR REPLACE TRIGGER rbac_bump_roles
|
||||
AFTER INSERT OR UPDATE OR DELETE ON roles
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE OR REPLACE TRIGGER rbac_bump_role_permissions
|
||||
AFTER INSERT OR UPDATE OR DELETE ON role_permissions
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE OR REPLACE TRIGGER rbac_bump_users
|
||||
AFTER UPDATE OF role_id, deleted_at OR DELETE ON users
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE OR REPLACE TRIGGER rbac_bump_branch_user
|
||||
AFTER INSERT OR UPDATE OR DELETE ON branch_user
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE OR REPLACE TRIGGER rbac_bump_branches
|
||||
AFTER UPDATE OF deleted_at OR DELETE ON branches
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
|
||||
Reference in New Issue
Block a user