1 | <?php |
||
40 | class AuthUserComponent extends Component |
||
41 | { |
||
42 | /** |
||
43 | * Component name |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | public $name = 'CurrentUser'; |
||
48 | |||
49 | /** |
||
50 | * Component's components |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | public $components = [ |
||
55 | 'Authentication.Authentication', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * Current user |
||
60 | * |
||
61 | * @var CurrentUserInterface |
||
62 | */ |
||
63 | protected $CurrentUser; |
||
64 | |||
65 | /** |
||
66 | * UsersTableInstance |
||
67 | * |
||
68 | * @var UsersTable |
||
69 | */ |
||
70 | protected $UsersTable = null; |
||
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | public function initialize(array $config) |
||
76 | { |
||
77 | Stopwatch::start('CurrentUser::initialize()'); |
||
78 | |||
79 | /** @var UsersTable */ |
||
80 | $UsersTable = TableRegistry::getTableLocator()->get('Users'); |
||
81 | $this->UsersTable = $UsersTable; |
||
82 | |||
83 | if ($this->isBot()) { |
||
84 | $CurrentUser = CurrentUserFactory::createDummy(); |
||
85 | } else { |
||
86 | $controller = $this->getController(); |
||
87 | $request = $controller->getRequest(); |
||
88 | |||
89 | $user = $this->authenticate(); |
||
90 | if (!empty($user)) { |
||
91 | $CurrentUser = CurrentUserFactory::createLoggedIn($user->toArray()); |
||
92 | $userId = (string)$CurrentUser->getId(); |
||
93 | $isLoggedIn = true; |
||
94 | } else { |
||
95 | $CurrentUser = CurrentUserFactory::createVisitor($controller); |
||
96 | $userId = $request->getSession()->id(); |
||
97 | $isLoggedIn = false; |
||
98 | } |
||
99 | |||
100 | $this->UsersTable->UserOnline->setOnline($userId, $isLoggedIn); |
||
101 | } |
||
102 | |||
103 | $this->setCurrentUser($CurrentUser); |
||
104 | |||
105 | Stopwatch::stop('CurrentUser::initialize()'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | */ |
||
111 | public function startup() |
||
112 | { |
||
113 | if (!$this->isAuthorized($this->CurrentUser)) { |
||
|
|||
114 | throw new ForbiddenException(); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Detects if the current user is a bot |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function isBot() |
||
124 | { |
||
125 | return $this->request->is('bot'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Tries to log-in a user |
||
130 | * |
||
131 | * Call this from controllers to authenticate manually (from login-form-data). |
||
132 | * |
||
133 | * @return bool Was login successfull? |
||
134 | */ |
||
135 | public function login(): bool |
||
136 | { |
||
137 | // destroy any existing session or Authentication-data |
||
138 | $this->logout(); |
||
139 | |||
140 | // non-logged in session-id is lost after Authentication |
||
141 | $originalSessionId = session_id(); |
||
142 | |||
143 | $user = $this->authenticate(); |
||
144 | |||
145 | if (!$user) { |
||
146 | // login failed |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | $this->Authentication->setIdentity($user); |
||
151 | $CurrentUser = CurrentUserFactory::createLoggedIn($user->toArray()); |
||
152 | $this->setCurrentUser($CurrentUser); |
||
153 | |||
154 | $this->UsersTable->incrementLogins($user); |
||
155 | $this->UsersTable->UserOnline->setOffline($originalSessionId); |
||
156 | |||
157 | /// password update |
||
158 | $password = (string)$this->request->getData('password'); |
||
159 | if ($password) { |
||
160 | $this->UsersTable->autoUpdatePassword($this->CurrentUser->getId(), $password); |
||
161 | } |
||
162 | |||
163 | return true; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Tries to authenticate and login the user. |
||
168 | * |
||
169 | * @return null|User User if is logged-in, null otherwise. |
||
170 | */ |
||
171 | protected function authenticate(): ?User |
||
172 | { |
||
173 | $result = $this->Authentication->getResult(); |
||
174 | |||
175 | $loginFailed = !$result->isValid(); |
||
176 | if ($loginFailed) { |
||
177 | return null; |
||
178 | } |
||
179 | |||
180 | /** @var User User is always retrieved from ORM */ |
||
181 | $user = $result->getData(); |
||
182 | |||
183 | $isUnactivated = $user['activate_code'] !== 0; |
||
184 | $isLocked = $user['user_lock'] == true; |
||
185 | |||
186 | if ($isUnactivated || $isLocked) { |
||
187 | /// User isn't allowed to be logged-in |
||
188 | // Destroy any existing (session) storage information. |
||
189 | $this->logout(); |
||
190 | |||
191 | return null; |
||
192 | } |
||
193 | |||
194 | $this->refreshAuthenticationProvider(); |
||
195 | |||
196 | return $user; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Logs-out user: clears session data and cookies. |
||
201 | * |
||
202 | * @return void |
||
203 | */ |
||
204 | public function logout(): void |
||
214 | |||
215 | /** |
||
216 | * {@inheritDoc} |
||
217 | */ |
||
218 | public function shutdown(Event $event) |
||
219 | { |
||
222 | |||
223 | /** |
||
224 | * Update persistent authentication providers for regular visitors. |
||
225 | * |
||
226 | * Users who visit somewhat regularly shall not be logged-out. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | private function refreshAuthenticationProvider() |
||
258 | |||
259 | /** |
||
260 | * Stores (or deletes) the JS-Web-Token as Cookie for access in front-end |
||
261 | * |
||
262 | * @param Controller $controller The controller |
||
263 | * @return void |
||
264 | */ |
||
265 | private function setJwtCookie(Controller $controller): void |
||
316 | |||
317 | /** |
||
318 | * Returns the current-user |
||
319 | * |
||
320 | * @return CurrentUserInterface |
||
321 | */ |
||
322 | public function getUser(): CurrentUserInterface |
||
326 | |||
327 | /** |
||
328 | * Makes the current user available throughout the application |
||
329 | * |
||
330 | * @param CurrentUserInterface $CurrentUser current-user to set |
||
331 | * @return void |
||
332 | */ |
||
333 | private function setCurrentUser(CurrentUserInterface $CurrentUser): void |
||
345 | |||
346 | /** |
||
347 | * Check if user is authorized to access the current action. |
||
348 | * |
||
349 | * @param CurrentUser $user The current user. |
||
350 | * @return bool True if authorized False otherwise. |
||
351 | */ |
||
352 | private function isAuthorized(CurrentUser $user) |
||
374 | } |
||
375 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.