forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create models for userexpiration, add tests and events (magento#22833:…
… Short-term admin accounts)
- Loading branch information
Showing
30 changed files
with
1,742 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/code/Magento/Security/Model/Plugin/UserValidationRules.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Security\Model\Plugin; | ||
|
||
/** | ||
* \Magento\User\Model\UserValidationRules decorator | ||
* | ||
* @package Magento\Security\Model\Plugin | ||
*/ | ||
class UserValidationRules | ||
{ | ||
/**@var \Magento\Security\Model\UserExpiration\Validator */ | ||
private $validator; | ||
|
||
/** | ||
* UserValidationRules constructor. | ||
* | ||
* @param \Magento\Security\Model\UserExpiration\Validator $validator | ||
*/ | ||
public function __construct(\Magento\Security\Model\UserExpiration\Validator $validator) | ||
{ | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* @param \Magento\User\Model\UserValidationRules $userValidationRules | ||
* @param \Magento\Framework\Validator\DataObject $result | ||
* @return \Magento\Framework\Validator\DataObject | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterAddUserInfoRules(\Magento\User\Model\UserValidationRules $userValidationRules, $result) | ||
{ | ||
return $result->addRule($this->validator, 'expires_at'); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/code/Magento/Security/Model/ResourceModel/UserExpiration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Security\Model\ResourceModel; | ||
|
||
/** | ||
* Admin User Expiration resource model | ||
*/ | ||
class UserExpiration extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb | ||
{ | ||
|
||
/** | ||
* Flag that notifies whether Primary key of table is auto-incremented | ||
* | ||
* @var bool | ||
*/ | ||
protected $_isPkAutoIncrement = false; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init('admin_user_expiration', 'user_id'); | ||
} | ||
|
||
/** | ||
* Perform actions before object save | ||
* | ||
* @param \Magento\Framework\Model\AbstractModel $object | ||
* @return $this | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function _beforeSave(\Magento\Framework\Model\AbstractModel $object) | ||
{ | ||
/** @var $object \Magento\Security\Model\UserExpiration */ | ||
if ($object->getExpiresAt() instanceof \DateTimeInterface) { | ||
|
||
// TODO: use this? need to check if we're ever passing in a \DateTimeInterface or if it's always a string | ||
$object->setExpiresAt($object->getExpiresAt()->format('Y-m-d H:i:s')); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/code/Magento/Security/Model/ResourceModel/UserExpiration/Collection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Security\Model\ResourceModel\UserExpiration; | ||
|
||
/** | ||
* Admin user expiration collection | ||
*/ | ||
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $_idFieldName = 'user_id'; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init( | ||
\Magento\Security\Model\UserExpiration::class, | ||
\Magento\Security\Model\ResourceModel\UserExpiration::class | ||
); | ||
} | ||
|
||
/** | ||
* Filter for expired, active users. | ||
* | ||
* @param string $now | ||
* @return $this | ||
*/ | ||
public function addActiveExpiredUsersFilter($now = null): Collection | ||
{ | ||
if ($now === null) { | ||
$now = new \DateTime(); | ||
$now->format('Y-m-d H:i:s'); | ||
} | ||
$this->getSelect()->joinLeft( | ||
['user' => $this->getTable('admin_user')], | ||
'main_table.user_id = user.user_id', | ||
['is_active'] | ||
); | ||
$this->addFieldToFilter('expires_at', ['lt' => $now]) | ||
->addFieldToFilter('user.is_active', 1); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Filter collection by user id. | ||
* @param array $userIds | ||
* @return Collection | ||
*/ | ||
public function addUserIdsFilter($userIds = []): Collection | ||
{ | ||
return $this->addFieldToFilter('main_table.user_id', ['in' => $userIds]); | ||
} | ||
|
||
/** | ||
* Get any expired records for the given user. | ||
* | ||
* @param $userId | ||
* @return Collection | ||
*/ | ||
public function addExpiredRecordsForUserFilter($userId): Collection | ||
{ | ||
return $this->addActiveExpiredUsersFilter() | ||
->addFieldToFilter('main_table.user_id', $userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Security\Model; | ||
|
||
/** | ||
* Admin User Expiration model. | ||
* @method string getExpiresAt() | ||
* @method \Magento\Security\Model\UserExpiration setExpiresAt(string $value) | ||
*/ | ||
class UserExpiration extends \Magento\Framework\Model\AbstractModel | ||
{ | ||
|
||
/** | ||
* @var UserExpiration\Validator | ||
*/ | ||
private $validator; | ||
|
||
/** | ||
* UserExpiration constructor. | ||
* | ||
* @param \Magento\Framework\Model\Context $context | ||
* @param \Magento\Framework\Registry $registry | ||
* @param UserExpiration\Validator $validator | ||
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource | ||
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Model\Context $context, | ||
\Magento\Framework\Registry $registry, | ||
\Magento\Security\Model\UserExpiration\Validator $validator, | ||
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, | ||
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $registry, $resource, $resourceCollection, $data); | ||
$this->validator = $validator; | ||
} | ||
|
||
/** | ||
* Resource initialization | ||
* | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init(\Magento\Security\Model\ResourceModel\UserExpiration::class); | ||
} | ||
|
||
/** | ||
* TODO: remove and use a plugin on UserValidationRules | ||
*/ | ||
// protected function _getValidationRulesBeforeSave() | ||
// { | ||
// return $this->validator; | ||
// } | ||
} |
Oops, something went wrong.