xphere / one-time-access-bundle
此包已被废弃且不再维护。未建议替代包。
通过一次访问链接在 Symfony2 应用程序中验证用户
1.2
2016-05-23 15:27 UTC
Requires
- symfony/symfony: ~2.3.41|~2.7.13|~2.8.6|~3.0.6
This package is not auto-updated.
Last update: 2024-02-28 21:19:00 UTC
README
你是否曾想在 Symfony2 应用程序中通过 一次性访问链接 来验证用户?
别再找了!这就是你的包! :D
⚠ 注意 ⚠
注意包名更改
- 在
1.0.0
之前:berny/one-time-access-bundle
- 在
1.0.0
之后:xphere/one-time-access-bundle
为什么需要这个?
你可以使用一次性访问链接来
- 访问“忘记密码?”表单
- 无密码系统
功能
- 可定制的链接
- 用户定义的令牌生成和检索
- 多个防火墙
兼容性
在 Symfony2 2.1.1 及更高版本下测试
安装
从 composer/packagist
- 将
"xphere/one-time-access-bundle": "^1.1"
添加到你的composer.json
文件 - 使用
new xPheRe\OneTimeAccessBundle\xPheReOneTimeAccessBundle()
将包添加到你的内核
用法
在任何防火墙中添加 one_time_access
键,至少包含一个 route
。
security: firewalls: root: one_time_access: route: acme_myapp_ota
当前用户提供者必须实现 OneTimeAccessBundle\Security\Provider\ProviderInterface
。
security: provider: users: entity: # AcmeMyAppBundle:UserRepository implements ProviderInterface class: AcmeMyAppBundle:User firewalls: root: provider: users one_time_access: route: acme_myapp_ota
你可以设置 ota_provider
键来定义一个实现该接口的不同服务。
services: acme.myapp.ota.repository: class: Acme\\MyAppSecurity\\UserProvider security: firewalls: root: one_time_access: route: acme_myapp_ota ota_provider: acme.myapp.ota.repository
默认情况下,route
必须有一个 _token
参数来提取一次性访问令牌。
acme_myapp_ota: pattern: ^/autologin/{_token} defaults: { _controller: AcmeMyAppBundle:Login:oneTimeAccess }
这可以通过 parameter
键进行自定义。
security: firewalls: root: one_time_access: route: acme_myapp_ota parameter: otatoken
当然,你可以像往常一样定义你的路由,使用 YAML、XML、注释等。
令牌生成
此包不涵盖令牌生成。你需要创建唯一的令牌并将它们与用户关联。
这可能是 Doctrine 实现的一部分
class OTARepository extends EntityRepository implements ProviderInterface { public function generateOTA($user) { $token = md5($user->getUsername() . time()); $ota = new YourOneTimeAccessEntity($user, $token); $this->getEntityManager()->persist($ota); $this->getEntityManager()->flush($ota); return $ota; } public function loadUserByOTA($token) { $ota = $this->findOneByToken($token); if ($ota) { // Remember, user must be defined as EAGER in OTAEntity return $ota->getUser(); } } public function invalidateByOTA($token) { $ota = $this->findOneByToken($token); $this->getEntityManager()->remove($ota); $this->getEntityManager()->flush(); } }
路由生成
路由生成也由你决定。是的!你说我们在偷懒吗?不是的!这意味着完全可定制的用于一次性访问链接的路由。
例如
$ota = $oneTimeAccessRepository->generateOnetimeAccess($user); $url = $this->generateUrl('acme_myapp_ota', array( '_token' => $ota->getToken(), ));