首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置magento 2中通讯确认链接的过期时间

问如何设置magento 2中通讯确认链接的过期时间
EN

Stack Overflow用户
提问于 2018-05-03 05:46:29
回答 1查看 400关注 0票数 1

我在我的magento网站上有通讯。在从管理配置订阅之前,我已经启用了确认。用户在邮件中获得确认链接。

但我想设定这个链接的有效期。magento是否提供默认配置?

我如何设置该链接的到期时间?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-03 11:38:47

我找到了解决办法。我只是做了两件事。

1)在created_at表中添加newsletter_subscriber字段。

2)覆盖以下文件

vendor/magento/module-newsletter/Model/Subscriber.php

至

Company/name/Model/Subscriber.php

覆盖的Subscriber.php文件代码

代码语言:javascript
复制
public function confirm($code) // existing function
    {
        $id = $this->getId();
        if ($this->validateConfirmLinkToken($id, $code)) {
            if ($this->getCode() == $code) {
                $this->setStatus(self::STATUS_SUBSCRIBED)
                    ->setStatusChanged(true)
                    ->save();
                $this->sendConfirmationSuccessEmail();
                return true;
            }

            return false;
        }
    }

    private function validateConfirmLinkToken($customerId, $code) //check validation for token
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $messageManager = $objectManager->get('Magento\Framework\Message\ManagerInterface');
        if (empty($customerId) || $customerId < 0) {
            $this->_messageManager->addError('Sorry you have not rigts to access this page');
            return false;
        }
        if (!is_string($code) || empty($code)) {
            $params = ['fieldName' => 'code'];
            //$messageManager->addError('Sorry Your subscription confirmation code is not valid.');
            return false;
        }
        $dcode = $this->getCode();
        $dcreated_at = $this->getCreatedAt();
        if (trim($dcode) != trim($code)) {
            //$messageManager->addError('Sorry Your subscription confirmation code is mismatch.');
            return false;
        } elseif ($this->isConfirmationLinkTokenExpired($dcode, $dcreated_at)) {
            //$messageManager->addError('Sorry Your subscription confirmation code is expired.');
            return false;

        }

        return true;
    }

    public function isConfirmationLinkTokenExpired($dcode, $dcreated_at) // check expiration token
    {
        if (empty($dcode) || empty($dcreated_at)) {
            return true;
        }

        $expirationPeriod = '720';

        $currentTimestamp = (new \DateTime())->getTimestamp();
        $tokenTimestamp = (new \DateTime($dcreated_at))->getTimestamp();
        if ($tokenTimestamp > $currentTimestamp) {
            return true;
        }

        $hourDifference = floor(($currentTimestamp - $tokenTimestamp) / (60 * 60));
        if ($hourDifference >= $expirationPeriod) {
            return true;
        }

        return false;
    }

希望它能对很多人有所帮助。

谢谢。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50147775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档