misuzu/public-legacy/settings/sessions.php

60 lines
1.7 KiB
PHP
Raw Normal View History

2019-06-06 20:09:27 +00:00
<?php
namespace Misuzu;
use RuntimeException;
2020-05-25 19:58:06 +00:00
use Misuzu\Users\User;
use Misuzu\Users\UserSession;
2020-05-21 15:05:30 +00:00
2020-05-25 19:58:06 +00:00
if(!User::hasCurrent()) {
2019-06-06 20:09:27 +00:00
echo render_error(401);
return;
}
$errors = [];
2020-05-25 19:58:06 +00:00
$currentUser = User::getCurrent();
$currentSession = UserSession::getCurrent();
$currentUserId = $currentUser->getId();
$sessionActive = $currentSession->getId();;
2019-06-06 20:09:27 +00:00
2019-12-11 18:10:54 +00:00
if(!empty($_POST['session']) && CSRF::validateRequest()) {
2019-06-06 20:09:27 +00:00
$currentSessionKilled = false;
if(is_array($_POST['session'])) {
foreach($_POST['session'] as $sessionId) {
$sessionId = (int)$sessionId;
2019-06-06 20:09:27 +00:00
2020-05-25 19:58:06 +00:00
try {
$sessionInfo = UserSession::byId($sessionId);
} catch(RuntimeException $ex) {}
2020-05-25 19:58:06 +00:00
if(empty($sessionInfo) || $sessionInfo->getUserId() !== $currentUser->getId()) {
2019-06-06 20:09:27 +00:00
$errors[] = "Session #{$sessionId} does not exist.";
continue;
2020-05-25 19:58:06 +00:00
} elseif($sessionInfo->getId() === $sessionActive) {
2019-06-06 20:09:27 +00:00
$currentSessionKilled = true;
}
2020-05-25 19:58:06 +00:00
$sessionInfo->delete();
$msz->createAuditLog('PERSONAL_SESSION_DESTROY', [$sessionInfo->getId()]);
2019-06-06 20:09:27 +00:00
}
} elseif($_POST['session'] === 'all') {
$currentSessionKilled = true;
2020-05-25 19:58:06 +00:00
UserSession::purgeUser($currentUser);
$msz->createAuditLog('PERSONAL_SESSION_DESTROY_ALL');
2019-06-06 20:09:27 +00:00
}
if($currentSessionKilled) {
url_redirect('index');
2019-06-06 20:09:27 +00:00
return;
}
}
2020-05-25 19:58:06 +00:00
$pagination = new Pagination(UserSession::countAll($currentUser), 15);
2019-06-06 20:09:27 +00:00
Template::render('settings.sessions', [
2019-06-06 20:09:27 +00:00
'errors' => $errors,
2020-05-25 19:58:06 +00:00
'session_list' => UserSession::all($pagination, $currentUser),
'session_current' => $currentSession,
'session_pagination' => $pagination,
2019-06-06 20:09:27 +00:00
]);