kabbouchi/nova-impersonate

Laravel Nova 字段允许您以用户身份进行认证。

v2.0.0 2022-05-08 20:30 UTC

README

Latest Version on Packagist Total Downloads

此字段允许您以用户身份进行认证。

screenshot1 screenshot2 screenshot3

幕后使用了 404labfr/laravel-impersonate

安装

您可以使用 composer 在使用 Nova 的 Laravel 应用中安装此包。

composer require kabbouchi/nova-impersonate

使用方法

App\Nova\User.php 中添加 Impersonate::make($this) 字段。

<?php

namespace App\Nova;

use KABBOUCHI\NovaImpersonate\Impersonate;

...

class User extends Resource
{
	...
	
	public function fields(Request $request)
	{
		return [
			ID::make()->sortable(),

			Gravatar::make(),

			Text::make('Name')
				->sortable()
				->rules('required', 'max:255'),

			Text::make('Email')
				->sortable()
				->rules('required', 'email', 'max:255')
				->creationRules('unique:users,email')
				->updateRules('unique:users,email,{{resourceId}}'),

			Password::make('Password')
				->onlyOnForms()
				->creationRules('required', 'string', 'min:6')
				->updateRules('nullable', 'string', 'min:6'),


			Impersonate::make($this),  // <---
		
			// or
            
            Impersonate::make($this->resource), // works in lenses
            
            // or
		
			Impersonate::make($this)->withMeta([
			    'hideText' => false,
			]),
		
			// or
		
			Impersonate::make($this)->withMeta([
			    'redirect_to' => '/custom-redirect-url'
			]),

		];
	}

    ...
}

高级使用

默认情况下,所有用户都可以 模拟 用户。
您需要向您的用户模型添加 canImpersonate() 方法。

    /**
     * @return bool
     */
    public function canImpersonate($impersonated = null)
    {
        // For example
        return $this->is_admin == 1;
    }

默认情况下,所有用户都可以 被模拟
您需要向您的用户模型添加 canBeImpersonated() 方法以扩展此行为:请确保传递实例模型或 Nova 资源 Impersonate::make($this) Impersonate::make($this->resource)

    /**
     * @return bool
     */
    public function canBeImpersonated(?\Illuminate\Contracts\Auth\Authenticatable $impersonator = null)
    {
        // For example
        return $this->can_be_impersonated == 1;
    }

默认情况下使用 name 字段来显示某一刻正在模拟的用户。您需要向您的用户模型添加 impersonateName() 方法以扩展此行为:请确保传递实例模型或 Nova 资源 Impersonate::make($this) Impersonate::make($this->resource)

    /**
     * @return string
     */
    public function impersonateName()
    {
        // For example
        return $this->email;
    }

事件

您可以将钩子连接到底层包的事件

这可能对设置会话数据等问题很有用

  • Lab404\Impersonate\Events\TakeImpersonation
  • Lab404\Impersonate\Events\LeaveImpersonation

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag=nova-impersonate-config

这是在 config/nova-impersonate.php 中发布的配置文件的默认内容

<?php

return [
	'enable_middleware' => true, // To inject the 'nova-impersonate::reverse' view in every route when impersonating 
	'redirect_back'     => true, // false (nova path), true or <url>
	'redirect_to'       => '/',
	'key_down'          => 'i', // Press `i` to impersonate user in details page
	'middleware'        => [
            'base' => 'web', // Middleware used for nova-impersonate routes
            'leave'  => 'auth', // Extra middleware used for leave route
    ],
];

您可以使用以下命令发布并自定义 nova-impersonate::reverse 视图

php artisan vendor:publish --tag=nova-impersonate-views

致谢

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件