codenamegary/l4-lock

这是一个 Laravel 4 的包,用于锁定网站并在执行身份验证之前要求简单的用户名/密码。这对于在上线前锁定网站以便客户测试,同时不干扰身份验证和其他标准框架选项非常有用。

v0.2.2-beta 2014-06-02 18:45 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:09:43 UTC


README

# Laravel 的锁定功能

Lock 是一个 Laravel 包,允许你在开发网站时非常快速且几乎不需要任何努力即可实现全局或选择性身份验证提示。

它高度可配置,允许你非常快速地更新登录屏幕的风格和内容,用户和 URL。

Login Screenshot

##安全提示

Lock 的主要目的是在开发过程中保护应用程序,同时使用密码而不干扰 Laravel 内置的身份验证驱动程序。它与内置的 Auth 完全不同,允许你为客户端访问只保护应用程序的所有部分,同时进行开发。

Lock 内置了一个简单的基于配置文件的用户名 => 密码验证器,用于验证用户名和密码。用户以纯文本形式存储在配置文件中。验证器绑定是可配置的,使用不同的方法验证用户只需创建自己的 Validator 类,实现 ValidatorInterface 即可。

有关更多信息,请参阅“自定义验证器”。

Lock 需要默认的 Laravel 会话来存储凭据,因此在使用之前必须配置并正常工作。

###安装

注意: 安装后,lock 将默认全局启用。您可以通过包配置来禁用它。

####Laravel 4.1

将 Lock v0.2.1-beta 添加到您的 composer.json。

"require": {
    "codenamegary/l4-lock": "v0.2.1-beta"
}

####Laravel 4.2 或更高版本

将 Lock v0.2.2-beta 或更高版本添加到您的 composer.json。

"require": {
    "codenamegary/l4-lock": "v0.2.2-beta"
}

Composer Update

composer update

将服务提供者添加到 app/config/app.php 的 providers 数组中。

'providers' => array(
    ...
    'codenamegary\Lock\LockServiceProvider',
),

使用 Artisan 发布配置。

php artisan config:publish codenamegary/l4-lock

在 app/config/packages/codenamegary/l4-lock/lock.php 下的配置中编辑或添加用户。

/**
 * Valid username and password combinations used by the default validator.
 */
'users' => array(
    'client' => 'alwaysright',
),

###自定义验证器

Lock 配置中包含用于验证用户名和密码的验证器绑定的设置。Validator 接口(如下)包含 1 个方法。

interface ValidatorInterface {
    
    /**
     * Check the given username and password, return true if they
     * are valid, false if not.
     * 
     * @param string $username
     * @param string $password
     * @return bool
     */
    public function valid($username, $password);
    
}

要实现自己的验证器,请按照以下步骤操作。

####创建你的验证器类并实现 ValidatorInterface。

class CustomLockValidator implements codenamegary\Lock\ValidatorInterface {

    public function valid($username, $password)
    {
        ... validate the username and password ...
        ... return true ...
        ... (or) ...
        ... return false ...
    }

}

####在容器中注册你的验证器

为了方便和允许你将可能需要的任何依赖注入到验证器中,将你的自定义验证器绑定到容器中。

示例

$app->bindShared('custom.lock.validator', function($app){
    $validatorDependency1 = $app['some.dependency'];
    $validatorDependency2 = $app['some.other.dependency'];
    return new CustomLockValidator($validatorDependency1, $validatorDependency2);
});

####更新 lock 配置以使用你创建的绑定

在 app/config/packages/codenamegary/l4-lock/lock.php 中,编辑验证器绑定。

/**
 * This refers to the app binding for the validator class that should
 * be used to validate usernames and passwords.
 */
'validator' => 'custom.lock.validator',

这就完成了!Lock 现在将调用验证器的 valid 方法,每当用户尝试登录时。

##选择性身份验证

你可能希望禁用全局身份验证过滤器,而不是选择性地为要保护的任何路由启用它。

首先禁用 app/config/packages/codenamegary/l4-lock/lock.php 中的全局身份验证设置。

/**
 * Enforce the lock across the entire site. If you'd rather enable
 * the lock selectively, disable this config option and apply
 * the lock.auth filter on whichever routes you want locked.
 */
'global' => false,

然后,将身份验证过滤器应用于你想要保护的任何路由。

Route::get('/', array(
    'before' => 'l4-lock.auth',
    function()
    {
       return View::make('hello')->render();
    }
));

###全局过滤器异常

如果你希望启用全局过滤器并添加一些异常,只需保持全局配置选项启用,并将你想要排除的模式和字符串添加到异常配置中。

/**
 * Here you can provide a list of regex URI patterns that will be excluded
 * from the global filter. These are checked against the actual URI so
 * so to exclude 'http://domain/thing/*', you would add an exception
 * for '/thing\/.*?/'.
 */
'exceptions' => array(
    // Route exception patterns go here
),

##自定义登录屏幕

Lock 内置了基于 Bootstrap3 风格的登录屏幕。配置中包含了许多设置,可以轻松更改登录提示,甚至使用不同的布局或视图。

/**
 * View that will be used for the login screen and related paramters.
 */
'views' => array(
    // The layout to use for the login screen
    'layout' => 'l4-lock::layout',
    // The section to put the login form inside the template
    'section' => 'content',
    // The view for the login screen
    'login' => 'l4-lock::login',
    // Title for the form legend
    'title' => 'ACCESS RESTRICTED',
    // Prompt displayed on the login form
    'prompt' => 'Access to this site is restricted. Please login to continue.',
    // Use this to add a foot note to the login screen if desired, you can also
    // specify the name of a partial here and it will be rendered for you.
    // e.g. - contact abc@xyz.com for support.
    'foot-note' => false,
),

##自定义 URL

默认情况下,Lock 使用 /lock/login 和 /lock/logout 提供登录/注销功能。这些也可以进行配置。

/**
 * URLs that lock will respond with / use.
 */
'urls' => array(
    'login' => 'lock/login',
    'logout' => 'lock/logout',
),