定制 laravolt/auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravolt/auth

最新稳定版本:4.10.5

Composer 安装命令:

composer require laravolt/auth

包简介

Laravel auth extended

README 文档

README

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

Laravel authentication with some additional features:

  • Activation
  • Enable/disable registration
  • Captcha
  • Custom email template
  • Functionally tested

Installation

  • Run composer require laravolt/auth
  • For Laravel 5.4 or below, add Laravolt\Auth\ServiceProvider::class as service providers
  • Optionally, you can run php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider" --tag="migrations" to publish migrations files for further editing

Configuration

<?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

If you enable captcha (by setting 'captcha' => true in config file), please add following entries to .env:

NOCAPTCHA_SECRET=YOUR_RECAPTCHA_SECRET
NOCAPTCHA_SITEKEY=YOUR_RECAPTCHA_SITEKEY

You can obtain them from www.google.com/recaptcha/admin.

Custom Login Form

Modify Form (View File)

Run php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider". You can modify the view located in resources/views/vendor/auth/login.blade.php.

Modify Logic

Create new class to handle user login that implements Laravolt\Auth\Contracts\Login contract. You must implement two method related to registration:

  1. rules(Request $request) to get validation rules.
  2. credentials(Request $request) to check valid credentials. optionally:
  3. authenticated(Request $request, $user) to handle after login, it should be returned \Illuminate\Http\Response or null
  4. failed(Request $request) to handle custom failed response

Custom Registration Form

Sometimes you need to modify registration form, e.g. add more fields, change logic, or add some validation. There are several way you can accomplish those.

Modify Form (View File)

Run php artisan vendor:publish --provider="Laravolt\Auth\ServiceProvider". You can modify the view located in resources/views/vendor/auth/register.blade.php.

Modify Logic

Create new class to handle user registration that implements Laravolt\Auth\Contracts\UserRegistrar contract. You must implement two method related to registration:

  1. validate($data) to handle validation logic.
  2. register($data) to handle user creation logic. optionally:
  3. registered(Request $request, $user) to handle after registration is completed, it should be returned \Illuminate\Http\Response or null
<?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;
    }
}

Modify Activation Logic

add Laravolt\Auth\Contracts\ShouldActivate implementation to your registration.implementation class by add these function to your registrar class.

  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'));
    }
}

After that, you must update auth config (located in config/laravolt/auth.php, if not, just run 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

Environment Variables

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

laravolt/auth 适用场景与选型建议

laravolt/auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.54k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2015 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「auth」 「laravel」 「laravolt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 laravolt/auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravolt/auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravolt/auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-28