changed role permission
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

This commit is contained in:
guwandyk committed 2026-09-26 16:34:28 +05:00
1 parent f0ecf23879
commit 323e131216
74 files changed
+2915 -4713

No files matched your search

@@ -0,0 +1,88 @@
-- 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;
+162
View File
@@ -0,0 +1,162 @@
-- 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;