Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set up a custom users table #72

Merged
merged 6 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 dist/css/filament.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/css/filament.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/filament.js": "/js/filament.js?id=a3029b69d87820f78653",
"/css/filament.css": "/css/filament.css?id=26860b98546f3b57d117"
"/css/filament.css": "/css/filament.css?id=daa5ea5fdc0044aa5d6b"
}
2 changes: 1 addition & 1 deletion packages/forms/src/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,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
2 changes: 1 addition & 1 deletion resources/views/components/branding/auth.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'title',
])

<h2 {{ $attributes->merge(['class' => 'text-center text-2xl md:text-3xl leading-tight']) }}>{{ $title }}</h2>
<h2 {{ $attributes->merge(['class' => 'text-center text-2xl md:text-3xl leading-tight font-medium']) }}>{{ $title }}</h2>
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: 8 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Filament\Http\Controllers;
use Filament\Http\Livewire;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\AuthorizeAdmins;
use Filament\Http\Middleware\AuthorizeUsers;
use Filament\Http\Middleware\RedirectIfAuthenticated;
use Filament\Resources\UserResource;
use Illuminate\Routing\Middleware\ValidateSignature;
Expand Down Expand Up @@ -40,8 +42,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 (UserResource::router()->routes as $route) {
danharrin marked this conversation as resolved.
Show resolved Hide resolved
Route::get(UserResource::getSlug() . '/' . $route->uri, $route->page)
->name(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
Loading