Close #269 added help for console commands + php-cs-fixer

This commit is contained in:
tonydspaniard
2019-06-29 08:32:35 +02:00
parent d4695a60d2
commit 8c551d9030
27 changed files with 123 additions and 85 deletions

View File

@ -6,6 +6,7 @@
- Enh #325: Added support for sqlite3 (santilin) - Enh #325: Added support for sqlite3 (santilin)
- Fix #326: Fix rule for the user auth_tf_enabled field (santilin) - Fix #326: Fix rule for the user auth_tf_enabled field (santilin)
- Fix #290: Fix wrong email message for resending confirmation (tonydspaniard) - Fix #290: Fix wrong email message for resending confirmation (tonydspaniard)
- Enh #269: Added help documentation to console commands (tonydspaniard)
## 1.5.0 April 19, 2019 ## 1.5.0 April 19, 2019
- Fix: Fix condition in EmailChangeService (it was always false) (borisaeric) - Fix: Fix condition in EmailChangeService (it was always false) (borisaeric)

View File

@ -32,17 +32,22 @@ class ConfirmController extends Controller
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);
} }
/**
* Confirms a a user by setting its field `confirmed_at` to current time.
*
* @param string $usernameOrEmail Username or email of the user
*
* @throws \yii\base\InvalidConfigException
*/
public function actionIndex($usernameOrEmail) public function actionIndex($usernameOrEmail)
{ {
$user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one(); $user = $this->userQuery->whereUsernameOrEmail($usernameOrEmail)->one();
if ($user === null) { if ($user === null) {
$this->stdout(Yii::t('usuario', 'User is not found') . "\n", Console::FG_RED); $this->stdout(Yii::t('usuario', 'User is not found') . "\n", Console::FG_RED);
} elseif ($this->make(UserConfirmationService::class, [$user])->run()) {
$this->stdout(Yii::t('usuario', 'User has been confirmed') . "\n", Console::FG_GREEN);
} else { } else {
if ($this->make(UserConfirmationService::class, [$user])->run()) { $this->stdout(Yii::t('usuario', 'Error occurred while confirming user') . "\n", Console::FG_RED);
$this->stdout(Yii::t('usuario', 'User has been confirmed') . "\n", Console::FG_GREEN);
} else {
$this->stdout(Yii::t('usuario', 'Error occurred while confirming user') . "\n", Console::FG_RED);
}
} }
} }
} }

View File

@ -23,6 +23,18 @@ class CreateController extends Controller
{ {
use ContainerAwareTrait; use ContainerAwareTrait;
/**
* This command creates a new user account. If no password is not set, an 8-char password will be generated. After
* saving user to database, this command uses mailer component to send credentials (username and password) to user
* via email. A role can be also assigned but it must exists previously on the database.
*
* @param string $email Email
* @param string $username Username
* @param string|null $password The password. If null it will be generated automatically
* @param string|null $role Role to assign (must already exist)
*
* @throws \yii\base\InvalidConfigException
*/
public function actionIndex($email, $username, $password = null, $role = null) public function actionIndex($email, $username, $password = null, $role = null)
{ {
/** @var User $user */ /** @var User $user */

View File

@ -12,9 +12,11 @@
namespace Da\User\Command; namespace Da\User\Command;
use Da\User\Query\UserQuery; use Da\User\Query\UserQuery;
use Throwable;
use Yii; use Yii;
use yii\base\Module; use yii\base\Module;
use yii\console\Controller; use yii\console\Controller;
use yii\db\StaleObjectException;
use yii\helpers\Console; use yii\helpers\Console;
class DeleteController extends Controller class DeleteController extends Controller
@ -27,6 +29,15 @@ class DeleteController extends Controller
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);
} }
/**
* This command deletes a user.
*
* @param string $usernameOrEmail Email or username of the user to delete
*
*
* @throws Throwable
* @throws StaleObjectException
*/
public function actionIndex($usernameOrEmail) public function actionIndex($usernameOrEmail)
{ {
if ($this->confirm(Yii::t('usuario', 'Are you sure? Deleted user can not be restored'))) { if ($this->confirm(Yii::t('usuario', 'Are you sure? Deleted user can not be restored'))) {

View File

@ -16,6 +16,7 @@ use Da\User\Query\UserQuery;
use Da\User\Service\ResetPasswordService; use Da\User\Service\ResetPasswordService;
use Da\User\Traits\ContainerAwareTrait; use Da\User\Traits\ContainerAwareTrait;
use Yii; use Yii;
use yii\base\InvalidConfigException;
use yii\base\Module; use yii\base\Module;
use yii\console\Controller; use yii\console\Controller;
use yii\helpers\Console; use yii\helpers\Console;
@ -32,6 +33,14 @@ class PasswordController extends Controller
parent::__construct($id, $module, $config); parent::__construct($id, $module, $config);
} }
/**
* This command updates the user's password.
*
* @param string $usernameOrEmail Username or email of the user who's password needs to be updated
* @param string $password The new password
*
* @throws InvalidConfigException
*/
public function actionIndex($usernameOrEmail, $password) public function actionIndex($usernameOrEmail, $password)
{ {
/** @var User $user */ /** @var User $user */

View File

@ -39,7 +39,6 @@ class RecoveryController extends Controller
protected $userQuery; protected $userQuery;
protected $tokenQuery; protected $tokenQuery;
/** /**
* RecoveryController constructor. * RecoveryController constructor.
* *

View File

@ -43,7 +43,6 @@ class RegistrationController extends Controller
protected $userQuery; protected $userQuery;
protected $socialNetworkAccountQuery; protected $socialNetworkAccountQuery;
/** /**
* RegistrationController constructor. * RegistrationController constructor.
* *

View File

@ -11,18 +11,18 @@
namespace Da\User\Controller; namespace Da\User\Controller;
use Da\User\Filter\AccessRuleFilter;
use Da\User\Model\Rule; use Da\User\Model\Rule;
use Da\User\Search\RuleSearch; use Da\User\Search\RuleSearch;
use Da\User\Service\AuthRuleEditionService; use Da\User\Service\AuthRuleEditionService;
use Da\User\Traits\AuthManagerAwareTrait; use Da\User\Traits\AuthManagerAwareTrait;
use Da\User\Traits\ContainerAwareTrait; use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator; use Da\User\Validator\AjaxRequestModelValidator;
use Da\User\Filter\AccessRuleFilter;
use Yii; use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
class RuleController extends Controller class RuleController extends Controller
{ {

View File

@ -38,7 +38,6 @@ class SecurityController extends Controller
protected $socialNetworkAccountQuery; protected $socialNetworkAccountQuery;
/** /**
* SecurityController constructor. * SecurityController constructor.
* *

View File

@ -11,7 +11,6 @@
namespace Da\User\Controller; namespace Da\User\Controller;
use Da\User\Contracts\MailChangeStrategyInterface; use Da\User\Contracts\MailChangeStrategyInterface;
use Da\User\Event\GdprEvent; use Da\User\Event\GdprEvent;
use Da\User\Event\ProfileEvent; use Da\User\Event\ProfileEvent;
@ -56,16 +55,15 @@ class SettingsController extends Controller
protected $userQuery; protected $userQuery;
protected $socialNetworkAccountQuery; protected $socialNetworkAccountQuery;
/** /**
* SettingsController constructor. * SettingsController constructor.
* *
* @param string $id * @param string $id
* @param Module $module * @param Module $module
* @param ProfileQuery $profileQuery * @param ProfileQuery $profileQuery
* @param UserQuery $userQuery * @param UserQuery $userQuery
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery * @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config * @param array $config
*/ */
public function __construct( public function __construct(
$id, $id,
@ -74,8 +72,7 @@ class SettingsController extends Controller
UserQuery $userQuery, UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery, SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config = [] array $config = []
) ) {
{
$this->profileQuery = $profileQuery; $this->profileQuery = $profileQuery;
$this->userQuery = $userQuery; $this->userQuery = $userQuery;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery; $this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
@ -159,9 +156,9 @@ class SettingsController extends Controller
public function actionPrivacy() public function actionPrivacy()
{ {
if (!$this->module->enableGdprCompliance) if (!$this->module->enableGdprCompliance) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
}
return $this->render('privacy', [ return $this->render('privacy', [
'module' => $this->module 'module' => $this->module
]); ]);
@ -169,9 +166,9 @@ class SettingsController extends Controller
public function actionGdprDelete() public function actionGdprDelete()
{ {
if (!$this->module->enableGdprCompliance) if (!$this->module->enableGdprCompliance) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
}
/** @var GdprDeleteForm $form */ /** @var GdprDeleteForm $form */
$form = $this->make(GdprDeleteForm::class); $form = $this->make(GdprDeleteForm::class);
@ -209,15 +206,12 @@ class SettingsController extends Controller
'website' => $anonymReplacement . ".tld", 'website' => $anonymReplacement . ".tld",
'bio' => Yii::t('usuario', 'Deleted by GDPR request') 'bio' => Yii::t('usuario', 'Deleted by GDPR request')
]); ]);
} }
$this->trigger(GdprEvent::EVENT_AFTER_DELETE, $event); $this->trigger(GdprEvent::EVENT_AFTER_DELETE, $event);
Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your personal information has been removed')); Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your personal information has been removed'));
return $this->goHome(); return $this->goHome();
} }
return $this->render('gdpr-delete', [ return $this->render('gdpr-delete', [
@ -225,32 +219,6 @@ class SettingsController extends Controller
]); ]);
} }
/**
* @param $id
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
* @throws \Exception
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*/
protected function disconnectSocialNetwork($id)
{
/** @var SocialNetworkAccount $account */
$account = $this->socialNetworkAccountQuery->whereId($id)->one();
if ($account === null) {
throw new NotFoundHttpException();
}
if ($account->user_id !== Yii::$app->user->id) {
throw new ForbiddenHttpException();
}
$event = $this->make(SocialNetworkConnectEvent::class, [Yii::$app->user->identity, $account]);
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_DISCONNECT, $event);
$account->delete();
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_DISCONNECT, $event);
}
/** /**
* Exports the data from the current user in a mechanical readable format (csv). Properties exported can be defined * Exports the data from the current user in a mechanical readable format (csv). Properties exported can be defined
* in the module configuration. * in the module configuration.
@ -260,9 +228,9 @@ class SettingsController extends Controller
*/ */
public function actionExport() public function actionExport()
{ {
if (!$this->module->enableGdprCompliance) if (!$this->module->enableGdprCompliance) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
}
try { try {
$properties = $this->module->gdprExportProperties; $properties = $this->module->gdprExportProperties;
$user = Yii::$app->user->identity; $user = Yii::$app->user->identity;
@ -294,7 +262,6 @@ class SettingsController extends Controller
} catch (\Throwable $e) { } catch (\Throwable $e) {
throw $e; throw $e;
} }
} }
public function actionAccount() public function actionAccount()
@ -444,4 +411,30 @@ class SettingsController extends Controller
$this->redirect(['account']); $this->redirect(['account']);
} }
/**
* @param $id
* @throws ForbiddenHttpException
* @throws NotFoundHttpException
* @throws \Exception
* @throws \Throwable
* @throws \yii\db\StaleObjectException
*/
protected function disconnectSocialNetwork($id)
{
/** @var SocialNetworkAccount $account */
$account = $this->socialNetworkAccountQuery->whereId($id)->one();
if ($account === null) {
throw new NotFoundHttpException();
}
if ($account->user_id !== Yii::$app->user->id) {
throw new ForbiddenHttpException();
}
$event = $this->make(SocialNetworkConnectEvent::class, [Yii::$app->user->identity, $account]);
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_DISCONNECT, $event);
$account->delete();
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_DISCONNECT, $event);
}
} }

View File

@ -19,7 +19,6 @@ use yii\base\Event;
*/ */
class GdprEvent extends Event class GdprEvent extends Event
{ {
const EVENT_BEFORE_DELETE = 'beforeDelete'; const EVENT_BEFORE_DELETE = 'beforeDelete';
const EVENT_AFTER_DELETE = 'afterDelete'; const EVENT_AFTER_DELETE = 'afterDelete';
/** /**

View File

@ -117,12 +117,12 @@ class MailFactory
/** /**
* Builds a MailerService. * Builds a MailerService.
* *
* @param string $type * @param string $type
* @param string|array|\Closure $from * @param string|array|\Closure $from
* @param string $to * @param string $to
* @param string $subject * @param string $subject
* @param string $view * @param string $view
* @param array $params * @param array $params
* *
* @throws InvalidConfigException * @throws InvalidConfigException
* @return MailService * @return MailService
@ -130,7 +130,7 @@ class MailFactory
*/ */
public static function makeMailerService($type, $from, $to, $subject, $view, $params = []) public static function makeMailerService($type, $from, $to, $subject, $view, $params = [])
{ {
if ($from instanceof \Closure){ if ($from instanceof \Closure) {
$from = $from($type); $from = $from($type);
} }
/** @noinspection PhpIncompatibleReturnTypeInspection */ /** @noinspection PhpIncompatibleReturnTypeInspection */

View File

@ -4,7 +4,6 @@
* This file is part of the 2amigos/yii2-usuario project. * This file is part of the 2amigos/yii2-usuario project.
* *
* (c) 2amigOS! <http://2amigos.us/> * (c) 2amigOS! <http://2amigos.us/>
* @author Lorenzo Milesi <maxxer@yetopen.it>
* *
* For the full copyright and license information, please view * For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.

View File

@ -4,7 +4,6 @@
* This file is part of the 2amigos/yii2-usuario project. * This file is part of the 2amigos/yii2-usuario project.
* *
* (c) 2amigOS! <http://2amigos.us/> * (c) 2amigOS! <http://2amigos.us/>
* @author E. Alamo <erosdelalamo@gmail.com>
* *
* For the full copyright and license information, please view * For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code. * the LICENSE file that was distributed with this source code.
@ -12,7 +11,6 @@
namespace Da\User\Form; namespace Da\User\Form;
use Da\User\Helper\SecurityHelper; use Da\User\Helper\SecurityHelper;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Traits\ContainerAwareTrait; use Da\User\Traits\ContainerAwareTrait;
@ -42,7 +40,7 @@ class GdprDeleteForm extends Model
/** /**
* @param SecurityHelper $securityHelper * @param SecurityHelper $securityHelper
* @param array $config * @param array $config
*/ */
public function __construct(SecurityHelper $securityHelper, $config = []) public function __construct(SecurityHelper $securityHelper, $config = [])
{ {

View File

@ -142,7 +142,7 @@ class LoginForm extends Model
* Validates form and logs the user in. * Validates form and logs the user in.
* *
* @throws InvalidParamException * @throws InvalidParamException
* @return bool whether the user is logged in successfully * @return bool whether the user is logged in successfully
*/ */
public function login() public function login()
{ {

View File

@ -103,13 +103,17 @@ class RegistrationForm extends Model
public function attributeHints() public function attributeHints()
{ {
return [ return [
'gdpr_consent' => Yii::t('usuario', 'I agree processing of my personal data and the use of cookies to facilitate the operation of this site. For more information read our {privacyPolicy}', 'gdpr_consent' => Yii::t(
'usuario',
'I agree processing of my personal data and the use of cookies to facilitate the operation of this site. For more information read our {privacyPolicy}',
[ [
'privacyPolicy' => Html::a(Yii::t('usuario', 'privacy policy'), 'privacyPolicy' => Html::a(
Yii::t('usuario', 'privacy policy'),
$this->module->gdprPrivacyPolicyUrl, $this->module->gdprPrivacyPolicyUrl,
['target' => '_blank'] ['target' => '_blank']
) )
]) ]
)
]; ];
} }
} }

View File

@ -55,7 +55,7 @@ class SettingsForm extends Model
* SettingsForm constructor. * SettingsForm constructor.
* *
* @param SecurityHelper $securityHelper * @param SecurityHelper $securityHelper
* @param array $config * @param array $config
*/ */
public function __construct(SecurityHelper $securityHelper, array $config = []) public function __construct(SecurityHelper $securityHelper, array $config = [])
{ {

View File

@ -34,7 +34,7 @@ class m000000_000002_create_profile_table extends Migration
MigrationHelper::resolveTableOptions($this->db->driverName) MigrationHelper::resolveTableOptions($this->db->driverName)
); );
$this->addPrimaryKey('{{%profile_pk}}','{{%profile}}','user_id'); $this->addPrimaryKey('{{%profile_pk}}', '{{%profile}}', 'user_id');
$restrict = MigrationHelper::isMicrosoftSQLServer($this->db->driverName) ? 'NO ACTION' : 'RESTRICT'; $restrict = MigrationHelper::isMicrosoftSQLServer($this->db->driverName) ? 'NO ACTION' : 'RESTRICT';

View File

@ -1,10 +1,19 @@
<?php <?php
/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Da\User\Migration; namespace Da\User\Migration;
use yii\db\Migration; use yii\db\Migration;
use yii\db\Schema; use yii\db\Schema;
/** /**
* Class m000000_000008_add_last_login_ip * Class m000000_000008_add_last_login_ip
* @author: Kartik Visweswaran * @author: Kartik Visweswaran

View File

@ -11,16 +11,16 @@
namespace Da\User\Migration; namespace Da\User\Migration;
use yii\db\Migration;
use Da\User\Helper\MigrationHelper; use Da\User\Helper\MigrationHelper;
use yii\db\Migration;
class m000000_000009_add_gdpr_consent_fields extends Migration class m000000_000009_add_gdpr_consent_fields extends Migration
{ {
public function safeUp() public function safeUp()
{ {
$this->addColumn('{{%user}}', 'gdpr_consent', $this->boolean()->defaultValue(MigrationHelper::getBooleanValue($this->db->driverName,false))); $this->addColumn('{{%user}}', 'gdpr_consent', $this->boolean()->defaultValue(MigrationHelper::getBooleanValue($this->db->driverName, false)));
$this->addColumn('{{%user}}', 'gdpr_consent_date', $this->integer(11)->null()); $this->addColumn('{{%user}}', 'gdpr_consent_date', $this->integer(11)->null());
$this->addColumn('{{%user}}', 'gdpr_deleted', $this->boolean()->defaultValue(MigrationHelper::getBooleanValue($this->db->driverName,false))); $this->addColumn('{{%user}}', 'gdpr_deleted', $this->boolean()->defaultValue(MigrationHelper::getBooleanValue($this->db->driverName, false)));
} }
public function safeDown() public function safeDown()

View File

@ -14,8 +14,8 @@ namespace Da\User\Model;
use Da\User\Traits\AuthManagerAwareTrait; use Da\User\Traits\AuthManagerAwareTrait;
use Da\User\Validator\RbacRuleNameValidator; use Da\User\Validator\RbacRuleNameValidator;
use Da\User\Validator\RbacRuleValidator; use Da\User\Validator\RbacRuleValidator;
use yii\base\Model;
use Yii; use Yii;
use yii\base\Model;
class Rule extends Model class Rule extends Model
{ {

View File

@ -75,7 +75,7 @@ class SocialNetworkAccount extends ActiveRecord
/** /**
* @throws Exception * @throws Exception
* @throws InvalidParamException * @throws InvalidParamException
* @return string the connection url * @return string the connection url
*/ */
public function getConnectionUrl() public function getConnectionUrl()
{ {

View File

@ -101,7 +101,7 @@ class Token extends ActiveRecord
/** /**
* @throws RuntimeException * @throws RuntimeException
* @return bool Whether token has expired * @return bool Whether token has expired
*/ */
public function getIsExpired() public function getIsExpired()
{ {

View File

@ -286,7 +286,7 @@ class User extends ActiveRecord implements IdentityInterface
/** /**
* @throws InvalidConfigException * @throws InvalidConfigException
* @return bool whether the user is an admin or not * @return bool whether the user is an admin or not
*/ */
public function getIsAdmin() public function getIsAdmin()
{ {

View File

@ -11,9 +11,9 @@
namespace Da\User\Service; namespace Da\User\Service;
use Yii;
use Da\User\Contracts\ServiceInterface; use Da\User\Contracts\ServiceInterface;
use Da\User\Model\User; use Da\User\Model\User;
use Yii;
class PasswordExpireService implements ServiceInterface class PasswordExpireService implements ServiceInterface
{ {

View File

@ -59,6 +59,7 @@ class UserCreateService implements ServiceInterface
? $model->password ? $model->password
: $this->securityHelper->generatePassword(8); : $this->securityHelper->generatePassword(8);
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$model]); $event = $this->make(UserEvent::class, [$model]);
$model->trigger(UserEvent::EVENT_BEFORE_CREATE, $event); $model->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
@ -75,15 +76,15 @@ class UserCreateService implements ServiceInterface
['email' => $model->email] ['email' => $model->email]
); );
// from web display a flash message (if enabled) // from web display a flash message (if enabled)
if($this->getModule()->enableFlashMessages == TRUE && is_a(Yii::$app, yii\web\Application::class)) { if ($this->getModule()->enableFlashMessages === true && is_a(Yii::$app, yii\web\Application::class)) {
Yii::$app->session->setFlash( Yii::$app->session->setFlash(
'warning', 'warning',
$error_msg $error_msg
); );
} }
// if we're from console add an error to the model in order to return an error message // if we're from console add an error to the model in order to return an error message
if(is_a(Yii::$app, yii\console\Application::class)) { if (is_a(Yii::$app, yii\console\Application::class)) {
$model->addError("username", $error_msg); $model->addError('username', $error_msg);
} }
$transaction->rollBack(); $transaction->rollBack();
return false; return false;

View File

@ -83,7 +83,7 @@ class ReCaptchaWidget extends InputWidget
/** /**
* @throws InvalidConfigException * @throws InvalidConfigException
* @return array the google recaptcha options. * @return array the google recaptcha options.
*/ */
protected function getCaptchaOptions() protected function getCaptchaOptions()
{ {