first commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
use App\Core\DB;
|
||||
use App\Core\View;
|
||||
use App\Services\Auth\LdapService;
|
||||
|
||||
class AuthController
|
||||
{
|
||||
public function showLogin(): void
|
||||
{
|
||||
View::render('auth/login', [], 'guest');
|
||||
}
|
||||
|
||||
public function login(): void
|
||||
{
|
||||
$login = trim($_POST['login'] ?? '');
|
||||
$password = trim($_POST['password'] ?? '');
|
||||
|
||||
if ($login === '' || $password === '') {
|
||||
$_SESSION['error'] = 'Введите логин и пароль';
|
||||
header('Location: /login');
|
||||
exit;
|
||||
}
|
||||
|
||||
$ldapService = new LdapService();
|
||||
$ldapUser = $ldapService->authenticate($login, $password);
|
||||
|
||||
if ($ldapUser === false) {
|
||||
$_SESSION['error'] = 'Неверный логин или пароль';
|
||||
header('Location: /login');
|
||||
exit;
|
||||
}
|
||||
|
||||
$pdo = DB::connection();
|
||||
|
||||
$stmt = $pdo->prepare("SELECT * FROM users WHERE login = ? LIMIT 1");
|
||||
$stmt->execute([$ldapUser['login']]);
|
||||
$user = $stmt->fetch();
|
||||
|
||||
if (!$user) {
|
||||
$stmt = $pdo->prepare("
|
||||
INSERT INTO users (login, display_name, email, is_admin, ad_groups, last_login_at, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, NOW(), NOW(), NOW())
|
||||
");
|
||||
$stmt->execute([
|
||||
$ldapUser['login'],
|
||||
$ldapUser['display_name'],
|
||||
$ldapUser['email'],
|
||||
$ldapUser['is_admin'] ? 1 : 0,
|
||||
json_encode($ldapUser['groups'], JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
} else {
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE users
|
||||
SET display_name = ?, email = ?, is_admin = ?, ad_groups = ?, last_login_at = NOW(), updated_at = NOW()
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([
|
||||
$ldapUser['display_name'],
|
||||
$ldapUser['email'],
|
||||
$ldapUser['is_admin'] ? 1 : 0,
|
||||
json_encode($ldapUser['groups'], JSON_UNESCAPED_UNICODE),
|
||||
$user['id'],
|
||||
]);
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("SELECT * FROM users WHERE login = ? LIMIT 1");
|
||||
$stmt->execute([$ldapUser['login']]);
|
||||
$user = $stmt->fetch();
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function logout(): void
|
||||
{
|
||||
Auth::logout();
|
||||
header('Location: /login');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user