1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User helpers. |
18
|
|
|
*/ |
19
|
|
|
class UserHelper extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Return true if the logged user as unread notifications. |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function hasNotifications() |
27
|
|
|
{ |
28
|
|
|
return $this->userUnreadNotificationModel->hasNotifications($this->userSession->getId()); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get initials from a user. |
33
|
|
|
* |
34
|
|
|
* @param string $name |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getInitials($name) |
39
|
|
|
{ |
40
|
|
|
$initials = ''; |
41
|
|
|
|
42
|
|
|
foreach (explode(' ', $name, 2) as $string) { |
43
|
|
|
$initials .= mb_substr($string, 0, 1, 'UTF-8'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return mb_strtoupper($initials, 'UTF-8'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the user full name. |
51
|
|
|
* |
52
|
|
|
* @param array $user User properties |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getFullname(array $user = []) |
57
|
|
|
{ |
58
|
|
|
$user = empty($user) ? $this->userSession->getAll() : $user; |
|
|
|
|
59
|
|
|
|
60
|
|
|
return $user['name'] ?: $user['username']; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get user id. |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
|
|
public function getId() |
69
|
|
|
{ |
70
|
|
|
return $this->userSession->getId(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the given user_id is the connected user. |
75
|
|
|
* |
76
|
|
|
* @param int $user_id User id |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isCurrentUser($user_id) |
81
|
|
|
{ |
82
|
|
|
return $this->userSession->getId() == $user_id; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return if the logged user is admin. |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function isAdmin() |
91
|
|
|
{ |
92
|
|
|
return $this->userSession->isAdmin(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get role name. |
97
|
|
|
* |
98
|
|
|
* @param string $role |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getRoleName($role = '') |
103
|
|
|
{ |
104
|
|
|
return $this->role->getRoleName($role ?: $this->userSession->getRole()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check application access. |
109
|
|
|
* |
110
|
|
|
* @param string $controller |
111
|
|
|
* @param string $action |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function hasAccess($controller, $action, $plugin = '') |
116
|
|
|
{ |
117
|
|
|
if (!$this->userSession->isLogged()) { |
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$key = 'app_access:'.$controller.$action; |
122
|
|
|
$result = $this->memoryCache->get($key); |
|
|
|
|
123
|
|
|
|
124
|
|
|
if ($result === null) { |
125
|
|
|
$result = $this->applicationAuthorization->isAllowed($controller, $action, $this->userSession->getRole(), $plugin); |
|
|
|
|
126
|
|
|
$this->memoryCache->set($key, $result); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check project access. |
134
|
|
|
* |
135
|
|
|
* @param string $controller |
136
|
|
|
* @param string $action |
137
|
|
|
* @param int $project_id |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function hasProjectAccess($controller, $action, $project_id) |
142
|
|
|
{ |
143
|
|
|
$key = 'project_access:'.$controller.$action.$project_id; |
144
|
|
|
$result = $this->memoryCache->get($key); |
|
|
|
|
145
|
|
|
|
146
|
|
|
if ($result === null) { |
147
|
|
|
$result = $this->helper->projectRole->checkProjectAccess($controller, $action, $project_id); |
|
|
|
|
148
|
|
|
$this->memoryCache->set($key, $result); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $result; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Check if a user is stargazer. |
156
|
|
|
* |
157
|
|
|
* @param int $project_id |
158
|
|
|
* @param int $user_id |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isStargazer($project_id, $user_id) |
163
|
|
|
{ |
164
|
|
|
return $this->projectStarModel->isStargazer($project_id, $user_id); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.