Skip to content

Commit

Permalink
move interface into Api/Data, make it extensible (#22833: Short-term …
Browse files Browse the repository at this point in the history
…admin accounts)
  • Loading branch information
lfolco committed Mar 4, 2020
1 parent 107cb5f commit bef0bd5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

declare(strict_types=1);

namespace Magento\Security\Model;
namespace Magento\Security\Api\Data;

/**
* Interface UserExpirationInterface to be used as a DTO for expires_at property on User model.
*/
interface UserExpirationInterface
interface UserExpirationInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{

public const EXPIRES_AT = 'expires_at';
Expand Down Expand Up @@ -47,4 +47,19 @@ public function getUserId();
* @return $this
*/
public function setUserId($userId);

/**
* Retrieve existing extension attributes object or create a new one.
*
* @return \Magento\Security\Api\Data\UserExpirationExtensionInterface|null
*/
public function getExtensionAttributes();

/**
* Set an extension attributes object.
*
* @param \Magento\Security\Api\Data\UserExpirationExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(\Magento\Security\Api\Data\UserExpirationExtensionInterface $extensionAttributes);
}
2 changes: 1 addition & 1 deletion app/code/Magento/Security/Model/Plugin/AdminUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AdminUserForm
private $userExpirationResource;

/**
* @var \Magento\Security\Model\UserExpirationInterfaceFactory
* @var \Magento\Security\Api\Data\UserExpirationInterfaceFactory
*/
private $userExpirationFactory;

Expand Down
20 changes: 19 additions & 1 deletion app/code/Magento/Security/Model/UserExpiration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace Magento\Security\Model;

use Magento\Security\Api\Data\UserExpirationInterface;

/**
* Admin User Expiration model.
*/
class UserExpiration extends \Magento\Framework\Model\AbstractModel implements UserExpirationInterface
class UserExpiration extends \Magento\Framework\Model\AbstractExtensibleModel implements UserExpirationInterface
{

/**
Expand Down Expand Up @@ -64,4 +66,20 @@ public function setUserId($userId)
{
return $this->setData(self::USER_ID, $userId);
}

/**
* @inheritDoc
*/
public function getExtensionAttributes()
{
return $this->_getExtensionAttributes();
}

/**
* @inheritDoc
*/
public function setExtensionAttributes(\Magento\Security\Api\Data\UserExpirationExtensionInterface $extensionAttributes)
{
return $this->_setExtensionAttributes($extensionAttributes);
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Security/Model/UserExpirationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private function processExpiredUsers(ExpiredUsersCollection $expiredRecords): vo
public function isUserExpired(string $userId): bool
{
$isExpired = false;
/** @var \Magento\Security\Model\UserExpirationInterface $expiredRecord */
/** @var \Magento\Security\Api\Data\UserExpirationInterface $expiredRecord */
$expiredRecord = $this->userExpirationCollectionFactory->create()
->addExpiredRecordsForUserFilter($userId)
->getFirstItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AdminUserAuthenticateBeforeTest extends \PHPUnit\Framework\TestCase
private $eventMock;

/**
* @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Security\Model\UserExpirationInterface
* @var \PHPUnit\Framework\MockObject\MockObject|\Magento\Security\Api\Data\UserExpirationInterface
*/
private $userExpirationMock;

Expand Down Expand Up @@ -77,8 +77,15 @@ protected function setUp()
$this->eventObserverMock = $this->createPartialMock(\Magento\Framework\Event\Observer::class, ['getEvent']);
$this->eventMock = $this->createPartialMock(\Magento\Framework\Event::class, ['getUsername']);
$this->userExpirationMock = $this->createPartialMock(
\Magento\Security\Model\UserExpirationInterface::class,
['getUserId', 'getExpiresAt', 'setUserId', 'setExpiresAt']
\Magento\Security\Api\Data\UserExpirationInterface::class,
[
'getUserId',
'getExpiresAt',
'setUserId',
'setExpiresAt',
'getExtensionAttributes',
'setExtensionAttributes'
]
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Security/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
</argument>
</arguments>
</type>
<preference for="Magento\Security\Model\UserExpirationInterface" type="Magento\Security\Model\UserExpiration"/>
<preference for="Magento\Security\Api\Data\UserExpirationInterface" type="Magento\Security\Model\UserExpiration"/>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public function testProcessProlongWithExpiredUser()
$user = $this->objectManager->create(\Magento\User\Model\User::class);
$user->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
$userExpirationFactory =
$this->objectManager->create(\Magento\Security\Model\UserExpirationInterfaceFactory::class);
/** @var \Magento\Security\Model\UserExpirationInterface $userExpiration */
$this->objectManager->create(\Magento\Security\Api\Data\UserExpirationInterfaceFactory::class);
/** @var \Magento\Security\Api\Data\UserExpirationInterface $userExpiration */
$userExpiration = $userExpirationFactory->create();
$userExpiration->setId($user->getId())
->setExpiresAt($expireDate->format('Y-m-d H:i:s'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ private function expireUser(\Magento\User\Model\User $user)
{
$expireDate = new \DateTime();
$expireDate->modify('-10 days');
/** @var \Magento\Security\Model\UserExpirationInterface $userExpiration */
$userExpiration = $this->objectManager->create(\Magento\Security\Model\UserExpirationInterface::class);
/** @var \Magento\Security\Api\Data\UserExpirationInterface $userExpiration */
$userExpiration = $this->objectManager->create(\Magento\Security\Api\Data\UserExpirationInterface::class);
$userExpiration->setId($user->getId())
->setExpiresAt($expireDate->format('Y-m-d H:i:s'))
->save();
Expand Down

0 comments on commit bef0bd5

Please sign in to comment.