first commit
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Core\DB;
|
||||
use App\Core\View;
|
||||
|
||||
class AdminAdController
|
||||
{
|
||||
private function requireAdmin(): void
|
||||
{
|
||||
if (!Auth::check()) {
|
||||
header('Location: /login');
|
||||
exit;
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
if ((int)($user['is_admin'] ?? 0) !== 1) {
|
||||
http_response_code(403);
|
||||
echo 'Доступ запрещен';
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function index(): void
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$pdo = DB::connection();
|
||||
|
||||
$groupCount = (int)$pdo->query("SELECT COUNT(*) FROM ad_groups")->fetchColumn();
|
||||
$userCount = (int)$pdo->query("SELECT COUNT(*) FROM user_ad_groups")->fetchColumn();
|
||||
|
||||
View::render('admin/ad/index', [
|
||||
'groupCount' => $groupCount,
|
||||
'userGroupCount' => $userCount,
|
||||
'user' => Auth::user(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function sync(): void
|
||||
{
|
||||
$this->requireAdmin();
|
||||
|
||||
$pdo = DB::connection();
|
||||
|
||||
// MVP: берем уже залогиненных пользователей и их ad_groups из users
|
||||
$stmt = $pdo->query("
|
||||
SELECT id, ad_groups
|
||||
FROM users
|
||||
WHERE ad_groups IS NOT NULL
|
||||
AND ad_groups <> ''
|
||||
");
|
||||
$users = $stmt->fetchAll();
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
try {
|
||||
$pdo->exec("DELETE FROM user_ad_groups");
|
||||
|
||||
foreach ($users as $user) {
|
||||
$groups = json_decode((string)$user['ad_groups'], true);
|
||||
|
||||
if (!is_array($groups)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($groups as $groupName) {
|
||||
$groupName = trim((string)$groupName);
|
||||
if ($groupName === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO ad_groups (name, created_at, updated_at)
|
||||
VALUES (?, NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE
|
||||
updated_at = NOW()
|
||||
");
|
||||
$stmt->execute([$groupName]);
|
||||
|
||||
$stmt = $pdo->prepare("SELECT id FROM ad_groups WHERE name = ? LIMIT 1");
|
||||
$stmt->execute([$groupName]);
|
||||
$groupId = (int)$stmt->fetchColumn();
|
||||
|
||||
if ($groupId > 0) {
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT IGNORE INTO user_ad_groups (user_id, group_id, created_at)
|
||||
VALUES (?, ?, NOW())
|
||||
");
|
||||
$stmt->execute([
|
||||
(int)$user['id'],
|
||||
$groupId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
$_SESSION['success'] = 'Синхронизация AD-групп завершена';
|
||||
} catch (\Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
$_SESSION['error'] = 'Ошибка синхронизации AD: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
header('Location: /admin/ad');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user