easycorp/easy-security-bundle

此包已被弃用,不再维护。未建议替代包。

Symfony应用程序的有用安全相关快捷方式

v1.0.4 2017-08-29 15:01 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:01:05 UTC


README

EasySecurityBundle

此包提供了一些有用的快捷方式,以简化Symfony安全组件的复杂性。

安装

步骤1:下载包

$ composer require easycorp/easy-security-bundle

此命令要求您全局安装Composer,如Composer文档中所述。

步骤2:启用包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new EasyCorp\Bundle\EasySecurityBundle\EasySecurityBundle(),
        );
    }

    // ...
}

基本用法

安装完成后,此包创建了一个名为 security 的新服务,提供了许多常见的安全操作的快捷方式。与Symfony安全组件/包相比的主要优点是

1) 它隐藏了内部复杂性

安全组件和包是Symfony中最复杂的组件之一。它们要求您学习许多您可能不关心的内部细节

// get the current user
$user = $this->get('security.token_storage')->getToken()->getUser();
// check their permissions
$user = $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN');
// get the last login attempt error, if any
$error = $this->get('security.authentication_utils')->getLastAuthenticationError();

此包通过将所有操作集中到 security 服务下,隐藏了这种复杂性

// get the current user
$user = $this->get('security')->getUser();
// check their permissions
$user = $this->get('security')->isGranted('ROLE_ADMIN');
// get the last login attempt error, if any
$error = $this->get('security')->getLoginError();

2) 它使代码更加简洁

有时,完成常见任务所需的代码非常冗长。例如,要程序化地登录用户,Symfony要求您执行以下操作

$user = ...
$token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles());
$token->setAuthenticated(true);
$this->get('security.token_storage')->setToken($token);
$this->get('session')->set('_security_main', serialize($token));
$this->get('session')->save();

此包使登录用户尽可能简单

$user = ...
$this->get('security')->login($user);

3) 它修复了一些不直观的行为

在Symfony应用程序中,检查用户是否为匿名、记住或完全认证的方式并不像大多数人预期的那样工作。例如,如果用户使用表单登录通过用户名 + 密码登录,则会出现以下情况

// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY');
// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED');
// returns true
$this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY');

Symfony将匿名和记住的属性授予完全认证的用户,因此区分它们变得复杂。此包改变了这种不直观的行为,并帮助您知道用户是否真正匿名、记住或认证。在上面的例子中

// returns false
$this->get('security')->isAnonymous();
// returns false
$this->get('security')->isRemembered();
// returns true
$this->get('security')->isFullyAuthenticated();

注入 security 服务

如果注入 security 服务,则可以使用这些快捷方式跨越您的应用程序。例如,如果您以YAML格式定义服务

# app/config/services.yml
services:
    app.my_service:
        # ...
        arguments: ['@security']

然后,更新您服务的构造函数以获取 security 服务

// src/AppBundle/MyService.php
// ...
use EasyCorp\Bundle\EasySecurityBundle\Security\Security;

class MyService
{
    private $security;

    public function __construct(Security $security)
    {
        $this->security = $security;
    }

    public function myMethod()
    {
        // ...
        $user = $this->security->getUser();
    }
}

快捷方式列表

获取用户

  • getUser():返回当前应用程序用户。
  • getImpersonatingUser():在模拟用户时,它返回开始模拟的原用户。

检查权限

  • isGranted($attributes, $object = null):检查当前应用程序用户和可选给出的对象是否授予了属性(通常为安全角色)。
  • hasRole($role, $user = null):如果当前应用程序用户(或可选给出的用户)具有指定的角色,则返回 true。它考虑了完整的角色层次结构。

用户类型

  • isAnonymous($user = null):如果当前应用程序用户(或可选给出的用户)是匿名用户,则返回 true。这与 Symfony 内置方法的行为不同,并且仅在用户真正匿名时返回 true
  • isRemembered($user = null):如果当前应用程序用户(或可选给出的用户)被记住,则返回 true。这与 Symfony 内置方法的行为不同,并且仅在用户真正被记住并且没有输入他们的凭证(用户名和密码)时返回 true。
  • isFullyAuthenticated($user = null):如果当前应用程序用户(或可选给出的用户)因为输入了他们的凭证(用户名和密码)而认证,则返回 true
  • isAuthenticated($user = null):如果当前应用程序用户(或可选给出的用户)以任何方式认证(因为他们输入了他们的凭证(用户名和密码)或者他们已经被记住),则返回 true

登录

  • login(UserInterface $user, $firewallName = 'main'):将给定用户登录到 main 应用程序防火墙(或可选给出的防火墙名称)。
  • getLoginError():如果有的话,返回最后一次失败的登录尝试的错误。
  • getLoginUsername():如果有的话,返回最后一次失败的登录尝试的用户名。

密码

  • encodePassword($plainPassword, $user = null):使用当前应用程序用户或可选给出的用户的编码器将给定的明文密码编码/哈希。
  • isPasswordValid($plainPassword, $user = null):如果给定的明文密码对当前应用程序用户或可选给出的用户有效,则返回 true