From 76e49ca1a75f055b768209d3621d1ab728f80c4e Mon Sep 17 00:00:00 2001 From: Antonio Ramirez Date: Sun, 11 Dec 2016 01:20:10 +0100 Subject: [PATCH] added console commands --- lib/User/Command/ConfirmController.php | 41 +++++++++++++++++++++++++ lib/User/Command/CreateController.php | 36 ++++++++++++++++++++++ lib/User/Command/DeleteController.php | 35 +++++++++++++++++++++ lib/User/Command/PasswordController.php | 40 ++++++++++++++++++++++++ lib/User/Service/UserCreateService.php | 5 +-- 5 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 lib/User/Command/ConfirmController.php create mode 100644 lib/User/Command/CreateController.php create mode 100644 lib/User/Command/DeleteController.php create mode 100644 lib/User/Command/PasswordController.php diff --git a/lib/User/Command/ConfirmController.php b/lib/User/Command/ConfirmController.php new file mode 100644 index 0000000..b21122e --- /dev/null +++ b/lib/User/Command/ConfirmController.php @@ -0,0 +1,41 @@ +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); + } + } + } +} + + diff --git a/lib/User/Command/CreateController.php b/lib/User/Command/CreateController.php new file mode 100644 index 0000000..905c8d0 --- /dev/null +++ b/lib/User/Command/CreateController.php @@ -0,0 +1,36 @@ +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); + } + } + } + + } +} diff --git a/lib/User/Command/DeleteController.php b/lib/User/Command/DeleteController.php new file mode 100644 index 0000000..73791db --- /dev/null +++ b/lib/User/Command/DeleteController.php @@ -0,0 +1,35 @@ +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); + } + } + } + } +} diff --git a/lib/User/Command/PasswordController.php b/lib/User/Command/PasswordController.php new file mode 100644 index 0000000..65f1a9e --- /dev/null +++ b/lib/User/Command/PasswordController.php @@ -0,0 +1,40 @@ +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); + } + } + } +} diff --git a/lib/User/Service/UserCreateService.php b/lib/User/Service/UserCreateService.php index 5d42a0c..e6c95d7 100644 --- a/lib/User/Service/UserCreateService.php +++ b/lib/User/Service/UserCreateService.php @@ -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();