cswni/filament-companies

基于 Filament 构建的公司之上的 Laravel 认证系统

1.1.10 2023-08-03 05:36 UTC

This package is auto-updated.

Last update: 2024-09-03 07:57:13 UTC


README

gif (1)

FILAMENT 8.x Packagist Downloads

Filament Companies

基于 Filament 构建公司的完整认证系统套件

  • 🔥 认证 - Fortify
  • 🔥 Socialite(可选)
  • 🔥 条款和隐私政策
  • 🔥 通过电子邮件重置密码
  • 🔥 个人资料管理
  • 🔥 双因素认证(2FA)
  • 🔥 浏览器会话管理
  • 🔥 Sanctum API
  • 🔥 公司管理
  • 🔥 通过电子邮件邀请员工
  • 🔥 角色和权限
  • 🔥 更多功能即将推出!

入门指南

警告

  • 此插件需要一个全新的 Filament 项目。
  • 如果您将此插件安装到现有的 Filament 项目中,您将遇到错误。

注意

设置环境

  • 创建一个新的 Laravel 项目
  • 配置数据库
  • 安装 filament admin 软件包
composer require filament/filament

安装

安装此软件包

composer require andrewdwallo/filament-companies

安装软件包后,您可能需要执行 filament-companies:install Artisan 命令。此命令需要堆栈名称为 filament,选项为 --companies。此外,您可以使用 --socialite 开关来启用 Socialite 支持。

使用以下命令之一生成应用程序

php artisan filament-companies:install filament --companies

php artisan filament-companies:install filament --companies --socialite

完成安装

php artisan migrate:fresh
npm run dev

翻译

如果您想翻译软件包,可以使用以下方法发布语言文件

php artisan vendor:publish --tag=filament-companies-translations

用法

如果您愿意,可以使用以下方式创建新账户

php artisan make:filament-companies-user

您也可以通过应用程序注册来创建新账户。

在 Laravel 欢迎页面中,您可以

  • 登录
  • 注册

在公司下拉菜单中,您可以

  • 创建新公司
  • 管理您当前公司的设置
  • 切换您当前公司

在用户下拉菜单中,您的头像所在位置,您可以

  • 创建 API 令牌
  • 管理您的个人资料设置

此软件包大量借鉴了 Taylor Otwell、他的贡献者和 Laravel Jetstream 软件包的工作。您可以通过审查 Jetstream 文档 来全面了解其功能。

如果您想更改 Filament 路径前缀为 "company" 等类似的内容,您可以在 config/filament.php 中像平常一样进行更改

    /*
    |--------------------------------------------------------------------------
    | Filament Path
    |--------------------------------------------------------------------------
    |
    | The default is `admin` but you can change it to whatever works best and
    | doesn't conflict with the routing in your application.
    |
    */

    'path' => env('FILAMENT_PATH', 'company'),

Laravel 欢迎页面和 Fortify 将尊重您的更改

Socialite

默认情况下,将启用 GitHub 提供商。

您可以使用任何 Laravel Socialite 支持的提供商。

您可以在 config/filament-companies.php 中添加或删除任何提供商

    /*
    |--------------------------------------------------------------------------
    | Socialite Providers
    |--------------------------------------------------------------------------
    |
    | Here you may specify the providers your application supports for OAuth.
    | Out of the box, FilamentCompanies provides support for all the OAuth
    | providers that are supported by Laravel Socialite.
    |
    */

    'providers' => [
        Providers::github(),
        Providers::google(),
        Providers::gitlab(),
        Providers::bitbucket(),
        Providers::facebook(),
        Providers::linkedin(),
        Providers::twitterOAuth1(),
        Providers::twitterOAuth2(),
    ],

如果需要 Twitter,您只能使用 Twitter OAuth1 或 Twitter OAuth2,不能同时使用两者。

如果您希望使用此语法,请按照以下步骤操作。

    /*
    |--------------------------------------------------------------------------
    | Socialite Providers
    |--------------------------------------------------------------------------
    |
    | Here you may specify the providers your application supports for OAuth.
    | Out of the box, FilamentCompanies provides support for all the OAuth
    | providers that are supported by Laravel Socialite.
    |
    */

    'providers' => [
        github,
        google,
        gitlab,
        bitbucket,
        facebook,
        linkedin,
        twitter,
        twitter-oauth-2,
    ],

config/services.php 中,通过提供商数组传递您的提供商凭据

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Mailgun, Postmark, AWS and more. This file provides the de facto
    | location for this type of information, allowing packages to have
    | a conventional file to locate the various service credentials.
    |
    */
    
    'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => 'https://filament.test/oauth/github/callback',
    ],

提供商的重定向 URI 必须类似于上述示例(例如:'APP_URL/oauth/provider_name/callback')

示例:如何设置 GitHub(使用 Filament 作为应用程序名称和 APP_URL)

  1. 前往 https://github.com/settings/applications/new
  2. 应用名称: Filament
  3. 主页 URL: https://filament.test/admin
  4. 授权回调 URL: https://filament.test/oauth/github/callback
  5. 点击设备流并保存
  6. 复制客户端密钥并存储在安全的地方。

授权回调 URL = 'redirect' 如上所示

.env 文件中,例如

GITHUB_CLIENT_ID=aluffgef97f9f79f434t
GITHUB_CLIENT_SECRET=hefliueoioffbo8338yhf2p9f4g2gg33

如果您在安装过程中将其作为选项选择了,可以暂时关闭 Socialite 支持

    /*
    |--------------------------------------------------------------------------
    | Features
    |--------------------------------------------------------------------------
    |
    | Some of Company's features are optional. You may disable the features
    | by removing them from this array. You're free to only remove some of
    | these features, or you can even remove all of these if you need to.
    |
    */

    'features' => [
        Features::termsAndPrivacyPolicy(),
        Features::profilePhotos(),
        Features::api(),
        Features::companies(['invitations' => true]),
        Features::accountDeletion(),
        // Features::socialite(['rememberSession' => true, 'providerAvatars' => true]),
    ],

Socialite 包大量“借鉴”了 Joel Butcher、他的贡献者和 Socialstream 包的工作。您可以通过查看 Socialstream 文档 来全面了解其功能。

以下示例是 Laravel Jetstream 中实现的方法支持的此包的功能的视觉表示。您可以在其文档 这里 找到 Laravel Jetstream 包提供的所有功能。

可以通过 Wallo\FilamentCompanies\HasCompanies 特性提供的功能访问有关用户公司信息。此特性在安装过程中自动应用于您的 App\Models.User 模型。此特性提供了各种有用的方法,允许您检查用户的公司或公司。

// Access a user's currently selected company...
$user->currentCompany : Wallo\FilamentCompanies\Company

// Access all of the companies (including owned companies) that a user belongs to...
$user->allCompanies() : Illuminate\Support\Collection

// Access all of a user's owned companies...
$user->ownedCompanies : Illuminate\Database\Eloquent\Collection

// Access all of the companies that a user belongs to but does not own...
$user->companies : Illuminate\Database\Eloquent\Collection

// Access a user's "personal" company...
$user->personalCompany() : Wallo\FilamentCompanies\Company

// Determine if a user owns a given company...
$user->ownsCompany($company) : bool

// Determine if a user belongs to a given company...
$user->belongsToCompany($company) : bool

// Get the role that the user is assigned on the company...
$user->companyRole($company) : \Wallo\FilamentCompanies\Role

// Determine if the user has the given role on the given company...
$user->hasCompanyRole($company, 'admin') : bool

// Access an array of all permissions a user has for a given company...
$user->companyPermissions($company) : array

// Determine if a user has a given company permission...
$user->hasCompanyPermission($company, 'server:create') : bool

$user 表示应用程序的当前用户。可以与 Auth::user() 互换

示例 #1:仅允许特定公司 ID 查看 & 访问 filament 页面、资源等...

protected static function shouldRegisterNavigation(): bool
{
    return Auth::user()->currentCompany->id === 3;
}

public function mount(): void
{
    abort_unless(Auth::user()->currentCompany->id === 3, 403);
}

示例 #2:使用当前公司名称

protected static function shouldRegisterNavigation(): bool
{
    return Auth::user()->currentCompany->name === "Filament";
}

public function mount(): void
{
    abort_unless(Auth::user()->currentCompany->name === "Filament", 403);
}

您可以使用不同公司的集合并将它们分组,或者您可以使用不同范围的值,等等。

公司邀请

在我看来,如果您使用 GMAIL 并进行测试,这是设置邮件服务器的最简单途径

  1. 前往 https://myaccount.google.com/apppasswords(可能会要求您登录)
  2. 点击“选择应用”,输入应用名称,然后点击“生成”。
  3. 复制您的应用密码并将其存储在安全的地方。

在您的应用程序的 .env 文件中,例如

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=yourgmailusername@gmail.com
MAIL_PASSWORD=of9f9279g924792g49t           # GMAIL App Password
MAIL_ENCRYPTION=tsl                         # tsl is recommended over ssl
MAIL_FROM_ADDRESS="filament@company.com"
MAIL_FROM_NAME="${APP_NAME}"

端口号不必是特定的

角色 & 权限

您可以在 app/Providers/FilamentCompaniesServiceProvider.php 中更改角色和权限

/**
 * Configure the roles and permissions that are available within the application.
 */
protected function configurePermissions(): void
{
    FilamentCompanies::defaultApiTokenPermissions(['read']);

    FilamentCompanies::role('admin', 'Administrator', [
        'create',
        'read',
        'update',
        'delete',
    ])->description('Administrator users can perform any action.');

    FilamentCompanies::role('editor', 'Editor', [
        'read',
        'create',
        'update',
    ])->description('Editor users have the ability to read, create, and update.');
}

注意

  • 该包计划在 Filament V3 中用作上下文。
  • 安装后的默认视图不应是“Admin”上下文,这将是“公司所有者”或“公司用户”会看到的视图。
  • 有方法支持所需的“Admin”上下文。

贡献

  • 将此存储库分叉到您的 GitHub 账户。
  • 创建一个新的 Laravel & Filament 项目。
  • 在您的应用程序的根目录中克隆您的分叉。
  • /filament-companies 目录中,为您的修复创建一个分支,例如 fix/error-message

使用带有您分支名称的 dev 前缀在您的应用程序的 composer.json 文件中安装此包

{
    ...
    "require": {
        "andrewdwallo/filament-companies": "dev-fix/error-message",
    },
    "repositories": [
        {
            "type": "path",
            "url": "filament-companies/"
        }
    ],
    ...
}

现在,运行 composer update 并继续按照上面的安装说明进行。