Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Dec 22, 2023
1 parent c90ac8e commit 9e963ea
Show file tree
Hide file tree
Showing 33 changed files with 838 additions and 36 deletions.
48 changes: 48 additions & 0 deletions app/Concerns/MustSetInitialPassword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Notifications\Admin\WelcomeNotification as AdminWelcomeNotification;
use App\Notifications\Organizations\WelcomeNotification;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

trait MustSetInitialPassword
{
protected static function bootMustSetInitialPassword(): void
{
static::creating(function (self $user) {
if (! $user->password) {
$user->password = Hash::make(Str::random(128));
}
});

static::created(function (self $user) {
$user->sendWelcomeNotification();
});
}

public function hasSetPassword(): bool
{
return ! \is_null($this->password_set_at);
}

public function setPassword(string $password): bool
{
return $this->update([
'password' => Hash::make($password),
'password_set_at' => $this->freshTimestamp(),
]);
}

public function sendWelcomeNotification(): void
{
$this->notify(
$this->is_admin
? new AdminWelcomeNotification
: new WelcomeNotification
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,21 @@ public function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id'),
TextColumn::make('first_name'),
TextColumn::make('last_name'),
TextColumn::make('email'),
TextColumn::make('role'),
TextColumn::make('created_at'),
TextColumn::make('id')
->label(__('field.id'))
->shrink(),

TextColumn::make('first_name')
->label(__('field.first_name')),

TextColumn::make('last_name')
->label(__('field.last_name')),

TextColumn::make('email')
->label(__('field.email')),

TextColumn::make('last_login_at')
->label(__('field.last_login_at')),
])
->filters([
//
Expand Down
67 changes: 63 additions & 4 deletions app/Filament/Admin/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

use App\Filament\Admin\Resources\UserResource\Pages;
use App\Models\User;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class UserResource extends Resource
Expand All @@ -36,8 +42,53 @@ public static function getPluralModelLabel(): string
public static function form(Form $form): Form
{
return $form
->inlineLabel()
->schema([
//
Section::make()
->maxWidth('3xl')
->schema([
TextInput::make('first_name')
->label(__('field.first_name'))
->maxLength(100)
->required(),

TextInput::make('last_name')
->label(__('field.last_name'))
->maxLength(100)
->required(),

TextInput::make('email')
->label(__('field.email'))
->unique(ignoreRecord: true)
->columnSpanFull()
->maxLength(200)
->email()
->required(),
]),

Section::make()
->maxWidth('3xl')
// ->columns()
->schema([
Radio::make('is_admin')
->label(__('field.role'))
->inlineLabel()
->boolean(
trueLabel: __('user.role.admin'),
falseLabel: __('user.role.user'),
)
->default(false)
->live(),

Select::make('organizations')
->relationship('organizations', titleAttribute: 'name')
->label(__('field.organizations'))
->inlineLabel()
->visible(fn (Get $get) => \boolval($get('is_admin')) === false)
->multiple()
->preload()
->required(),
]),
]);
}

Expand All @@ -51,15 +102,23 @@ public static function table(Table $table): Table
TextColumn::make('last_name')
->searchable(),

TextColumn::make('organizations.name'),
TextColumn::make('organizations.name')
->wrap(),

TextColumn::make('roles'),
TextColumn::make('is_admin')
->label(__('field.role')),

TextColumn::make('account_status'),

TextColumn::make('last_login_at'),
TextColumn::make('last_login_at')
->sortable(),
])
->filters([
TernaryFilter::make('is_admin')
->label(__('field.role'))
->trueLabel(__('user.role.admin'))
->falseLabel(__('user.role.user')),

SelectFilter::make('organizations')
->relationship('organizations', 'name')
->multiple(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Filament\Admin\Resources\UserResource\Pages;

use App\Filament\Admin\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected static bool $canCreateAnother = false;
}
7 changes: 7 additions & 0 deletions app/Filament/Admin/Widgets/StatsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Admin\Widgets;

use App\Models\Organization;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;

Expand All @@ -20,6 +21,12 @@ protected function getStats(): array
Organization::query()
->count()
),

Stat::make(
__('user.stats.total'),
User::query()
->count()
),
];
}
}
20 changes: 5 additions & 15 deletions app/Filament/Organizations/Resources/BeneficiaryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public static function table(Table $table): Table
->columns([
TextColumn::make('id')
->label(__('field.case_id'))
->extraHeaderAttributes([
'class' => 'w-1',
])
->shrink()
->sortable()
->searchable(),

Expand All @@ -67,25 +65,19 @@ public static function table(Table $table): Table
TextColumn::make('created_at')
->label(__('field.open_at'))
->date()
->extraHeaderAttributes([
'class' => 'w-1',
])
->shrink()
->sortable(),

TextColumn::make('last_evaluated_at')
->label(__('field.last_evaluated_at'))
->date()
->extraHeaderAttributes([
'class' => 'w-1',
])
->shrink()
->sortable(),

TextColumn::make('last_serviced_at')
->label(__('field.last_serviced_at'))
->date()
->extraHeaderAttributes([
'class' => 'w-1',
])
->shrink()
->sortable(),

TextColumn::make('status')
Expand All @@ -99,9 +91,7 @@ public static function table(Table $table): Table
default => dd($state)
})
->formatStateUsing(fn ($state) => $state?->label())
->extraHeaderAttributes([
'class' => 'w-1',
]),
->shrink(),
])
->filters([
//
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Middleware/UpdateDefaultTenant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Filament\Facades\Filament;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class UpdateDefaultTenant
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = Filament::auth()->user();
$tenant = Filament::getTenant();

if ($user->latest_organization_id !== $tenant->id) {
$user->update([
'latest_organization_id' => $tenant->id,
]);
}

return $next($request);
}
}
28 changes: 28 additions & 0 deletions app/Http/Responses/LoginResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Responses;

use Filament\Facades\Filament;
use Filament\Http\Responses\Auth\Contracts\LoginResponse as Responsable;
use Illuminate\Http\RedirectResponse;
use Livewire\Features\SupportRedirects\Redirector;

class LoginResponse implements Responsable
{
public function toResponse($request): RedirectResponse | Redirector
{
$user = Filament::auth()->user();

if ($user->is_admin) {
$panel = Filament::getPanel('admin');
$parameters = [];
} else {
$panel = Filament::getPanel('organization');
$parameters = $user->getDefaultTenant($panel);
}

return redirect()->intended($panel->route('pages.dashboard', $parameters));
}
}
23 changes: 23 additions & 0 deletions app/Listeners/LogSuccessfulLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Listeners;

use Illuminate\Auth\Events\Login;

class LogSuccessfulLogin
{
public function handle(Login $event): void
{
activity('system')
->by($event->user)
->on($event->user)
->withProperties([
'ip' => request()->ip(),
'user_agent' => request()->userAgent(),
])
->event('logged_in')
->log('logged_in');
}
}
Loading

0 comments on commit 9e963ea

Please sign in to comment.