add admin controller and many updates
This commit is contained in:
73
lib/User/Service/UserRegisterService.php
Normal file
73
lib/User/Service/UserRegisterService.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user