RemoveInactiveTokens.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Setup\Patch\Data;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Setup\Patch\DataPatchInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. /**
  11. * Class RemoveInactiveTokens
  12. * @package Magento\Integration\Setup\Patch
  13. */
  14. class RemoveInactiveTokens implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * PatchInitial constructor.
  22. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  23. */
  24. public function __construct(
  25. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  26. ) {
  27. $this->moduleDataSetup = $moduleDataSetup;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function apply()
  33. {
  34. $this->moduleDataSetup->getConnection()->startSetup();
  35. $this->removeRevokedTokens();
  36. $this->removeTokensFromInactiveAdmins();
  37. $this->removeTokensFromInactiveCustomers();
  38. $this->moduleDataSetup->getConnection()->endSetup();
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function getDependencies()
  44. {
  45. return [];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public static function getVersion()
  51. {
  52. return '2.2.0';
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getAliases()
  58. {
  59. return [];
  60. }
  61. /**
  62. * Remove revoked tokens.
  63. *
  64. * @return void
  65. */
  66. private function removeRevokedTokens()
  67. {
  68. $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
  69. $where = ['revoked = ?' => 1];
  70. $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
  71. }
  72. /**
  73. * Remove inactive admin users tokens
  74. *
  75. * @return void
  76. */
  77. private function removeTokensFromInactiveAdmins()
  78. {
  79. $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
  80. $adminUserTable = $this->moduleDataSetup->getTable('admin_user');
  81. $select = $this->moduleDataSetup->getConnection()->select()->from(
  82. $adminUserTable,
  83. ['user_id', 'is_active']
  84. );
  85. $admins = $this->moduleDataSetup->getConnection()->fetchAll($select);
  86. foreach ($admins as $admin) {
  87. if ($admin['is_active'] == 0) {
  88. $where = ['admin_id = ?' => (int)$admin['user_id']];
  89. $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
  90. }
  91. }
  92. }
  93. /**
  94. * Remove tokens for inactive customers
  95. *
  96. * @return void
  97. */
  98. private function removeTokensFromInactiveCustomers()
  99. {
  100. $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
  101. $adminUserTable = $this->moduleDataSetup->getTable('customer_entity');
  102. $select = $this->moduleDataSetup->getConnection()->select()->from(
  103. $adminUserTable,
  104. ['entity_id', 'is_active']
  105. );
  106. $admins = $this->moduleDataSetup->getConnection()->fetchAll($select);
  107. foreach ($admins as $admin) {
  108. if ($admin['is_active'] == 0) {
  109. $where = ['customer_id = ?' => (int)$admin['entity_id']];
  110. $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
  111. }
  112. }
  113. }
  114. }