v1.0.10
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* 删除角色接口 POST /api/system/role_delete.php
|
||||
* 入参:id(或 ids 逗号分隔批量)
|
||||
* 保护:不允许删除内置超级管理员角色(id=1)及仍被用户使用的角色
|
||||
*/
|
||||
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);
|
||||
$ids = trim($_POST['ids'] ?? '');
|
||||
$idList = [];
|
||||
if ($id > 0) $idList[] = $id;
|
||||
if ($ids !== '') {
|
||||
foreach (explode(',', $ids) as $v) {
|
||||
$v = (int)trim($v);
|
||||
if ($v > 0) $idList[] = $v;
|
||||
}
|
||||
}
|
||||
$idList = array_unique($idList);
|
||||
if (!$idList) {
|
||||
Response::error('参数错误', 400);
|
||||
}
|
||||
if (in_array(1, $idList, true)) {
|
||||
Response::error('不允许删除内置超级管理员角色', 400);
|
||||
}
|
||||
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
|
||||
// 检查角色是否被使用
|
||||
$in = implode(',', array_fill(0, count($idList), '?'));
|
||||
$stmt = $pdo->prepare("SELECT role_id, COUNT(*) AS cnt FROM system_users WHERE role_id IN ($in) GROUP BY role_id");
|
||||
$stmt->execute($idList);
|
||||
$inUse = $stmt->fetchAll();
|
||||
if ($inUse) {
|
||||
$names = [];
|
||||
foreach ($inUse as $r) {
|
||||
$names[] = "角色ID {$r['role_id']}({$r['cnt']}个用户)";
|
||||
}
|
||||
Response::error('以下角色仍被用户使用,无法删除:' . implode('、', $names), 400);
|
||||
}
|
||||
|
||||
$del = $pdo->prepare("DELETE FROM system_roles WHERE id IN ($in)");
|
||||
$del->execute($idList);
|
||||
$affected = $del->rowCount();
|
||||
|
||||
logCurrent('delete', 'system', 'system_roles', null, ['ids' => $idList]);
|
||||
Response::success(['affected' => $affected], "已删除 $affected 个角色");
|
||||
Reference in New Issue
Block a user