89 lines
3.4 KiB
PL/PgSQL
89 lines
3.4 KiB
PL/PgSQL
-- 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;
|