maize-tech/laravel-tenant-aware

Laravel 租户感知

2.1.0 2024-04-11 14:57 UTC

This package is auto-updated.

Last update: 2024-09-08 22:54:58 UTC


README

Laravel 租户感知

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

轻松将单数据库多租户功能集成到您的 Laravel 应用程序中。

安装

您可以通过 composer 安装此包

composer require maize-tech/laravel-tenant-aware

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

php artisan vendor:publish --tag="tenant-aware-config"

这是发布配置文件的内容

return [

    'tenant' => [

        /*
        |--------------------------------------------------------------------------
        | Tenant Model
        |--------------------------------------------------------------------------
        |
        | Here you may specify the fully qualified class name of the tenant model.
        |
        */

        'model' => null,

        /*
        |--------------------------------------------------------------------------
        | Tenant key name
        |--------------------------------------------------------------------------
        |
        | Here you may specify the column name for the tenant identifier.
        | All models using the multi-tenant feature should include this field
        | in their migration.
        |
        */

        'foreign_key_name' => 'tenant_id',

        /*
        |--------------------------------------------------------------------------
        | Actions
        |--------------------------------------------------------------------------
        |
        | Here you may specify the list of actions used to retrieve che current
        | and landlord tenants.
        |
        */

        'actions' => [
            'current' => Maize\TenantAware\Actions\TenantCurrentAction::class,
            'landlord' => Maize\TenantAware\Actions\TenantLandlordAction::class,
            'current_or_landlord' => Maize\TenantAware\Actions\TenantCurrentOrLandlordAction::class,
            'only_current' => Maize\TenantAware\Actions\TenantOnlyCurrentAction::class,
        ],

    ],

    'models' => [
        /*
        |--------------------------------------------------------------------------
        | Global Models
        |--------------------------------------------------------------------------
        |
        | Here you may specify the full list of global models who should return
        | all entities, including the ones related to the landlord tenant.
        |
        */

        'global' => [
            // App\Models\Article::class,
        ],

        'listen' => [

            /*
            |--------------------------------------------------------------------------
            | Set tenant key
            |--------------------------------------------------------------------------
            |
            | Here you may specify the action invoked when creating a multi-tenant
            | model entity.
            | By default, the action sets the tenant field to the current tenant key.
            |
            */

            'creating' => Maize\TenantAware\Listeners\SetTenantKey::class,
        ],
    ],

    'scope' => [

        /*
        |--------------------------------------------------------------------------
        | Scope apply
        |--------------------------------------------------------------------------
        |
        | Here you may override the default scope method applied to all models
        | who belong to a tenant.
        | The default class adds a where constraint to exclude entities not related
        | to the current user's tenant or to the landlord tenant.
        |
        */

        'apply' => Maize\TenantAware\Scopes\ScopeTenantAware::class,

        /*
        |--------------------------------------------------------------------------
        | Scope Methods
        |--------------------------------------------------------------------------
        |
        | Here you may override the default scope methods used to add the
        | where constraints to all models who belong to a tenant.
        |
        */

        'methods' => [
            Maize\TenantAware\Scopes\ScopeOrTenantWhere::class,
            Maize\TenantAware\Scopes\ScopeTenantWhere::class,
            Maize\TenantAware\Scopes\ScopeTenantAware::class,
        ],
    ],
];

使用方法

配置

在开始之前,请确保在 config/tenant-aware.php 中的 modelforeign_key_name 属性下填写。

此外,不要忘记定义自己的 TenantCurrentActionTenantLandlordAction 动作以检索当前租户和房东租户。

例如,如果您正在使用 Spatie 的 laravel-multitenancy 包,则自定义动作可能如下所示

<?php

namespace Support\TenantAware;

use Spatie\Multitenancy\Models\Tenant;

class TenantCurrentAction
{
    public function __invoke(): ?Model
    {
        return Tenant::current();
    }
}
<?php

namespace Support\TenantAware;

use Maize\TenantAware\Support\Config;
use Spatie\Multitenancy\Models\Tenant;

class TenantLandlordAction
{
    public function __invoke(): ?Model
    {
        $tenantModel = Config::getTenantModelName();
        $landlordCode = 'landlord'; // get landlord tenant from your config, env, or wherever you defined it

        return app($tenantModel)::query()
            ->firstWhere('code', $landlordCode);
    }
}

基本

要使用此包,请将 Maize\TenantAware\BelongsToTenant 特性添加到您想使用多租户功能的模型中。

请记住,所有您的多租户模型都应该在 config/tenant-aware.php 中的 foreign_key_name 属性下指定租户字段。

<?php

namespace App\Models;

use Maize\TenantAware\BelongsToTenant;

class User extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'fist_name',
        'last_name',
        'email',
        'tenant_id',
    ];
}

完成后,当查询任何多租户模型时,您将只检索与您同租户的实体。

假设,例如,我们数据库中有一个用户列表,当前认证用户是租户 2 的一部分

当查询用户模型时,您将只得到前三个用户

use App\Models\User;

User::all()->modelKeys(); // returns [1, 2, 3]

查询全局实体

有一个模型需要处理全局和租户相关实体吗?没关系!

您只需将模型类名添加到 config/tenant-aware.php 中的 models.global 属性下。同时,确保您的房东租户在自定义 TenantLandlordAction 动作类中返回!

'models' => [
    'global' => [
        App\Models\Course::class,
    ],
],

假设您有一个具有全局课程和每个租户自定义课程的学习多租户平台。

这就是您在查询租户 2 下的用户时从 Course 模型得到的

use App\Models\Course;

Course::all()->modelKeys(); // returns [1, 2, 3]

测试

composer test

更新日志

有关最近更改的更多信息,请参阅 更新日志

贡献

有关详细信息,请参阅 贡献

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

致谢

许可

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