Skip to content

Commit d4bc679

Browse files
viktymYevSent
authored andcommitted
MAGETWO-68794: Password policy does not work correctly
1 parent c350871 commit d4bc679

File tree

11 files changed

+341
-191
lines changed

11 files changed

+341
-191
lines changed

app/code/Magento/User/Model/Backend/Config/ObserverConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function __construct(
3535
*/
3636
public function _isLatestPasswordExpired($latestPassword)
3737
{
38-
if (!isset($latestPassword['expires']) || $this->getAdminPasswordLifetime() == 0) {
38+
if (!isset($latestPassword['last_updated']) || $this->getAdminPasswordLifetime() == 0) {
3939
return false;
40-
} else {
41-
return (int)$latestPassword['expires'] < time();
4240
}
41+
42+
return (int)$latestPassword['last_updated'] + $this->getAdminPasswordLifetime() < time();
4343
}
4444

4545
/**

app/code/Magento/User/Model/ResourceModel/User.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
1010
use Magento\Authorization\Model\Acl\Role\User as RoleUser;
1111
use Magento\Authorization\Model\UserContextInterface;
12+
use Magento\Framework\Acl\Data\CacheInterface;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\User\Model\Backend\Config\ObserverConfig;
1215
use Magento\User\Model\User as ModelUser;
1316

1417
/**
@@ -32,32 +35,38 @@ class User extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
3235
protected $dateTime;
3336

3437
/**
35-
* @var \Magento\Framework\Acl\Data\CacheInterface
38+
* @var CacheInterface
3639
*/
3740
private $aclDataCache;
3841

42+
/**
43+
* @var ObserverConfig|null
44+
*/
45+
private $observerConfig;
46+
3947
/**
4048
* Construct
4149
*
4250
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
4351
* @param \Magento\Authorization\Model\RoleFactory $roleFactory
4452
* @param \Magento\Framework\Stdlib\DateTime $dateTime
4553
* @param string $connectionName
46-
* @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
54+
* @param CacheInterface $aclDataCache
55+
* @param ObserverConfig|null $observerConfig
4756
*/
4857
public function __construct(
4958
\Magento\Framework\Model\ResourceModel\Db\Context $context,
5059
\Magento\Authorization\Model\RoleFactory $roleFactory,
5160
\Magento\Framework\Stdlib\DateTime $dateTime,
5261
$connectionName = null,
53-
\Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null
62+
CacheInterface $aclDataCache = null,
63+
ObserverConfig $observerConfig = null
5464
) {
5565
parent::__construct($context, $connectionName);
5666
$this->_roleFactory = $roleFactory;
5767
$this->dateTime = $dateTime;
58-
$this->aclDataCache = $aclDataCache ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
59-
\Magento\Framework\Acl\Data\CacheInterface::class
60-
);
68+
$this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(CacheInterface::class);
69+
$this->observerConfig = $observerConfig ?: ObjectManager::getInstance()->get(ObserverConfig::class);
6170
}
6271

6372
/**
@@ -559,12 +568,14 @@ public function getOldPasswords($user, $retainLimit = 4)
559568
->select()
560569
->from($table, 'password_id')
561570
->where('user_id = :user_id')
562-
->order('expires ' . \Magento\Framework\DB\Select::SQL_DESC)
563571
->order('password_id ' . \Magento\Framework\DB\Select::SQL_DESC)
564572
->limit($retainLimit),
565573
[':user_id' => $userId]
566574
);
567-
$where = ['user_id = ?' => $userId, 'expires <= ?' => time()];
575+
$where = [
576+
'user_id = ?' => $userId,
577+
'last_updated <= ?' => time() - $this->observerConfig->getAdminPasswordLifetime()
578+
];
568579
if ($retainPasswordIds) {
569580
$where['password_id NOT IN (?)'] = $retainPasswordIds;
570581
}
@@ -585,19 +596,21 @@ public function getOldPasswords($user, $retainLimit = 4)
585596
*
586597
* @param ModelUser $user
587598
* @param string $passwordHash
588-
* @param int $lifetime
599+
* @param int $lifetime deprecated, password expiration date doesn't save anymore,
600+
* it is calculated in runtime based on password created date and lifetime config value
589601
* @return void
602+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
603+
*
604+
* @see \Magento\User\Model\Backend\Config\ObserverConfig::_isLatestPasswordExpired()
590605
*/
591-
public function trackPassword($user, $passwordHash, $lifetime)
606+
public function trackPassword($user, $passwordHash, $lifetime = 0)
592607
{
593-
$now = time();
594608
$this->getConnection()->insert(
595609
$this->getTable('admin_passwords'),
596610
[
597611
'user_id' => $user->getId(),
598612
'password_hash' => $passwordHash,
599-
'expires' => $now + $lifetime,
600-
'last_updated' => $now
613+
'last_updated' => time()
601614
]
602615
);
603616
}

app/code/Magento/User/Observer/Backend/TrackAdminNewPasswordObserver.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public function execute(EventObserver $observer)
7272
$user = $observer->getEvent()->getObject();
7373
if ($user->getId()) {
7474
$passwordHash = $user->getPassword();
75-
$passwordLifetime = $this->observerConfig->getAdminPasswordLifetime();
76-
if ($passwordLifetime && $passwordHash && !$user->getForceNewPassword()) {
77-
$this->userResource->trackPassword($user, $passwordHash, $passwordLifetime);
75+
if ($passwordHash && !$user->getForceNewPassword()) {
76+
$this->userResource->trackPassword($user, $passwordHash);
7877
$this->messageManager->getMessages()->deleteMessageByIdentifier('magento_user_password_expired');
7978
$this->authSession->unsPciAdminUserIsPasswordExpired();
8079
}

0 commit comments

Comments
 (0)