Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdminTokenService.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Integration\Model\Oauth\TokenFactory as TokenModelFactory;
14 use Magento\Integration\Model\ResourceModel\Oauth\Token\CollectionFactory as TokenCollectionFactory;
15 use Magento\User\Model\User as UserModel;
17 
22 {
28  private $tokenModelFactory;
29 
35  private $userModel;
36 
40  private $validatorHelper;
41 
47  private $tokenModelCollectionFactory;
48 
52  private $requestThrottler;
53 
62  public function __construct(
63  TokenModelFactory $tokenModelFactory,
64  UserModel $userModel,
65  TokenCollectionFactory $tokenModelCollectionFactory,
66  CredentialsValidator $validatorHelper
67  ) {
68  $this->tokenModelFactory = $tokenModelFactory;
69  $this->userModel = $userModel;
70  $this->tokenModelCollectionFactory = $tokenModelCollectionFactory;
71  $this->validatorHelper = $validatorHelper;
72  }
73 
77  public function createAdminAccessToken($username, $password)
78  {
79  $this->validatorHelper->validate($username, $password);
80  $this->getRequestThrottler()->throttle($username, RequestThrottler::USER_TYPE_ADMIN);
81  $this->userModel->login($username, $password);
82  if (!$this->userModel->getId()) {
83  $this->getRequestThrottler()->logAuthenticationFailure($username, RequestThrottler::USER_TYPE_ADMIN);
84  /*
85  * This message is same as one thrown in \Magento\Backend\Model\Auth to keep the behavior consistent.
86  * Constant cannot be created in Auth Model since it uses legacy translation that doesn't support it.
87  * Need to make sure that this is refactored once exception handling is updated in Auth Model.
88  */
89  throw new AuthenticationException(
90  __(
91  'The account sign-in was incorrect or your account is disabled temporarily. '
92  . 'Please wait and try again later.'
93  )
94  );
95  }
96  $this->getRequestThrottler()->resetAuthenticationFailuresCount($username, RequestThrottler::USER_TYPE_ADMIN);
97  return $this->tokenModelFactory->create()->createAdminToken($this->userModel->getId())->getToken();
98  }
99 
109  public function revokeAdminAccessToken($adminId)
110  {
111  $tokenCollection = $this->tokenModelCollectionFactory->create()->addFilterByAdminId($adminId);
112  if ($tokenCollection->getSize() == 0) {
113  throw new LocalizedException(__('This user has no tokens.'));
114  }
115  try {
116  foreach ($tokenCollection as $token) {
117  $token->delete();
118  }
119  } catch (\Exception $e) {
120  throw new LocalizedException(__("The tokens couldn't be revoked."));
121  }
122  return true;
123  }
124 
131  private function getRequestThrottler()
132  {
133  if (!$this->requestThrottler instanceof RequestThrottler) {
134  return \Magento\Framework\App\ObjectManager::getInstance()->get(RequestThrottler::class);
135  }
136  return $this->requestThrottler;
137  }
138 }
__()
Definition: __.php:13
__construct(TokenModelFactory $tokenModelFactory, UserModel $userModel, TokenCollectionFactory $tokenModelCollectionFactory, CredentialsValidator $validatorHelper)