RequestLog.php 3.46 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Integration\Model\ResourceModel\Oauth\Token;

use Magento\Integration\Model\Oauth\Token\RequestLog\ReaderInterface;
use Magento\Integration\Model\Oauth\Token\RequestLog\WriterInterface;
use Magento\Integration\Model\Oauth\Token\RequestLog\Config as RequestLogConfig;

/**
 * Resource model for failed authentication attempts to retrieve admin/customer token.
 */
class RequestLog extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements
    ReaderInterface,
    WriterInterface
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    private $dateTime;

    /**
     * @var RequestLogConfig
     */
    private $requestLogConfig;

    /**
     * Initialize dependencies.
     *
     * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
     * @param RequestLogConfig $requestLogConfig
     * @param string|null $connectionName
     */
    public function __construct(
        \Magento\Framework\Model\ResourceModel\Db\Context $context,
        \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
        RequestLogConfig $requestLogConfig,
        $connectionName = null
    ) {
        parent::__construct($context, $connectionName);
        $this->dateTime = $dateTime;
        $this->requestLogConfig = $requestLogConfig;
    }

    /**
     * {@inheritdoc}
     */
    protected function _construct()
    {
        $this->_init('oauth_token_request_log', 'entity_id');
    }

    /**
     * {@inheritdoc}
     */
    public function getFailuresCount($userName, $userType)
    {
        $select = $this->getConnection()->select();
        $select->from($this->getMainTable(), 'failures_count')
            ->where('user_name = :user_name AND user_type = :user_type');

        return (int)$this->getConnection()->fetchOne($select, ['user_name' => $userName, 'user_type' => $userType]);
    }

    /**
     * {@inheritdoc}
     */
    public function resetFailuresCount($userName, $userType)
    {
        $this->getConnection()->delete(
            $this->getMainTable(),
            ['user_name = ?' => $userName, 'user_type = ?' => $userType]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function incrementFailuresCount($userName, $userType)
    {
        $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
        $date->add(new \DateInterval('PT' . $this->requestLogConfig->getLockTimeout() . 'S'));
        $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);

        $this->getConnection()->insertOnDuplicate(
            $this->getMainTable(),
            [
                'user_name' => $userName,
                'user_type' => $userType,
                'failures_count' => 1,
                'lock_expires_at' => $dateTime
            ],
            [
                'failures_count' => new \Zend_Db_Expr('failures_count+1'),
                'lock_expires_at' => new \Zend_Db_Expr("'" . $dateTime . "'")
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function clearExpiredFailures()
    {
        $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
        $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
        $this->getConnection()->delete($this->getMainTable(), ['lock_expires_at <= ?' => $dateTime]);
    }
}