linkorb/silex-provider-user-basic

为Silex应用定制的Symfony安全用户和内存用户提供程序。

v1.0 2017-05-15 13:57 UTC

This package is auto-updated.

Last update: 2024-08-26 13:47:33 UTC


README

为Silex应用定制的Symfony安全用户和内存用户提供程序。

首先,创建一个用户数组:

// for example, in src/app.php
$users = [
    'jo' => [
        'password' => '$2y$12$G7...',
        'roles' => ['admin'],
        'display_name' => 'Joseph',
        'enabled' => false,
    ],
    'jen' => [
        'password' => '$2y$12$oD...',
        'roles' => ['admin'],
        'display_name' => 'Jenifer',
    ],
];

password 的值是哈希后的密码;这些可以使用附带的密码哈希命令生成:

$ vendor/bin/hashpasswd -h
Usage:
  hashpasswd [options]
Options:
  -c, --cost=COST       Algorithmic cost of the hash function. [default: 12]
  -h, --help            Display this help message
Help:
  The command interactively asks for the password before printing its hash.

接下来,注册Silex的核心SecurityServiceProvider并提供一个防火墙参数:

$app->register(
    new SecurityServiceProvider,
    [
        'security.firewalls' => [
            'api' => [
                'pattern' => '^/api',
                'http' => true,
                'users' => function () use ($users) {
                    return new \LinkORB\BasicUser\Provider\UserProvider($users);
                },
            ],
        ],
    ]
);