Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Token.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  protected $_dateTime;
19 
25  protected $date;
26 
33  public function __construct(
34  \Magento\Framework\Model\ResourceModel\Db\Context $context,
35  \Magento\Framework\Stdlib\DateTime $dateTime,
36  \Magento\Framework\Stdlib\DateTime\DateTime $date,
37  $connectionName = null
38  ) {
39  $this->_dateTime = $dateTime;
40  $this->date = $date;
41  parent::__construct($context, $connectionName);
42  }
43 
49  protected function _construct()
50  {
51  $this->_init('oauth_token', 'entity_id');
52  }
53 
61  public function cleanOldAuthorizedTokensExcept(\Magento\Integration\Model\Oauth\Token $exceptToken)
62  {
63  if (!$exceptToken->getId() || !$exceptToken->getAuthorized()) {
64  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid token to except'));
65  }
66  $connection = $this->getConnection();
67  $where = $connection->quoteInto(
68  'authorized = 1 AND consumer_id = ?',
69  $exceptToken->getConsumerId(),
71  );
72  $where .= $connection->quoteInto(' AND entity_id <> ?', $exceptToken->getId(), \Zend_Db::INT_TYPE);
73 
74  if ($exceptToken->getCustomerId()) {
75  $where .= $connection->quoteInto(' AND customer_id = ?', $exceptToken->getCustomerId(), \Zend_Db::INT_TYPE);
76  } elseif ($exceptToken->getAdminId()) {
77  $where .= $connection->quoteInto(' AND admin_id = ?', $exceptToken->getAdminId(), \Zend_Db::INT_TYPE);
78  } else {
79  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid token to except'));
80  }
81  return $connection->delete($this->getMainTable(), $where);
82  }
83 
90  public function deleteOldEntries($minutes)
91  {
92  if ($minutes > 0) {
93  $connection = $this->getConnection();
94 
95  return $connection->delete(
96  $this->getMainTable(),
97  $connection->quoteInto(
98  'type = "' . \Magento\Integration\Model\Oauth\Token::TYPE_REQUEST . '" AND created_at <= ?',
99  $this->_dateTime->formatDate($this->date->gmtTimestamp() - $minutes * 60)
100  )
101  );
102  } else {
103  return 0;
104  }
105  }
106 
114  public function deleteExpiredTokens($hours, $userTypes)
115  {
116  if ($hours > 0) {
117  $connection = $this->getConnection();
118 
119  $userTypeCondition = $connection->quoteInto('user_type IN (?)', $userTypes);
120  $createdAtCondition = $connection->quoteInto(
121  'created_at <= ?',
122  $this->_dateTime->formatDate($this->date->gmtTimestamp() - $hours * 60 * 60)
123  );
124  return $connection->delete(
125  $this->getMainTable(),
126  $userTypeCondition . ' AND ' . $createdAtCondition
127  );
128  } else {
129  return 0;
130  }
131  }
132 
140  public function selectTokenByType($consumerId, $type)
141  {
142  $connection = $this->getConnection();
143  $select = $connection->select()
144  ->from($this->getMainTable())
145  ->where('consumer_id = ?', $consumerId)
146  ->where('type = ?', $type);
147  return $connection->fetchRow($select);
148  }
149 
157  public function selectTokenByConsumerIdAndUserType($consumerId, $userType)
158  {
159  $connection = $this->getConnection();
160  $select = $connection->select()
161  ->from($this->getMainTable())
162  ->where('consumer_id = ?', (int)$consumerId)
163  ->where('user_type = ?', (int)$userType);
164  return $connection->fetchRow($select);
165  }
166 
173  public function selectTokenByAdminId($adminId)
174  {
175  $connection = $this->getConnection();
176  $select = $connection->select()
177  ->from($this->getMainTable())
178  ->where('admin_id = ?', $adminId)
179  ->where('user_type = ?', UserContextInterface::USER_TYPE_ADMIN);
180  return $connection->fetchRow($select);
181  }
182 
190  {
191  $connection = $this->getConnection();
192  $select = $connection->select()
193  ->from($this->getMainTable())
194  ->where('customer_id = ?', $customerId)
195  ->where('user_type = ?', UserContextInterface::USER_TYPE_CUSTOMER);
196  return $connection->fetchRow($select);
197  }
198 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Stdlib\DateTime\DateTime $date, $connectionName=null)
Definition: Token.php:33
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
const INT_TYPE
Definition: Db.php:68
__()
Definition: __.php:13
$type
Definition: item.phtml:13
selectTokenByConsumerIdAndUserType($consumerId, $userType)
Definition: Token.php:157
$connection
Definition: bulk.php:13
cleanOldAuthorizedTokensExcept(\Magento\Integration\Model\Oauth\Token $exceptToken)
Definition: Token.php:61
$dateTime