Skip to content

Commit

Permalink
Merge pull request #26 from mailcarrierapp/feat/users-dashboard
Browse files Browse the repository at this point in the history
[2.x] Users management dashboard
  • Loading branch information
danilopolani authored Mar 5, 2024
2 parents 4a1d0ee + fa91894 commit 5cb221a
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 12 deletions.
2 changes: 1 addition & 1 deletion resources/dist/css/theme.css

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions src/Providers/Filament/MailCarrierPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use MailCarrier\Pages\Login;
use MailCarrier\Resources\ApiTokenResource;
use MailCarrier\Resources\LayoutResource;
use MailCarrier\Resources\LogResource;
use MailCarrier\Resources\TemplateResource;
use MailCarrier\Resources;
use MailCarrier\Widgets\SentFailureChartWidget;
use MailCarrier\Widgets\StatsOverviewWidget;

Expand All @@ -45,10 +42,11 @@ public function panel(Panel $panel): Panel
->collapsibleNavigationGroups(false)
->discoverResources(in: '../../Resources', for: 'MailCarrier\\Resources')
->resources([
LogResource::class,
LayoutResource::class,
TemplateResource::class,
ApiTokenResource::class,
Resources\LogResource::class,
Resources\LayoutResource::class,
Resources\TemplateResource::class,
Resources\ApiTokenResource::class,
Resources\UserResource::class,
])
->discoverPages(in: app_path('Filament/Pages'), for: 'MailCarrier\\Pages')
->pages([
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/ApiTokenResource/Actions/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\Carbon;
use Filament\Actions\Action;
use Filament\Actions\Concerns\CanCustomizeProcess;
use Filament\Forms;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
Expand All @@ -13,8 +12,6 @@

class CreateAction extends Action
{
use CanCustomizeProcess;

const GENERATED_TOKEN_FIELD_NAME = 'generated_token';

public static function getDefaultName(): ?string
Expand Down
60 changes: 60 additions & 0 deletions src/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace MailCarrier\Resources;

use Filament\Resources\Resource;
use Filament\Tables;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use MailCarrier\Enums\Auth;
use MailCarrier\Models\User;
use MailCarrier\Resources\UserResource\Pages;

class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationIcon = 'heroicon-o-users';

protected static ?string $navigationGroup = 'Management';

/**
* List all the records.
*/
public static function table(Tables\Table $table): Tables\Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->placeholder('No name provided'),

Tables\Columns\TextColumn::make('email')
->formatStateUsing(
fn (string $state) => Str::of($state)
->when(
Str::of($state)->before('@')->length() > 5,
fn (Stringable $str) => $str->mask('*', 3, strpos($state, '@') - 3),
fn (Stringable $str) => $str->mask('*', 0, strpos($state, '@')),
)
),

Tables\Columns\TextColumn::make('created_at')
->label('Creation date')
->dateTime(),
])
->actions([
Tables\Actions\DeleteAction::make(),
])
->query(User::query()->whereNot('email', Auth::AuthManagerEmail->value));
}

/**
* Get Filament CRUD pages.
*/
public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
];
}
}
68 changes: 68 additions & 0 deletions src/Resources/UserResource/Actions/CreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace MailCarrier\Resources\UserResource\Actions;

use Filament\Actions\CreateAction as BaseCreateAction;
use Filament\Forms;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\HtmlString;
use MailCarrier\Facades\MailCarrier;
use MailCarrier\Models\User;
use MailCarrier\Resources\UserResource;

class CreateAction extends BaseCreateAction
{
protected function setUp(): void
{
parent::setUp();

$this->authorize(fn (): bool => UserResource::canCreate());

$this->modalFooterActionsAlignment(Alignment::End);
$this->modalWidth(MaxWidth::Large);

if (!is_null(MailCarrier::getSocialAuthDriver())) {
$this->form([
Forms\Components\Placeholder::make('cannot_create_users')
->label('')
->content(new HtmlString(<<<'HTML'
<div class="bg-warning-100 dark:bg-warning-500/20 border border-warning-300 dark:border-warning-600 rounded py-2 px-4">
Cannot create users with social authentication enabled.
</div>
HTML)),
]);

$this->modalHeading('Cannot create users');
$this->modalIcon('heroicon-o-exclamation-triangle');
$this->modalIconColor('warning');
$this->modalSubmitAction(false);
$this->modalCancelActionLabel('Close');
$this->extraModalFooterActions([]);

return;
}

$this->form([
Forms\Components\TextInput::make('name')
->required(),

Forms\Components\TextInput::make('email')
->email()
->required()
->unique((new User())->getTable(), 'email'),

Forms\Components\TextInput::make('password')
->password()
->revealable()
->required(),
]);

$this->mutateFormDataUsing(function (array $data): array {
$data['password'] = Hash::make($data['password']);

return $data;
});
}
}
19 changes: 19 additions & 0 deletions src/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace MailCarrier\Resources\UserResource\Pages;

use Filament\Resources\Pages\ListRecords;
use MailCarrier\Resources\UserResource;
use MailCarrier\Resources\UserResource\Actions\CreateAction;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
CreateAction::make(),
];
}
}

0 comments on commit 5cb221a

Please sign in to comment.