Skip to content

Commit

Permalink
Merge branch 'custom-auth-guard' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Mar 4, 2021
2 parents 35c3751 + bb3fb7c commit a4ec37e
Show file tree
Hide file tree
Showing 34 changed files with 438 additions and 356 deletions.
33 changes: 29 additions & 4 deletions config/filament.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@
|
*/

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

/*
|--------------------------------------------------------------------------
| Filament Domain
|--------------------------------------------------------------------------
|
| You may change the domain where Filament should be active. If the domain
| You may change the domain where Filament should be active. If the domain
| is empty, all domains will be valid.
|
*/

'domain' => env('FILAMENT_DOMAIN', null),

/*
|--------------------------------------------------------------------------
| Auth
|--------------------------------------------------------------------------
|
| This is the configuration that Filament will use to handle authentication
| into the admin panel.
|
*/

'auth' => [
'guard' => env('FILAMENT_AUTH_GUARD', 'filament'),
],

/*
|--------------------------------------------------------------------------
| Pages
Expand Down Expand Up @@ -97,6 +111,17 @@
*/
'default_filesystem_disk' => env('FILAMENT_FILESYSTEM_DRIVER', 'public'),

/*
|--------------------------------------------------------------------------
| User Resource
|--------------------------------------------------------------------------
|
| This is the user resource class that Filament will use to generate tables
| and forms to manage users.
|
*/
'user_resource' => \Filament\Resources\UserResource::class,

/*
|--------------------------------------------------------------------------
| Cache
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function hidden()

public function schema($schema)
{
$this->schema = collect($schema)
$this->schema = collect(value($schema))
->map(fn ($component) => $component->parent($this))
->toArray();

Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function rules($rules)

public function schema($schema)
{
$this->schema = $schema;
$this->schema = value($schema);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/tables/src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public static function make()

public function columns($columns)
{
$this->columns = $columns;
$this->columns = value($columns);

return $this;
}

public function filters($filters)
{
$this->filters = $filters;
$this->filters = value($filters);

return $this;
}
Expand Down
36 changes: 0 additions & 36 deletions resources/lang/en/edit-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,6 @@

],

'form' => [

'avatar' => [
'label' => 'Avatar',
],

'email' => [
'label' => 'Email address',
],

'name' => [
'label' => 'Name',
],

'newPassword' => [

'fieldset' => [
'label' => 'Set a new password',
],

'fields' => [

'newPassword' => [
'label' => 'Password',
],

'newPasswordConfirmation' => [
'label' => 'Confirm password',
],

],

],

],

'messages' => [

'saved' => 'Saved!',
Expand Down
28 changes: 15 additions & 13 deletions resources/lang/en/resources/user-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,43 @@
],

'isAdmin' => [
'label' => 'Administrator?',
'label' => 'Filament admin?',
'helpMessage' => 'Filament admins are able to access all areas of Filament and manage other users.',
],

'isUser' => [
'label' => 'Filament user?',
],

'name' => [
'label' => 'Name',
],

'newPassword' => [
'password' => [

'fieldset' => [
'label' => 'Set a new password',

'label' => [
'create' => 'Password',
'edit' => 'Set a new password',
],

],

'fields' => [

'newPassword' => [
'password' => [
'label' => 'Password',
],

'newPasswordConfirmation' => [
'passwordConfirmation' => [
'label' => 'Confirm password',
],

],

],

'password' => [
'label' => 'Password',
],

'passwordConfirmation' => [
'label' => 'Confirm password',
],

'roles' => [
'label' => 'Roles',
'placeholder' => 'Select a role',
Expand Down
6 changes: 3 additions & 3 deletions resources/views/components/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class="flex flex-col flex-grow space-y-4 overflow-y-auto text-gray-400 bg-gray-8
<x-filament::dropdown
class="flex items-center flex-grow w-full p-4 space-x-3 text-left transition-colors duration-200 hover:text-white hover:bg-gray-900">
<x-slot name="button">
<x-filament-avatar :user="Auth::guard('filament')->user()" :size="32" class="flex-shrink-0 w-8 h-8 rounded-full" />
<x-filament-avatar :user="\Filament\Filament::auth()->user()" :size="32" class="flex-shrink-0 w-8 h-8 rounded-full" />

<span class="flex-grow text-sm font-medium leading-tight">{{ Auth::guard('filament')->user()->name }}</span>
<span class="flex-grow text-sm font-medium leading-tight">{{ \Filament\Filament::auth()->user()->name }}</span>
</x-slot>

@if (Auth::guard('filament')->user()->is_admin)
@if (\Filament\Filament::auth()->user()->isFilamentAdmin())
<x-filament::dropdown-link href="{{ route('filament.users.index') }}">
{{ __('filament::nav.dropdown.users.label') }}
</x-filament::dropdown-link>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class="grid grid-cols-1 gap-4 lg:grid-cols-2 lg:gap-8"
>
<x-filament::card class="flex">
<div class="flex items-center space-x-4">
<x-filament-avatar :user="Auth::guard('filament')->user()" :size="160" class="flex-shrink-0 w-20 h-20 rounded-full" />
<x-filament-avatar :user="\Filament\Filament::auth()->user()" :size="160" class="flex-shrink-0 w-20 h-20 rounded-full" />

<div class="space-y-1">
<h2 class="text-2xl">{{ __('filament::dashboard.widgets.account.heading', ['name' => Auth::guard('filament')->user()->name]) }}</h2>
<h2 class="text-2xl">{{ __('filament::dashboard.widgets.account.heading', ['name' => \Filament\Filament::auth()->user()->name]) }}</h2>
<p class="text-sm"><a href="{{ route('filament.account') }}" class="link">{{ __('filament::dashboard.widgets.account.links.account.label') }}</a></p>
</div>
</div>
Expand Down
12 changes: 7 additions & 5 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Filament\Http\Controllers;
use Filament\Http\Livewire;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\AuthorizeAdmins;
use Filament\Http\Middleware\RedirectIfAuthenticated;
use Filament\Resources\UserResource;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Support\Facades\Route;

Expand Down Expand Up @@ -40,8 +40,10 @@
}
}

foreach (UserResource::router()->routes as $route) {
Route::get(UserResource::getSlug() . '/' . $route->uri, $route->page)
->name(UserResource::getSlug() . '.' . $route->name);
}
Route::middleware(AuthorizeAdmins::class)->group(function () {
foreach (Filament::userResource()::router()->routes as $route) {
Route::get(Filament::userResource()::getSlug() . '/' . $route->uri, $route->page)
->name(Filament::userResource()::getSlug() . '.' . $route->name);
}
});
});
10 changes: 4 additions & 6 deletions src/AuthorizationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Filament;

use Illuminate\Support\Facades\Auth;

class AuthorizationManager
{
public $authorizations = [];
Expand Down Expand Up @@ -43,18 +41,18 @@ public function authorize($authorizations)

public function can($action = null)
{
$user = Auth::guard('filament')->user();
$user = Filament::auth()->user();

if (
! count($this->authorizations) ||
$user->is_admin
$user->isFilamentAdmin()
) {
return true;
}

if ($this->mode === 'allow') {
foreach ($this->authorizations as $authorization) {
if ($user->hasRole($authorization->role)) {
if ($user->hasFilamentRole($authorization->role)) {
return true;
}
}
Expand All @@ -64,7 +62,7 @@ public function can($action = null)

if ($this->mode === 'deny') {
foreach ($this->authorizations as $authorization) {
if ($user->hasRole($authorization->role)) {
if ($user->hasFilamentRole($authorization->role)) {
return false;
}
}
Expand Down
31 changes: 19 additions & 12 deletions src/Commands/MakeUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Filament\Commands;

use Filament\Models\User;
use Filament\Filament;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

Expand All @@ -16,25 +16,32 @@ class MakeUserCommand extends Command

public function handle()
{
$name = $this->validateInput(fn () => $this->ask('Name'), 'name', ['required']);
$userModel = Filament::auth()->getProvider()->getModel();

$email = $this->validateInput(fn () => $this->ask('Email'), 'email', ['required', 'email', 'unique:filament_users']);
$details = [];

$password = $this->validateInput(fn () => $this->secret('Password'), 'password', ['required', 'min:8']);
$details['name'] = $this->validateInput(fn () => $this->ask('Name'), 'name', ['required']);

$isAdmin = $this->confirm('Would you like this user to be an administrator?', true);
$details['email'] = $this->validateInput(fn () => $this->ask('Email'), 'email', ['required', 'email', 'unique:' . $userModel]);

$user = User::create([
'is_admin' => $isAdmin,
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
]);
$details['password'] = Hash::make($this->validateInput(fn () => $this->secret('Password'), 'password', ['required', 'min:8']));

$adminColumn = $userModel::getFilamentAdminColumn();
if ($adminColumn !== null) {
$details[$adminColumn] = $this->confirm('Would you like this user to be an administrator?', true);
}

$userColumn = $userModel::getFilamentUserColumn();
if ($userColumn !== null) {
$details[$userColumn] = true;
}

$user = $userModel::create($details);

$loginUrl = route('filament.auth.login');
$this->info("Success! {$user->email} may now log in at {$loginUrl}.");

if (User::count() === 1 && $this->confirm('Would you like to show some love by starring the repo?', true)) {
if (Filament::auth()->getProvider()->getModel()::count() === 1 && $this->confirm('Would you like to show some love by starring the repo?', true)) {
if (PHP_OS_FAMILY === 'Darwin') exec('open https://github.com/laravel-filament/filament');
if (PHP_OS_FAMILY === 'Linux') exec('xdg-open https://github.com/laravel-filament/filament');
if (PHP_OS_FAMILY === 'Windows') exec('start https://github.com/laravel-filament/filament');
Expand Down
8 changes: 6 additions & 2 deletions src/Filament.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

namespace Filament;

use Filament\Resources\Resource;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Facade;

/**
* @method static StatefulGuard auth()
* @method static array getPages()
* @method static array getResources()
* @method static array getResources()
* @method static array getRoles()
* @method static array getWidgets()
* @method static void registerPage(string $page)
* @method static void registerResource(string $resource)
* @method static void registerResource(string $resource)
* @method static void registerRole(string $role)
* @method static void registerWidget(string $widget)
* @method static Resource userResource()
*
* @see \Filament\FilamentManager
*/
Expand Down
13 changes: 13 additions & 0 deletions src/FilamentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Filament;

use Filament\Resources\UserResource;
use Illuminate\Support\Facades\Auth;

class FilamentManager
{
public $pages = [];
Expand All @@ -12,6 +15,11 @@ class FilamentManager

public $widgets = [];

public function auth()
{
return Auth::guard(config('filament.auth.guard', 'filament'));
}

public function getPages()
{
return $this->pages;
Expand Down Expand Up @@ -51,4 +59,9 @@ public function registerWidget($widget)
{
$this->widgets = array_merge($this->widgets, [$widget]);
}

public function userResource()
{
return config('filament.user_resource', UserResource::class);
}
}
Loading

0 comments on commit a4ec37e

Please sign in to comment.