icedevelop / authentication-bundle
此包实现了通过oauth2进行SSO Google身份验证
Requires
- php: >=5.5.9
- knpuniversity/oauth2-client-bundle: ^1.19
- league/oauth2-google: ^2.2
This package is auto-updated.
Last update: 2024-09-08 20:46:20 UTC
README
此包实现了与Google的oauth2身份验证
注意
该包正在积极开发中,目前不建议使用。
文档
此包与Guard集成,并验证用户是否已登录。如果未登录,将通过oauth2调用Google进行身份验证。授权过程正在开发中,并依赖于宿主应用程序。
安装
步骤1:下载包
打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本
$ composer require retitalia/authentication-bundle
如果返回有关无法安装的oauth2库的错误,这是由于bundle paragonie的问题。在这种情况下,请输入以下命令
$ composer require paragonie/random_compat 2.0.17
然后重复包安装过程
此命令需要您全局安装Composer,如Composer文档的安装章节中所述。
步骤2:启用包
然后,通过将其添加到项目app/AppKernel.php
文件中注册的包列表来启用包
<?php // app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new retItalia\AuthenticationBundle\retItaliaAuthenticationBundle(), new KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle(), ); // ... } // ... }
在app/config/security.yml中添加
在firewalls:下,与dev相同的高度
main: anonymous: ~ logout: path: /logout target: / guard: authenticators: - authenticator
providers: dbal: entity: class: retItaliaAuthenticationBundle:SaUtente property: idUtente
providers: dbal: entity: class: retItaliaAuthenticationBundle:SaUtente property: idUtente
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/authentication, roles: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/non-autorizzato, roles: IS_AUTHENTICATED_ANONYMOUSLY }
添加角色是个好主意
role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
在app/config/routing.yml中添加
ret_italia_authentication:
resource: "@retItaliaAuthenticationBundle/Controller/"
type: annotation
prefix: /
在app/config/config.yml中添加配置
//.. twig: debug: '%kernel.debug%' strict_variables: '%kernel.debug%' globals: # the value is the service's id //.. userAuthorizedFunctions: "@userAuthorizedFunctions" //.. //.. # Doctrine Configuration doctrine: dbal: connections: bdc: driver: '%bdc_driver%' host: '%bdc_host%' port: '%bdc_port%' dbname: '%bdc_name%' user: '%bdc_user%' password: '%bdc_password%' charset: UTF8 //.. knpu_oauth2_client: clients: # will create service: "knpu.oauth2.client.google" # an instance of: KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient # composer require league/oauth2-google google_main: type: google # add and configure client_id and client_secret in parameters.yml client_id: '%google_client_id%' client_secret: '%google_client_secret%' # a route name you'll create redirect_route: '%google_redirect_url%' redirect_params: {} ret_italia_authentication: parameters: google_client_id: '%google_client_id%' google_client_secret: '%google_client_secret%' google_redirect_url: '%google_redirect_url%' application_id: '%application_id%'
在app/config/parameters.yml中添加
parameters: bdc_host: <bdc server> bdc_port: <bdc port> bdc_name: <bdc name> bdc_user: <bdc user> bdc_password: <bdc password> bdc_driver: oci8 //.. google_client_id: '<google_client_id>' google_client_secret: '<google_client_secret>' google_redirect_url: '<google_redirect_url>' application_id: <application_id>
对于测试和开发环境,google_redirect_url必须与'connect_google_check'相同,因此对于parameters.yml,本地生产环境的google_redirect_url='connect_google_check_dev'
在app/config/parameters.yml中,您还必须添加必须授权进行Google身份验证的应用程序
scope_auth: ['<application-1>','<application-2>']
如果您不知道必须使用哪个应用程序,您可以保留该参数为空
scope_auth: []
参数的正确值可以从https://gitlab.com/retitalia/contenitore-bundle-comuni 获取
许可
此包采用MIT许可。
用法
身份验证是自动的,它通过oauth2调用Google SSO并执行过程。
授权基于特定数据库。用户必须在正确的表中启用,并且必须为指定应用程序具有角色。
它通过application_id参数测试参数.yml中指定的应用程序。
但是,还有一些可以手动调用的其他有用功能。
测试指定功能
要测试已登录用户是否具有指定功能的权限,只需以这种方式调用isFunctionAuthorized即可
if ($this->get('userAuthorizedFunctions')->isFunctionAuthorized(<functionId>) { } else { }
无需传递已登录的用户,功能已知道它。
测试用户是否有角色
要测试已登录用户是否具有指定角色,只需以这种方式调用hasRole即可
if ($this->get('userAuthorizedFunctions')->hasRole(<roleId>) { } else { }
无需传递已登录的用户,功能已知道它。