Last active
May 20, 2024 05:57
-
-
Save chihiro-adachi/8d59928803753c8c180eea5686ca0f7e to your computer and use it in GitHub Desktop.
AdminUserCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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\Command; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Eccube\Entity\Master\Authority; | |
use Eccube\Entity\Master\Work; | |
use Eccube\Entity\Member; | |
use Eccube\Repository\MemberRepository; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Style\SymfonyStyle; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; | |
class AdminUserCommand extends Command | |
{ | |
protected static $defaultName = 'admin:user'; | |
/** | |
* @var SymfonyStyle | |
*/ | |
protected $io; | |
protected $memberRepository; | |
protected $encoderFactory; | |
protected $tokenStrage; | |
protected $em; | |
public function __construct( | |
EncoderFactoryInterface $encoderFactory, | |
MemberRepository $memberRepository, | |
TokenStorageInterface $tokenStorage, | |
EntityManagerInterface $entityManager | |
) { | |
parent::__construct(); | |
$this->memberRepository = $memberRepository; | |
$this->encoderFactory = $encoderFactory; | |
$this->tokenStrage = $tokenStorage; | |
$this->em = $entityManager; | |
} | |
protected function initialize(InputInterface $input, OutputInterface $output) | |
{ | |
$this->io = new SymfonyStyle($input, $output); | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$Member = new Member(); | |
$Member->setName('admin'); | |
$Member->setSortNo(999); | |
$Member->setPassword('password'); | |
$Member->setLoginId('admin1234'); | |
$Member->setAuthority($this->em->find(Authority::class, Authority::ADMIN)); | |
$Member->setWork($this->em->find(Work::class, Work::ACTIVE)); | |
$encoder = $this->encoderFactory->getEncoder($Member); | |
$salt = $encoder->createSalt(); | |
$rawPassword = $Member->getPassword(); | |
$encodedPassword = $encoder->encodePassword($rawPassword, $salt); | |
$Member | |
->setSalt($salt) | |
->setPassword($encodedPassword); | |
$this->memberRepository->save($Member); | |
$this->io->success('successful.'); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment