Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Jun 17, 2024
1 parent ac96eda commit 55106fc
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 76 deletions.
30 changes: 30 additions & 0 deletions app/Concerns/HasUserStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Enums\UserStatus;

trait HasUserStatus
{
public function isActive(): bool
{
return UserStatus::isValue($this->status, UserStatus::ACTIVE);
}

public function isPending(): bool
{
return UserStatus::isValue($this->status, UserStatus::PENDING);
}

public function setPendingStatus(): void
{
$this->update(['status' => UserStatus::PENDING]);
}

public function deactivate(): void
{
$this->update(['status' => UserStatus::INACTIVE]);
}
}
73 changes: 40 additions & 33 deletions app/Filament/Organizations/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
Expand Down Expand Up @@ -69,7 +70,9 @@ public static function table(Table $table): Table
->searchable(),
TextColumn::make('roles')
->sortable()
->label(__('user.labels.roles')),
->badge()
->label(__('user.labels.roles'))
->formatStateUsing(fn ($state) => $state->label()),
TextColumn::make('status')
->sortable()
->label(__('user.labels.account_status'))
Expand Down Expand Up @@ -115,38 +118,42 @@ public static function getPages(): array
public static function getSchema(): array
{
return [
TextInput::make('first_name')
->label(__('user.labels.first_name'))
->required(),
TextInput::make('last_name')
->label(__('user.labels.last_name'))
->required(),
TextInput::make('email')
->label(__('user.labels.email'))
->required(),
TextInput::make('phone_number')
->label(__('user.labels.phone_number'))
->tel()
->required(),
Select::make('roles')
->label(__('user.labels.select_roles'))
->options(Role::options())
->multiple()
->required(),
Checkbox::make('can_be_case_manager')
->label(__('user.labels.can_be_case_manager')),
Placeholder::make('obs')
->content(__('user.placeholders.obs'))
->label('')
->columnSpanFull(),
CheckboxList::make('case_permissions')
->label(__('user.labels.case_permissions'))
->options(CasePermission::options())
->columnSpanFull(),
CheckboxList::make('admin_permissions')
->label(__('user.labels.admin_permissions'))
->options(AdminPermission::options())
->columnSpanFull(),
Section::make()
->columns()
->schema([
TextInput::make('first_name')
->label(__('user.labels.first_name'))
->required(),
TextInput::make('last_name')
->label(__('user.labels.last_name'))
->required(),
TextInput::make('email')
->label(__('user.labels.email'))
->required(),
TextInput::make('phone_number')
->label(__('user.labels.phone_number'))
->tel()
->required(),
Select::make('roles')
->label(__('user.labels.select_roles'))
->options(Role::options())
->multiple()
->required(),
Checkbox::make('can_be_case_manager')
->label(__('user.labels.can_be_case_manager')),
Placeholder::make('obs')
->content(__('user.placeholders.obs'))
->label('')
->columnSpanFull(),
CheckboxList::make('case_permissions')
->label(__('user.labels.case_permissions'))
->options(CasePermission::options())
->columnSpanFull(),
CheckboxList::make('admin_permissions')
->label(__('user.labels.admin_permissions'))
->options(AdminPermission::options())
->columnSpanFull(),
]),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Filament\Organizations\Resources\UserResource\Actions;

use App\Enums\UserStatus;
use App\Models\User;
use Filament\Actions\Action;

Expand All @@ -19,13 +18,13 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => UserStatus::isValue($record->status, UserStatus::ACTIVE));
$this->visible(fn (User $record) => $record->isActive());

$this->label(__('user.actions.deactivate'));

$this->color('danger');

// $this->icon('heroicon-s-ban');
$this->icon('heroicon-o-user-minus');

$this->modalHeading(__('user.action_deactivate_confirm.title'));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Filament\Organizations\Resources\UserResource\Actions;

use App\Enums\UserStatus;
use App\Models\User;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
Expand All @@ -21,11 +20,11 @@ protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => UserStatus::isValue($record->status, UserStatus::PENDING));
$this->visible(fn (User $record) => $record->isPending());

$this->label(__('user.actions.resend_invitation'));

// $this->icon('heroicon-o-mail');
$this->icon('heroicon-o-envelope-open');

$this->modalHeading(__('user.action_resend_invitation_confirm.title'));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Filament\Organizations\Resources\UserResource\Actions;

use App\Models\User;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\RateLimiter;

class ResetPassword extends Action
{
protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->isActive());

$this->label(__('user.actions.reset_password'));

$this->icon('heroicon-o-lock-open');

$this->modalHeading(__('user.action_reset_password_confirm.title'));

$this->modalWidth('md');

$this->action(function (User $record) {
$key = $this->getRateLimiterKey($record);
$maxAttempts = 1;

if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
return $this->failure();
}

RateLimiter::increment($key, HOUR_IN_SECONDS);

$record->resetPassword();
// $record->sendWelcomeNotification();
// $this->success();
});

$this->successNotificationTitle(__('user.action_reset_password_confirm.success'));

$this->failureNotification(
fn (Notification $notification) => $notification
->danger()
->title(__('user.action_reset_password_confirm.failure_title'))
->body(__('user.action_reset_password_confirm.failure_body'))
);
}

private function getRateLimiterKey(User $user): string
{
return 'reset-password:' . $user->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ protected function getHeaderActions(): array
Actions\DeleteAction::make(),
];
}

public function getBreadcrumbs(): array
{
return [
self::$resource::getUrl() => self::$resource::getBreadcrumb(),
self::$resource::getUrl('view', ['record' => $this->record->id]) => $this->record->getFilamentName(),
];
}

protected function getRedirectUrl(): string
{
return self::$resource::getUrl('view', ['record' => $this->record->id]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,66 @@

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

use App\Enums\UserStatus;
use App\Filament\Organizations\Resources\UserResource;
use App\Models\User;
use Filament\Actions;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Form;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Str;

class ViewUser extends ViewRecord
{
protected static string $resource = UserResource::class;

public function form(Form $form): Form
public function infolist(Infolist $infolist): Infolist
{
return $form->schema([
...[Group::make([
Placeholder::make('status')
->content(fn (User $record) => $record->status?->label()),
Placeholder::make('updated_at')
->content(fn (User $record) => $record->updated_at),
])
return $infolist->schema([
Section::make()
->columns()
->columnSpanFull()],
...UserResource::getSchema(),
->schema([
TextEntry::make('status')
->formatStateUsing(fn ($state) => $state->label()),
TextEntry::make('updated_at'),
]),
Section::make()
->columns()
->schema([
TextEntry::make('first_name')
->label(__('user.labels.first_name')),
TextEntry::make('last_name')
->label(__('user.labels.last_name')),
TextEntry::make('email')
->label(__('user.labels.email')),
TextEntry::make('phone_number')
->label(__('user.labels.phone_number')),
TextEntry::make('roles')
->label(__('user.labels.select_roles'))
->badge(fn ($state) => $state != '-')
->formatStateUsing(fn ($state) => $state != '-' ? $state->label() : $state),
TextEntry::make('can_be_case_manager')
->label(__('user.labels.can_be_case_manager'))
->default('0')
->formatStateUsing(fn ($state) => $state != '-' ? __('enum.ternary.' . $state) : $state),
TextEntry::make('obs')
->default(
Str::of(__('user.placeholders.obs'))
->inlineMarkdown()
->toHtmlString()
)
->hiddenLabel()
->columnSpanFull(),
TextEntry::make('case_permissions')
->label(__('user.labels.case_permissions'))
->badge(fn ($state) => $state != '-')
->formatStateUsing(fn ($state) => $state != '-' ? __('enum.case_permissions.' . $state) : $state)
->columnSpanFull(),
TextEntry::make('admin_permissions')
->label(__('user.labels.admin_permissions'))
->badge(fn ($state) => $state != '-')
->formatStateUsing(fn ($state) => $state != '-' ? __('enum.admin_permission.' . $state) : $state)
->columnSpanFull(),
]),
]);
}

Expand All @@ -40,21 +74,18 @@ protected function getHeaderActions(): array

UserResource\Actions\DeactivateUserAction::make(),

Actions\Action::make('reset_password')
->label(__('user.actions.reset_password'))
->visible(fn (User $record) => UserStatus::isValue($record->status, UserStatus::ACTIVE))
->action(fn (User $record) => $record->resetPassword()),
// UserResource\Actions\ResetPassword::make('reset-password'),

UserResource\Actions\ResendInvitationAction::make(),
];
}

public function getBreadcrumb(): string
{
return $this->record->getFilamentName();
return $this->getTitle();
}

public function getHeading(): string|Htmlable
public function getTitle(): string
{
return $this->record->getFilamentName();
}
Expand Down
7 changes: 2 additions & 5 deletions app/Models/Beneficiary.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,14 @@ class Beneficiary extends Model
'act_location' => AsEnumCollection::class . ':' . ActLocation::class,
];

protected static function boot()
protected static function booted()
{
parent::boot();
static::created(function (Beneficiary $beneficiary) {
if (auth()->user()?->can_be_case_manager) {
$team = new CaseTeam([
'beneficiary_id' => $beneficiary->id,
$beneficiary->team()->create([
'user_id' => auth()->user()->id,
'roles' => [Role::MANGER],
]);
$team->save();
}
});
}
Expand Down
Loading

0 comments on commit 55106fc

Please sign in to comment.