add block/unblock service

This commit is contained in:
Antonio Ramirez
2016-12-04 23:20:33 +01:00
parent bda69d38af
commit be80b12a03
4 changed files with 35 additions and 4 deletions

View File

@ -255,7 +255,7 @@ class AdminController extends Controller
/** @var UserEvent $event */ /** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]); $event = $this->make(UserEvent::class, [$user]);
if ($this->make(UserBlockService::class, [$user, $event])->run()) { if ($this->make(UserBlockService::class, [$user, $event, $this])->run()) {
Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User block status has been updated.')); Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User block status has been updated.'));
} else { } else {
Yii::$app->getSession()->setFlash('danger', Yii::t('user', 'Unable to update block status.')); Yii::$app->getSession()->setFlash('danger', Yii::t('user', 'Unable to update block status.'));

View File

@ -14,6 +14,11 @@ class UserEvent extends Event
const EVENT_AFTER_PROFILE_UPDATE = 'afterProfileUpdate'; const EVENT_AFTER_PROFILE_UPDATE = 'afterProfileUpdate';
const EVENT_BEFORE_CONFIRMATION = 'beforeConfirmation'; const EVENT_BEFORE_CONFIRMATION = 'beforeConfirmation';
const EVENT_AFTER_CONFIRMATION = 'afterConfirmation'; const EVENT_AFTER_CONFIRMATION = 'afterConfirmation';
const EVENT_BEFORE_UNBLOCK = 'beforeUnblock';
const EVENT_AFTER_UNBLOCK = 'afterUnblock';
const EVENT_BEFORE_BLOCK = 'beforeBlock';
const EVENT_AFTER_BLOCK = 'afterBlock';
protected $user; protected $user;

View File

@ -29,6 +29,11 @@ class SecurityHelper
return $this->security->generatePasswordHash($password, $cost); return $this->security->generatePasswordHash($password, $cost);
} }
public function generateRandomString($length = 32)
{
return $this->security->generateRandomString($length);
}
public function validatePassword($password, $hash) public function validatePassword($password, $hash)
{ {
return $this->security->validatePassword($password, $hash); return $this->security->validatePassword($password, $hash);

View File

@ -2,22 +2,43 @@
namespace Da\User\Service; namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use Da\User\Controller\AdminController;
use Da\User\Event\UserEvent; use Da\User\Event\UserEvent;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\User; use Da\User\Model\User;
class UserBlockService implements ServiceInterface class UserBlockService implements ServiceInterface
{ {
protected $model; protected $model;
protected $event; protected $event;
protected $controller;
protected $securityHelper;
public function __construct(User $model, UserEvent $event) public function __construct(
{ User $model,
UserEvent $event,
AdminController $controller,
SecurityHelper $securityHelper
) {
$this->model = $model; $this->model = $model;
$this->event = $event; $this->event = $event;
$this->controller = $controller;
$this->securityHelper = $securityHelper;
} }
public function run() public function run()
{ {
if ($this->model->getIsBlocked()) {
$this->controller->trigger(UserEvent::EVENT_BEFORE_UNBLOCK, $this->event);
$result = (bool)$this->model->updateAttributes(['blocked_at' => null]);
$this->controller->trigger(UserEvent::EVENT_AFTER_UNBLOCK, $this->event);
} else {
$this->controller->trigger(UserEvent::EVENT_BEFORE_BLOCK, $this->event);
$result = (bool)$this->model->updateAttributes(
['blocked_at' => time(), 'auth_key' => $this->securityHelper->generateRandomString()]
);
$this->controller->trigger(UserEvent::EVENT_AFTER_BLOCK, $this->event);
}
return $result;
} }
} }