Selaa lähdekoodia

Add command to update uris

gabriel 6 vuotta sitten
vanhempi
commit
bd1fedc37d
1 muutettua tiedostoa jossa 68 lisäystä ja 0 poistoa
  1. 68 0
      Command/OAuthClientUpdateRedirectsCommand.php

+ 68 - 0
Command/OAuthClientUpdateRedirectsCommand.php

@@ -0,0 +1,68 @@
+<?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 OAuthClientUpdateRedirectsCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+            ->setName('oauth:client:updateRedirects')
+            ->setDescription('Update OAuht client for uri\'s.')
+            ->setHelp('This command allows you to updates uris an OAuth client with many uri\'s')
+            ->addOption(
+                'redirect_uri', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'OAuth Redirect URI. Example: http://127.0.0.1/ftth/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->updateClient($redirectUri, $random, $secret);
+        $output->writeln('<info>UPDATE OK</info>');
+    }
+
+    /**
+     * @param array $redirectUri
+     * @param string $random
+     * @param string $secret
+     *
+     * @return OAuthClient
+     */
+    protected function updateClient($redirectUri, $random = null, $secret = null)
+    {
+        /* @var $clientManager ClientManager */
+        $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
+
+        $em = $this->getContainer()->get('doctrine')->getEntityManager();
+        $newUri = [];
+        $clients = $em->getRepository('BaseOAuthServerBundle:OAuthClient')->findAll();
+        foreach ($clients as $client) {
+            $client->setRedirectUris(
+                $redirectUri
+            );
+            $clientManager->updateClient($client);
+        }
+        return $client;
+    }
+}