EC-CUBE4のソーシャルログインで会員登録するときパスワード不要で登録させる方法です。
EC-CUBEでYahoo!ID連携とLineログインする方法は下記を参照してください。
EC-CUBE4ではパスワード登録が必須なので、ソーシャルログインで会員登録したときのパスワードはランダムに生成して保存しています。
この方法がセキュリティ的に危険であればコメント頂けたら幸いです。
ソーシャルログインの場合、ランダムに生成したパスワードを保存
<?php
namespace Customize\Form\Extension;
use Eccube\Form\Type\Front\EntryType;
use Eccube\Util\StringUtil;
use KnpU\OAuth2ClientBundle\Security\Helper\FinishRegistrationBehavior;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
class EntryTypePasswordExtension extends AbstractTypeExtension
{
use FinishRegistrationBehavior;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var EncoderFactoryInterface
*/
private $encoderFactory;
public function __construct(
RequestStack $requestStack,
EncoderFactoryInterface $encoderFactory
) {
$this->requestStack = $requestStack;
$this->encoderFactory = $encoderFactory;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$userInfo = $this->getUserInfoFromSession($this->requestStack->getCurrentRequest());
if($userInfo) {
$builder->remove('password');
$builder
->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event){
$Customer = $event->getData();
if($Customer instanceof Customer) {
// ランダムパスワードを生成
$password = StringUtil::random();
$encoder = $this->encoderFactory->getEncoder($Customer);
$Customer->setPassword($encoder->encodePassword($password, $Customer->getSalt()));
}
});
}
}
/**
* @inheritDoc
*/
public function getExtendedType()
{
return EntryType::class;
}
}
ソーシャルログインの場合、会員登録の入力ページと確認ページのパスワード項目を非表示にする
Entry/index.twigを編集してください。
{% if app.session.get('guard.finish_registration.user_information') == false %}
<dl>
<dt>
{{ form_label(form.password, 'common.password', { 'label_attr': {'class': 'ec-label' }}) }}
</dt>
<dd>
<div class="ec-input{{ has_errors(form.password.first) ? ' error' }}">
{{ form_widget(form.password.first, {
'attr': { 'placeholder': 'common.password_sample'|trans({ '%min%': eccube_config.eccube_password_min_len, '%max%': eccube_config.eccube_password_max_len }) },
'type': 'password'
}) }}
{{ form_errors(form.password.first) }}
</div>
<div class="ec-input{{ has_errors(form.password.second) ? ' error' }}">
{{ form_widget(form.password.second, {
'attr': { 'placeholder': 'common.repeated_confirm'|trans },
'type': 'password'
}) }}
{{ form_errors(form.password.second) }}
</div>
</dd>
</dl>
{% endif %}
以上で完成です。
