misuzu/public-legacy/settings/sessions.php

58 lines
1.8 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
2022-09-13 13:14:49 +00:00
if(!$msz->authInfo->isLoggedIn)
Template::throwError(401);
2022-09-13 13:14:49 +00:00
$errors = [];
$currentUser = $msz->authInfo->userInfo;
$activeSessionId = $msz->authInfo->sessionId;
2023-07-28 20:06:12 +00:00
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$sessionId = (string)filter_input(INPUT_POST, 'session');
$activeSessionKilled = false;
if($sessionId === 'all') {
$activeSessionKilled = true;
$msz->authCtx->sessions->deleteSessions(userInfos: $currentUser);
$msz->createAuditLog('PERSONAL_SESSION_DESTROY_ALL');
2023-07-28 20:06:12 +00:00
} else {
try {
$sessionInfo = $msz->authCtx->sessions->getSession(sessionId: $sessionId);
2023-07-28 20:06:12 +00:00
} catch(RuntimeException $ex) {}
if(empty($sessionInfo) || $sessionInfo->userId !== $currentUser->getId()) {
2023-07-28 20:06:12 +00:00
$errors[] = "That session doesn't exist.";
break;
}
$activeSessionKilled = $sessionInfo->id === $activeSessionId;
$msz->authCtx->sessions->deleteSessions(sessionInfos: $sessionInfo);
$msz->createAuditLog('PERSONAL_SESSION_DESTROY', [$sessionInfo->id]);
2022-09-13 13:14:49 +00:00
}
2023-07-28 20:06:12 +00:00
if($activeSessionKilled) {
Tools::redirect($msz->urls->format('index'));
2022-09-13 13:14:49 +00:00
return;
2023-07-28 20:06:12 +00:00
} else break;
2022-09-13 13:14:49 +00:00
}
$pagination = new Pagination($msz->authCtx->sessions->countSessions(userInfo: $currentUser), 10);
2023-07-28 20:06:12 +00:00
$sessionList = [];
$sessionInfos = $msz->authCtx->sessions->getSessions(userInfo: $currentUser, pagination: $pagination);
2023-07-28 20:06:12 +00:00
foreach($sessionInfos as $sessionInfo)
$sessionList[] = [
'info' => $sessionInfo,
'active' => $sessionInfo->id === $activeSessionId,
2023-07-28 20:06:12 +00:00
];
2022-09-13 13:14:49 +00:00
Template::render('settings.sessions', [
'errors' => $errors,
2023-07-28 20:06:12 +00:00
'session_list' => $sessionList,
2022-09-13 13:14:49 +00:00
'session_pagination' => $pagination,
]);