155 lines
4.3 KiB
PHP
155 lines
4.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of EC-CUBE
|
|
*
|
|
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
|
|
*
|
|
* http://www.ec-cube.co.jp/
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Plugin\Passkeys\Repository;
|
|
|
|
use Carbon\Carbon;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Eccube\Entity\Customer;
|
|
use Eccube\Repository\AbstractRepository;
|
|
use Eccube\Util\StringUtil;
|
|
use Plugin\Passkeys\Entity\PasskeysCustomerCookie;
|
|
|
|
/**
|
|
* PasskeysAuthConfigRepository.
|
|
*
|
|
* This class was generated by the Doctrine ORM. Add your own custom
|
|
* repository methods below.
|
|
*/
|
|
class PasskeysAuthCustomerCookieRepository extends AbstractRepository
|
|
{
|
|
/**
|
|
* PasskeysAuthConfigRepository constructor.
|
|
*
|
|
* @param ManagerRegistry $registry
|
|
*/
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, PasskeysCustomerCookie::class);
|
|
}
|
|
|
|
/**
|
|
* 2段階認証クッキーの発行
|
|
*
|
|
* @param Customer $customer
|
|
* @param string $cookieName
|
|
* @param int $expireSeconds
|
|
* @param int $CookieValueCharacterLength
|
|
*
|
|
* @return PasskeysCustomerCookie
|
|
*/
|
|
public function generateCookieData(
|
|
Customer $customer,
|
|
string $cookieName,
|
|
int $expireSeconds,
|
|
int $CookieValueCharacterLength
|
|
): PasskeysCustomerCookie {
|
|
/** @var PasskeysCustomerCookie[]|null $previousCookies */
|
|
|
|
$previousCookies = $this->findOldCookies($customer, $cookieName);
|
|
foreach ($previousCookies as $cookie) {
|
|
$this->getEntityManager()->remove($cookie);
|
|
}
|
|
$this->getEntityManager()->flush();
|
|
|
|
$cookie = new PasskeysCustomerCookie();
|
|
$cookie->setCookieName($cookieName);
|
|
$cookie->setCookieValue(StringUtil::random($CookieValueCharacterLength));
|
|
$cookie->setCookieExpireDate($expireSeconds != 0 ? Carbon::now()->addSeconds($expireSeconds) : null);
|
|
$cookie->setCustomer($customer);
|
|
$cookie->updatedTimestamps();
|
|
|
|
return $cookie;
|
|
}
|
|
|
|
/**
|
|
* 過去のクッキーデータの取得
|
|
*
|
|
* @param Customer $customer
|
|
* @param string $cookieName
|
|
*
|
|
* @return float|int|mixed|string
|
|
*/
|
|
public function findOldCookies(Customer $customer, string $cookieName)
|
|
{
|
|
$expireDate = Carbon::now()->setTimezone('UTC')->format('Y-m-d H:i:s');
|
|
|
|
return $this->createQueryBuilder('tfcc')
|
|
->where('tfcc.Customer = :customer_id')
|
|
->andWhere('tfcc.cookie_name = :cookie_name')
|
|
->andWhere('tfcc.cookie_expire_date < :expire_date')
|
|
->setParameters([
|
|
'customer_id' => $customer->getId(),
|
|
'cookie_name' => $cookieName,
|
|
'expire_date' => $expireDate,
|
|
])
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/**
|
|
* @return PasskeysCustomerCookie|null $result
|
|
*/
|
|
public function findOne()
|
|
{
|
|
return $this->findOneBy([], ['id' => 'DESC']);
|
|
}
|
|
|
|
/***
|
|
* 有効クッキーを取得する
|
|
*
|
|
* @param Customer $customer
|
|
* @param string $cookieName
|
|
* @return PasskeysCustomerCookie[]|null
|
|
*/
|
|
public function searchForCookie(Customer $customer, string $cookieName)
|
|
{
|
|
$expireDate = Carbon::now()->setTimezone('UTC')->format('Y-m-d H:i:s');
|
|
|
|
return $this->createQueryBuilder('tfcc')
|
|
->where('tfcc.Customer = :customer_id')
|
|
->andWhere('tfcc.cookie_name = :cookie_name')
|
|
->andWhere('tfcc.cookie_expire_date > :expire_date')
|
|
->setParameters([
|
|
'customer_id' => $customer->getId(),
|
|
'cookie_name' => $cookieName,
|
|
'expire_date' => $expireDate,
|
|
])
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/***
|
|
* 会員のクッキーを削除
|
|
*
|
|
* @param Customer $customer
|
|
*/
|
|
public function deleteByCustomer(Customer $customer)
|
|
{
|
|
$em = $this->getEntityManager();
|
|
$em->beginTransaction();
|
|
|
|
$this->createQueryBuilder('tfcc')
|
|
->delete()
|
|
->where('tfcc.Customer = :customer')
|
|
->setParameter('customer', $customer)
|
|
->getQuery()
|
|
->execute();
|
|
|
|
$em->flush();
|
|
|
|
$em->commit();
|
|
}
|
|
|
|
}
|