Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Log.php
Go to the documentation of this file.
1 <?php
7 
14 {
19 
23  const TYPE_LOGIN = 2;
24 
30  protected $_coreDate;
31 
35  protected $_remoteAddress;
36 
43  public function __construct(
44  \Magento\Framework\Model\ResourceModel\Db\Context $context,
45  \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
46  \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
47  $connectionName = null
48  ) {
49  $this->_coreDate = $coreDate;
50  $this->_remoteAddress = $remoteAddress;
51  parent::__construct($context, $connectionName);
52  }
53 
59  protected function _construct()
60  {
61  $this->_setMainTable('captcha_log');
62  }
63 
71  public function logAttempt($login)
72  {
73  if ($login != null) {
74  $this->getConnection()->insertOnDuplicate(
75  $this->getMainTable(),
76  [
77  'type' => self::TYPE_LOGIN,
78  'value' => $login,
79  'count' => 1,
80  'updated_at' => $this->_coreDate->gmtDate()
81  ],
82  ['count' => new \Zend\Db\Sql\Expression('count+1'), 'updated_at']
83  );
84  }
85  $ip = $this->_remoteAddress->getRemoteAddress();
86  if ($ip != null) {
87  $this->getConnection()->insertOnDuplicate(
88  $this->getMainTable(),
89  [
90  'type' => self::TYPE_REMOTE_ADDRESS,
91  'value' => $ip,
92  'count' => 1,
93  'updated_at' => $this->_coreDate->gmtDate()
94  ],
95  ['count' => new \Zend\Db\Sql\Expression('count+1'), 'updated_at']
96  );
97  }
98  return $this;
99  }
100 
108  public function deleteUserAttempts($login)
109  {
110  if ($login != null) {
111  $this->getConnection()->delete(
112  $this->getMainTable(),
113  ['type = ?' => self::TYPE_LOGIN, 'value = ?' => $login]
114  );
115  }
116  $ip = $this->_remoteAddress->getRemoteAddress();
117  if ($ip != null) {
118  $this->getConnection()->delete(
119  $this->getMainTable(),
120  ['type = ?' => self::TYPE_REMOTE_ADDRESS, 'value = ?' => $ip]
121  );
122  }
123 
124  return $this;
125  }
126 
134  {
135  $ip = $this->_remoteAddress->getRemoteAddress();
136  if (!$ip) {
137  return 0;
138  }
139  $connection = $this->getConnection();
140  $select = $connection->select()->from(
141  $this->getMainTable(),
142  'count'
143  )->where(
144  'type = ?',
145  self::TYPE_REMOTE_ADDRESS
146  )->where(
147  'value = ?',
148  $ip
149  );
150  return $connection->fetchOne($select);
151  }
152 
160  public function countAttemptsByUserLogin($login)
161  {
162  if (!$login) {
163  return 0;
164  }
165  $connection = $this->getConnection();
166  $select = $connection->select()->from(
167  $this->getMainTable(),
168  'count'
169  )->where(
170  'type = ?',
171  self::TYPE_LOGIN
172  )->where(
173  'value = ?',
174  $login
175  );
176  return $connection->fetchOne($select);
177  }
178 
185  public function deleteOldAttempts()
186  {
187  $this->getConnection()->delete(
188  $this->getMainTable(),
189  ['updated_at < ?' => $this->_coreDate->gmtDate(null, time() - 60 * 30)]
190  );
191  }
192 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $coreDate, \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress, $connectionName=null)
Definition: Log.php:43
$connection
Definition: bulk.php:13
_setMainTable($mainTable, $idFieldName=null)
Definition: AbstractDb.php:230