ahsankhatri/wordpress-auth-provider 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ahsankhatri/wordpress-auth-provider

Composer 安装命令:

composer require ahsankhatri/wordpress-auth-provider

包简介

A package to provide wordpress users in laravel authentication system.

README 文档

README

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Intelligence Status License

Laravel wordpress-auth-driver-laravel
5.2 to 5.5 ^1.0
5.6 to 8.x ^2.0

Installation

To install this package you will need

The best way to install this package is with the help of composer. Run

composer require ahsankhatri/wordpress-auth-provider

or install it by adding it to composer.json then run composer update

"require": {
    "ahsankhatri/wordpress-auth-provider": "^2.0",
}

Setup

Once you have installed this package from the composer, make sure to follow the below steps to configure.

To register authentication guard.

config/auth.php
'guards' => [
    ...,
    'wordpress' => [
        'driver' => 'session',
        'provider' => 'wordpress',
    ],
'providers' => [
    ...,
    'wordpress' => [
        'driver' => 'eloquent.wordpress',
        'model' => MrShan0\WordpressAuth\Models\WordpressUser::class,
    ],
'passwords' => [
    ...,
    'wordpress' => [
        'provider' => 'wordpress',
        'table' => 'password_resets',
        'expire' => 60,
    ],

Publish config file (optional)

php artisan vendor:publish --provider="MrShan0\WordpressAuth\WordpressAuthServiceProvider"

It will publish config file (config/wordpress-auth.php) where you can define your own connection type e.g wp-mysql. Make sure to fill prefix in config/database.php for wp_ prefix in your tables if you're using prefix in wordpress tabels.

For example:

'wp-mysql' => [
    'driver' => 'mysql',
    'host' => env('WP_DB_HOST', '127.0.0.1'),
    'port' => env('WP_DB_PORT', '3306'),
    'database' => env('WP_DB_DATABASE', 'forge'),
    'username' => env('WP_DB_USERNAME', 'forge'),
    'password' => env('WP_DB_PASSWORD', ''),
    'unix_socket' => env('WP_DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => 'wp_',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

Add following option along if using Laravel v7 (optional)

    // ...
    'url' => env('DATABASE_URL'),
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],

Configuration

password_resets table (from Laravel default auth mechanism) is required to hold reset password token. If you do not have password_resets table then use this migration instead

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

Extension

Alternatively, if you want to use a custom user model, you should have it extend MrShan0\WordpressAuth\Models\WordpressUser and specify the name of your model in config/auth.php under providers -> wordpress -> model.

Customization

If you've renamed your user_email column of wordpress database, you need to first publish configurations of this package if you've not already, extend the model as mentioned above and make sure you've override your changes in your $fillable property and config/wordpress-auth.php config file which is being used for authentication scaffolding and sending notifications.

Usage

You need to define wordpress guard explicitly to load the driver.

Examples

Auth::guard('wordpress')->loginUsingId(5);

// or login using email and password
Auth::guard('wordpress')->attempt([
    'user_email' => 'demo@example.com',
    'user_pass' => 'quickbrownfox'
]);

// get user object
Auth::guard('wordpress')->user();

// Update wordpress compatible password
$user->user_pass = app('wordpress-auth')->make('new_password');
$user->save();

// logout
Auth::guard('wordpress')->logout();

You may also change default guard in config/auth.php then your code will look like

Auth::loginUsingId(5);

If you haven't set default guard and wanted to take advantage of Password Resets (Auth Scaffolding) in laravel. You may need to define guard and broker explicitly in Auth/ForgotPasswordController.php and Auth/ResetPasswordController.php as

/**
 * Get the broker to be used during password reset.
 *
 * @return \Illuminate\Contracts\Auth\PasswordBroker
 */
public function broker()
{
    return \Password::broker('wordpress');
}

/**
 * Get the guard to be used during password reset.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return \Auth::guard('wordpress');
}

Changelog

CHANGELOG

Credits

Thanks to the community of Laravel.

Copyright and License

Copyright (c) 2016 Ahsaan Muhammad Yousuf, MIT License

ahsankhatri/wordpress-auth-provider 适用场景与选型建议

ahsankhatri/wordpress-auth-provider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.68k 次下载、GitHub Stars 达 52, 最近一次更新时间为 2017 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 52
  • Watchers: 3
  • Forks: 34
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-10