Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RemoveInactiveTokens.php
Go to the documentation of this file.
1 <?php
8 
12 
18 {
22  private $moduleDataSetup;
23 
28  public function __construct(
29  \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
30  ) {
31  $this->moduleDataSetup = $moduleDataSetup;
32  }
33 
37  public function apply()
38  {
39  $this->moduleDataSetup->getConnection()->startSetup();
40 
41  $this->removeRevokedTokens();
42  $this->removeTokensFromInactiveAdmins();
43  $this->removeTokensFromInactiveCustomers();
44 
45  $this->moduleDataSetup->getConnection()->endSetup();
46  }
47 
51  public static function getDependencies()
52  {
53  return [];
54  }
55 
59  public static function getVersion()
60  {
61  return '2.2.0';
62  }
63 
67  public function getAliases()
68  {
69  return [];
70  }
71 
77  private function removeRevokedTokens()
78  {
79  $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
80 
81  $where = ['revoked = ?' => 1];
82  $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
83  }
84 
90  private function removeTokensFromInactiveAdmins()
91  {
92  $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
93  $adminUserTable = $this->moduleDataSetup->getTable('admin_user');
94 
95  $select = $this->moduleDataSetup->getConnection()->select()->from(
96  $adminUserTable,
97  ['user_id', 'is_active']
98  );
99 
100  $admins = $this->moduleDataSetup->getConnection()->fetchAll($select);
101  foreach ($admins as $admin) {
102  if ($admin['is_active'] == 0) {
103  $where = ['admin_id = ?' => (int)$admin['user_id']];
104  $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
105  }
106  }
107  }
108 
114  private function removeTokensFromInactiveCustomers()
115  {
116  $oauthTokenTable = $this->moduleDataSetup->getTable('oauth_token');
117  $adminUserTable = $this->moduleDataSetup->getTable('customer_entity');
118 
119  $select = $this->moduleDataSetup->getConnection()->select()->from(
120  $adminUserTable,
121  ['entity_id', 'is_active']
122  );
123 
124  $admins = $this->moduleDataSetup->getConnection()->fetchAll($select);
125  foreach ($admins as $admin) {
126  if ($admin['is_active'] == 0) {
127  $where = ['customer_id = ?' => (int)$admin['entity_id']];
128  $this->moduleDataSetup->getConnection()->delete($oauthTokenTable, $where);
129  }
130  }
131  }
132 }
__construct(\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup)