一个专为 Filament 设计的全面的 Laravel 身份验证和授权系统,重点关注多租户公司管理。
A comprehensive multi-tenant authentication and authorization solution designed for Filament, with a focus on company-based tenancy.
After installing the Panel Builder, ensure you create a panel using the following command:
php artisan filament:install --panelsIf you've followed the Panel Builder documentation, you should have already done this.
Install the package
composer require andrewdwallo/filament-companiesExecute the following Artisan command to scaffold the application. You will be prompted to choose between installing the Base package or enabling Socialite support.
php artisan filament-companies:installRun migrations:
php artisan migrate:freshIf you encounter any issues while setting up your application with this package, refer to the example implementation here: Filament Companies Example App.
After installation, there will be a company panel registered for your application. It is located within the FilamentCompaniesServiceProvider.php file.
In order for Tailwind to process the CSS used within this package and for the company panel, a user must create a custom theme.
Once the custom theme for the company panel is created, add this package's vendor directory to your resources/css/filament/company/theme.css file:
@source '../../../../vendor/andrewdwallo/filament-companies/resources/views';As you may have noticed, after installation, there will be a company panel registered for your application. In order for this package to work you must also have a "User" panel to contain the Profile page and Personal Access Tokens page.
For this example, I will use the default panel that Filament provides when installing the panel builder, the "Admin" panel.
In your "Admin" panel, make sure to register the following pages:
use Wallo\FilamentCompanies\Pages\User\PersonalAccessTokens;
use Wallo\FilamentCompanies\Pages\User\Profile;
public function panel(Panel $panel): Panel
{
return $panel
// ...
->pages([
Profile::class,
PersonalAccessTokens::class,
])
}You may create a separate User Panel following the documentation for creating a new panel
Ensure users have a way to navigate to the Profile and Personal Access Tokens pages.
Users should also have a way to navigate back to the Company Panel.
You may use the following as a guide:
…If Auto-Accept Invitations is enabled, the Company menu item logic must handle cases where the user does not have a personal company to avoid errors. One way to do this is as follows:
MenuItem::make()
->label('Company')
->icon('heroicon-o-building-office')
->url(static function (): ?string {
$user = Auth::user();
if ($company = $user?->primaryCompany()) {
return Pages\Dashboard::getUrl(panel: FilamentCompanies::getCompanyPanel(), tenant: $company);
}
return Filament::getPanel(FilamentCompanies::getCompanyPanel())->getTenantRegistrationUrl();
}),[!NOTE] This modification is necessary because, when Auto-Accept Invitations is enabled, an invited user may not have a personal company. Without this adjustment, generating a URL with a
nulltenant may cause an error.
You may change the value used for the User Panel using the id of the panel:
use Filament\Panel;
use Wallo\FilamentCompanies\FilamentCompanies;
class FilamentCompaniesServiceProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
FilamentCompanies::make()
->userPanel('user')
)
}
}Make sure to create a panel with the id you're passing
If you want users to directly access the Company panel's Register or Login page from the Laravel Welcome page, ensure the Company panel is set as the default in Filament. This involves two key steps:
->default() method. ->default() method to your Company panel configuration to make it the default entry point.By making the Company panel the default, links to Register or Login on the Laravel Welcome page will lead directly to the Company panel's authentication pages.
If you wish to translate the package, you may publish the language files using:
php artisan vendor:publish --tag=filament-companies-translationsIf you wish to customize the views, you may publish them using:
php artisan vendor:publish --tag=filament-companies-viewsIf you are using email verification make sure to register your verify-email url. If this is not properly registerd, you may see errors on the user profile page. You can either add this via Filament's VerifyEmail facade or a new route in your app.
Place this in your AppServiceProvider's boot method:
use Filament\Facades\Filament;
use Filament\Notifications\Auth\VerifyEmail;
// Verify email notification url
VerifyEmail::createUrlUsing(function ($notifiable) {
return Filament::getVerifyEmailUrl($notifiable);
});Place this in routes file - typically web.php:
use Filament\Http\Controllers\Auth\EmailVerificationController;
Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, '__invoke'])
->middleware(['signed'])
->name('verification.verify');Filament has a built-in event that is fired when the application needs to set the tenant for the current request. This event is Filament\Events\TenantSet. If you would like to either enable or disable the ability to switch the current company, you may do so by using the switchCurrentCompany() method in your FilamentCompaniesServiceProvider class.
use Filament\Panel;
use Wallo\FilamentCompanies\FilamentCompanies;
class FilamentCompaniesServiceProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
FilamentCompanies::make()
->switchCurrentCompany()
);
}
}[!CAUTION] This feature is a core part of the package and should typically remain enabled. Disabling it will prevent users from switching between companies, which may not be desirable for most use cases.
You can selectively enable or disable certain profile features. If a feature is omitted, it will be disabled by default.
To do so, modify your FilamentCompaniesServiceProvider class as shown below:
…Personalize your application by replacing default components with your own custom components. This is done by passing your custom component's class name to the component parameter of the relevant method.
[!IMPORTANT] Your custom component must have a unique class name. This is crucial to prevent conflicts and ensure proper functioning, as Livewire differentiates components primarily by their class names. Even if your custom component is in a different namespace, having the same class name as a component in the package can lead to unexpected errors and behavior.
Here's an example of how to use a custom component for updating profile information:
use App\Livewire\CustomComponent;
FilamentCompanies::make()
->updateProfileInformation(component: CustomComponent::class);If you would like to change the order of the profile features, you may do so by setting the sort parameter to the corresponding method.
The default sort order is as follows:
FilamentCompanies::make()
->updateProfileInformation(sort: 0)
->updatePasswords(sort: 1)
->setPasswords(sort: 2)
->connectedAccounts(sort: 3)
->manageBrowserSessions(sort: 4)
->accountDeletion(sort: 5);If you would like to add custom profile components, you may do so by passing the component class name along with the sort order to the addProfileComponents() method:
use App\Livewire\CustomComponent;
FilamentCompanies::make()
->addProfileComponents([
7 => CustomComponent::class,
]);Within your component's view, you may use the grid section component to match the style of other components:
{{ __('My Custom Component') }}
{{ __('This is my custom component.') }}
{{ $this->form }}
Similar to profile features, you can customize the company settings page by enabling specific features and customizing their components.
use Filament\Panel;
use Wallo\FilamentCompanies\FilamentCompanies;
class FilamentCompaniesServiceProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
FilamentCompanies::make()
->updateCompanyInformation() // Enables updating company names
->manageCompanyEmployees() // Enables company employee management
->companyDeletion() // Enables company deletion
);
}
}You can replace default company settings components with your own custom components:
use App\Livewire\CustomCompanyComponent;
FilamentCompanies::make()
->updateCompanyInformation(component: CustomCompanyComponent::class);Change the order of company settings features by setting the sort parameter:
FilamentCompanies::make()
->updateCompanyInformation(sort: 0)
->manageCompanyEmployees(sort: 1)
->companyDeletion(sort: 2);If you would like to add custom company settings components, you may do so by passing the component class name along with the sort order to the addCompanyComponents() method:
use App\Livewire\CustomCompanyComponent;
FilamentCompanies::make()
->addCompanyComponents([
5 => CustomCompanyComponent::class,
]);To allow users to upload custom profile photos, you can enable this feature by including the profilePhotos() method in your FilamentCompaniesServiceProvider.
use Filament\Panel;
use Wallo\FilamentCompanies\FilamentCompanies;
class FilamentCompaniesServiceProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(
Fi暂无开放 Issues,或尚未同步最近议题。