feat(M02-C): 建立RBAC与门店数据范围

This commit is contained in:
Codex
2026-06-18 10:50:43 +08:00
parent a8c2a3b3e1
commit caacc78545
16 changed files with 334 additions and 16 deletions
@@ -0,0 +1,6 @@
DELETE FROM qipai_schema_migrations WHERE version = '2026061805';
DROP TABLE IF EXISTS qipai_user_store_scopes;
DROP TABLE IF EXISTS qipai_user_roles;
DROP TABLE IF EXISTS qipai_role_permissions;
DROP TABLE IF EXISTS qipai_roles;
DROP TABLE IF EXISTS qipai_permissions;
@@ -0,0 +1,69 @@
CREATE TABLE IF NOT EXISTS qipai_permissions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(128) NOT NULL,
name VARCHAR(128) NOT NULL,
category VARCHAR(64) NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
UNIQUE KEY uq_qipai_permissions_code (code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS qipai_roles (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
tenant_id BIGINT UNSIGNED NOT NULL,
code VARCHAR(32) NOT NULL,
name VARCHAR(64) NOT NULL,
role_version INT UNSIGNED NOT NULL DEFAULT 1,
status VARCHAR(32) NOT NULL DEFAULT 'ACTIVE',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
deleted_at DATETIME(3) NULL,
CONSTRAINT fk_qipai_roles_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
UNIQUE KEY uq_qipai_roles_tenant_code (tenant_id, code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS qipai_role_permissions (
tenant_id BIGINT UNSIGNED NOT NULL,
role_id BIGINT UNSIGNED NOT NULL,
permission_id BIGINT UNSIGNED NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
PRIMARY KEY (tenant_id, role_id, permission_id),
CONSTRAINT fk_qipai_role_permissions_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
CONSTRAINT fk_qipai_role_permissions_role FOREIGN KEY (role_id) REFERENCES qipai_roles(id),
CONSTRAINT fk_qipai_role_permissions_permission FOREIGN KEY (permission_id) REFERENCES qipai_permissions(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS qipai_user_roles (
tenant_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
role_id BIGINT UNSIGNED NOT NULL,
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
PRIMARY KEY (tenant_id, user_id, role_id),
CONSTRAINT fk_qipai_user_roles_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
CONSTRAINT fk_qipai_user_roles_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
CONSTRAINT fk_qipai_user_roles_role FOREIGN KEY (role_id) REFERENCES qipai_roles(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS qipai_user_store_scopes (
tenant_id BIGINT UNSIGNED NOT NULL,
user_id BIGINT UNSIGNED NOT NULL,
store_id BIGINT UNSIGNED NOT NULL,
scope_type VARCHAR(32) NOT NULL DEFAULT 'STAFF',
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
PRIMARY KEY (tenant_id, user_id, store_id, scope_type),
CONSTRAINT fk_qipai_user_store_scopes_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
CONSTRAINT fk_qipai_user_store_scopes_user FOREIGN KEY (user_id) REFERENCES qipai_users(id),
CONSTRAINT fk_qipai_user_store_scopes_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
KEY idx_qipai_user_store_scopes_store (tenant_id, store_id, scope_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT IGNORE INTO qipai_permissions (code, name, category) VALUES
('profile.read', '查看个人资料', 'account'),
('order.self.read', '查看本人订单', 'order'),
('cleaning.task.read', '查看保洁任务', 'cleaning'),
('store.operation.read', '查看门店运营', 'store'),
('store.operation.write', '管理门店运营', 'store'),
('tenant.manage', '管理租户', 'tenant'),
('platform.manage', '管理平台', 'platform');
INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026061805', 'm02c_rbac');
@@ -0,0 +1,19 @@
SELECT table_name FROM information_schema.tables
WHERE table_schema = DATABASE()
AND table_name IN ('qipai_permissions', 'qipai_roles', 'qipai_role_permissions',
'qipai_user_roles', 'qipai_user_store_scopes')
ORDER BY table_name;
SELECT table_name, index_name FROM information_schema.statistics
WHERE table_schema = DATABASE()
AND ((table_name = 'qipai_permissions' AND index_name = 'uq_qipai_permissions_code')
OR (table_name = 'qipai_roles' AND index_name = 'uq_qipai_roles_tenant_code')
OR (table_name = 'qipai_user_store_scopes' AND index_name = 'idx_qipai_user_store_scopes_store'))
GROUP BY table_name, index_name ORDER BY table_name, index_name;
SELECT code FROM qipai_permissions
WHERE code IN ('profile.read', 'order.self.read', 'cleaning.task.read',
'store.operation.read', 'store.operation.write', 'tenant.manage', 'platform.manage')
ORDER BY code;
SELECT version, name FROM qipai_schema_migrations WHERE version = '2026061805';