浏览代码

Refactory oauth:client:create command

Guillermo Espinoza 7 年之前
父节点
当前提交
95d5893f04
共有 3 个文件被更改,包括 60 次插入42 次删除
  1. 35 41
      Command/OAuthClientCreateCommand.php
  2. 1 1
      Entity/OAuthClient.php
  3. 24 0
      Repository/OAuthClientRepository.php

+ 35 - 41
Command/OAuthClientCreateCommand.php

@@ -15,28 +15,18 @@ class OAuthClientCreateCommand extends ContainerAwareCommand
     protected function configure()
     {
         $this
-            ->setName('oauth:client:create')
-            ->setDescription('Create OAuht client')
-            ->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'
-            );
+                ->setName('oauth:client:create')
+                ->setDescription('Create OAuht client')
+                ->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'
+        );
     }
 
     /**
@@ -48,14 +38,14 @@ class OAuthClientCreateCommand extends ContainerAwareCommand
         $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('OAUTH_CLIENT_ID='.$client->getPublicId());
-        $output->writeln('OAUTH_CLIENT_SECRET='.$client->getSecret());
+        $output->writeln('<info>OAUTH_CLIENT_ID</info>=' . $client->getPublicId());
+        $output->writeln('<info>OAUTH_CLIENT_SECRET</info>=' . $client->getSecret());
     }
-    
+
     /**
      * @param string $redirectUri
      * @param string $random
@@ -67,21 +57,25 @@ class OAuthClientCreateCommand extends ContainerAwareCommand
     {
         /* @var $clientManager ClientManager */
         $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
-        
-        /* @var $client OAuthClient */
-        $client = $clientManager->createClient();
-        if ($random) {
-            $client->setRandomId($random);
-        }
-        if ($secret) {
-            $client->setSecret($secret);
+
+        $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);
         }
-        $client->setRedirectUris(array(
-            $redirectUri,
-        ));
-        $client->setAllowedGrantTypes(array_keys(OAuthClient::getGrantTypesChoices()));
-        $clientManager->updateClient($client);
-        
+
         return $client;
     }
 

+ 1 - 1
Entity/OAuthClient.php

@@ -7,7 +7,7 @@ use Doctrine\ORM\Mapping as ORM;
 use OAuth2\OAuth2;
 
 /**
- * @ORM\Entity
+ * @ORM\Entity(repositoryClass="Base\OAuthServerBundle\Repository\OAuthClientRepository")
  */
 class OAuthClient extends BaseClient
 {

+ 24 - 0
Repository/OAuthClientRepository.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Base\OAuthServerBundle\Repository;
+
+class OAuthClientRepository extends \Doctrine\ORM\EntityRepository
+{
+
+    /**
+     * @param string $redirectUri
+     * 
+     * @return \Base\OAuthServerBundle\Entity\OAuthClient
+     */
+    public function findOneByRedirectUri($redirectUri)
+    {
+        $qb = $this->createQueryBuilder('OAuthClient');
+        $qb->where($qb->expr()->like('OAuthClient.redirectUris', ':redirectUri'))
+                ->setParameter('redirectUri', "%{$redirectUri}%")
+                ->setMaxResults(1)
+        ;
+
+        return $qb->getQuery()->getOneOrNullResult();
+    }
+
+}