re #108 use app main logger + multiple fixes

This commit is contained in:
Antonio Ramirez
2017-11-12 21:58:48 +01:00
parent 77430fbb74
commit 190fafa5f1
30 changed files with 100 additions and 63 deletions

View File

@ -4,6 +4,8 @@
- Bug #106: Correct exception value returned in `MailEvent::getException` (kartik-v)
- Enh #99: Added German translation (jkmssoft)
- Enh #100: Added pt-BR translation (gugoan)
- Enh #105: Consolidate 2fa messages (maxxer)
- Fix #108: Use main logger app (tonydspaniard)
## 1.1.0 - October 22, 2017
- Enh #91: Documentation for Mail events (kartik-v)

View File

@ -20,6 +20,7 @@ use yii\authclient\Collection;
use yii\base\Application;
use yii\base\BootstrapInterface;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\console\Application as ConsoleApplication;
use yii\i18n\PhpMessageSource;
use yii\web\Application as WebApplication;
@ -161,6 +162,8 @@ class Bootstrap implements BootstrapInterface
* Registers module translation messages.
*
* @param Application $app
*
* @throws InvalidConfigException
*/
protected function initTranslations(Application $app)
{
@ -177,6 +180,8 @@ class Bootstrap implements BootstrapInterface
* Ensures the auth manager is the one provided by the library.
*
* @param Application $app
*
* @throws InvalidConfigException
*/
protected function initAuthManager(Application $app)
{
@ -194,6 +199,8 @@ class Bootstrap implements BootstrapInterface
* Initializes web url routes (rules in Yii2).
*
* @param WebApplication $app
*
* @throws InvalidConfigException
*/
protected function initUrlRoutes(WebApplication $app)
{
@ -236,6 +243,8 @@ class Bootstrap implements BootstrapInterface
* Ensures the authCollection component is configured.
*
* @param WebApplication $app
*
* @throws InvalidConfigException
*/
protected function initAuthCollection(WebApplication $app)
{

View File

@ -68,7 +68,6 @@ class AuthDbManagerComponent extends DbManager implements AuthManagerInterface
$roles = [];
foreach ($query->all($this->db) as $row) {
$roles[$row['name']] = $this->populateItem($row);
$roles[$row['name']] = $this->populateItem($row);
}
return $roles;

View File

@ -29,6 +29,8 @@ class ReCaptchaComponent extends Component
/**
* @inheritdoc
*
* @throws InvalidConfigException
*/
public function init()
{

View File

@ -15,11 +15,6 @@ use Da\User\Model\User;
use Da\User\Service\MailService;
use yii\base\Event;
/**
* Mailer event to trap and handle mail exceptions for user model actions
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
*/
class MailEvent extends Event
{
const TYPE_WELCOME = 'welcome';

View File

@ -22,7 +22,7 @@ class ProfileEvent extends Event
{
$this->profile = $profile;
return parent::__construct($config);
parent::__construct($config);
}
public function getProfile()

View File

@ -17,13 +17,8 @@ use Da\User\Model\User;
use Da\User\Module;
use Da\User\Service\MailService;
use Yii;
use yii\base\InvalidConfigException;
/**
* Class MailFactory
*
* @package Da\User\Factory
* @modified 2017-10-18 by Kartik Visweswaran <kartikv2@gmail.com>
*/
class MailFactory
{
/**
@ -126,6 +121,8 @@ class MailFactory
* @param array $params
*
* @return MailService
*
* @throws InvalidConfigException
*/
public static function makeMailerService($type, $from, $to, $subject, $view, $params = [])
{

View File

@ -11,6 +11,7 @@
namespace Da\User\Form;
use Da\TwoFA\Exception\InvalidSecretKeyException;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
@ -79,6 +80,8 @@ class LoginForm extends Model
/**
* {@inheritdoc}
*
* @throws InvalidSecretKeyException
*/
public function rules()
{

View File

@ -37,6 +37,8 @@ class RegistrationForm extends Model
/**
* {@inheritdoc}
*
* @throws \Exception
*/
public function rules()
{

View File

@ -17,6 +17,7 @@ use Da\User\Model\User;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
class SettingsForm extends Model
@ -60,6 +61,9 @@ class SettingsForm extends Model
/**
* @return array
*
* @throws InvalidConfigException
* @throws \Exception
*/
public function rules()
{
@ -75,7 +79,7 @@ class SettingsForm extends Model
['email', 'username'],
'unique',
'when' => function ($model, $attribute) {
return $this->getUser()->$attribute != $model->$attribute;
return $this->getUser()->$attribute !== $model->$attribute;
},
'targetClass' => $this->getClassMap()->get(User::class),
],
@ -110,7 +114,7 @@ class SettingsForm extends Model
*/
public function getUser()
{
if ($this->user == null) {
if (null === $this->user) {
$this->user = Yii::$app->user->identity;
}
@ -121,6 +125,8 @@ class SettingsForm extends Model
* Saves new account settings.
*
* @return bool
*
* @throws \Exception
*/
public function save()
{
@ -130,9 +136,9 @@ class SettingsForm extends Model
$user->scenario = 'settings';
$user->username = $this->username;
$user->password = $this->new_password;
if ($this->email == $user->email && $user->unconfirmed_email != null) {
if ($this->email === $user->email && $user->unconfirmed_email !== null) {
$user->unconfirmed_email = null;
} elseif ($this->email != $user->email) {
} elseif ($this->email !== $user->email) {
$strategy = EmailChangeStrategyFactory::makeByStrategyType(
$this->getModule()->emailChangeStrategy,
$this

View File

@ -56,7 +56,7 @@ class AuthHelper
? Yii::$app->getUser()->can($module->administratorPermissionName)
: false;
return $hasAdministratorPermissionName || in_array($username, $module->administrators);
return $hasAdministratorPermissionName || in_array($username, $module->administrators, false);
}
/**
@ -99,7 +99,7 @@ class AuthHelper
public function getUnassignedItems(AbstractAuthItem $model)
{
$excludeItems = $model->item !== null ? [$model->item->name] : [];
$type = $model->getType() == Permission::TYPE_PERMISSION ? Permission::TYPE_PERMISSION : null;
$type = $model->getType() === Permission::TYPE_PERMISSION ? Permission::TYPE_PERMISSION : null;
$items = $this->getAuthManager()->getItems($type, $excludeItems);
return ArrayHelper::map(

View File

@ -11,14 +11,6 @@
namespace Da\User\Helper;
/**
* ModelMapHelper.php.
*
* Date: 3/12/16
* Time: 18:10
*
* @author Antonio Ramirez <hola@2amigos.us>
*/
class ClassMapHelper
{
protected $map = [];

View File

@ -19,6 +19,8 @@ class MigrationHelper
* @param string $driverName
*
* @return null|string
*
* @throws RuntimeException
*/
public static function resolveTableOptions($driverName)
{
@ -39,6 +41,8 @@ class MigrationHelper
* @param $driverName
*
* @return string
*
* @throws RuntimeException
*/
public static function resolveDbType($driverName)
{
@ -60,9 +64,11 @@ class MigrationHelper
* @param string $driverName
*
* @return bool
*
* @throws RuntimeException
*/
public static function isMicrosoftSQLServer($driverName)
{
return self::resolveDbType($driverName) == 'sqlsrv';
return self::resolveDbType($driverName) === 'sqlsrv';
}
}

View File

@ -11,6 +11,7 @@
namespace Da\User\Helper;
use yii\base\Exception;
use yii\base\Security;
class SecurityHelper
@ -32,12 +33,23 @@ class SecurityHelper
* @param null|int $cost
*
* @return string
*
* @throws Exception
*/
public function generatePasswordHash($password, $cost = null)
{
return $this->security->generatePasswordHash($password, $cost);
}
/**
* Generates a random string
*
* @param int $length
*
* @return string
*
* @throws Exception
*/
public function generateRandomString($length = 32)
{
return $this->security->generateRandomString($length);

View File

@ -13,6 +13,7 @@ namespace Da\User\Helper;
use DateTime;
use DateTimeZone;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
class TimezoneHelper
@ -21,6 +22,8 @@ class TimezoneHelper
* Get all of the time zones with the offsets sorted by their offset.
*
* @return array
*
* @throws InvalidParamException
*/
public static function getAll()
{

View File

@ -20,6 +20,7 @@ use DateTime;
use DateTimeZone;
use Exception;
use Yii;
use yii\base\InvalidParamException;
use yii\db\ActiveRecord;
/**
@ -41,6 +42,8 @@ class Profile extends ActiveRecord
/**
* {@inheritdoc}
*
* @throws InvalidParamException
*/
public function beforeSave($insert)
{

View File

@ -16,6 +16,7 @@ use Da\User\Query\UserQuery;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Yii;
use yii\base\InvalidParamException;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
@ -71,6 +72,8 @@ class User extends ActiveRecord implements IdentityInterface
/**
* {@inheritdoc}
*
* @throws InvalidParamException
*/
public function beforeSave($insert)
{
@ -277,6 +280,8 @@ class User extends ActiveRecord implements IdentityInterface
/**
* @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ]
*
* @throws \Exception
*/
public function getSocialNetworkAccounts()
{
@ -307,6 +312,8 @@ class User extends ActiveRecord implements IdentityInterface
/**
* {@inheritdoc}
*
* @throws NotSupportedException
*/
public static function findIdentityByAccessToken($token, $type = null)
{

View File

@ -31,14 +31,14 @@ class AuthRuleEditionService implements ServiceInterface
public function run()
{
if (!$this->model->validate() || (!in_array($this->model->scenario, ['create', 'update']))) {
if (!$this->model->validate() || (!in_array($this->model->scenario, ['create', 'update'], false))) {
return false;
}
$rule = $this->make($this->model->className, [], ['name' => $this->model->name]);
try {
if ($this->model->scenario == 'create') {
if ($this->model->scenario === 'create') {
$this->getAuthManager()->add($rule);
} else {
$this->getAuthManager()->update($this->model->previousName, $rule);

View File

@ -76,8 +76,8 @@ class EmailChangeService implements ServiceInterface
);
}
}
if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_DEFAULT
|| ($this->model->flags & User::NEW_EMAIL_CONFIRMED & $this->model->flags & User::OLD_EMAIL_CONFIRMED)
if (($this->model->flags & User::NEW_EMAIL_CONFIRMED & $this->model->flags & User::OLD_EMAIL_CONFIRMED)
|| $this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_DEFAULT
) {
$this->model->email = $this->model->unconfirmed_email;
$this->model->unconfirmed_email = null;

View File

@ -13,13 +13,11 @@ namespace Da\User\Service;
use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory;
use Da\User\Model\Token;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Da\User\Traits\MailAwareTrait;
use Exception;
use Yii;
use yii\log\Logger;
class PasswordRecoveryService implements ServiceInterface
{
@ -30,14 +28,12 @@ class PasswordRecoveryService implements ServiceInterface
protected $email;
protected $mailService;
protected $securityHelper;
protected $logger;
public function __construct($email, MailService $mailService, UserQuery $query, Logger $logger)
public function __construct($email, MailService $mailService, UserQuery $query)
{
$this->email = $email;
$this->mailService = $mailService;
$this->query = $query;
$this->logger = $logger;
}
public function run()
@ -65,7 +61,7 @@ class PasswordRecoveryService implements ServiceInterface
return true;
} catch (Exception $e) {
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
Yii::error($e->getMessage(), 'usuario');
return false;
}

View File

@ -15,7 +15,6 @@ use Da\User\Contracts\ServiceInterface;
use Da\User\Factory\TokenFactory;
use Da\User\Model\User;
use Da\User\Traits\MailAwareTrait;
use yii\log\Logger;
class ResendConfirmationService implements ServiceInterface
{
@ -23,13 +22,11 @@ class ResendConfirmationService implements ServiceInterface
protected $model;
protected $mailService;
protected $logger;
public function __construct(User $model, MailService $mailService, Logger $logger)
public function __construct(User $model, MailService $mailService)
{
$this->model = $model;
$this->mailService = $mailService;
$this->logger = $logger;
}
public function run()
@ -37,6 +34,7 @@ class ResendConfirmationService implements ServiceInterface
if ($this->model && !$this->model->getIsConfirmed()) {
$token = TokenFactory::makeConfirmationToken($this->model->id);
$this->mailService->setViewParam('token', $token);
return $this->sendMail($this->model);
}

View File

@ -47,8 +47,7 @@ class TwoFactorQrCodeUriGeneratorService implements ServiceInterface
}
$totpUri = (new TOTPSecretKeyUriGeneratorService(Yii::$app->name, $user->email, $user->auth_tf_key))->run();
$dataUri = (new QrCodeDataUriGeneratorService($totpUri))->run();
return $dataUri;
return (new QrCodeDataUriGeneratorService($totpUri))->run();
}
}

View File

@ -19,7 +19,6 @@ use Da\User\Traits\MailAwareTrait;
use Exception;
use Yii;
use yii\base\InvalidCallException;
use yii\log\Logger;
class UserCreateService implements ServiceInterface
{
@ -28,18 +27,19 @@ class UserCreateService implements ServiceInterface
protected $model;
protected $securityHelper;
protected $mailService;
protected $logger;
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper, Logger $logger)
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper)
{
$this->model = $model;
$this->mailService = $mailService;
$this->securityHelper = $securityHelper;
$this->logger = $logger;
}
/**
* @return bool
*
* @throws InvalidCallException
* @throws \yii\db\Exception
*/
public function run()
{
@ -49,7 +49,7 @@ class UserCreateService implements ServiceInterface
throw new InvalidCallException('Cannot create a new user from an existing one.');
}
$transaction = $model->getDb()->beginTransaction();
$transaction = $model::getDb()->beginTransaction();
try {
$model->confirmed_at = time();
@ -81,7 +81,7 @@ class UserCreateService implements ServiceInterface
return true;
} catch (Exception $e) {
$transaction->rollBack();
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
Yii::error($e->getMessage(), 'usuario');
return false;
}

View File

@ -20,7 +20,6 @@ use Da\User\Traits\MailAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Exception;
use yii\base\InvalidCallException;
use yii\log\Logger;
use Yii;
class UserRegisterService implements ServiceInterface
@ -31,14 +30,12 @@ class UserRegisterService implements ServiceInterface
protected $model;
protected $securityHelper;
protected $mailService;
protected $logger;
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper, Logger $logger)
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper)
{
$this->model = $model;
$this->mailService = $mailService;
$this->securityHelper = $securityHelper;
$this->logger = $logger;
}
public function run()
@ -49,7 +46,7 @@ class UserRegisterService implements ServiceInterface
throw new InvalidCallException('Cannot register user from an existing one.');
}
$transaction = $model->getDb()->beginTransaction();
$transaction = $model::getDb()->beginTransaction();
try {
$model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time();
@ -91,7 +88,7 @@ class UserRegisterService implements ServiceInterface
return true;
} catch (Exception $e) {
$transaction->rollBack();
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
Yii::error($e->getMessage(), 'usuario');
return false;
}

View File

@ -14,6 +14,7 @@ namespace Da\User\Traits;
use Da\User\Helper\AuthHelper;
use Da\User\Helper\ClassMapHelper;
use Yii;
use yii\base\InvalidConfigException;
use yii\di\Container;
/**
@ -48,6 +49,8 @@ trait ContainerAwareTrait
/**
* @return \Da\User\Helper\AuthHelper|object
*
* @throws InvalidConfigException
*/
public function getAuth()
{
@ -56,6 +59,8 @@ trait ContainerAwareTrait
/**
* @return \Da\User\Helper\ClassMapHelper|object
*
* @throws InvalidConfigException
*/
public function getClassMap()
{

View File

@ -15,12 +15,10 @@ use Da\User\Event\MailEvent;
use Da\User\Model\User;
use Da\User\Service\MailService;
use Exception;
use yii\log\Logger;
use Yii;
/**
* @property MailService $mailService
* @property Logger $logger
* @author Kartik Visweswaran <kartikv2@gmail.com>
*/
trait MailAwareTrait
{
@ -42,7 +40,7 @@ trait MailAwareTrait
$this->mailService->run();
} catch (Exception $e) {
$event = $this->make(MailEvent::class, [$type, $user, $this->mailService, $e]);
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
Yii::error($e->getMessage(), 'usuario');
$user->trigger(MailEvent::EVENT_AFTER_SEND_MAIL, $event);
return false;
}

View File

@ -26,7 +26,7 @@ class RbacItemsValidator extends Validator
}
foreach ($value as $item) {
if ($this->getAuthManager()->getItem($item) == null) {
if ($this->getAuthManager()->getItem($item) === null) {
return [Yii::t('usuario', 'There is neither role nor permission with name "{0}"', [$item]), []];
}
}

View File

@ -30,7 +30,7 @@ class RbacRuleNameValidator extends Validator
*/
protected function validateValue($value)
{
if ($this->previousName != $value) {
if ($this->previousName !== $value) {
$rule = $this->getAuthManager()->getRule($value);
if ($rule instanceof Rule) {

View File

@ -11,6 +11,7 @@
namespace Da\User\Validator;
use Da\TwoFA\Exception\InvalidSecretKeyException;
use Da\TwoFA\Manager;
use Da\User\Contracts\ValidatorInterface;
use Da\User\Model\User;
@ -37,6 +38,8 @@ class TwoFactorCodeValidator implements ValidatorInterface
/**
* @return bool|int
*
* @throws InvalidSecretKeyException
*/
public function validate()
{

View File

@ -16,6 +16,7 @@ use Da\User\Service\UpdateAuthAssignmentsService;
use Da\User\Traits\AuthManagerAwareTrait;
use Da\User\Traits\ContainerAwareTrait;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
@ -48,6 +49,8 @@ class AssignmentsWidget extends Widget
/**
* {@inheritdoc}
*
* @throws InvalidParamException
*/
public function run()
{