ch mg
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
DROP TABLE IF EXISTS rbac_version;
|
||||
DROP FUNCTION IF EXISTS rbac_bump_version() CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS visa_master_payment_logs;
|
||||
DROP TABLE IF EXISTS visa_master_order_payments;
|
||||
DROP TABLE IF EXISTS payment_settings;
|
||||
@@ -34,13 +37,8 @@ DROP INDEX IF EXISTS idx_cards_user_id;
|
||||
DROP INDEX IF EXISTS idx_branches_province_id;
|
||||
|
||||
DROP INDEX IF EXISTS idx_branch_user_branch_id;
|
||||
DROP INDEX IF EXISTS idx_users_role_id;
|
||||
DROP INDEX IF EXISTS idx_branch_user_user_id;
|
||||
DROP INDEX IF EXISTS idx_permissions_code;
|
||||
DROP INDEX IF EXISTS idx_permissions_module;
|
||||
DROP INDEX IF EXISTS idx_permissions_guard_name;
|
||||
DROP INDEX IF EXISTS idx_roles_code;
|
||||
DROP INDEX IF EXISTS idx_permission_role_role_id;
|
||||
DROP INDEX IF EXISTS idx_role_user_user_id;
|
||||
|
||||
DROP TABLE IF EXISTS currencies;
|
||||
DROP TABLE IF EXISTS backups;
|
||||
@@ -71,13 +69,13 @@ DROP TABLE IF EXISTS branches;
|
||||
|
||||
DROP TABLE IF EXISTS provinces;
|
||||
|
||||
DROP INDEX IF EXISTS permission_role_permission_id_role_id_idx;
|
||||
DROP TABLE IF EXISTS permission_role;
|
||||
DROP TABLE IF EXISTS role_user;
|
||||
DROP TABLE IF EXISTS permissions;
|
||||
DROP TABLE IF EXISTS roles;
|
||||
DROP TABLE IF EXISTS registration_verifications;
|
||||
|
||||
DROP INDEX IF EXISTS idx_users_phone;
|
||||
DROP INDEX IF EXISTS idx_users_username;
|
||||
DROP TABLE IF EXISTS users;
|
||||
|
||||
DROP TABLE IF EXISTS role_permissions;
|
||||
DROP TABLE IF EXISTS permissions;
|
||||
DROP INDEX IF EXISTS roles_code_uq;
|
||||
DROP TABLE IF EXISTS roles;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
-- RBAC v2 → köne gurluş. Modul permission-lary "X.view" + "X.manage" jübütine,
|
||||
-- cards/loans bolsa köne "cards"/"loans" koduna gaýtarylýar.
|
||||
|
||||
BEGIN;
|
||||
|
||||
DROP TRIGGER IF EXISTS rbac_bump_branches ON branches;
|
||||
DROP TRIGGER IF EXISTS rbac_bump_branch_user ON branch_user;
|
||||
DROP TRIGGER IF EXISTS rbac_bump_users ON users;
|
||||
DROP TRIGGER IF EXISTS rbac_bump_role_permissions ON role_permissions;
|
||||
DROP TRIGGER IF EXISTS rbac_bump_roles ON roles;
|
||||
DROP FUNCTION IF EXISTS rbac_bump_version();
|
||||
DROP TABLE IF EXISTS rbac_version;
|
||||
|
||||
-- users.role + role_user
|
||||
ALTER TABLE users ADD COLUMN role character varying(250) NOT NULL DEFAULT 'user';
|
||||
UPDATE users u SET role = r.code FROM roles r WHERE r.id = u.role_id;
|
||||
|
||||
CREATE TABLE 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
|
||||
);
|
||||
INSERT INTO role_user (role_id, user_id) SELECT role_id, id FROM users;
|
||||
|
||||
DROP INDEX IF EXISTS idx_users_role_id;
|
||||
ALTER TABLE users DROP COLUMN role_id;
|
||||
|
||||
-- roles.guard_name
|
||||
ALTER TABLE roles ADD COLUMN guard_name character varying(255) NOT NULL DEFAULT 'admin';
|
||||
UPDATE roles SET guard_name = CASE scope WHEN 'own' THEN 'web' WHEN 'branch' THEN 'operator' ELSE 'admin' END;
|
||||
UPDATE roles SET guard_name = 'admin' WHERE code = 'manager';
|
||||
ALTER TABLE roles ALTER COLUMN guard_name DROP DEFAULT;
|
||||
|
||||
-- permissions
|
||||
ALTER TABLE permissions RENAME TO permissions_v2;
|
||||
|
||||
CREATE TABLE 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
|
||||
);
|
||||
|
||||
INSERT INTO permissions (code, name, guard_name, module)
|
||||
SELECT code, name, 'operator', code FROM permissions_v2 WHERE code IN ('cards', 'loans');
|
||||
|
||||
INSERT INTO permissions (code, name, guard_name, module)
|
||||
SELECT v.code || s.suffix, initcap(split_part(s.suffix, '.', 2)) || ' ' || v.name, 'admin', v.code
|
||||
FROM permissions_v2 v
|
||||
CROSS JOIN (VALUES ('.view'), ('.manage')) s(suffix)
|
||||
WHERE v.code NOT IN ('cards', 'loans', 'dashboard')
|
||||
AND NOT (v.code = 'activity-logs' AND s.suffix = '.manage');
|
||||
|
||||
CREATE TABLE 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;
|
||||
|
||||
INSERT INTO permission_role (permission_id, role_id)
|
||||
SELECT p.id, rp.role_id
|
||||
FROM role_permissions rp
|
||||
JOIN permissions p ON p.module = rp.permission_code
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
DROP TABLE role_permissions;
|
||||
DROP TABLE permissions_v2;
|
||||
|
||||
DROP INDEX IF EXISTS roles_code_uq;
|
||||
ALTER TABLE roles DROP CONSTRAINT IF EXISTS roles_scope_check;
|
||||
ALTER TABLE roles DROP COLUMN is_system;
|
||||
ALTER TABLE roles DROP COLUMN is_super;
|
||||
ALTER TABLE roles DROP COLUMN scope;
|
||||
|
||||
COMMIT;
|
||||
@@ -1,162 +0,0 @@
|
||||
-- RBAC v2: permission = modul (bir permission her modul üçin), rol = permission-lar + scope.
|
||||
-- * roles.guard_name → roles.scope (own | branch | all) + is_super + is_system
|
||||
-- * permissions → code PRIMARY KEY; "X.view"/"X.manage" → "X"
|
||||
-- * permission_role → role_permissions (soft-delete ýok)
|
||||
-- * role_user, users.role → users.role_id (bir ulanyja bir rol)
|
||||
-- * rbac_version → trigger-ler bilen artdyrylýar; app cache-i şony görüp arassalaýar
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- roles
|
||||
-- ---------------------------------------------------------------------------
|
||||
ALTER TABLE roles ADD COLUMN scope varchar(16);
|
||||
ALTER TABLE roles ADD COLUMN is_super boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE roles ADD COLUMN is_system boolean NOT NULL DEFAULT false;
|
||||
|
||||
UPDATE roles SET scope = CASE
|
||||
WHEN lower(trim(code)) IN ('superadmin', 'admin') THEN 'all'
|
||||
WHEN lower(trim(code)) IN ('manager', 'operator', 'operatot') THEN 'branch'
|
||||
WHEN lower(trim(code)) IN ('user', '') THEN 'own'
|
||||
WHEN lower(trim(guard_name)) = 'operator' THEN 'branch'
|
||||
WHEN lower(trim(guard_name)) = 'web' THEN 'own'
|
||||
ELSE 'all'
|
||||
END;
|
||||
|
||||
INSERT INTO roles (code, name, guard_name, scope)
|
||||
SELECT 'superadmin', 'Superadmin', 'admin', 'all'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'superadmin' AND deleted_at IS NULL);
|
||||
|
||||
INSERT INTO roles (code, name, guard_name, scope)
|
||||
SELECT 'user', 'User', 'web', 'own'
|
||||
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'user' AND deleted_at IS NULL);
|
||||
|
||||
UPDATE roles SET is_super = true, is_system = true WHERE code = 'superadmin';
|
||||
UPDATE roles SET is_system = true WHERE code = 'user';
|
||||
|
||||
ALTER TABLE roles ALTER COLUMN scope SET NOT NULL;
|
||||
ALTER TABLE roles ADD CONSTRAINT roles_scope_check CHECK (scope IN ('own', 'branch', 'all'));
|
||||
|
||||
-- Dublikat kod bar bolsa, bu ýerde ýykylýar: göçüşden öň el bilen arassalaň.
|
||||
CREATE UNIQUE INDEX roles_code_uq ON roles (code) WHERE deleted_at IS NULL;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- permissions → modul derejesinde
|
||||
-- ---------------------------------------------------------------------------
|
||||
ALTER TABLE permissions RENAME TO permissions_old;
|
||||
|
||||
CREATE TABLE permissions (
|
||||
code varchar(64) PRIMARY KEY,
|
||||
name varchar(255) NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
-- Katalog kodda (internal/identity/rbac/perm); app başlanda şu sanawy sinhronlaýar.
|
||||
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');
|
||||
|
||||
CREATE TABLE role_permissions (
|
||||
role_id bigint NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
|
||||
permission_code varchar(64) NOT NULL REFERENCES permissions(code) ON DELETE CASCADE,
|
||||
PRIMARY KEY (role_id, permission_code)
|
||||
);
|
||||
|
||||
-- "X.view" ýa-da "X.manage" (ýa-da köne "X") → "X"
|
||||
INSERT INTO role_permissions (role_id, permission_code)
|
||||
SELECT DISTINCT pr.role_id, split_part(p.code, '.', 1)
|
||||
FROM permission_role pr
|
||||
JOIN permissions_old p ON p.id = pr.permission_id AND p.deleted_at IS NULL
|
||||
JOIN roles r ON r.id = pr.role_id AND r.deleted_at IS NULL
|
||||
JOIN permissions np ON np.code = split_part(p.code, '.', 1)
|
||||
WHERE pr.deleted_at IS NULL
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- Öň müşderiler (web guard) permission-syz, gaty ýazylan ýol sanawy bilen
|
||||
-- öz kart/karz ýazgylaryna girýärdi; indi bu permission-lar bilen.
|
||||
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.scope = 'own' AND r.deleted_at IS NULL
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- Öň /dashboard ähli admin/operator rollaryna permission-syz açykdy.
|
||||
INSERT INTO role_permissions (role_id, permission_code)
|
||||
SELECT id, 'dashboard' FROM roles WHERE scope <> 'own' AND deleted_at IS NULL
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- users.role_id
|
||||
-- ---------------------------------------------------------------------------
|
||||
ALTER TABLE users ADD COLUMN role_id bigint REFERENCES roles(id);
|
||||
|
||||
UPDATE users u SET role_id = x.role_id
|
||||
FROM (
|
||||
SELECT DISTINCT ON (ru.user_id) ru.user_id, ru.role_id
|
||||
FROM role_user ru
|
||||
JOIN roles r ON r.id = ru.role_id AND r.deleted_at IS NULL
|
||||
WHERE ru.deleted_at IS NULL
|
||||
ORDER BY ru.user_id, ru.id
|
||||
) x
|
||||
WHERE u.id = x.user_id;
|
||||
|
||||
UPDATE users u SET role_id = r.id
|
||||
FROM roles r
|
||||
WHERE u.role_id IS NULL AND r.code = u.role AND r.deleted_at IS NULL;
|
||||
|
||||
UPDATE users SET role_id = (SELECT id FROM roles WHERE code = 'user' AND deleted_at IS NULL)
|
||||
WHERE role_id IS NULL;
|
||||
|
||||
ALTER TABLE users ALTER COLUMN role_id SET NOT NULL;
|
||||
CREATE INDEX idx_users_role_id ON users (role_id);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Köne gurluşy aýyrmak
|
||||
-- ---------------------------------------------------------------------------
|
||||
DROP TABLE permission_role;
|
||||
DROP TABLE role_user;
|
||||
DROP TABLE permissions_old;
|
||||
ALTER TABLE roles DROP COLUMN guard_name;
|
||||
ALTER TABLE users DROP COLUMN role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Cache wersiýasy
|
||||
-- ---------------------------------------------------------------------------
|
||||
CREATE TABLE 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);
|
||||
|
||||
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 TRIGGER rbac_bump_roles
|
||||
AFTER INSERT OR UPDATE OR DELETE ON roles
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE TRIGGER rbac_bump_role_permissions
|
||||
AFTER INSERT OR UPDATE OR DELETE ON role_permissions
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE TRIGGER rbac_bump_users
|
||||
AFTER UPDATE OF role_id, deleted_at OR DELETE ON users
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE TRIGGER rbac_bump_branch_user
|
||||
AFTER INSERT OR UPDATE OR DELETE ON branch_user
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
CREATE TRIGGER rbac_bump_branches
|
||||
AFTER UPDATE OF deleted_at OR DELETE ON branches
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION rbac_bump_version();
|
||||
|
||||
COMMIT;
|
||||
@@ -689,6 +689,11 @@ INSERT INTO activity_logs (user_id, method, path, module, action, status_code, r
|
||||
(2, 'PUT', '/api/admin/profile', 'auth', 'update', 200, '{"firstName":"Nurmuhammet"}'::jsonb, '10.0.0.5'),
|
||||
(5, 'GET', '/api/admin/cards', 'cards', 'list', 200, NULL, '10.0.0.30');
|
||||
|
||||
-- TRUNCATE ... CASCADE payment_settings-i hem arassalaýar (users FK); ýeke setiri gaýtadan goý.
|
||||
INSERT INTO payment_settings (id, bank_fee_tmt, gbus_fee_tmt, sber_bank_fee_tmt, tveb_fee_usd)
|
||||
VALUES (1, 100, 15, 100, 18)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
COMMIT;
|
||||
|
||||
-- Quick counts
|
||||
|
||||
Reference in New Issue
Block a user