Provider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /**
  21. * MageSpecialist
  22. *
  23. * NOTICE OF LICENSE
  24. *
  25. * This source file is subject to the Open Software License (OSL 3.0)
  26. * that is bundled with this package in the file LICENSE.txt.
  27. * It is also available through the world-wide-web at this URL:
  28. * http://opensource.org/licenses/osl-3.0.php
  29. * If you did not receive a copy of the license and are unable to
  30. * obtain it through the world-wide-web, please send an email
  31. * to info@magespecialist.it so we can send you a copy immediately.
  32. *
  33. * @category MSP
  34. * @package MSP_NoSpam
  35. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  36. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  37. */
  38. namespace MSP\TwoFactorAuth\Model;
  39. use MSP\TwoFactorAuth\Api\ProviderInterface;
  40. use MSP\TwoFactorAuth\Api\UserConfigManagerInterface;
  41. use MSP\TwoFactorAuth\Api\EngineInterface;
  42. class Provider implements ProviderInterface
  43. {
  44. /**
  45. * @var EngineInterface
  46. */
  47. private $engine;
  48. /**
  49. * @var string
  50. */
  51. private $code;
  52. /**
  53. * @var string
  54. */
  55. private $name;
  56. /**
  57. * @var UserConfigManagerInterface
  58. */
  59. private $userConfigManager;
  60. /**
  61. * @var string
  62. */
  63. private $configureAction;
  64. /**
  65. * @var string
  66. */
  67. private $authAction;
  68. /**
  69. * @var string[]
  70. */
  71. private $extraActions;
  72. /**
  73. * @var bool
  74. */
  75. private $canReset;
  76. /**
  77. * @var string
  78. */
  79. private $icon;
  80. public function __construct(
  81. EngineInterface $engine,
  82. UserConfigManagerInterface $userConfigManager,
  83. $code,
  84. $name,
  85. $icon,
  86. $configureAction,
  87. $authAction,
  88. $extraActions = [],
  89. $canReset = true
  90. ) {
  91. $this->engine = $engine;
  92. $this->userConfigManager = $userConfigManager;
  93. $this->code = $code;
  94. $this->name = $name;
  95. $this->configureAction = $configureAction;
  96. $this->authAction = $authAction;
  97. $this->extraActions = $extraActions;
  98. $this->canReset = $canReset;
  99. $this->icon = $icon;
  100. }
  101. /**
  102. * Return true if this provider has been enabled by admin
  103. * @return boolean
  104. */
  105. public function isEnabled()
  106. {
  107. return $this->getEngine()->isEnabled();
  108. }
  109. /**
  110. * Get provider engine
  111. * @return EngineInterface
  112. */
  113. public function getEngine()
  114. {
  115. return $this->engine;
  116. }
  117. /**
  118. * Get provider code
  119. * @return string
  120. */
  121. public function getCode()
  122. {
  123. return $this->code;
  124. }
  125. /**
  126. * Get provider name
  127. * @return string
  128. */
  129. public function getName()
  130. {
  131. return $this->name;
  132. }
  133. /**
  134. * Get provider icon
  135. * @return string
  136. */
  137. public function getIcon()
  138. {
  139. return $this->icon;
  140. }
  141. /**
  142. * Return true if this provider configuration can be reset
  143. * @return boolean
  144. */
  145. public function isResetAllowed()
  146. {
  147. return $this->canReset;
  148. }
  149. /**
  150. * Return true if this provider allows trusted devices
  151. * @return boolean
  152. */
  153. public function isTrustedDevicesAllowed()
  154. {
  155. return $this->engine->isTrustedDevicesAllowed();
  156. }
  157. /**
  158. * @inheritdoc
  159. */
  160. public function resetConfiguration($userId)
  161. {
  162. $this->userConfigManager->setProviderConfig($userId, $this->getCode(), null);
  163. return $this;
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function isConfigured($userId)
  169. {
  170. return $this->getConfiguration($userId) !== null;
  171. }
  172. /**
  173. * @inheritdoc
  174. */
  175. public function getConfiguration($userId)
  176. {
  177. return $this->userConfigManager->getProviderConfig($userId, $this->getCode());
  178. }
  179. /**
  180. * @inheritdoc
  181. */
  182. public function isActive($userId)
  183. {
  184. return $this->userConfigManager->isProviderConfigurationActive($userId, $this->getCode());
  185. }
  186. /**
  187. * @inheritdoc
  188. */
  189. public function activate($userId)
  190. {
  191. $this->userConfigManager->activateProviderConfiguration($userId, $this->getCode());
  192. return $this;
  193. }
  194. /**
  195. * Get configure action
  196. * @return string
  197. */
  198. public function getConfigureAction()
  199. {
  200. return $this->configureAction;
  201. }
  202. /**
  203. * Get auth action
  204. * @return string
  205. */
  206. public function getAuthAction()
  207. {
  208. return $this->authAction;
  209. }
  210. /**
  211. * Get allowed extra actions
  212. * @return string[]
  213. */
  214. public function getExtraActions()
  215. {
  216. return $this->extraActions;
  217. }
  218. }