v1.0.10
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* 新增角色接口(含权限分配) POST /api/system/role_add.php
|
||||
* 入参: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');
|
||||
|
||||
$roleName = trim($_POST['role_name'] ?? '');
|
||||
$perms = json_decode($_POST['permissions'] ?? '[]', true);
|
||||
$isActive = isset($_POST['is_active']) ? ((int)$_POST['is_active'] ? 1 : 0) : 1;
|
||||
|
||||
if ($roleName === '') {
|
||||
Response::error('角色名称不能为空', 400);
|
||||
}
|
||||
if (!is_array($perms)) {
|
||||
Response::error('权限格式错误', 400);
|
||||
}
|
||||
|
||||
$pdo = DB::getInstance()->getPdo();
|
||||
$chk = $pdo->prepare("SELECT COUNT(*) FROM system_roles WHERE role_name = ?");
|
||||
$chk->execute([$roleName]);
|
||||
if ((int)$chk->fetchColumn() > 0) {
|
||||
Response::error('角色名称已存在');
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("INSERT INTO system_roles (role_name, permissions, is_active) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$roleName, json_encode(array_values($perms), JSON_UNESCAPED_UNICODE), $isActive]);
|
||||
$newId = (int)$pdo->lastInsertId();
|
||||
|
||||
logCurrent('add', 'system', 'system_roles', $newId, ['role_name' => $roleName, 'permissions' => $perms]);
|
||||
Response::success(['id' => $newId], '新增成功');
|
||||
Reference in New Issue
Block a user