12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace Base\OAuthServerBundle\Command;
- use \Base\OAuthServerBundle\Entity\OAuthClient;
- use \FOS\OAuthServerBundle\Entity\ClientManager;
- use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Input\InputOption;
- class OAuthClientCreateCommand extends ContainerAwareCommand
- {
- protected function configure()
- {
- $this
- ->setName('oauth:client:create')
- ->setDescription('Create OAuht client. DEPRECATED see oauth:client:createRedirects')
- ->setHelp('This command allows you to create an OAuth client')
- ->addOption(
- 'redirect_uri', null, InputOption::VALUE_REQUIRED, 'OAuth Redirect URI', 'http://127.0.0.1/ftth/app_dev.php/login_check'
- )
- ->addOption(
- 'client_id', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Id'
- )
- ->addOption(
- 'client_secret', null, InputOption::VALUE_OPTIONAL, 'OAuth Client random Secret'
- );
- }
- /**
- * @param InputInterface $input
- * @param OutputInterface $output
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $redirectUri = $input->getOption('redirect_uri');
- $random = $input->getOption('client_id');
- $secret = $input->getOption('client_secret');
- $client = $this->createClient($redirectUri, $random, $secret);
- $output->writeln('#OAuth client successfully generated!');
- $output->writeln('<info>OAUTH_CLIENT_ID</info>=' . $client->getPublicId());
- $output->writeln('<info>OAUTH_CLIENT_SECRET</info>=' . $client->getSecret());
- }
- /**
- * @param string $redirectUri
- * @param string $random
- * @param string $secret
- *
- * @return OAuthClient
- */
- protected function createClient($redirectUri, $random = null, $secret = null)
- {
- /* @var $clientManager ClientManager */
- $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
- $em = $this->getContainer()->get('doctrine')->getEntityManager();
- $client = $em->getRepository('BaseOAuthServerBundle:OAuthClient')->findOneByRedirectUri($redirectUri);
- if (!$client) {
- /* @var $client OAuthClient */
- $client = $clientManager->createClient();
- if ($random) {
- $client->setRandomId($random);
- }
- if ($secret) {
- $client->setSecret($secret);
- }
- $client->setRedirectUris(array(
- $redirectUri,
- ));
- $client->setAllowedGrantTypes(array_keys(OAuthClient::getGrantTypesChoices()));
- $clientManager->updateClient($client);
- }
- return $client;
- }
- }
|