AdminUserSaveAfter.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\TwoFactorAuth\Observer;
  21. use Magento\Framework\AuthorizationInterface;
  22. use Magento\Framework\Event\Observer;
  23. use Magento\Framework\Event\ObserverInterface;
  24. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  25. class AdminUserSaveAfter implements ObserverInterface
  26. {
  27. /**
  28. * @var UserConfigManagerInterface
  29. */
  30. private $userConfigManager;
  31. /**
  32. * @var AuthorizationInterface
  33. */
  34. private $authorization;
  35. public function __construct(
  36. UserConfigManagerInterface $userConfigManager,
  37. AuthorizationInterface $authorization
  38. ) {
  39. $this->userConfigManager = $userConfigManager;
  40. $this->authorization = $authorization;
  41. }
  42. /**
  43. * @param Observer $observer
  44. * @return void
  45. * @throws \Magento\Framework\Exception\NoSuchEntityException
  46. */
  47. public function execute(Observer $observer)
  48. {
  49. if ($this->authorization->isAllowed('MSP_TwoFactorAuth::tfa')) {
  50. $user = $observer->getEvent()->getObject();
  51. $data = $user->getData();
  52. if (isset($data['msp_tfa_providers'])) {
  53. if (!is_array($data['msp_tfa_providers'])) {
  54. $data['msp_tfa_providers'] = [];
  55. }
  56. $this->userConfigManager->setProvidersCodes($user->getId(), $data['msp_tfa_providers']);
  57. }
  58. }
  59. }
  60. }