kavalanche/security 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kavalanche/security

Composer 安装命令:

composer require kavalanche/security

包简介

Security component for web applications

README 文档

README

Simple security library for web applications.

Usage

  1. Require kavalanche/security.

     composer require kavalanche/security
    
  2. Create UserProvider. Refer to Custom UserProvider section.

  3. Create instance of Kavalanche\Security\Service\RememberUserService.

  4. Create instance of AuthenticationProvider and inject your UserProvider and RememberUserService into it.

     $authenticationProvider = new Kavalanche\Security\Provider\AuthenticationProvider($userProvider, $rememberUserService);
    
  5. Check if user is authenticated.

     try {
         $user = $authenticationProvider->authenticate();
     } catch (Kavalanche\Security\Exception\SecurityException $ex) {
         // if you want to allow unauthenticated users, then assign false or null to $user
            
         // if you require user to be authenticated do as follows
         if (!$e instanceof Kavalanche\Security\Exception\UserNotAuthenticatedException) {
             // put message in flash session and redirect to user form
             // or do whatever your use case demands
         }
     }
    

    You can specify redirect-path in security.yaml or pass desired path to $_SESSION['redirect-path']. Default is /.

    By default expected login form field names are email and password.

    You can change them by creating a config file named {app_root}/config/security.yaml and setting these variables:

    • login-form-identifier-field for identifier field
    • login-form-password-field for password field
  6. Don't forget to put session_start() at the beginning of your file.

Custom UserProvider

If you want to user different UserProvider, you can create your. It must implement Kavalanche\Security\Interface\UserProviderInterface.

class UserProvider implements Kavalanche\Security\Interface\UserProviderInterface {

    public function loadUser($identifier) {
    
        // Fetch your User
        // Don't forget which type of identifier you defined in `security.yaml`
        
        // You can create your own User class (it must implement Kavalanche\Security\UserInterface)
        if ($user instanceof Kavalanche\Security\UserInterface) {
            return $user;
        }

        throw new Kavalanche\Security\Exception\SecurityException('Invalid username.');
    }

}

You can configure identifier type in your {app_root}/config/security.yaml file by setting identifier variable. Possible values are: email, username.

Remember user

From version v1.1.0 there is a possibility to add Remember me checkbox to login form. This option sets a cookie in user's browser and logs him in automatically.

Default configuration:

  • login-form-remember-me-field: "rememberme"
  • remember-me-cookie-lifetime: 2592000 # 30 days

Password reset

From version v2.0.0 there is a new functionality of resetting user password. You can use it as follows:

  1. Add route to form with email field (configurable)
  2. Check default email template and replace it with your own if needed (password-reset-mail-template in configuration file)
  3. Add route to send email that includes code:
# You must create a service for sending e-mails that implements Kavalanche\Security\Interfaces\MailerInterface
$passwordResetService = new Kavalanche\Security\Service\PasswordResetService($passwordResetRepo, $userProvider, $mailer);

# Form data is handled internally if you use correctly configured fields (please refer to parameters.yaml file)
$passwordResetService->processResetRequest();

# You can redirect user according to the output of this method
  1. Add new password handling route that includes code:
# Form data is handled internally if you use correctly configured fields (please refer to parameters.yaml file)
$passwordResetService->resetPassword();

# You can redirect user according to the output of this method

Other informations

  • It's your obligation to ensure that the usernames are unique.
  • You can use this library however you want. To secure the whole application or just some routes.
  • You can add multiple roles to each user. Simply assign an array with roles or permissions with $user->setRoles() setter.

To do

  • Implement some kind of request abstraction to encapsulate Requests (symfony/http-foundation?)
  • Add helper to check permissions

kavalanche/security 适用场景与选型建议

kavalanche/security 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kavalanche/security 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kavalanche/security 我们能提供哪些服务?
定制开发 / 二次开发

基于 kavalanche/security 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 142
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-06-12