UserConfigManager.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\Model;
  21. use Magento\Framework\Json\DecoderInterface;
  22. use Magento\Framework\Json\EncoderInterface;
  23. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  24. class UserConfigManager implements UserConfigManagerInterface
  25. {
  26. private $configurationRegistry = [];
  27. /**
  28. * @var EncoderInterface
  29. */
  30. private $encoder;
  31. /**
  32. * @var DecoderInterface
  33. */
  34. private $decoder;
  35. /**
  36. * @var UserConfigFactory
  37. */
  38. private $userConfigFactory;
  39. public function __construct(
  40. EncoderInterface $encoder,
  41. DecoderInterface $decoder,
  42. UserConfigFactory $userConfigFactory
  43. ) {
  44. $this->encoder = $encoder;
  45. $this->decoder = $decoder;
  46. $this->userConfigFactory = $userConfigFactory;
  47. }
  48. /**
  49. * Get a provider configuration for a given user
  50. * @param int $userId
  51. * @param string $providerCode
  52. * @return array
  53. */
  54. public function getProviderConfig($userId, $providerCode)
  55. {
  56. $userConfig = $this->getUserConfiguration($userId);
  57. $providersConfig = $userConfig->getData('config');
  58. if (!isset($providersConfig[$providerCode])) {
  59. return null;
  60. }
  61. return $providersConfig[$providerCode];
  62. }
  63. /**
  64. * @inheritdoc
  65. */
  66. public function setProviderConfig($userId, $providerCode, $config)
  67. {
  68. $userConfig = $this->getUserConfiguration($userId);
  69. $providersConfig = $userConfig->getData('config');
  70. if ($config === null) {
  71. if (isset($providersConfig[$providerCode])) {
  72. unset($providersConfig[$providerCode]);
  73. }
  74. } else {
  75. $providersConfig[$providerCode] = $config;
  76. }
  77. $userConfig->setData('config', $providersConfig);
  78. $userConfig->getResource()->save($userConfig);
  79. return true;
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function addProviderConfig($userId, $providerCode, $config)
  85. {
  86. $userConfig = $this->getProviderConfig($userId, $providerCode);
  87. if ($userConfig === null) {
  88. $newConfig = $config;
  89. } else {
  90. $newConfig = array_merge($userConfig, $config);
  91. }
  92. return $this->setProviderConfig($userId, $providerCode, $newConfig);
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function resetProviderConfig($userId, $providerCode)
  98. {
  99. $this->setProviderConfig($userId, $providerCode, null);
  100. return true;
  101. }
  102. /**
  103. * Get user TFA config
  104. * @param int $userId
  105. * @return UserConfig
  106. */
  107. private function getUserConfiguration($userId)
  108. {
  109. if (!isset($this->configurationRegistry[$userId])) {
  110. /** @var $userConfig UserConfig */
  111. $userConfig = $this->userConfigFactory->create();
  112. $userConfig->getResource()->load($userConfig, $userId, 'user_id');
  113. $userConfig->setData('user_id', $userId);
  114. $this->configurationRegistry[$userId] = $userConfig;
  115. }
  116. return $this->configurationRegistry[$userId];
  117. }
  118. /**
  119. * @inheritdoc
  120. */
  121. public function setProvidersCodes($userId, $providersCodes)
  122. {
  123. if (is_string($providersCodes)) {
  124. $providersCodes = preg_split('/\s*,\s*/', $providersCodes);
  125. }
  126. $userConfig = $this->getUserConfiguration($userId);
  127. $userConfig->setData('providers', $providersCodes);
  128. $userConfig->getResource()->save($userConfig);
  129. return true;
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public function getProvidersCodes($userId)
  135. {
  136. $userConfig = $this->getUserConfiguration($userId);
  137. return $userConfig->getData('providers');
  138. }
  139. /**
  140. * @inheritdoc
  141. */
  142. public function activateProviderConfiguration($userId, $providerCode)
  143. {
  144. return $this->addProviderConfig($userId, $providerCode, [
  145. UserConfigManagerInterface::ACTIVE_CONFIG_KEY => true
  146. ]);
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function isProviderConfigurationActive($userId, $providerCode)
  152. {
  153. $config = $this->getProviderConfig($userId, $providerCode);
  154. return $config &&
  155. isset($config[UserConfigManagerInterface::ACTIVE_CONFIG_KEY]) &&
  156. $config[UserConfigManagerInterface::ACTIVE_CONFIG_KEY];
  157. }
  158. /**
  159. * @inheritdoc
  160. */
  161. public function setDefaultProvider($userId, $providerCode)
  162. {
  163. $userConfig = $this->getUserConfiguration($userId);
  164. $userConfig->setData('default_provider', $providerCode);
  165. $userConfig->getResource()->save($userConfig);
  166. return true;
  167. }
  168. /**
  169. * @inheritdoc
  170. */
  171. public function getDefaultProvider($userId)
  172. {
  173. $userConfig = $this->getUserConfiguration($userId);
  174. return $userConfig->getData('default_provider');
  175. }
  176. }