added console commands
This commit is contained in:
41
lib/User/Command/ConfirmController.php
Normal file
41
lib/User/Command/ConfirmController.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Da\User\Command;
|
||||
|
||||
use Da\User\Query\UserQuery;
|
||||
use Da\User\Service\UserConfirmationService;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use yii\base\Module;
|
||||
use yii\console\Controller;
|
||||
use Yii;
|
||||
use yii\helpers\Console;
|
||||
|
||||
class ConfirmController extends Controller
|
||||
{
|
||||
use ContainerTrait;
|
||||
|
||||
protected $userQuery;
|
||||
|
||||
public function __construct($id, Module $module, UserQuery $userQuery, array $config)
|
||||
{
|
||||
$this->userQuery = $userQuery;
|
||||
|
||||
parent::__construct($id, $module, $config);
|
||||
}
|
||||
|
||||
public function actionIndex($usernameOrEmail)
|
||||
{
|
||||
$user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one();
|
||||
if ($user === null) {
|
||||
$this->stdout(Yii::t('user', 'User is not found') . "\n", Console::FG_RED);
|
||||
} else {
|
||||
|
||||
if ($this->make(UserConfirmationService::class, [$user])->run()) {
|
||||
$this->stdout(Yii::t('user', 'User has been confirmed') . "\n", Console::FG_GREEN);
|
||||
} else {
|
||||
$this->stdout(Yii::t('user', 'Error occurred while confirming user') . "\n", Console::FG_RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
36
lib/User/Command/CreateController.php
Normal file
36
lib/User/Command/CreateController.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace Da\User\Command;
|
||||
|
||||
use Da\User\Factory\MailFactory;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Service\UserCreateService;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use yii\console\Controller;
|
||||
use Yii;
|
||||
use yii\helpers\Console;
|
||||
|
||||
class CreateController extends Controller
|
||||
{
|
||||
use ContainerTrait;
|
||||
|
||||
public function actionIndex($email, $username, $password = null)
|
||||
{
|
||||
$user = $this->make(
|
||||
User::class,
|
||||
['scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]
|
||||
);
|
||||
$mailService = MailFactory::makeWelcomeMailerService($user);
|
||||
|
||||
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
|
||||
$this->stdout(Yii::t('user', 'User has been created') . "!\n", Console::FG_GREEN);
|
||||
} else {
|
||||
$this->stdout(Yii::t('user', 'Please fix following errors:') . "\n", Console::FG_RED);
|
||||
foreach ($user->errors as $errors) {
|
||||
foreach ($errors as $error) {
|
||||
$this->stdout(' - ' . $error . "\n", Console::FG_RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
35
lib/User/Command/DeleteController.php
Normal file
35
lib/User/Command/DeleteController.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Da\User\Command;
|
||||
|
||||
use Da\User\Query\UserQuery;
|
||||
use Yii;
|
||||
use yii\base\Module;
|
||||
use yii\console\Controller;
|
||||
use yii\helpers\Console;
|
||||
|
||||
class DeleteController extends Controller
|
||||
{
|
||||
protected $userQuery;
|
||||
|
||||
public function __construct($id, Module $module, UserQuery $userQuery, array $config)
|
||||
{
|
||||
$this->userQuery = $userQuery;
|
||||
parent::__construct($id, $module, $config);
|
||||
}
|
||||
|
||||
public function actionIndex($usernameOrEmail)
|
||||
{
|
||||
if ($this->confirm(Yii::t('user', 'Are you sure? Deleted user can not be restored'))) {
|
||||
$user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one();
|
||||
if ($user === null) {
|
||||
$this->stdout(Yii::t('user', 'User is not found') . "\n", Console::FG_RED);
|
||||
} else {
|
||||
if ($user->delete()) {
|
||||
$this->stdout(Yii::t('user', 'User has been deleted') . "\n", Console::FG_GREEN);
|
||||
} else {
|
||||
$this->stdout(Yii::t('user', 'Error occurred while deleting user') . "\n", Console::FG_RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
lib/User/Command/PasswordController.php
Normal file
40
lib/User/Command/PasswordController.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Da\User\Command;
|
||||
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Da\User\Service\ResetPasswordService;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use Yii;
|
||||
use yii\base\Module;
|
||||
use yii\console\Controller;
|
||||
use yii\helpers\Console;
|
||||
|
||||
class PasswordController extends Controller
|
||||
{
|
||||
use ContainerTrait;
|
||||
|
||||
protected $userQuery;
|
||||
|
||||
public function __construct($id, Module $module, UserQuery $userQuery, array $config)
|
||||
{
|
||||
$this->userQuery = $userQuery;
|
||||
parent::__construct($id, $module, $config);
|
||||
}
|
||||
|
||||
public function actionIndex($usernameOrEmail, $password)
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one();
|
||||
|
||||
if ($user === null) {
|
||||
$this->stdout(Yii::t('user', 'User is not found') . "\n", Console::FG_RED);
|
||||
} else {
|
||||
if ($this->make(ResetPasswordService::class, [$password, $user])->run()) {
|
||||
$this->stdout(Yii::t('user', 'Password has been changed') . "\n", Console::FG_GREEN);
|
||||
} else {
|
||||
$this->stdout(Yii::t('user', 'Error occurred while changing password') . "\n", Console::FG_RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
use yii\base\InvalidCallException;
|
||||
@ -43,7 +44,7 @@ class UserCreateService implements ServiceInterface
|
||||
? $model->password
|
||||
: $this->securityHelper->generatePassword(8);
|
||||
|
||||
$model->trigger(ActiveRecord::EVENT_BEFORE_INSERT);
|
||||
$model->trigger(UserEvent::EVENT_BEFORE_CREATE);
|
||||
|
||||
if (!$model->save()) {
|
||||
$transaction->rollBack();
|
||||
@ -51,7 +52,7 @@ class UserCreateService implements ServiceInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$model->trigger(ActiveRecord::EVENT_AFTER_INSERT);
|
||||
$model->trigger(UserEvent::EVENT_AFTER_CREATE);
|
||||
|
||||
$this->mailService->run();
|
||||
$transaction->commit();
|
||||
|
||||
Reference in New Issue
Block a user