Upload files to "config"
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
if (session_status() === PHP_SESSION_NONE) {
|
||||
session_start();
|
||||
}
|
||||
|
||||
function is_logged_in(): bool
|
||||
{
|
||||
return isset($_SESSION['user']);
|
||||
}
|
||||
|
||||
function current_user(): ?array
|
||||
{
|
||||
return $_SESSION['user'] ?? null;
|
||||
}
|
||||
|
||||
function current_role(): string
|
||||
{
|
||||
return $_SESSION['user']['role'] ?? 'guest';
|
||||
}
|
||||
|
||||
function require_login(): void
|
||||
{
|
||||
if (!is_logged_in()) {
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function api_require_login(): void
|
||||
{
|
||||
if (!is_logged_in()) {
|
||||
http_response_code(401);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Sesi login tidak ditemukan. Silakan login ulang.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function role_in(array $roles): bool
|
||||
{
|
||||
return in_array(current_role(), $roles, true);
|
||||
}
|
||||
|
||||
function api_require_role(array $roles): void
|
||||
{
|
||||
if (!role_in($roles)) {
|
||||
http_response_code(403);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Akses ditolak untuk peran pengguna ini.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user