mstaples/google-bundle

将谷歌服务集成到 Symfony 2 应用中。

安装: 6

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2014-07-07 17:37 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:12:25 UTC


README

这个包是 bitgandtter/google-bundle 的旧版本。我独立维护这个版本,因为它被用于我的当前项目。注意:这个分支与 Symfony2 >= v2.3 版本的发布兼容

安装

  1. 将此包和 Google PHP SDK 添加到您的 vendor/ 目录

    • 使用 vendors 脚本。

      在您的 deps 文件中添加以下行:

      {
      "require": {
          "bitgandtter/google-bundle": "0.1"
      	}
      }
      
  2. 运行 composer 下载此包

       $ composer update
    
  3. 将此包添加到您的应用程序的内核

     // app/ApplicationKernel.php
     public function registerBundles()
     {
         return array(
             // ...
             new BIT\GoogleBundle\BITGoogleBundle(),
             // ...
         );
     }
    
  4. 将以下路由添加到您的应用程序,并将它们指向实际控制器操作

     #application/config/routing.yml
     _security_check:
         pattern:  /login_check
     _security_logout:
         pattern:  /logout
    
     #application/config/routing.xml
     <route id="_security_check" pattern="/login_check" />
     <route id="_security_logout" pattern="/logout" />     
    
  5. 在您的配置中配置 google 服务

     # application/config/config.yml
     bit_google:
       app_name: appName
       client_id: 123456789
       client_secret: s3cr3t
       state: auth
       access_type: online
       scopes: [userinfo.email, userinfo.profile]
       approval_prompt: auto
       callback_url: http://yourdomain.com/login_check?google=true
    

注意:此回调_url 中的额外参数是必须的,以定位谷歌防火墙

  1. 如果您想使用 security component,请添加此配置

     # application/config/config.yml
     security:
         firewalls:
             public:
                 # since anonymous is allowed users will not be forced to login
                 pattern:   ^/.*
         bit_google:
               provider: google
    
         access_control:
             - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] } # This is the route secured with bit_google
             - { path: ^/.*, role: [IS_AUTHENTICATED_ANONYMOUSLY] }
    

    您必须将 /secured/ 添加到您的路由中才能使其工作。一个例子是...

         _google_secured:
             pattern: /secured/
             defaults: { _controller: AcmeDemoBundle:Welcome:index }
    
  2. 可选地定义一个自定义用户提供者类并将其用作提供者或定义登录路径

     # application/config/config.yml
     security:
         providers:
             # choose the provider name freely
                 google:
                   id: google.user # see "Example Customer User Provider using the BIT\UserBundle" chapter further down
    
         firewalls:
             public:
                 pattern:   ^/.*
                 bit_google:
                   provider: google
                 anonymous: true
                 logout: true 
    
  3. 可选地使用访问控制来保护特定 URL

     # application/config/config.yml
     security:
         # ...
         
         access_control:
             - { path: ^/google/,           role: [ROLE_GOOGLE] }
             - { path: ^/.*,                role: [IS_AUTHENTICATED_ANONYMOUSLY] }
    

在您的模板中包含登录按钮

只需在您的模板中添加以下代码即可

{{ google_login_button() }}

或者如果您想使用

{% autoescape false %}{{ google_login_url() }}{% endautoescape %}

这将获取登录 URL

使用 FOS\UserBundle 的示例客户用户提供者

这需要添加一个用于自定义用户提供者的服务,然后将它设置为配置中的 "provider" 部分的提供者 ID

services:
    google.user:
  class: class: Acme\MyBundle\Security\User\Provider\googleProvider
  arguments:
      google: @bit_google.api
      userManager: @bit_user.user_manager
      validator: @validator
      em: @doctrine.orm.entity_manager

<?php
Acme\MyBundle\Security\User\Provider;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class GoogleProvider implements UserProviderInterface
{
  /**
   * @var \GoogleApi
   */
  protected $googleApi;
  protected $userManager;
  protected $validator;
  protected $em;

  public function __construct( $googleApi, $userManager, $validator, $em )
  {
    $this->googleApi = $googleApi;
    $this->userManager = $userManager;
    $this->validator = $validator;
    $this->em = $em;
  }

  public function supportsClass( $class )
  {
    return $this->userManager->supportsClass( $class );
  }

  public function findUserByGIdOrEmail( $gId, $email = null )
  {
    $user = $this->userManager->findUserByUsernameOrEmail( $email );
    if ( !$user )
      $user = $this->userManager->findUserBy( array( 'googleID' => $gId ) );
    return $user;
  }

  public function loadUserByUsername( $username )
  {
    try
    {
      $gData = $this->googleApi->getOAuth( )->userinfo->get( );
    }
    catch ( \Exception $e )
    {
      $gData = null;
    }

    if ( !empty( $gData ) )
    {

      $email = $gData["email"];
      $user = $this->findUserByGIdOrEmail( $username, isset( $email ) ? $email : null );

      if ( empty( $user ) )
      {
  		$user = $this->userManager->createUser( );
  		$user->setEnabled( true );
  		$user->setPassword( '' );
  		$user->setSalt( '' );
      }

      $id = $gData->getId();

      if ( isset( $id ) )
      {
	      $user->setGoogleID( $id );
      }

      $name = $gData["full_name"];

      if ( isset( $name ) )
      {
  		$nameAndLastNames = explode( " ", $name );
  		if ( count( $nameAndLastNames ) > 1 )
  		{
  		  $user->setFirstname( $nameAndLastNames[0] );
  		  $user->setLastname( $nameAndLastNames[1] );
  		  $user->setLastname2( ( count( $nameAndLastNames ) > 2 ) ? $nameAndLastNames[2] : "" );
  		}
  		else
  		{
  		  $user->setFirstname( $nameAndLastNames[0] );
  		  $user->setLastname( "" );
  		  $user->setLastname2( "" );
  		}
      }

      if ( isset( $email ) )
      {
  		$user->setEmail( $email );
  		$user->setUsername( $email );
      }
      else
      {
  		$user->setEmail( $id . "@google.com" );
  		$user->setUsername( $id . "@google.com" );
      }

      if ( count( $this->validator->validate( $user, 'Google' ) ) )
      {
	// TODO: the user was found obviously, but doesnt match our expectations, do something smart
	throw new UsernameNotFoundException( 'The google user could not be stored');
      }
      $this->userManager->updateUser( $user );
    }

    if ( empty( $user ) )
    {
      throw new UsernameNotFoundException( 'The user is not authenticated on google');
    }

    return $user;
  }

  public function refreshUser( UserInterface $user )
  {
    if ( !$this->supportsClass( get_class( $user ) ) || !$user->getGoogleId( ) )
    {
      throw new UnsupportedUserException( sprintf( 'Instances of "%s" are not supported.', get_class( $user ) ));
    }

    return $this->loadUserByUsername( $user->getGoogleId( ) );
  }
}

最后,还需要在用户模型中添加一个 getGoogleId() 方法。以下示例还添加了 "firstname" 和 "lastname" 属性

<?php

Acme\MyBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="system_user")
 */
class User extends BaseUser
{
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\generatedValue(strategy="AUTO")
   */
  protected $id;

  /**
   * @ORM\Column(type="string", length=40, nullable=true)
   */
  protected $googleID;

  /**
   * @ORM\Column(type="string", length=100, nullable=true)
   */
  protected $firstname;

  /**
   * @ORM\Column(type="string", length=100, nullable=true)
   */
  protected $lastname;

  /**
   * @ORM\Column(type="string", length=100, nullable=true)
   */
  protected $lastname2;

  public function __construct( )
  {
    parent::__construct( );
  }

  public function getId( )
  {
    return $this->id;
  }

  public function getFirstName( )
  {
    return $this->firstname;
  }

  public function setFirstName( $firstname )
  {
    $this->firstname = $firstname;
  }

  public function getLastName( )
  {
    return $this->lastname;
  }

  public function setLastName( $lastname )
  {
    $this->lastname = $lastname;
  }

  public function getLastName2( )
  {
    return $this->lastname2;
  }

  public function setLastName2( $lastname2 )
  {
    $this->lastname2 = $lastname2;
  }

  public function getFullName( )
  {
    $fullName = ( $this->getFirstName( ) ) ? $this->getFirstName( ) . ' ' : '';
    $fullName .= ( $this->getLastName( ) ) ? $this->getLastName( ) . ' ' : '';
    $fullName .= ( $this->getLastName2( ) ) ? $this->getLastName2( ) . ' ' : '';
    return $fullName;
  }

  public function setGoogleID( $googleID )
  {
    $this->googleID = $googleID;
  }

  public function getGoogleID( )
  {
    return $this->googleID;
  }


  public function setSalt( $salt )
  {
    $this->salt = $salt;
  }
}