1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\User; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use Yiisoft\Access\AccessCheckerInterface; |
9
|
|
|
use Yiisoft\Auth\IdentityInterface; |
10
|
|
|
use Yiisoft\User\CurrentIdentity\CurrentIdentityInterface; |
11
|
|
|
|
12
|
|
|
final class CurrentUser |
13
|
|
|
{ |
14
|
|
|
private ?AccessCheckerInterface $accessChecker = null; |
15
|
|
|
|
16
|
|
|
private CurrentIdentityInterface $currentIdentity; |
17
|
|
|
|
18
|
|
|
public function __construct(CurrentIdentityInterface $currentIdentity) |
19
|
|
|
{ |
20
|
|
|
$this->currentIdentity = $currentIdentity; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setAccessChecker(AccessCheckerInterface $accessChecker): void |
24
|
|
|
{ |
25
|
|
|
$this->accessChecker = $accessChecker; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the identity object associated with the currently logged-in user. |
30
|
|
|
*/ |
31
|
|
|
public function getIdentity(): IdentityInterface |
32
|
|
|
{ |
33
|
|
|
return $this->currentIdentity->get(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns a value that uniquely represents the user. |
38
|
|
|
* |
39
|
|
|
* @throws Throwable |
40
|
|
|
* |
41
|
|
|
* @return string the unique identifier for the user. If `null`, it means the user is a guest. |
42
|
|
|
* |
43
|
|
|
* @see getIdentity() |
44
|
|
|
*/ |
45
|
|
|
public function getId(): ?string |
46
|
|
|
{ |
47
|
|
|
return $this->currentIdentity->get()->getId(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns a value indicating whether the user is a guest (not authenticated). |
52
|
|
|
* |
53
|
|
|
* @return bool whether the current user is a guest. |
54
|
|
|
*/ |
55
|
|
|
public function isGuest(): bool |
56
|
|
|
{ |
57
|
|
|
return $this->currentIdentity->get() instanceof GuestIdentity; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks if the user can perform the operation as specified by the given permission. |
62
|
|
|
* |
63
|
|
|
* Note that you must provide access checker via {{@see CurrentUser::setAccessChecker()}} in order |
64
|
|
|
* to use this method. Otherwise it will always return false. |
65
|
|
|
* |
66
|
|
|
* @param string $permissionName the name of the permission (e.g. "edit post") that needs access check. |
67
|
|
|
* @param array $params name-value pairs that would be passed to the rules associated |
68
|
|
|
* with the roles and permissions assigned to the user. |
69
|
|
|
* |
70
|
|
|
* @throws Throwable |
71
|
|
|
* |
72
|
|
|
* @return bool whether the user can perform the operation as specified by the given permission. |
73
|
|
|
*/ |
74
|
|
|
public function can(string $permissionName, array $params = []): bool |
75
|
|
|
{ |
76
|
|
|
if ($this->accessChecker === null) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->accessChecker->userHasPermission($this->getId(), $permissionName, $params); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|