alpsify / reset-password-api-bundle
此包已被弃用且不再维护。没有建议的替代包。
通过API端点处理多种用户类型的重置密码逻辑。
v1.0.0-BETA
2020-11-25 10:24 UTC
Requires
- php: ^7.4
- doctrine/orm: ^2.7
- symfony/config: ^5.0
- symfony/dependency-injection: ^5.0
- symfony/http-kernel: ^5.0
- symfony/mailer: ^5.0
- symfony/security-bundle: ^5.0
- symfony/twig-bundle: ^5.0
Requires (Dev)
- symfony/framework-bundle: ^5.0
- symfony/phpunit-bridge: ^5.1
Suggests
- api-platform/api-pack: ^1.2
This package is auto-updated.
Last update: 2022-12-25 15:44:27 UTC
README
ResetPasswordApiBundle是一个生成API端点并发送电子邮件以处理用户重置密码逻辑的简单方法。可以处理多种用户类型/类。
使用以下命令安装包
composer require alpsify/reset-password-api-bundle
只是让您知道这个包受到了ResetPasswordBundle的启发。我们只是开发了我们的,以适应我们的需求和构建处理许多用户类型的方式。如果您需要标准的重置密码逻辑,您应该去检查它。并给创建团队Symfony Cast买一杯啤酒。
使用方法
目前没有食谱... MakerBundle还不支持...真是太糟糕了...
我们正在努力解决这两个问题 :)
手动设置
1- 创建一个名为ResetPasswordRequest
的PHP类,它扩展了AbstractResetPasswordRequest
并实现了registerUser()和fetchUser()方法。这里有一个包含多种用户类型的示例。
// src/Entity/ResetPasswordRequest.php use Alpsify\ResetPasswordAPIBundle\Model\AbstractResetPasswordRequest; use App\Repository\ResetPasswordRequestRepository; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass=ResetPasswordRequestRepository::class) */ class ResetPasswordRequest extends AbstractResetPasswordRequest { /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) */ private $user; /** * @ORM\ManyToOne(targetEntity=Accountant::class) * @ORM\JoinColumn(nullable=true) */ private $accountant; /** * @ORM\ManyToOne(targetEntity=Client::class) * @ORM\JoinColumn(nullable=true) */ private $client; /** * @return mixed */ public function getUser() { return $this->user; } /** * @param mixed $user */ public function setUser($user): void { $this->user = $user; } /** * @return Accountant|object */ public function getAccountant() { return $this->accountant; } /** * @param Accountant|object $accountant */ public function setAccountant($accountant): void { $this->accountant = $accountant; } /** * @return Client|object */ public function getClient() { return $this->client; } /** * @param Client|object $client */ public function setClient($client): void { $this->client = $client; } public function registerUser(object $user): void { if($user instanceof User) { $this->user = $user; } elseif ($user instanceof Accountant) { $this->accountant = $user; } elseif ($user instanceof Client) { $this->client = $user; } } public function fetchUser(): object { if($this->user) { return $this->user; } elseif ($this->accountant) { return $this->accountant; } elseif ($this->client) { return $this->client; } } }
创建一个新的迁移
php bin/console make:migration
执行迁移以在数据库中创建表
php bin/console make:migration
2- 创建仓库类,使其扩展ServiceEntityRepository
并实现ResetPasswordRequestRepositoryInterface
。别忘了还要使用ResetPasswordRequestRepositoryTrait
:如果您想重写,请随意。
// App/Repository/ResetPasswordRequestRepository.php use Alpsify\ResetPasswordAPIBundle\Persistence\Repository\ResetPasswordRequestRepositoryInterface; use Alpsify\ResetPasswordAPIBundle\Persistence\Repository\ResetPasswordRequestRepositoryTrait; use App\Entity\ResetPasswordRequest; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; class ResetPasswordRequestRepository extends ServiceEntityRepository implements ResetPasswordRequestRepositoryInterface { use ResetPasswordRequestRepositoryTrait; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, ResetPasswordRequest::class); } }
3- 创建重置电子邮件模板。让它变得闪亮且五彩缤纷 🌈 🦄
{# templates/email/reset_password.html.twig #} {# Here you can do what ever you want to do. Send unicorns if you wish. Most important you can access your token and the tokenLifetime in order to build something helpful for your user. Like a button to redirect to the frontend page for reset password ... #} <p>{{ token }}</p> <p>{{ tokenLifetime }}</p>
配置
使用之前生成的文件来完成配置。
#config/packages/alpsify_reset_password.yaml alpsify_reset_password_api: token: # Life time of the request in seconds. After that the token is invalid and the user need to ask for a new one. lifetime: 3600 # Customize the selector size of the token you send. selector_size: 20 hash_algo: ~ # Time between 2 requests. throttle_time: 3600 # Describe all your user types more # name: # class: user_types: # Required # Prototype user: class: App\Entity\User # Required ... persistence: # Class of the entity used for storing the user reset password request. class: ~ # Repository class linked to the entity repository: ~ mailer: # Your choosen email. The reset email will be send through this one. from_email: ~ # Your choosen name link to the email. from_name: ~ # The template used by the mailer in order the send the reset link. template: ~
参数
//TODO
访问新的API端点
别忘了通过将其添加到access_controle中授权每个人访问这些端点。
#config/packages/security.yaml ... access_control: - { path: ^/api/request-reset-password, roles: PUBLIC_ACCESS, methods: [POST] } - { path: ^/api/reset-password, roles: PUBLIC_ACCESS, methods: [POST] } ...