Files
online-backend/db/migrations/20260926120000_rbac_v2.up.sql
T
guwandyk 323e131216
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
changed role permission
2026-09-26 16:34:28 +05:00

163 lines
6.6 KiB
PL/PgSQL

-- 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;