quazardous/silex-user-pack

Silex 的用户包

dev-master / 1.0.x-dev 2016-06-01 14:23 UTC

This package is auto-updated.

Last update: 2024-09-20 09:46:58 UTC


README

Silex 用户包提供基本的用户 安全提供者

它受到了 Silex SimpleUser 的启发

它基于 Silex 2.x 安全提供者文档

功能

  • 登录表单
  • 注册表单,带有邮箱验证
  • 找回密码表单

安装

composer require quazardous/silex-user-pack

使用

有关如何使用包的更多信息,请参阅 Silex pack

实例化一个 PackableApplication

您必须扩展 Quazardous\Silex\PackableApplication 并使用

  • Silex\Application\TwigTrait
  • Silex\Application\UrlGeneratorTrait
  • Silex\Application\FormTrait
namespace Acme {
    class Application extends \Quazardous\Silex\PackableApplication {
        use \Silex\Application\TwigTrait;
        use \Silex\Application\UrlGeneratorTrait;
        use \Silex\Application\FormTrait;
    };
}

$app = new \Acme\Application;

设置 Doctrine ORM

用户包默认包含 Doctrine ORM 用户提供者。

// provide Doctrine
$app->register(new \Silex\Provider\DoctrineServiceProvider, [
    ...
]);

// provide Doctrine ORM
$app->register(new \Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProviderDoctrineOrmServiceProvider, [
    ...
]);

设置表单

Symfony 表单组件需要验证和翻译。

...
$app->register(new \Silex\Provider\ValidatorServiceProvider());
// provide Symfony Doctrine Bridge for UniqueEntity validator
$app->register(new \Saxulum\DoctrineOrmManagerRegistry\Provider\DoctrineOrmManagerRegistryProvider());
$app->register(new \Silex\Provider\LocaleServiceProvider);
$app->register(new \Silex\Provider\TranslationServiceProvider(), [
    'locale' => 'fr',
    'locale_fallbacks' => ['en'],
]);
$app->register(new \Silex\Provider\CsrfServiceProvider());
$app->register(new \Silex\Provider\FormServiceProvider());
...

设置安全

用户包符合安全提供者。

它将尝试使设置更简单,并在安全层中猜测注入简单逻辑。

// provide security
$app->register(new SecurityServiceProvider(), [
    'security.firewalls' => [
        'secured' => array(
            'pattern' => '^/admin/',
            'form' => [
                // user pack will populate the missing mandatory options but you have to set the 'form' key.
                // 'login_path' => '/login',
                // not specifying the login_path here means that user pack has to provide the path and the controller
                // 'check_path' => '/admin/login_check'
                // you can add all the custom scurity options you need
                'default_target_path' => '/admin',
                'failure_path' => '/',
            ], 
            'logout' => [
                // user pack will populate the missing mandatory options but you have to set the 'logout' key.
                //'logout_path' => '/admin/logout',
                //'invalidate_session' => true,
            ], 
            'users' => null, // if empty or not set, user pack will provide it for you with the built in Doctrine implementation.
        ),
    ],
]);

注册用户包

// register the user pack wich tries to make the authentication easier...
// the user pack will try to complete the 'security.firewalls' for you.
// it will also provide a basic Doctrine ORM user provider and an overridable login form.
$app->register(new SilexUserPack(), [
    'user.firewalls' => [
        // one or more firewalls to manage, see below
        'secured' => [
            // 'secured_mount_prefix' => '/admin' // user pack will try to guess it from the 'pattern' key
            // you can specify non default values for:
            // 'login_path' => '/login', // default, prefixed with 'unsecured_mount_prefix'
            // 'check_path' => '/check_login', // default, prefixed with 'secured_mount_prefix'
            // 'logout_path' => '/logout', // default, prefixed with 'secured_mount_prefix'
            // 'invalidate_session' => true, // default
            // 'register_path' => '/register', // default, prefixed with 'unsecured_mount_prefix'
            // 'recover_password_path' => '/recover_password', // default, prefixed with 'unsecured_mount_prefix'
            // 'mailer_from' => '', // default, the from for the messages sent for registration
        ],
    ]    
]);

...

自动创建路由 user.login。其名称源自完整的挂载 login_path(所有 / 都被替换为 _,并删除开头的 /,然后前缀为用户包的驼峰式简短命名空间加 .)。

自动创建路由 user.register。其名称源自完整的挂载 register_path(所有 / 都被替换为 _,并删除开头的 /,然后前缀为用户包的驼峰式简短命名空间加 .)。

注意:用户包应该挂载在 / 上(选项 user.mount_prefix,见 Optionnable Pack);

选项

注意:前缀 user. 是从包名(驼峰式简短命名空间)派生的。

user.firewalls

user.firewalls 是 Silex 用户包要管理的防火墙列表。

...
'user.firewalls' => [
        // one or more firewalls to manage
        'secured' => [/* specific options see below */]
        ],
...

Silex 用户包将尝试猜测防火墙认证提供者并注入所需配置。它知道如何管理 form,对于其他情况则注入 users 用户提供者。对于 form,您必须至少放置一个空的 form 条目(见上文)。用户包还将尝试设置 logout

  • unsecured_mount_prefix:默认为 /
  • secured_mount_prefix:默认情况下,我们根据 pattern 键猜测
  • login_path:默认为 /login,前缀为 unsecured_mount_prefix
  • check_path:默认为 /check_login,前缀为 secured_mount_prefix
  • logout_path:默认为 /logout,前缀为 secured_mount_prefix
  • register_path:默认为 /register,前缀为 unsecured_mount_prefix,可以是 false 以不处理注册
  • recover_password_path:默认为 /recover_password,前缀为 unsecured_mount_prefix
  • use_email_as_username:默认为 false,如果为 true,则注册表单不会要求用户名

其他选项

请参阅 $app['user.default.options']

  • user.user_entity_class:用于用户实体的类

演示

克隆 Silex pack 演示