-
Notifications
You must be signed in to change notification settings - Fork 611
/
User.php
98 lines (86 loc) · 2.4 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\console;
use craft\elements\User as UserElement;
use yii\base\Component;
/**
* The User component provides APIs for managing the user authentication status.
* An instance of the User component is globally accessible in Craft via [[\craft\console\Application::getUser()|`Craft::$app->user`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class User extends Component
{
/**
* @var UserElement|null
* @see getIdentity()
* @see setIdentity()
*/
private ?UserElement $_identity = null;
/**
* Returns whether the current user is an admin.
*
* @return bool Whether the current user is an admin.
*/
public function getIsAdmin(): bool
{
$user = $this->getIdentity();
return ($user && $user->admin);
}
/**
* Returns whether the current user has a given permission.
*
* @param string $permissionName The name of the permission.
* @return bool Whether the current user has the permission.
*/
public function checkPermission(string $permissionName): bool
{
$user = $this->getIdentity();
return ($user && $user->can($permissionName));
}
/**
* Returns the current identity object.
*
* @param bool $autoRenew
* @return UserElement|null
*/
public function getIdentity(bool $autoRenew = true): UserElement|null
{
return $this->_identity;
}
/**
* Sets the user identity object.
*
* @param UserElement|null $identity The identity object. If null, it
* means the current user will be a guest without any associated identity.
*/
public function setIdentity(?UserElement $identity = null): void
{
$this->_identity = $identity;
}
/**
* Returns whether the current user is a guest (not authenticated).
*
* @return bool Whether the current user is a guest.
*/
public function getIsGuest(): bool
{
return $this->getIdentity() === null;
}
/**
* Returns the current user’s ID, if they are logged in.
*
* @return int|null
* @see getIdentity()
*/
public function getId(): ?int
{
$identity = $this->getIdentity();
return $identity?->getId();
}
}