ChangeProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Block;
  21. use Magento\Backend\Block\Template;
  22. use Magento\Backend\Model\Auth\Session;
  23. use Magento\Framework\Exception\LocalizedException;
  24. use Magento\User\Model\User;
  25. use MSP\TwoFactorAuth\Api\TfaInterface;
  26. use MSP\TwoFactorAuth\Api\ProviderInterface;
  27. class ChangeProvider extends Template
  28. {
  29. /**
  30. * @var TfaInterface
  31. */
  32. private $tfa;
  33. /**
  34. * @var Session
  35. */
  36. private $session;
  37. /**
  38. * ChangeProvider constructor.
  39. * @param Template\Context $context
  40. * @param Session $session
  41. * @param TfaInterface $tfa
  42. * @param array $data
  43. */
  44. public function __construct(
  45. Template\Context $context,
  46. Session $session,
  47. TfaInterface $tfa,
  48. array $data = []
  49. ) {
  50. parent::__construct($context, $data);
  51. $this->tfa = $tfa;
  52. $this->session = $session;
  53. if (!isset($data['provider'])) {
  54. throw new \InvalidArgumentException('A provider must be specified');
  55. }
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function getJsLayout()
  61. {
  62. $providers = [];
  63. foreach ($this->getProvidersList() as $provider) {
  64. $providers[] = [
  65. 'code' => $provider->getCode(),
  66. 'name' => $provider->getName(),
  67. 'auth' => $this->getUrl($provider->getAuthAction()),
  68. 'icon' => $this->getViewFileUrl($provider->getIcon()),
  69. ];
  70. }
  71. $this->jsLayout['components']['msp-twofactorauth-change-provider']['switchIcon'] =
  72. $this->getViewFileUrl('MSP_TwoFactorAuth::images/change_provider.png');
  73. $this->jsLayout['components']['msp-twofactorauth-change-provider']['providers'] = $providers;
  74. return parent::getJsLayout();
  75. }
  76. /**
  77. * Get user
  78. * @return User|null
  79. */
  80. private function getUser()
  81. {
  82. return $this->session->getUser();
  83. }
  84. /**
  85. * Get a list of available providers
  86. * @return ProviderInterface[]
  87. */
  88. private function getProvidersList()
  89. {
  90. $res = [];
  91. $providers = $this->tfa->getUserProviders($this->getUser()->getId());
  92. foreach ($providers as $provider) {
  93. if ($provider->getCode() != $this->getData('provider')) {
  94. $res[] = $provider;
  95. }
  96. }
  97. return $res;
  98. }
  99. }