elao / voucher-authentication-bundle
通过优惠券进行身份验证(用于电子邮件链接)。
dev-master / 0.1.x-dev
2016-11-23 21:50 UTC
Requires
- php: >=5.5.9
- symfony/framework-bundle: ~2.7|~3.0
- symfony/security-bundle: ~2.7|~3.0
This package is auto-updated.
Last update: 2024-09-05 19:23:29 UTC
README
通过优惠券进行身份验证(用于电子邮件链接)。
安装
通过composer安装此包
composer require elao/voucher-authentication-bundle
在您的 app/AppKernel.php
中注册此包
class AppKernel extends Kernel { public function registerBundles() { return [ // ... new Elao\Bundle\VoucherAuthenticationBundle\ElaoVoucherAuthenticationBundle(), ]; } }
声明优惠券路由
通过在您的 app/config/routing.yml
中导入提供的路由配置
# routing.yml elao_voucher_authentication: resource: "@ElaoVoucherAuthenticationBundle/Resources/config/routing.xml" prefix: /
注意:您也可以声明自己的路由,只要在您的优惠券安全配置中指定相应的 check_path
和 token_parameter
参数(请参阅“自定义优惠券路由”)。
使用
启用优惠券身份验证
优惠券身份验证包提供 voucher
安全提供者。
您可以在 security.yml
中非常简单地启用优惠券身份验证
security: firewalls: main: voucher: ~
在您的应用程序中生成优惠券令牌
创建一个新的 VoucherInterface
(您可以使用提供的 DisposableAuthenticationVoucher
实现,或者创建自己的实现)。然后使用 getToken()
获取其令牌,例如,通过电子邮件将其发送给用户
use Elao\Bundle\VoucherAuthenticationBundle\Voucher\DisposableAuthenticationVoucher; class SecurityController extends Controller { /** * @Route("forgot-password", name="forgot_password") */ public function forgotPasswordAction() { $voucher = new DisposableAuthenticationVoucher('jane_doe', '+1 hour'); $activationUrl = $this->generateUrl('voucher', ['token' => $voucher->getToken()]); // Don't forget to persist the voucher, or the user won't be able to log in. $this->get('elao_voucher_authentication.voucher_provider.default')->persist($voucher); $this->mailer->sendResetPasswordEmail($activationUrl); } }
从CLI生成优惠券令牌
为指定的用户名生成优惠券(可选设置过期时间)
bin/console voucher:generate:authenticatio [username] (--ttl="+1 hour")
将产生
用户admin的认证优惠券,过期时间为2016-11-15 13:42:24: 6fb11ec1eecd07865d940dd0f990d66b
使用优惠券限制用户访问
您可以通过要求特定的优惠券身份验证来保护路由或应用程序的任何部分。例如,您可以将重置密码的路由仅允许通过带有 reset_password
意图的优惠券进行身份验证的用户重置密码。
使用以下安全表达式:is_granted('voucher', $intent)
,其中 $intent 是您提供给 Voucher
对象的意图。
class SecurityController extends Controller { /** * @Route("reset_password", name="reset_password") * @Security("is_granted('voucher', 'password')") */ public function resetPasswordAction() {} } ## Full configuration ```yml security: firewalls: main: voucher: remember_me: true check_path: voucher use_forward: false require_previous_session: true token_parameter: token always_use_default_target_path: false default_target_path: / login_path: /login target_path_parameter: _target_path use_referer: false failure_path: null failure_forward: false failure_path_parameter: _failure_path voucher_provider: elao_voucher_authentication.voucher_provider.default
自定义优惠券路由
# routing.yml my_voucher_route: path: /activate/{my_token}
security: firewalls: main: voucher: check_path: my_voucher_route token_parameter: my_token