User.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Model;
  7. use Magento\Backend\Model\Auth\Credential\StorageInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Model\AbstractModel;
  10. use Magento\Framework\Exception\AuthenticationException;
  11. use Magento\Framework\Serialize\Serializer\Json;
  12. use Magento\User\Api\Data\UserInterface;
  13. use Magento\User\Model\Spi\NotificationExceptionInterface;
  14. use Magento\User\Model\Spi\NotificatorInterface;
  15. use Magento\Framework\App\DeploymentConfig;
  16. /**
  17. * Admin user model
  18. *
  19. * @api
  20. * @method string getLogdate()
  21. * @method \Magento\User\Model\User setLogdate(string $value)
  22. * @method int getLognum()
  23. * @method \Magento\User\Model\User setLognum(int $value)
  24. * @method int getReloadAclFlag()
  25. * @method \Magento\User\Model\User setReloadAclFlag(int $value)
  26. * @method string getExtra()
  27. * @method \Magento\User\Model\User setExtra(string $value)
  28. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  29. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  30. * @SuppressWarnings(PHPMD.LongVariable)
  31. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  32. * @api
  33. * @since 100.0.2
  34. */
  35. class User extends AbstractModel implements StorageInterface, UserInterface
  36. {
  37. /**
  38. * @deprecated
  39. * @see \Magento\User\Model\Spi\NotificatorInterface
  40. */
  41. const XML_PATH_FORGOT_EMAIL_TEMPLATE = 'admin/emails/forgot_email_template';
  42. /**
  43. * @deprecated
  44. * @see \Magento\User\Model\Spi\NotificatorInterface
  45. */
  46. const XML_PATH_FORGOT_EMAIL_IDENTITY = 'admin/emails/forgot_email_identity';
  47. /**
  48. * @deprecated
  49. * @see \Magento\User\Model\Spi\NotificatorInterface
  50. */
  51. const XML_PATH_USER_NOTIFICATION_TEMPLATE = 'admin/emails/user_notification_template';
  52. /** @deprecated */
  53. const XML_PATH_RESET_PASSWORD_TEMPLATE = 'admin/emails/reset_password_template';
  54. const MESSAGE_ID_PASSWORD_EXPIRED = 'magento_user_password_expired';
  55. /**
  56. * Model event prefix
  57. *
  58. * @var string
  59. */
  60. protected $_eventPrefix = 'admin_user';
  61. /**
  62. * Admin role
  63. *
  64. * @var \Magento\Authorization\Model\Role
  65. */
  66. protected $_role;
  67. /**
  68. * Available resources flag
  69. *
  70. * @var bool
  71. */
  72. protected $_hasResources = true;
  73. /**
  74. * User data
  75. *
  76. * @var \Magento\User\Helper\Data
  77. */
  78. protected $_userData = null;
  79. /**
  80. * Core store config
  81. *
  82. * @var \Magento\Backend\App\ConfigInterface
  83. */
  84. protected $_config;
  85. /**
  86. * Factory for validator composite object
  87. *
  88. * @var \Magento\Framework\Validator\DataObjectFactory
  89. */
  90. protected $_validatorObject;
  91. /**
  92. * Role model factory
  93. *
  94. * @var \Magento\Authorization\Model\RoleFactory
  95. */
  96. protected $_roleFactory;
  97. /**
  98. * @var \Magento\Framework\Encryption\EncryptorInterface
  99. */
  100. protected $_encryptor;
  101. /**
  102. * @deprecated 101.1.0
  103. */
  104. protected $_transportBuilder;
  105. /**
  106. * @deprecated 101.1.0
  107. */
  108. protected $_storeManager;
  109. /**
  110. * @var UserValidationRules
  111. */
  112. protected $validationRules;
  113. /**
  114. * @var Json
  115. */
  116. private $serializer;
  117. /**
  118. * @var NotificatorInterface
  119. */
  120. private $notificator;
  121. /**
  122. * @deprecated 101.1.0
  123. */
  124. private $deploymentConfig;
  125. /**
  126. * @param \Magento\Framework\Model\Context $context
  127. * @param \Magento\Framework\Registry $registry
  128. * @param \Magento\User\Helper\Data $userData
  129. * @param \Magento\Backend\App\ConfigInterface $config
  130. * @param \Magento\Framework\Validator\DataObjectFactory $validatorObjectFactory
  131. * @param \Magento\Authorization\Model\RoleFactory $roleFactory
  132. * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
  133. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  134. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  135. * @param UserValidationRules $validationRules
  136. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  137. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  138. * @param array $data
  139. * @param Json $serializer
  140. * @param DeploymentConfig|null $deploymentConfig
  141. * @param NotificatorInterface|null $notificator
  142. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  143. */
  144. public function __construct(
  145. \Magento\Framework\Model\Context $context,
  146. \Magento\Framework\Registry $registry,
  147. \Magento\User\Helper\Data $userData,
  148. \Magento\Backend\App\ConfigInterface $config,
  149. \Magento\Framework\Validator\DataObjectFactory $validatorObjectFactory,
  150. \Magento\Authorization\Model\RoleFactory $roleFactory,
  151. \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
  152. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  153. \Magento\Store\Model\StoreManagerInterface $storeManager,
  154. UserValidationRules $validationRules,
  155. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  156. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  157. array $data = [],
  158. Json $serializer = null,
  159. DeploymentConfig $deploymentConfig = null,
  160. ?NotificatorInterface $notificator = null
  161. ) {
  162. $this->_encryptor = $encryptor;
  163. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  164. $this->_userData = $userData;
  165. $this->_config = $config;
  166. $this->_validatorObject = $validatorObjectFactory;
  167. $this->_roleFactory = $roleFactory;
  168. $this->_transportBuilder = $transportBuilder;
  169. $this->_storeManager = $storeManager;
  170. $this->validationRules = $validationRules;
  171. $this->serializer = $serializer
  172. ?: ObjectManager::getInstance()->get(Json::class);
  173. $this->deploymentConfig = $deploymentConfig
  174. ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
  175. $this->notificator = $notificator
  176. ?: ObjectManager::getInstance()->get(NotificatorInterface::class);
  177. }
  178. /**
  179. * Initialize user model
  180. *
  181. * @return void
  182. */
  183. protected function _construct()
  184. {
  185. $this->_init(\Magento\User\Model\ResourceModel\User::class);
  186. }
  187. /**
  188. * Removing dependencies and leaving only entity's properties.
  189. *
  190. * @return string[]
  191. */
  192. public function __sleep()
  193. {
  194. $properties = parent::__sleep();
  195. return array_diff(
  196. $properties,
  197. [
  198. '_eventManager',
  199. '_userData',
  200. '_config',
  201. '_validatorObject',
  202. '_roleFactory',
  203. '_encryptor',
  204. '_transportBuilder',
  205. '_storeManager',
  206. '_validatorBeforeSave',
  207. 'validationRules',
  208. 'serializer',
  209. 'deploymentConfig',
  210. 'notificator'
  211. ]
  212. );
  213. }
  214. /**
  215. * Restoring required objects after serialization.
  216. *
  217. * @return void
  218. */
  219. public function __wakeup()
  220. {
  221. parent::__wakeup();
  222. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  223. $this->serializer = $objectManager->get(Json::class);
  224. $this->_eventManager = $objectManager->get(\Magento\Framework\Event\ManagerInterface::class);
  225. $this->_userData = $objectManager->get(\Magento\User\Helper\Data::class);
  226. $this->_config = $objectManager->get(\Magento\Backend\App\ConfigInterface::class);
  227. $this->_registry = $objectManager->get(\Magento\Framework\Registry::class);
  228. $this->_validatorObject = $objectManager->get(\Magento\Framework\Validator\DataObjectFactory::class);
  229. $this->_roleFactory = $objectManager->get(\Magento\Authorization\Model\RoleFactory::class);
  230. $this->_encryptor = $objectManager->get(\Magento\Framework\Encryption\EncryptorInterface::class);
  231. $this->_transportBuilder = $objectManager->get(\Magento\Framework\Mail\Template\TransportBuilder::class);
  232. $this->_storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
  233. $this->validationRules = $objectManager->get(UserValidationRules::class);
  234. $this->deploymentConfig = $objectManager->get(DeploymentConfig::class);
  235. $this->notificator = $objectManager->get(NotificatorInterface::class);
  236. }
  237. /**
  238. * Processing data before model save
  239. *
  240. * @return $this
  241. */
  242. public function beforeSave()
  243. {
  244. $data = [
  245. 'extra' => $this->serializer->serialize($this->getExtra()),
  246. ];
  247. if ($this->_willSavePassword()) {
  248. $data['password'] = $this->_getEncodedPassword($this->getPassword());
  249. }
  250. if ($this->getIsActive() !== null) {
  251. $data['is_active'] = (int)$this->getIsActive();
  252. }
  253. $this->addData($data);
  254. return parent::beforeSave();
  255. }
  256. /**
  257. * Whether the password saving is going to occur
  258. *
  259. * @return bool
  260. */
  261. protected function _willSavePassword()
  262. {
  263. return $this->isObjectNew() || $this->hasData('password') && $this->dataHasChangedFor('password');
  264. }
  265. /**
  266. * Add validation rules for particular fields
  267. *
  268. * @return \Zend_Validate_Interface
  269. */
  270. protected function _getValidationRulesBeforeSave()
  271. {
  272. /** @var $validator \Magento\Framework\Validator\DataObject */
  273. $validator = $this->_validatorObject->create();
  274. $this->validationRules->addUserInfoRules($validator);
  275. // Add validation rules for the password management fields
  276. if ($this->_willSavePassword()) {
  277. $this->validationRules->addPasswordRules($validator);
  278. if ($this->hasPasswordConfirmation()) {
  279. $this->validationRules->addPasswordConfirmationRule($validator, $this->getPasswordConfirmation());
  280. }
  281. }
  282. return $validator;
  283. }
  284. /**
  285. * Validate admin user data.
  286. *
  287. * Existing user password confirmation will be validated only when password is set
  288. *
  289. * @return bool|string[]
  290. */
  291. public function validate()
  292. {
  293. /** @var $validator \Magento\Framework\Validator\DataObject */
  294. $validator = $this->_validatorObject->create();
  295. $this->validationRules->addUserInfoRules($validator);
  296. if (!$validator->isValid($this)) {
  297. return $validator->getMessages();
  298. }
  299. return $this->validatePasswordChange();
  300. }
  301. /**
  302. * Make sure admin password was changed.
  303. *
  304. * New password is compared to at least 4 previous passwords to prevent setting them again
  305. *
  306. * @return bool|string[]
  307. * @since 100.0.3
  308. */
  309. protected function validatePasswordChange()
  310. {
  311. $password = $this->getPassword();
  312. if ($password && !$this->getForceNewPassword() && $this->getId()) {
  313. $errorMessage = __('Sorry, but this password has already been used. Please create another.');
  314. // Check if password is equal to the current one
  315. if ($this->_encryptor->isValidHash($password, $this->getOrigData('password'))) {
  316. return [$errorMessage];
  317. }
  318. // Check whether password was used before
  319. foreach ($this->getResource()->getOldPasswords($this) as $oldPasswordHash) {
  320. if ($this->_encryptor->isValidHash($password, $oldPasswordHash)) {
  321. return [$errorMessage];
  322. }
  323. }
  324. }
  325. return true;
  326. }
  327. /**
  328. * Process data after model is saved
  329. *
  330. * @return $this
  331. */
  332. public function afterSave()
  333. {
  334. $this->_role = null;
  335. return parent::afterSave();
  336. }
  337. /**
  338. * Save admin user extra data (like configuration sections state)
  339. *
  340. * @param array $data
  341. * @return $this
  342. */
  343. public function saveExtra($data)
  344. {
  345. if (is_array($data)) {
  346. $data = $this->serializer->serialize($data);
  347. }
  348. $this->_getResource()->saveExtra($this, $data);
  349. return $this;
  350. }
  351. /**
  352. * Retrieve user roles
  353. *
  354. * @return array
  355. */
  356. public function getRoles()
  357. {
  358. return $this->_getResource()->getRoles($this);
  359. }
  360. /**
  361. * Get admin role model
  362. *
  363. * @return \Magento\Authorization\Model\Role
  364. */
  365. public function getRole()
  366. {
  367. if (null === $this->_role) {
  368. $this->_role = $this->_roleFactory->create();
  369. $roles = $this->getRoles();
  370. if ($roles && isset($roles[0]) && $roles[0]) {
  371. $this->_role->load($roles[0]);
  372. }
  373. }
  374. return $this->_role;
  375. }
  376. /**
  377. * Unassign user from his current role
  378. *
  379. * @return $this
  380. */
  381. public function deleteFromRole()
  382. {
  383. $this->_getResource()->deleteFromRole($this);
  384. return $this;
  385. }
  386. /**
  387. * Check if such combination role/user exists.
  388. *
  389. * @return bool
  390. */
  391. public function roleUserExists()
  392. {
  393. $result = $this->_getResource()->roleUserExists($this);
  394. return is_array($result) && count($result) > 0 ? true : false;
  395. }
  396. /**
  397. * Send email with reset password confirmation link.
  398. *
  399. * @deprecated 101.1.0
  400. * @see NotificatorInterface::sendForgotPassword()
  401. *
  402. * @return $this
  403. */
  404. public function sendPasswordResetConfirmationEmail()
  405. {
  406. $this->notificator->sendForgotPassword($this);
  407. return $this;
  408. }
  409. /**
  410. * Send email to when password is resetting
  411. *
  412. * @throws NotificationExceptionInterface
  413. * @return $this
  414. * @deprecated 100.1.0
  415. */
  416. public function sendPasswordResetNotificationEmail()
  417. {
  418. $this->sendNotificationEmailsIfRequired();
  419. return $this;
  420. }
  421. /**
  422. * Check changes and send notification emails.
  423. *
  424. * @throws NotificationExceptionInterface
  425. * @return $this
  426. * @since 100.1.0
  427. */
  428. public function sendNotificationEmailsIfRequired()
  429. {
  430. if ($this->isObjectNew()) {
  431. //Notification about a new user.
  432. $this->notificator->sendCreated($this);
  433. } elseif ($changes = $this->createChangesDescriptionString()) {
  434. //User changed.
  435. $this->notificator->sendUpdated($this, explode(', ', $changes));
  436. }
  437. return $this;
  438. }
  439. /**
  440. * Create changes description string
  441. *
  442. * @return string
  443. * @since 100.1.0
  444. */
  445. protected function createChangesDescriptionString()
  446. {
  447. $changes = [];
  448. if ($this->getEmail() != $this->getOrigData('email') && $this->getOrigData('email')) {
  449. $changes[] = __('email');
  450. }
  451. if ($this->getPassword()
  452. && $this->getOrigData('password')
  453. && $this->getPassword() != $this->getOrigData('password')) {
  454. $changes[] = __('password');
  455. }
  456. if ($this->getUserName() != $this->getOrigData('username') && $this->getOrigData('username')) {
  457. $changes[] = __('username');
  458. }
  459. return implode(', ', $changes);
  460. }
  461. /**
  462. * Send user notification email.
  463. *
  464. * @param string $changes
  465. * @param string $email
  466. * @throws NotificationExceptionInterface
  467. * @return $this
  468. * @since 100.1.0
  469. * @deprecated 101.1.0
  470. * @see NotificatorInterface::sendUpdated()
  471. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  472. */
  473. protected function sendUserNotificationEmail($changes, $email = null)
  474. {
  475. $this->notificator->sendUpdated($this, explode(', ', $changes));
  476. return $this;
  477. }
  478. /**
  479. * Retrieve user name
  480. *
  481. * @param string $separator
  482. * @return string
  483. */
  484. public function getName($separator = ' ')
  485. {
  486. return $this->getFirstName() . $separator . $this->getLastName();
  487. }
  488. /**
  489. * Get user ACL role
  490. *
  491. * @return string
  492. */
  493. public function getAclRole()
  494. {
  495. return $this->getRole()->getId();
  496. }
  497. /**
  498. * Authenticate user name and password and save loaded record
  499. *
  500. * @param string $username
  501. * @param string $password
  502. * @return bool
  503. * @throws \Magento\Framework\Exception\LocalizedException
  504. */
  505. public function authenticate($username, $password)
  506. {
  507. $config = $this->_config->isSetFlag('admin/security/use_case_sensitive_login');
  508. $result = false;
  509. try {
  510. $this->_eventManager->dispatch(
  511. 'admin_user_authenticate_before',
  512. ['username' => $username, 'user' => $this]
  513. );
  514. $this->loadByUsername($username);
  515. $sensitive = $config ? $username == $this->getUserName() : true;
  516. if ($sensitive && $this->getId()) {
  517. $result = $this->verifyIdentity($password);
  518. }
  519. $this->_eventManager->dispatch(
  520. 'admin_user_authenticate_after',
  521. ['username' => $username, 'password' => $password, 'user' => $this, 'result' => $result]
  522. );
  523. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  524. $this->unsetData();
  525. throw $e;
  526. }
  527. if (!$result) {
  528. $this->unsetData();
  529. }
  530. return $result;
  531. }
  532. /**
  533. * Ensure that provided password matches the current user password. Check if the current user account is active.
  534. *
  535. * @param string $password
  536. * @return bool
  537. * @throws \Magento\Framework\Exception\AuthenticationException
  538. */
  539. public function verifyIdentity($password)
  540. {
  541. $result = false;
  542. if ($this->_encryptor->validateHash($password, $this->getPassword())) {
  543. if ($this->getIsActive() != '1') {
  544. throw new AuthenticationException(
  545. __(
  546. 'The account sign-in was incorrect or your account is disabled temporarily. '
  547. . 'Please wait and try again later.'
  548. )
  549. );
  550. }
  551. if (!$this->hasAssigned2Role($this->getId())) {
  552. throw new AuthenticationException(__('More permissions are needed to access this.'));
  553. }
  554. $result = true;
  555. }
  556. return $result;
  557. }
  558. /**
  559. * Login user
  560. *
  561. * @param string $username
  562. * @param string $password
  563. * @return $this
  564. */
  565. public function login($username, $password)
  566. {
  567. if ($this->authenticate($username, $password)) {
  568. $this->getResource()->recordLogin($this);
  569. }
  570. return $this;
  571. }
  572. /**
  573. * Reload current user
  574. *
  575. * @return $this
  576. */
  577. public function reload()
  578. {
  579. $userId = $this->getId();
  580. $this->setId(null);
  581. $this->load($userId);
  582. return $this;
  583. }
  584. /**
  585. * Load user by its username
  586. *
  587. * @param string $username
  588. * @return $this
  589. */
  590. public function loadByUsername($username)
  591. {
  592. $data = $this->getResource()->loadByUsername($username);
  593. if ($data !== false) {
  594. $this->setData($data);
  595. $this->setOrigData();
  596. }
  597. return $this;
  598. }
  599. /**
  600. * Check if user is assigned to any role
  601. *
  602. * @param int|\Magento\User\Model\User $user
  603. * @return null|array
  604. */
  605. public function hasAssigned2Role($user)
  606. {
  607. return $this->getResource()->hasAssigned2Role($user);
  608. }
  609. /**
  610. * Retrieve encoded password
  611. *
  612. * @param string $password
  613. * @return string
  614. */
  615. protected function _getEncodedPassword($password)
  616. {
  617. return $this->_encryptor->getHash($password, true);
  618. }
  619. /**
  620. * Change reset password link token
  621. *
  622. * Stores new reset password link token and its creation time
  623. *
  624. * @param string $newToken
  625. * @return $this
  626. * @throws \Magento\Framework\Exception\LocalizedException
  627. */
  628. public function changeResetPasswordLinkToken($newToken)
  629. {
  630. if (!is_string($newToken) || empty($newToken)) {
  631. throw new \Magento\Framework\Exception\LocalizedException(
  632. __('The password reset token is incorrect. Verify the token and try again.')
  633. );
  634. }
  635. $this->setRpToken($newToken);
  636. $this->setRpTokenCreatedAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
  637. return $this;
  638. }
  639. /**
  640. * Check if current reset password link token is expired
  641. *
  642. * @return bool
  643. */
  644. public function isResetPasswordLinkTokenExpired()
  645. {
  646. $linkToken = $this->getRpToken();
  647. $linkTokenCreatedAt = $this->getRpTokenCreatedAt();
  648. if (empty($linkToken) || empty($linkTokenCreatedAt)) {
  649. return true;
  650. }
  651. $expirationPeriod = $this->_userData->getResetPasswordLinkExpirationPeriod();
  652. $currentTimestamp = (new \DateTime())->getTimestamp();
  653. $tokenTimestamp = (new \DateTime($linkTokenCreatedAt))->getTimestamp();
  654. if ($tokenTimestamp > $currentTimestamp) {
  655. return true;
  656. }
  657. $hourDifference = floor(($currentTimestamp - $tokenTimestamp) / (60 * 60));
  658. if ($hourDifference >= $expirationPeriod) {
  659. return true;
  660. }
  661. return false;
  662. }
  663. /**
  664. * Check if user has available resources
  665. *
  666. * @return bool
  667. */
  668. public function hasAvailableResources()
  669. {
  670. return $this->_hasResources;
  671. }
  672. /**
  673. * Set user has available resources
  674. *
  675. * @param bool $hasResources
  676. * @return $this
  677. */
  678. public function setHasAvailableResources($hasResources)
  679. {
  680. $this->_hasResources = $hasResources;
  681. return $this;
  682. }
  683. /**
  684. * @inheritDoc
  685. */
  686. public function getFirstName()
  687. {
  688. return $this->_getData('firstname');
  689. }
  690. /**
  691. * @inheritDoc
  692. */
  693. public function setFirstName($firstName)
  694. {
  695. return $this->setData('firstname', $firstName);
  696. }
  697. /**
  698. * @inheritDoc
  699. */
  700. public function getLastName()
  701. {
  702. return $this->_getData('lastname');
  703. }
  704. /**
  705. * @inheritDoc
  706. */
  707. public function setLastName($lastName)
  708. {
  709. return $this->setData('lastname', $lastName);
  710. }
  711. /**
  712. * @inheritDoc
  713. */
  714. public function getEmail()
  715. {
  716. return $this->_getData('email');
  717. }
  718. /**
  719. * @inheritDoc
  720. */
  721. public function setEmail($email)
  722. {
  723. return $this->setData('email', $email);
  724. }
  725. /**
  726. * @inheritDoc
  727. */
  728. public function getUserName()
  729. {
  730. return $this->_getData('username');
  731. }
  732. /**
  733. * @inheritDoc
  734. */
  735. public function setUserName($userName)
  736. {
  737. return $this->setData('username', $userName);
  738. }
  739. /**
  740. * @inheritDoc
  741. */
  742. public function getPassword()
  743. {
  744. return $this->_getData('password');
  745. }
  746. /**
  747. * @inheritDoc
  748. */
  749. public function setPassword($password)
  750. {
  751. return $this->setData('password', $password);
  752. }
  753. /**
  754. * @inheritDoc
  755. */
  756. public function getCreated()
  757. {
  758. return $this->_getData('created');
  759. }
  760. /**
  761. * @inheritDoc
  762. */
  763. public function setCreated($created)
  764. {
  765. return $this->setData('created', $created);
  766. }
  767. /**
  768. * @inheritDoc
  769. */
  770. public function getModified()
  771. {
  772. return $this->_getData('modified');
  773. }
  774. /**
  775. * @inheritDoc
  776. */
  777. public function setModified($modified)
  778. {
  779. return $this->setData('modified', $modified);
  780. }
  781. /**
  782. * @inheritDoc
  783. */
  784. public function getIsActive()
  785. {
  786. return $this->_getData('is_active');
  787. }
  788. /**
  789. * @inheritDoc
  790. */
  791. public function setIsActive($isActive)
  792. {
  793. return $this->setData('is_active', $isActive);
  794. }
  795. /**
  796. * @inheritDoc
  797. */
  798. public function getInterfaceLocale()
  799. {
  800. return $this->_getData('interface_locale');
  801. }
  802. /**
  803. * @inheritDoc
  804. */
  805. public function setInterfaceLocale($interfaceLocale)
  806. {
  807. return $this->setData('interface_locale', $interfaceLocale);
  808. }
  809. /**
  810. * Security check for admin user
  811. *
  812. * @param string $passwordString
  813. * @return $this
  814. * @throws \Magento\Framework\Exception\State\UserLockedException
  815. * @throws \Magento\Framework\Exception\AuthenticationException
  816. * @since 100.1.0
  817. */
  818. public function performIdentityCheck($passwordString)
  819. {
  820. try {
  821. $isCheckSuccessful = $this->verifyIdentity($passwordString);
  822. } catch (\Magento\Framework\Exception\AuthenticationException $e) {
  823. $isCheckSuccessful = false;
  824. }
  825. $this->_eventManager->dispatch(
  826. 'admin_user_authenticate_after',
  827. [
  828. 'username' => $this->getUserName(),
  829. 'password' => $passwordString,
  830. 'user' => $this,
  831. 'result' => $isCheckSuccessful
  832. ]
  833. );
  834. // Check if lock information has been updated in observers
  835. $clonedUser = clone($this);
  836. $clonedUser->reload();
  837. if ($clonedUser->getLockExpires()) {
  838. throw new \Magento\Framework\Exception\State\UserLockedException(
  839. __('Your account is temporarily disabled. Please try again later.')
  840. );
  841. }
  842. if (!$isCheckSuccessful) {
  843. throw new \Magento\Framework\Exception\AuthenticationException(
  844. __('The password entered for the current user is invalid. Verify the password and try again.')
  845. );
  846. }
  847. return $this;
  848. }
  849. }