web_writer/controllers/BaseController.php

50 lines
1.2 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// controllers/BaseController.php
class BaseController {
protected $pdo;
public function __construct() {
global $pdo;
$this->pdo = $pdo;
}
protected function render($view, $data = []) {
extract($data);
include "views/$view.php";
}
protected function redirect($url) {
header("Location: " . SITE_URL . $url);
exit;
}
protected function requireLogin() {
if (!is_logged_in()) {
$this->redirect('/login');
}
}
protected function requireAdmin() {
if (!is_logged_in()) {
$this->redirect('/login');
return;
}
global $pdo;
$userModel = new User($pdo);
$user = $userModel->findById($_SESSION['user_id']);
if (!$user || $user['id'] != 1) { // Предполагаем, что администратор имеет ID = 1
$_SESSION['error'] = "У вас нет прав администратора";
$this->redirect('/dashboard');
exit;
}
}
protected function jsonResponse($data) {
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
}
?>