From 936a8febe6d244a06389cd843ca56f373fc3325d Mon Sep 17 00:00:00 2001 From: Kayn Ty Date: Wed, 25 Apr 2018 12:33:58 +0200 Subject: [PATCH] oauth google --- .../views/default/login-choices.html.twig | 9 +++ app/config/routing.yml | 48 ++++++++-------- app/config/security.yml | 1 + app/config/services.yml | 5 ++ composer.json | 4 +- composer.lock | 2 +- .../Core/User/MyFOSUBUserProvider.php | 56 +++++++++++++++++++ var/SymfonyRequirements.php | 18 +++--- web/config.php | 2 +- 9 files changed, 109 insertions(+), 36 deletions(-) create mode 100644 src/AppBundle/Security/Core/User/MyFOSUBUserProvider.php diff --git a/app/Resources/views/default/login-choices.html.twig b/app/Resources/views/default/login-choices.html.twig index fcbb0d58..8b6ed232 100755 --- a/app/Resources/views/default/login-choices.html.twig +++ b/app/Resources/views/default/login-choices.html.twig @@ -111,7 +111,16 @@ {% else %} + + {##} + {##} + {#Via {{ 'disqus' | trans({}, 'HWIOAuthBundle') }}#} + {##}

+ + + Via {{ 'google' | trans({}, 'HWIOAuthBundle') }} + diff --git a/app/config/routing.yml b/app/config/routing.yml index 0b3bcb6d..2c405028 100755 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -1,3 +1,28 @@ +hwi_oauth_redirect: + resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" + prefix: /connect + +hwi_oauth_connect: + resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" + prefix: /connect + +hwi_oauth_login: + resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" + prefix: /login + +facebook_login: + path: /login/check-facebook + +google_login: + path: /login/check-google + +disqus_login: + path: /login/check-disqus + +twitter_login: + path: /login/check-twitter + +## app specific stuff app_festival: resource: "@AppBundle/Controller/FestivalController.php" type: annotation @@ -14,29 +39,6 @@ app_sell_record: resource: "@AppBundle/Controller/SellRecordController.php" type: annotation -hwi_oauth_redirect: - resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" - prefix: /connect - -hwi_oauth_connect: - resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" - prefix: /connect - -#hwi_oauth_login: -# resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" -# prefix: /login -facebook_login: - path: /login/check-facebook - -google_login: - path: /login/check-google - -custom_login: - path: /login/check-custom - -twitter_login: - path: /login/check-twitter - app: resource: '@AppBundle/Controller/' type: annotation diff --git a/app/config/security.yml b/app/config/security.yml index 18f8186f..655d938b 100755 --- a/app/config/security.yml +++ b/app/config/security.yml @@ -34,6 +34,7 @@ security: resource_owners: facebook: "/login/check-facebook" google: "/login/check-google" + disqus: "/login/check-disqus" # my_custom_provider: "/login/check-custom" twitter: "/login/check-twitter" login_path: /login diff --git a/app/config/services.yml b/app/config/services.yml index 864e2a86..fe6d3d5a 100755 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -33,8 +33,13 @@ services: # AppBundle\Service\ExampleService: # arguments: # $someArgument: 'some_value' + # OAUTH my.oauth_aware.user_provider.service: class: HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider arguments: - '@fos_user.user_manager' - ['pass properties as array'] + + my.custom.user_provider: + class: MyBundle\Security\Core\User\MyFOSUBUserProvider + arguments: ['@fos_user.user_manager', { facebook: facebook_id }] diff --git a/composer.json b/composer.json index a5c91f4a..b9e8e9f1 100755 --- a/composer.json +++ b/composer.json @@ -24,10 +24,10 @@ "doctrine/doctrine-bundle": "^1.6", "doctrine/orm": "^2.5", "friendsofsymfony/user-bundle": "~2.0", - "hwi/oauth-bundle": "^0.6.1", + "hwi/oauth-bundle": "^0.6.2", "incenteev/composer-parameter-handler": "^2.0", "php-http/guzzle6-adapter": "^1.1", - "php-http/httplug-bundle": "^1.9", + "php-http/httplug-bundle": "^1.10", "sensio/distribution-bundle": "^5.0.19", "sensio/framework-extra-bundle": "^5.0.0", "symfony/monolog-bundle": "^3.1.0", diff --git a/composer.lock b/composer.lock index a3fbdb15..0725d7f0 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "c18164d1db85ea9fbbe32f2bfbea9ebc", + "content-hash": "340327e74f4d3713baf607c9a61fc0e9", "packages": [ { "name": "clue/stream-filter", diff --git a/src/AppBundle/Security/Core/User/MyFOSUBUserProvider.php b/src/AppBundle/Security/Core/User/MyFOSUBUserProvider.php new file mode 100644 index 00000000..056984ca --- /dev/null +++ b/src/AppBundle/Security/Core/User/MyFOSUBUserProvider.php @@ -0,0 +1,56 @@ +getProperty( $response ); + $username = $response->getUsername(); // get the unique user identifier + +//we "disconnect" previously connected users + $existingUser = $this->userManager->findUserBy( [ $property => $username ] ); + if ( null !== $existingUser ) { +// set current user id and token to null for disconnect +// ... + + $this->userManager->updateUser( $existingUser ); + } +// we connect current user, set current user id and token +// ... + $this->userManager->updateUser( $user ); + } + + /** + * {@inheritdoc} + */ + public function loadUserByOAuthUserResponse( UserResponseInterface $response ) { + $userEmail = $response->getEmail(); + $user = $this->userManager->findUserByEmail( $userEmail ); + +// if null just create new user and set it properties + if ( null === $user ) { + $username = $response->getRealName(); + $user = new User(); + $user->setUsername( $username ); + +// ... save user to database + + return $user; + } +// else update access token of existing user + $serviceName = $response->getResourceOwner()->getName(); + $setter = 'set' . ucfirst( $serviceName ) . 'AccessToken'; + $user->$setter( $response->getAccessToken() );//update access token + + return $user; + } +} diff --git a/var/SymfonyRequirements.php b/var/SymfonyRequirements.php index 6ab7f718..3b14a402 100755 --- a/var/SymfonyRequirements.php +++ b/var/SymfonyRequirements.php @@ -148,7 +148,7 @@ class PhpIniRequirement extends Requirement } if (null === $helpHtml) { - $helpHtml = sprintf('Set %s to %s in php.ini*.', + $helpHtml = sprintf('Set %s to %s in php.ini*.', $cfgName, $evaluation ? 'on' : 'off' ); @@ -419,7 +419,7 @@ class SymfonyRequirements extends RequirementCollection $this->addRequirement( is_dir(__DIR__.'/../vendor/composer'), 'Vendor libraries must be installed', - 'Vendor libraries are missing. Install composer following instructions from http://getcomposer.org/. '. + 'Vendor libraries are missing. Install composer following instructions from http://getcomposer.org/. '. 'Then run "php composer.phar install" to install them.' ); @@ -443,7 +443,7 @@ class SymfonyRequirements extends RequirementCollection $this->addPhpIniRequirement( 'date.timezone', true, false, 'date.timezone setting must be set', - 'Set the "date.timezone" setting in php.ini* (like Europe/Paris).' + 'Set the "date.timezone" setting in php.ini* (like Europe/Paris).' ); } @@ -458,7 +458,7 @@ class SymfonyRequirements extends RequirementCollection $this->addRequirement( isset($timezones[@date_default_timezone_get()]), sprintf('Configured default timezone "%s" must be supported by your installation of PHP', @date_default_timezone_get()), - 'Your default timezone is not supported by PHP. Check for typos in your php.ini file and have a look at the list of deprecated timezones at http://php.net/manual/en/timezones.others.php.' + 'Your default timezone is not supported by PHP. Check for typos in your php.ini file and have a look at the list of deprecated timezones at http://php.net/manual/en/timezones.others.php.' ); } @@ -522,7 +522,7 @@ class SymfonyRequirements extends RequirementCollection create_function('$cfgValue', 'return false !== stripos($cfgValue, "phar");'), false, 'suhosin.executor.include.whitelist must be configured correctly in php.ini', - 'Add "phar" to suhosin.executor.include.whitelist in php.ini*.' + 'Add "phar" to suhosin.executor.include.whitelist in php.ini*.' ); } @@ -540,7 +540,7 @@ class SymfonyRequirements extends RequirementCollection create_function('$cfgValue', 'return $cfgValue > 100;'), true, 'xdebug.max_nesting_level should be above 100 in php.ini', - 'Set "xdebug.max_nesting_level" to e.g. "250" in php.ini* to stop Xdebug\'s infinite recursion protection erroneously throwing a fatal error in your project.' + 'Set "xdebug.max_nesting_level" to e.g. "250" in php.ini* to stop Xdebug\'s infinite recursion protection erroneously throwing a fatal error in your project.' ); } @@ -558,7 +558,7 @@ class SymfonyRequirements extends RequirementCollection create_function('$cfgValue', 'return (int) $cfgValue === 0;'), true, 'string functions should not be overloaded', - 'Set "mbstring.func_overload" to 0 in php.ini* to disable function overloading by the mbstring extension.' + 'Set "mbstring.func_overload" to 0 in php.ini* to disable function overloading by the mbstring extension.' ); } @@ -707,7 +707,7 @@ class SymfonyRequirements extends RequirementCollection create_function('$cfgValue', 'return (int) $cfgValue === 0;'), true, 'intl.error_level should be 0 in php.ini', - 'Set "intl.error_level" to "0" in php.ini* to inhibit the messages when an error occurs in ICU functions.' + 'Set "intl.error_level" to "0" in php.ini* to inhibit the messages when an error occurs in ICU functions.' ); } @@ -735,7 +735,7 @@ class SymfonyRequirements extends RequirementCollection $this->addRecommendation( $this->getRealpathCacheSize() >= 5 * 1024 * 1024, 'realpath_cache_size should be at least 5M in php.ini', - 'Setting "realpath_cache_size" to e.g. "5242880" or "5M" in php.ini* may improve performance on Windows significantly in some cases.' + 'Setting "realpath_cache_size" to e.g. "5242880" or "5M" in php.ini* may improve performance on Windows significantly in some cases.' ); } diff --git a/web/config.php b/web/config.php index 2719592b..fd7e17e6 100644 --- a/web/config.php +++ b/web/config.php @@ -410,7 +410,7 @@ $hasMinorProblems = (bool) count($minorProblems);