Skip to content

Commit eb74354

Browse files
authored
Merge pull request #2 from mnoskov/patch-2
Fix separated mgr/web logout
2 parents acf7635 + 617871c commit eb74354

4 files changed

Lines changed: 145 additions & 343 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace EvolutionCMS\UserManager\Services\Users;
4+
5+
trait SafelyDestroyUserSessionTrait
6+
{
7+
private $userSessionFields = [
8+
'Shortname',
9+
'Fullname',
10+
'Email',
11+
'Validated',
12+
'InternalKey',
13+
'Failedlogins',
14+
'Lastlogin',
15+
'Logincount',
16+
'Role',
17+
'Permissions',
18+
'Docgroups',
19+
'Token',
20+
];
21+
22+
protected function safelyDestroyUserSession()
23+
{
24+
if (defined('NO_SESSION')) {
25+
return;
26+
}
27+
28+
foreach ($this->userSessionFields as $field) {
29+
unset($_SESSION[$this->context . $field]);
30+
}
31+
}
32+
}

src/Services/Users/UserLogin.php

Lines changed: 83 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class UserLogin implements UserServiceInterface
1212
{
13+
use SafelyDestroyUserSessionTrait;
14+
1315
/**
1416
* @var \string[][]
1517
*/
@@ -47,21 +49,22 @@ class UserLogin implements UserServiceInterface
4749
/**
4850
* @var int
4951
*/
50-
private $blockedMinutes;
52+
protected $blockedMinutes;
5153
/**
5254
* @var int
5355
*/
54-
private $failedLoginAttempts;
56+
protected $failedLoginAttempts;
5557

5658
/**
5759
* @var
5860
*/
59-
private $userSettings;
61+
protected $userSettings;
6062

6163
/**
6264
* @var string
6365
*/
64-
private $context;
66+
protected $context;
67+
6568
/**
6669
* UserRegistration constructor.
6770
* @param array $userData
@@ -85,17 +88,22 @@ public function __construct(array $userData, bool $events = true, bool $cache =
8588
*/
8689
public function getValidationRules(): array
8790
{
88-
return ['username' => ['required'],
89-
'password' => ['required']];
91+
return [
92+
'username' => ['required'],
93+
'password' => ['required'],
94+
'context' => ['nullable', 'in:web,mgr'],
95+
];
9096
}
9197

9298
/**
9399
* @return array
94100
*/
95101
public function getValidationMessages(): array
96102
{
97-
return ['username.required' => Lang::get("global.required_field", ['field' => 'username']),
98-
'password.required' => Lang::get("global.required_field", ['field' => 'password'])];
103+
return [
104+
'username.required' => Lang::get("global.required_field", ['field' => 'username']),
105+
'password.required' => Lang::get("global.required_field", ['field' => 'password']),
106+
];
99107
}
100108

101109
/**
@@ -115,6 +123,10 @@ public function process(): \Illuminate\Database\Eloquent\Model
115123
throw $exception;
116124
}
117125

126+
if (isset($this->userData['context'])) {
127+
$this->context = $this->userData['context'];
128+
}
129+
118130
if ($this->events) {
119131
// invoke OnBeforeManagerLogin event
120132
EvolutionCMS()->invokeEvent('OnBeforeManagerLogin', array(
@@ -181,64 +193,87 @@ public function validateAuth(): bool
181193
$this->user->attributes->save();
182194
}
183195

184-
// this user has been blocked by an admin, so no way he's loggin in!
185-
if ($this->user->attributes->blocked == '1') {
186-
if(!defined('NO_SESSION')) {
187-
@session_destroy();
188-
session_unset();
196+
try {
197+
// this user has been blocked by an admin, so no way he's loggin in!
198+
if ($this->user->attributes->blocked == '1') {
199+
throw new ServiceActionException(\Lang::get('global.login_processor_blocked1'));
189200
}
190-
throw new ServiceActionException(\Lang::get('global.login_processor_blocked1'));
191-
}
192201

193-
if ($this->user->attributes->verified != 1) {
194-
if(!defined('NO_SESSION')) {
195-
@session_destroy();
196-
session_unset();
202+
if ($this->user->attributes->verified != 1) {
203+
throw new ServiceActionException(\Lang::get('global.login_processor_verified'));
197204
}
198-
throw new ServiceActionException(\Lang::get('global.login_processor_verified'));
199-
}
200205

201-
// blockuntil: this user has a block until date
202-
if ($this->user->attributes->blockeduntil > time()) {
203-
if(!defined('NO_SESSION')) {
204-
@session_destroy();
205-
session_unset();
206+
// blockuntil: this user has a block until date
207+
if ($this->user->attributes->blockeduntil > time()) {
208+
throw new ServiceActionException(\Lang::get('global.login_processor_blocked2'));
206209
}
207-
throw new ServiceActionException(\Lang::get('global.login_processor_blocked2'));
208-
}
209210

210-
// blockafter: this user has a block after date
211-
if ($this->user->attributes->blockedafter > 0 && $this->user->attributes->blockedafter < time()) {
212-
if(!defined('NO_SESSION')) {
213-
@session_destroy();
214-
session_unset();
211+
// blockafter: this user has a block after date
212+
if ($this->user->attributes->blockedafter > 0 && $this->user->attributes->blockedafter < time()) {
213+
throw new ServiceActionException(\Lang::get('global.login_processor_blocked2'));
215214
}
216-
throw new ServiceActionException(\Lang::get('global.login_processor_blocked2'));
217-
}
218215

219-
// allowed ip
220-
if (isset($this->userSettings['allowed_ip'])) {
221-
if (($hostname = gethostbyaddr($_SERVER['REMOTE_ADDR'])) && ($hostname != $_SERVER['REMOTE_ADDR'])) {
222-
if (gethostbyname($hostname) != $_SERVER['REMOTE_ADDR']) {
223-
throw new ServiceActionException(\Lang::get('global.login_processor_remotehost_ip'));
224-
}
216+
if (!$this->isUserHostCorrespondsToIP()) {
217+
throw new ServiceActionException(\Lang::get('global.login_processor_remotehost_ip'));
225218
}
226-
if (!in_array($_SERVER['REMOTE_ADDR'], array_filter(array_map('trim', explode(',', $this->userSettings['allowed_ip']))))) {
219+
220+
if (!$this->isUserHasAllowedIP()) {
227221
throw new ServiceActionException(\Lang::get('global.login_processor_remote_ip'));
228222
}
229-
}
230223

231-
// allowed days
232-
if (isset($this->userSettings['allowed_days'])) {
233-
$date = getdate();
234-
$day = $date['wday'] + 1;
235-
if (!in_array($day, explode(',', $this->userSettings['allowed_days']))) {
224+
if (!$this->isUserAllowedToLogInToday()) {
236225
throw new ServiceActionException(\Lang::get('global.login_processor_date'));
237226
}
227+
} catch (ServiceActionException $e) {
228+
$this->safelyDestroyUserSession();
229+
throw $e;
238230
}
231+
239232
return true;
240233
}
241234

235+
protected function isUserHostCorrespondsToIP(): bool
236+
{
237+
if (!isset($this->userSettings['allowed_ip'])) {
238+
return true;
239+
}
240+
241+
$remoteAddress = request()->server('REMOTE_ADDR');
242+
$hostname = gethostbyaddr($remoteAddress);
243+
244+
if (!$hostname || $hostname == $remoteAddress) {
245+
return false;
246+
}
247+
248+
if (gethostbyname($hostname) == $remoteAddress) {
249+
return false;
250+
}
251+
252+
return true;
253+
}
254+
255+
protected function isUserHasAllowedIP()
256+
{
257+
if (!isset($this->userSettings['allowed_ip'])) {
258+
return true;
259+
}
260+
261+
$ips = array_filter(array_map('trim', explode(',', $this->userSettings['allowed_ip'])));
262+
263+
return in_array(request()->server('REMOTE_ADDR'), $ips);
264+
}
265+
266+
protected function isUserAllowedToLogInToday()
267+
{
268+
if (!isset($this->userSettings['allowed_days'])) {
269+
return true;
270+
}
271+
272+
$date = getdate();
273+
$day = $date['wday'] + 1;
274+
275+
return in_array($day, explode(',', $this->userSettings['allowed_days']));
276+
}
242277

243278
public function authProcess()
244279
{

0 commit comments

Comments
 (0)