add admin controller and many updates

This commit is contained in:
Antonio Ramirez
2016-12-04 23:10:06 +01:00
parent abe959d8cc
commit bda69d38af
20 changed files with 601 additions and 117 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Event\UserEvent;
use Da\User\Model\User;
class UserBlockService implements ServiceInterface
{
protected $model;
protected $event;
public function __construct(User $model, UserEvent $event)
{
$this->model = $model;
$this->event = $event;
}
public function run()
{
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Event\UserEvent;
use Da\User\Model\User;
class UserConfirmationService implements ServiceInterface
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function run()
{
$this->model->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION);
$result = (bool) $this->model->updateAttributes(['confirmed_at' => time()]);
$this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION);
return $result;
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Event\UserEvent;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\Token;
use Da\User\Model\User;
use yii\base\InvalidCallException;
use yii\log\Logger;
use Exception;
class UserRegisterService implements ServiceInterface
{
protected $model;
protected $securityHelper;
protected $mailService;
protected $logger;
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper, Logger $logger)
{
$this->model = $model;
$this->mailService = $mailService;
$this->securityHelper = $securityHelper;
$this->logger = $logger;
}
public function run()
{
$model = $this->model;
if ($model->getIsNewRecord() === false) {
throw new InvalidCallException('Cannot register user from an existing one.');
}
$transaction = $model->getDb()->beginTransaction();
try {
$model->confirmed_at = $this->model->module->enableEmailConfirmation ? null : time();
$model->password = $this->model->module->generatePasswords
? $this->securityHelper->generatePassword(8)
: $model->password;
$model->trigger(UserEvent::EVENT_BEFORE_REGISTER);
if(!$model->save()) {
$transaction->rollBack();
return false;
}
if($model->module->enableEmailConfirmation) {
$token = $model->make(Token::class, ['type' => Token::TYPE_CONFIRMATION]);
$token->link('user', $model);
}
$this->mailService->run();
$model->trigger(UserEvent::EVENT_AFTER_REGISTER);
$transaction->commit();
return true;
} catch(Exception $e) {
$transaction->rollBack();
$this->logger->log($e->getMessage(), Logger::LEVEL_WARNING);
return false;
}
}
}