1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User\Service; |
13
|
|
|
|
14
|
|
|
use Da\User\Contracts\ServiceInterface; |
15
|
|
|
use Da\User\Controller\AdminController; |
16
|
|
|
use Da\User\Event\UserEvent; |
17
|
|
|
use Da\User\Helper\SecurityHelper; |
18
|
|
|
use Da\User\Model\User; |
19
|
|
|
|
20
|
|
|
class UserBlockService implements ServiceInterface |
21
|
|
|
{ |
22
|
|
|
protected $model; |
23
|
|
|
protected $event; |
24
|
|
|
protected $controller; |
25
|
|
|
protected $securityHelper; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
User $model, |
29
|
|
|
UserEvent $event, |
30
|
|
|
AdminController $controller, |
31
|
|
|
SecurityHelper $securityHelper |
32
|
|
|
) { |
33
|
|
|
$this->model = $model; |
34
|
|
|
$this->event = $event; |
35
|
|
|
$this->controller = $controller; |
36
|
|
|
$this->securityHelper = $securityHelper; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function run() |
40
|
|
|
{ |
41
|
|
|
if ($this->model->getIsBlocked()) { |
42
|
|
|
$this->triggerEvents(UserEvent::EVENT_BEFORE_UNBLOCK); |
43
|
|
|
$result = (bool)$this->model->updateAttributes(['blocked_at' => null]); |
44
|
|
|
$this->triggerEvents(UserEvent::EVENT_AFTER_UNBLOCK); |
45
|
|
|
} else { |
46
|
|
|
$this->triggerEvents(UserEvent::EVENT_BEFORE_BLOCK); |
47
|
|
|
$result = (bool)$this->model->updateAttributes( |
48
|
|
|
['blocked_at' => time(), 'auth_key' => $this->securityHelper->generateRandomString()] |
49
|
|
|
); |
50
|
|
|
$this->triggerEvents(UserEvent::EVENT_AFTER_BLOCK); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function triggerEvents($name) |
57
|
|
|
{ |
58
|
|
|
$this->controller->trigger($name, $this->event); |
59
|
|
|
$this->model->trigger($name, $this->event); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|