1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula - https://ziku.la/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Zikula\UsersModule\Twig\Runtime; |
15
|
|
|
|
16
|
|
|
use DateTime; |
17
|
|
|
use Doctrine\Common\Collections\Criteria; |
18
|
|
|
use Twig\Extension\RuntimeExtensionInterface; |
19
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface; |
20
|
|
|
use Zikula\SecurityCenterModule\Constant; |
21
|
|
|
use Zikula\UsersModule\Entity\RepositoryInterface\UserSessionRepositoryInterface; |
22
|
|
|
use Zikula\UsersModule\Entity\UserEntity; |
23
|
|
|
|
24
|
|
|
class OnlineRuntime implements RuntimeExtensionInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var UserSessionRepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $sessionRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $sessionStorageInFile; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
UserSessionRepositoryInterface $sessionRepository, |
38
|
|
|
VariableApiInterface $variableApi |
39
|
|
|
) { |
40
|
|
|
$this->sessionRepository = $sessionRepository; |
41
|
|
|
$this->sessionStorageInFile = $variableApi->getSystemVar('sessionstoretofile', 1); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return bool|void |
46
|
|
|
*/ |
47
|
|
|
public function onlineSince(UserEntity $userEntity = null, int $minutes = 10) |
48
|
|
|
{ |
49
|
|
|
if (null === $userEntity) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
if (Constant::SESSION_STORAGE_FILE === $this->sessionStorageInFile) { |
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$since = new DateTime(); |
57
|
|
|
$since->modify("-${minutes} minutes"); |
58
|
|
|
$c = Criteria::create() |
59
|
|
|
->where(Criteria::expr()->eq('uid', $userEntity->getUid())) |
60
|
|
|
->andWhere(Criteria::expr()->gt('lastused', $since)); |
61
|
|
|
$online = $this->sessionRepository->matching($c)->count(); |
62
|
|
|
|
63
|
|
|
return (bool) $online; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|