laravolt/auth

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

Laravel 扩展认证

4.10.5 2020-10-28 05:01 UTC

README

https://travis-ci.org/laravolt/auth https://coveralls.io/github/laravolt/auth SensioLabsInsight

具有一些附加功能的 Laravel 认证

  • 激活
  • 启用/禁用注册
  • 验证码
  • 自定义电子邮件模板
  • 功能测试

安装

  • 运行 composer require laravolt/auth
  • 对于 Laravel 5.4 或更低版本,添加 Laravolt\Auth\ServiceProvider::class 作为服务提供者
  • 可选,您可以运行 php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider" --tag="migrations" 以发布迁移文件以便进一步编辑

配置

<?php
return [
    // Base layout to extend by every view
    'layout'       => 'ui::layouts.auth',

    // Enable captcha (Google reCaptcha) on login form
    'captcha'      => false,

    // Column name to be checked for authentication (login)
    'identifier'   => 'email',

    // Configuration related to login process
    'login' => [
        'implementation' => \Laravolt\Auth\DefaultLogin::class,
    ],

    // Configuration related to registration process
    'registration' => [

        // Enable or disable registration form
        'enable'         => true,

        // Default status for newly registered user
        'status'         => 'ACTIVE',

        // During the process, data from registration form will be passed to this class.
        // You may create your own implementation by creating UserRegistrar class.
        'implementation' => \Laravolt\Auth\DefaultUserRegistrar::class,
    ],

    // Configuration related to registration process
    'activation'   => [
        // If enabled, newly registered user are not allowed to login until they click
        // activation link that sent to their email.
        'enable'        => false,

        // Status for newly registered user, before activation
        'status_before' => 'PENDING',

        // Status for newly registered user, after successfully activate their account
        'status_after'  => 'ACTIVE',
    ],

    // Routes configuration
    'router'       => [
        'middleware' => ['web'],
        'prefix'     => 'auth',
    ],

    // Redirect configuration
    'redirect'     => [
        // Where to redirect after successfully login
        'after_login'          => '/',

        // Where to redirect after successfully register
        'after_register'       => '/',

        // Where to redirect after successfully reset password
        'after_reset_password' => '/',
    ],

    // Whether to auto load migrations or not.
    // If set to false, then you must publish the migration files first before running the migrate command
    'migrations' => false,
];

验证码

如果您启用验证码(在配置文件中将 'captcha' => true 设置为 true),请将以下条目添加到 .env

NOCAPTCHA_SECRET=YOUR_RECAPTCHA_SECRET
NOCAPTCHA_SITEKEY=YOUR_RECAPTCHA_SITEKEY

您可以从 www.google.com/recaptcha/admin 获取它们。

自定义登录表单

修改表单(视图文件)

运行 php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider"。您可以修改位于 resources/views/vendor/auth/login.blade.php 的视图。

修改逻辑

创建一个新类来处理用户登录,该类实现了 Laravolt\Auth\Contracts\Login 协议。您必须实现两个与注册相关的方法

  1. rules(Request $request) 获取验证规则。
  2. credentials(Request $request) 检查有效凭证。可选
  3. authenticated(Request $request, $user) 处理登录后,应返回 \Illuminate\Http\Responsenull
  4. failed(Request $request) 处理自定义失败响应

自定义注册表单

有时您需要修改注册表单,例如添加更多字段、更改逻辑或添加一些验证。您可以通过几种方法来完成这些操作。

修改表单(视图文件)

运行 php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider"。您可以修改位于 resources/views/vendor/auth/register.blade.php 的视图。

修改逻辑

创建一个新类来处理用户注册,该类实现了 Laravolt\Auth\Contracts\UserRegistrar 协议。您必须实现两个与注册相关的方法

  1. validate($data) 处理验证逻辑。
  2. register($data) 处理用户创建逻辑。可选
  3. registered(Request $request, $user) 处理注册完成后,应返回 \Illuminate\Http\Responsenull
<?php
namespace App\Registration;

use Illuminate\Support\Facades\Validator;
use Laravolt\Auth\Contracts\UserRegistrar;

class CustomUserRegistrar implements UserRegistrar
{
    /**
     * Validate data.
     *
     * @param array $data
     */
    public function validate(array $data)
    {
        // Modify default behaviour, or completely change it
        return Validator::make(
            $data,
            [
                'name'     => 'required|max:255',
                'email'    => 'required|email|max:255|unique:users',
                'password' => 'required|min:6',
            ]
        );
    }

    /**
     * Create model.
     *
     * @param $
     *
     */
    public function register(array $data)
    {
        // create Authenticatable model.
        $user = User::create($data);

        // return Authenticatable model.
        return $user;
    }
}

修改激活逻辑

通过将这些函数添加到您的注册器类中,将 Laravolt\Auth\Contracts\ShouldActivate 实现添加到您的 registration.implementation 类中。

  1. notify(Model $user, $token)
  2. activate($token)
...
class CustomUserRegistrar implements UserRegistrar, ShouldActivate
{
    ...

    /**
     * Notify if user to activate the user with the token provided.
     *
     * @param \Illuminate\Database\Eloquent\Model|Authenticatable $user
     * @param string $token
     * @return void
     */
    public function notify(Model $user, $token)
    {
        //
    }

    /**
     * Activation method by the token provided.
     *
     * @param string $token
     * @return \Illuminate\Http\Response
     */
    public function activate($token)
    {
        $token = \DB::table('users_activation')->whereToken($token)->first();

        if (! $token) {
            abort(404);
        }

        \User::where('id', $token->user_id)->update(['status' => config('laravolt.auth.activation.status_after')]);
        \DB::table('users_activation')->where('user_id', $token->user_id)->delete();

        return redirect()->route('auth::login')->withSuccess(trans('auth::auth.activation_success'));
    }
}

之后,您必须更新认证配置(位于 config/laravolt/auth.php,如果没有,只需运行 php artisan vendor:publish)。

...
    'registration' => [
        // During the process, data from registration form will be passed to this class.
        // You may create your own implementation by creating UserRegistrar class.
        'implementation' => \App\Registration\CustomUserRegistrar::class,
    ],

...

LDAP

环境变量

LDAP_HOSTS=ldap.forumsys.com
LDAP_BASE_DN='dc=example,dc=com'
LDAP_PORT=389
LDAP_USERNAME='cn=read-only-admin,dc=example,dc=com'
LDAP_PASSWORD='password'
LDAP_USE_SSL=false
LDAP_USE_TLS=false