v1.0.10
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* 编辑角色接口(含权限分配) POST /api/system/role_update.php
|
||||
* 入参:id / role_name / permissions(JSON数组)/ is_active
|
||||
*/
|
||||
require_once __DIR__ . '/../common/db.php';
|
||||
require_once __DIR__ . '/../common/response.php';
|
||||
require_once __DIR__ . '/../common/auth.php';
|
||||
require_once __DIR__ . '/../common/logger.php';
|
||||
|
||||
checkAjax();
|
||||
checkPermission('system');
|
||||
|
||||
$id = (int)($_POST['id'] ?? 0);
|
||||
if ($id <= 0) {
|
||||
Response::error('参数错误', 400);
|
||||
}
|
||||
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
$check = $pdo->prepare("SELECT * FROM system_roles WHERE id = ?");
|
||||
$check->execute([$id]);
|
||||
$old = $check->fetch();
|
||||
if (!$old) {
|
||||
Response::error('角色不存在');
|
||||
}
|
||||
|
||||
// 超级管理员角色保护:不允许移除其自身
|
||||
if ($id === 1) {
|
||||
Response::error('内置超级管理员角色不允许修改', 400);
|
||||
}
|
||||
|
||||
$sets = [];
|
||||
$params = [];
|
||||
|
||||
if (isset($_POST['role_name'])) {
|
||||
$roleName = trim($_POST['role_name']);
|
||||
if ($roleName === '') {
|
||||
Response::error('角色名称不能为空', 400);
|
||||
}
|
||||
$chk = $pdo->prepare("SELECT COUNT(*) FROM system_roles WHERE role_name = ? AND id <> ?");
|
||||
$chk->execute([$roleName, $id]);
|
||||
if ((int)$chk->fetchColumn() > 0) {
|
||||
Response::error('角色名称已存在');
|
||||
}
|
||||
$sets[] = 'role_name = ?';
|
||||
$params[] = $roleName;
|
||||
}
|
||||
if (isset($_POST['permissions'])) {
|
||||
$perms = json_decode($_POST['permissions'], true);
|
||||
if (!is_array($perms)) {
|
||||
Response::error('权限格式错误', 400);
|
||||
}
|
||||
$sets[] = 'permissions = ?';
|
||||
$params[] = json_encode(array_values($perms), JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
if (isset($_POST['is_active'])) {
|
||||
$sets[] = 'is_active = ?';
|
||||
$params[] = (int)$_POST['is_active'] ? 1 : 0;
|
||||
}
|
||||
|
||||
if (!$sets) {
|
||||
Response::error('没有需要更新的字段', 400);
|
||||
}
|
||||
|
||||
$params[] = $id;
|
||||
$pdo->prepare("UPDATE system_roles SET " . implode(', ', $sets) . " WHERE id = ?")->execute($params);
|
||||
|
||||
logCurrent('update', 'system', 'system_roles', $id, ['before' => $old, 'after' => $sets]);
|
||||
Response::success(null, '更新成功');
|
||||
Reference in New Issue
Block a user