承接 esalazarv/multiauth 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

esalazarv/multiauth

Composer 安装命令:

composer require esalazarv/multiauth

包简介

Multiple auth driver for Laravel 5

README 文档

README

For Laravel 5.1.0 version, see Branch https://github.com/esalazarv/multiauth/tree/L5.1.0 For Laravel 4.2 version, see https://github.com/ollieread/multiauth

IMPORTANT: Laravel 5.1 Default AuthController with its traits

\Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers

more specifically

Illuminate\Foundation\Auth\AuthenticatesUsers::postLogin()

This package is not a replacement for laravels default Auth library, but instead something that sits between your code and the library.

Think of it as a factory class for Auth. Now, instead of having a single table/model to authenticate users against, you can now have multiple, and unlike the previous version of this package, you have access to all functions, and can even use a different driver for each user type.

On top of that, you can use multiple authentication types, simultaneously, so you can be logged in as a user, a master account and an admin, without conflicts!

Installation

Firstly you want to include this package in your composer.json file.

    "require": {
    		"esalazarv/multiauth" : "5.0.*"
    }

Now you'll want to update or install via composer.

composer update

Next you open up app/config/app.php and replace the 'Illuminate\Auth\AuthServiceProvider', with

    'Ollieread\Multiauth\MultiauthServiceProvider',

and 'Illuminate\Auth\Passwords\PasswordResetServiceProvider' with

	'Ollieread\Multiauth\Passwords\PasswordResetServiceProvider',

NOTE It is very important that you replace the default service providers.

Remove the original database migration for password_resets.

##Configuration##

is pretty easy too, take config/auth.php with its default values:

    return [

		'driver' => 'eloquent',

		'model' => 'App\User',

		'table' => 'users',

		'password' => [
        		'email' => 'emails.password',
        		'table' => 'password_resets',
        		'expire' => 60,
        	],

	];

Now remove the first three options (driver, model and table) and replace as follows (Custom authentication drivers now work):

    return [

		'multi'	=> [
			'admin' => [
				'driver' => 'eloquent',
				'model'	=> 'App\Admin',
			],
			'client' => [
				'driver' => 'database',
				'table' => 'clients',
				'email' => 'client.emails.password',
			],
			'other' => [
				'driver' => 'customDriver',
				'model'	=> 'App\Other',
			],
		],

		'password' => [
        		'email' => 'emails.password',
        		'table' => 'password_resets',
        		'expire' => 60,
        	],

	];

This is an example configuration. Note that you will have to create Models and migrations for each type of user. Use App\User.php and 2014_10_12_000000_create_users_table.php as an example.

If you wish to use a reminders email view per usertype, simply add an email option to the type, as shown in the above example.

To generate the reminders table you will need to run the following command.

php artisan multiauth:resets-table

Likewise, if you want to clear all reminders, you have to run the following command.

php artisan multiauth:clear-resets

You will also need to change the existing default Laravel 5 files to accommodate multiple auth and password types. Do as described in this gist:

https://gist.github.com/sboo/10943f39429b001dd9d0

Usage

Everything is done the exact same way as the original library, the one exception being that all method calls are prefixed with the key (account or user in the above examples) as a method itself.

    Auth::admin()->attempt(array(
    	'email'		=> $attributes['email'],
    	'password'	=> $attributes['password'],
    ));
    Auth::client()->attempt(array(
    	'email'		=> $attributes['email'],
    	'password'	=> $attributes['password'],
    ));
    Auth::admin()->check();
    Auth::client()->check();

I found that have to call the user() method on a user type called user() looked messy, so I have added in a nice get method to wrap around it.

	Auth::admin()->get();

But if you prefer, you can specify which type of user you will use to use method uses('YourUserType'), once this is done you can access the current user as the original Auth Facade , as easy as Auth::user().

This method sets 'admin' as the current user with which to work.

	/** You can switch users as needed **/
	Auth::uses('admin');

Note: By default current user is the first in the config array.

You can now access the user just as you would with the original Facade Auth.

	/** Accessing the user using the 'user()' (original method) **/
	if(Auth::user()->check()){
		//
	}

	/** Accessing the user using the 'get()' (additional method)  **/
	if(Auth::get()->check()){
		//
	}

or using the descriptive methods that existed.

	if(Auth::admin()->user()->check()){
		//
	}

	if(Auth::admin()->get()->check()){
		//
	}

In the instance where you have a user type that can impersonate another user type, example being an admin impersonating a user to recreate or check something, I added in an impersonate() method which simply wraps loginUsingId() on the request user type.

	Auth::impersonate('client', 1, true);

or

	Auth::admin()->impersonate('client', 1, true);

The first argument is the user type, the second is the id of said user, and the third is whether or not to remember the user, which will default to false, so can be left out more often than not.

And so on and so forth.

Note: The method 'impersonate()' returns and sets the new user as the current user.

You can Know if an user is impersonated

	/** @return boolean **/
	if(Auth::isImpersonated()){
		//
	}

or

You may know the key name impersonator of current user

	/** @return string | null **/
	Auth::getImpersonatorName();

or specifying

	/** @return string | null **/
	Auth::client()->getImpersonatorName();

There we go, done! Enjoy yourselves.

Testing

Laravel integration/controller testing implements $this->be($user) to the base TestCase class. The implementation of #be() does not work correctly with Multiauth. To get around this, implement your own version of #be() as follows:

    public function authenticateAs($type, $user) {
      $this->app['auth']->$type()->setUser($user);
    }

License

This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced software licensed under the MIT license

esalazarv/multiauth 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 107
  • 开发语言: PHP

其他信息

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