|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Zemit Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Zemit Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Zemit\Models\Interfaces\UserInterface; |
|
15
|
|
|
|
|
16
|
|
|
trait Identity |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get the current identity service from the DI |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getIdentity(): \Zemit\Identity |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->getDI()->get('identity'); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Return true or false whether a user is logged in or not into the current session |
|
28
|
|
|
*/ |
|
29
|
|
|
public function isLoggedIn(bool $as = false): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getIdentity()->isLoggedIn($as); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Return true or false whether a user is logged in as or not into the current session |
|
36
|
|
|
*/ |
|
37
|
|
|
public function isLoggedInAs(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->isLoggedIn(true); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get the current active user using the identity service |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getCurrentUser(bool $as = false): ?UserInterface |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->getIdentity()->getUser($as); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the current active user using the identity service |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getCurrentUserAs(): ?UserInterface |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->getCurrentUser(true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the current user id |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getCurrentUserId(bool $as = false): ?int |
|
62
|
|
|
{ |
|
63
|
|
|
$user = $this->getCurrentUser($as); |
|
64
|
|
|
return $user? $user->getId() : null; |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get current user id callable (used for events behaviors) |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCurrentUserIdCallback($as = false): \Closure |
|
71
|
|
|
{ |
|
72
|
|
|
return function () use ($as) { |
|
73
|
|
|
return $this->getCurrentUserId($as); |
|
74
|
|
|
}; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|