-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
85 lines (69 loc) · 2.01 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
<?php
namespace Xanweb\C5\Request;
use Concrete\Core\User\User as ConcreteUser;
use Concrete\Core\User\UserInfo;
use Xanweb\Common\Traits\ApplicationTrait;
use Xanweb\Common\Traits\SingletonTrait;
use Xanweb\C5\Request\Traits\AttributesTrait;
/**
* Class User
*
* @method static bool isRegistered()
* @method static bool isSuperUser()
* @method static int getUserID()
* @method static string getUserName()
* @method static string getUserEmail()
*/
class User
{
use ApplicationTrait;
use AttributesTrait;
use SingletonTrait;
protected array $cache = [];
protected ConcreteUser $user;
private UserInfo $ui;
public function __construct()
{
$this->user = $this->app(ConcreteUser::class);
}
public static function canAccessDashboard(): bool
{
$ru = self::get();
return $ru->cache['canAccessDashboard'] ??= ($ru->user->isRegistered() && $ru->app('helper/concrete/dashboard')->canRead());
}
public static function getUserInfoObject(): ?UserInfo
{
$ru = self::get();
if (!$ru->user->isRegistered()) {
return null;
}
return $ru->ui ??= $ru->user->getUserInfoObject();
}
public static function getAttribute($ak, $mode = false)
{
$ui = self::getUserInfoObject();
if ($ui === null) {
return null;
}
return self::_getAttribute($ui, $ak, $mode);
}
public function __call($name, $arguments)
{
return $this->user->$name(...$arguments);
}
public static function __callStatic($name, $arguments)
{
$ru = self::get();
if (method_exists($ru->user, $name)) {
return $ru->user->$name(...$arguments);
}
$ui = self::getUserInfoObject();
if ($ui === null) {
return null;
}
if (method_exists($ui, $name)) {
return $ui->$name(...$arguments);
}
throw new \LogicException(t('Cannot call non existing method %s->%s.', static::class, $name));
}
}