<?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 Customize\Controller;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Customize\Form\Type\Front\ForgotType;
use Eccube\Form\Type\Front\PasswordResetType;
use Eccube\Repository\CustomerRepository;
use Customize\Service\MailService;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception as HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Validator\ValidatorInterface;
//追加
use Eccube\Controller\AbstractController;
use Eccube\Service\CartService;
use Customize\Repository\CategoryRepository;
class ForgotController extends AbstractController
{
protected $categoryRepository;
protected $cartService;
protected $validator;
protected $mailService;
protected $customerRepository;
protected $encoderFactory;
public function __construct(
CategoryRepository $categoryRepository,
CartService $cartService,
ValidatorInterface $validator,
MailService $mailService,
CustomerRepository $customerRepository,
EncoderFactoryInterface $encoderFactory
) {
$this->categoryRepository = $categoryRepository;
$this->cartService = $cartService;
$this->validator = $validator;
$this->mailService = $mailService;
$this->customerRepository = $customerRepository;
$this->encoderFactory = $encoderFactory;
}
/**
* パスワードリマインダ.
*
* @Route("/forgot", name="forgot")
* @Template("Forgot/index.twig")
*/
public function index(Request $request)
{
if ($this->isGranted('ROLE_USER')) {
throw new HttpException\NotFoundHttpException();
}
// カートサービスを使用してカート情報を取得
$cart = $this->cartService->getCart();
// 数量と合計金額を取得
$cartTotalQuantity = 0;
$cartTotalPrice = 0;
if(!empty($cart)){
$cartTotalQuantity = $cart->getTotalQuantity();
$cartTotalPrice = $cart->getTotalPrice();
}
$builder = $this->formFactory->createNamedBuilder('', ForgotType::class);
$form = $builder->getForm();
$form->handleRequest($request);
$error = 0;
if ($form->isSubmitted() && $form->isValid()) {
$Customer = $this->customerRepository->getRegularCustomerByEmail($form->get('login_email')->getData());
if (!is_null($Customer)) {
// パスワード生成
$pwLength = 8; //パスワードの文字数
srand((double) microtime() * 54234853); // 乱数表のシードを決定
$character = 'abcdefghkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345679'; // パスワード文字列の配列を作成
$pw = preg_split('//', $character, 0, PREG_SPLIT_NO_EMPTY);
$pass = '';
for ($i = 0; $i < $pwLength; $i++) {
$pass .= $pw[array_rand($pw, 1)];
}
// パスワードの発行・更新
$encoder = $this->encoderFactory->getEncoder($Customer);
$Customer->setPassword($pass);
// 発行したパスワードの暗号化
if ($Customer->getSalt() === null) {
$Customer->setSalt($this->encoderFactory->getEncoder($Customer)->createSalt());
}
$encPass = $encoder->encodePassword($pass, $Customer->getSalt());
// パスワードを更新
$Customer->setPassword($encPass);
// リセットキーをクリア
$Customer->setResetKey(null);
// パスワードを更新
$this->entityManager->persist($Customer);
$this->entityManager->flush();
// 完了メッセージを設定
$this->addFlash('forgot_ok', trans('front.forgot.reset_complete'));
// メール送信
$this->mailService->sendPasswordResetMail($Customer, $pass);
// ログ出力
log_info('send reset password mail to:'."{$Customer->getId()} {$Customer->getEmail()} {$request->getClientIp()}");
// ログインページへリダイレクト
return $this->redirectToRoute('forgot_complete', ['id' => $Customer->getId()]);
} else {
$error = 1;
log_warning(
'Un active customer try send reset password email: ',
['Enter email' => $form->get('login_email')->getData()]
);
}
}
// メニュー用カテゴリー一覧
$Cate1st = $this->categoryRepository->findOneBy(['id' => 1]);
$sortedChildren = $Cate1st ? $Cate1st->getChildren()->toArray() : [];
usort($sortedChildren, function($a, $b) { // sort_no 昇順でソート
return $a->getSortNo() <=> $b->getSortNo();
});
return [
'error' => $error,
'form' => $form->createView(),
'cartTotalQuantity' => $cartTotalQuantity,
'cartTotalPrice' => $cartTotalPrice,
'sortedChildren' => $sortedChildren,
];
}
/**
* 再設定URL送信完了画面.
*
* @Route("/forgot/complete/{id}", name="forgot_complete", requirements={"id" = "\d+"})
* @Template("Forgot/complete.twig")
*/
public function complete(Request $request, $id = null)
{
if ($this->isGranted('ROLE_USER')) {
throw new HttpException\NotFoundHttpException();
}
// カートサービスを使用してカート情報を取得
$cart = $this->cartService->getCart();
// 数量と合計金額を取得
$cartTotalQuantity = 0;
$cartTotalPrice = 0;
if(!empty($cart)){
$cartTotalQuantity = $cart->getTotalQuantity();
$cartTotalPrice = $cart->getTotalPrice();
}
// メニュー用カテゴリー一覧
$Cate1st = $this->categoryRepository->findOneBy(['id' => 1]);
$sortedChildren = $Cate1st ? $Cate1st->getChildren()->toArray() : [];
usort($sortedChildren, function($a, $b) { // sort_no 昇順でソート
return $a->getSortNo() <=> $b->getSortNo();
});
return [
'cartTotalQuantity' => $cartTotalQuantity,
'cartTotalPrice' => $cartTotalPrice,
'sortedChildren' => $sortedChildren,
];
}
}