Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed May 22, 2024
1 parent 4434342 commit b87aff2
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 90 deletions.
25 changes: 25 additions & 0 deletions app/Enums/AdminPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum AdminPermission: string
{
use Arrayable;
use Comparable;
use HasLabel;

case CAN_CHANGE_NOMENCLATURE = 'can_change_nomenclature';
case CAN_CHANGE_STAFF = 'can_change_staff';
case CAN_CHANGE_ORGANISATION_PROFILE = 'can_change_organisation_profile';

protected function labelKeyPrefix(): ?string
{
return 'enum.admin_permission';
}
}
26 changes: 26 additions & 0 deletions app/Enums/CasePermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use App\Concerns\Enums\Arrayable;
use App\Concerns\Enums\Comparable;
use App\Concerns\Enums\HasLabel;

enum CasePermission: string
{
use Arrayable;
use Comparable;
use HasLabel;

case HAS_ACCESS_TO_ALL_CASES = 'has_access_to_all_cases';
case CAN_SEARCH_CASES_IN_ALL_CENTERS = 'can_search_cases_in_all_centers';
case CAN_COPY_CASES_IN_ALL_CENTERS = 'can_copy_cases_in_all_centers';
case HAS_ACCESS_TO_STATISTICS = 'has_access_to_statistics';

protected function labelKeyPrefix(): ?string
{
return 'enum.case_permissions';
}
}
29 changes: 13 additions & 16 deletions app/Filament/Organizations/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Filament\Organizations\Resources;

use App\Enums\AdminPermission;
use App\Enums\CasePermission;
use App\Filament\Organizations\Resources\UserResource\Pages;
use App\Models\User;
use Filament\Forms\Components\Checkbox;
Expand Down Expand Up @@ -128,11 +130,15 @@ public static function getSchema(): array
// TODO get roles from enum after merge with #23
Select::make('roles')
->label(__('user.labels.select_roles'))
->options([
'aaaa' => 'aaaa',
'bbbb' => 'bbbb',
'cccc' => 'cccc',
])
->options(
// Role::options()
[
'aaaa' => 'aaaa',
'bbbb' => 'bbbb',
'cccc' => 'cccc',
]
)
// ->enum(Role::class)
->multiple()
->required(),
Checkbox::make('can_be_case_manager')
Expand All @@ -143,20 +149,11 @@ public static function getSchema(): array
->columnSpanFull(),
CheckboxList::make('case_permissions')
->label(__('user.labels.case_permissions'))
->options([
'has_access_to_all_cases' => __('user.labels.has_access_to_all_cases'),
'can_search_cases_in_all_centers' => __('user.labels.can_search_cases_in_all_centers'),
'can_copy_cases_in_all_centers' => __('user.labels.can_copy_cases_in_all_centers'),
'has_access_to_statistics' => __('user.labels.has_access_to_statistics'),
])
->options(CasePermission::options())
->columnSpanFull(),
CheckboxList::make('admin_permissions')
->label(__('user.labels.admin_permissions'))
->options([
'can_change_nomenclature' => __('user.labels.can_change_nomenclature'),
'can_change_staff' => __('user.labels.can_change_staff'),
'can_change_organisation_profile' => __('user.labels.can_change_organisation_profile'),
])
->options(AdminPermission::options())
->columnSpanFull(),
];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

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

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

class DeactivateUserAction extends Action
{
public static function getDefaultName(): ?string
{
return 'deactivate';
}

protected function setUp(): void
{
parent::setUp();

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

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

$this->color('danger');

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

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

$this->modalWidth('md');

$this->action(function (User $record) {
$record->deactivate();
$this->success();
});

$this->successNotificationTitle(__('user.action_deactivate_confirm.success'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

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

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

class ResendInvitationAction extends Action
{
public static function getDefaultName(): ?string
{
return 'resend_invitation';
}

protected function setUp(): void
{
parent::setUp();

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

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

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

$this->modalHeading(__('user.action_resend_invitation_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->sendWelcomeNotification();
$this->success();
});

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

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

private function getRateLimiterKey(User $user): string
{
return 'resend-invitation:' . $user->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,32 @@ class ViewUser extends ViewRecord

public function form(Form $form): Form
{
return $form->schema(
array_merge(
[
Group::make([
Placeholder::make('status')
->content(fn (User $record) => $record->status?->label()),
Placeholder::make('updated_at')
->content(fn (User $record) => $record->updated_at),
])
->columns()
->columnSpanFull()],
UserResource::getSchema()
)
);
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),
])
->columns()
->columnSpanFull()],
...UserResource::getSchema(),
]);
}

protected function getHeaderActions(): array
{
return [
Actions\EditAction::make('edit'),

Actions\Action::make('deactivate')
->label(__('user.actions.deactivate'))
->visible(fn (User $record) => UserStatus::isValue($record->status, UserStatus::ACTIVE))
->action(fn (User $record) => $record->deactivate()),
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()),

Actions\Action::make('resend_invitation')
->label(__('user.actions.resend_invitation'))
->visible(fn (User $record) => UserStatus::isValue($record->status, UserStatus::PENDING))
->action(fn (User $record) => $record->resendInvitation()),
UserResource\Actions\ResendInvitationAction::make(),
];
}

Expand Down
6 changes: 2 additions & 4 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Filament\Models\Contracts\HasTenants;
use Filament\Panel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -83,6 +84,7 @@ class User extends Authenticatable implements FilamentUser, HasAvatar, HasName,
'password' => 'hashed',
'is_admin' => 'boolean',
'roles' => 'json',
// 'roles' => AsEnumCollection::class . ':' . Role::class,
'case_permissions' => 'json',
'admin_permissions' => 'json',
'status' => UserStatus::class,
Expand Down Expand Up @@ -197,8 +199,4 @@ public function deactivate(): void
public function resetPassword(): void
{
}

public function resendInvitation(): void
{
}
}
7 changes: 7 additions & 0 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public function up(): void
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('phone_number')->nullable();
$table->string('status')->nullable();
$table->json('roles')->nullable();
$table->boolean('can_be_case_manager')->nullable();
$table->boolean('has_access_to_all_cases')->nullable();
$table->json('case_permissions')->nullable();
$table->json('admin_permissions')->nullable();
$table->boolean('is_admin')->default(false);
$table->timestamp('password_set_at')->nullable();
$table->string('password');
Expand Down

This file was deleted.

13 changes: 13 additions & 0 deletions lang/ro/enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,17 @@
'inactive' => 'Dezactivat',
'pending' => 'In asteptare',
],

'case_permissions' => [
'has_access_to_all_cases' => 'Are acces la toate cazurile din Centru',
'can_search_cases_in_all_centers' => 'Poate căuta cazuri (după CNP) în baza de date a tuturor centrelor instituției',
'can_copy_cases_in_all_centers' => 'Poate copia date identificare beneficiar dintr-o bază de date în alta a instituției',
'has_access_to_statistics' => 'Are acces la rapoarte statistice',
],

'admin_permission' => [
'can_change_nomenclature' => 'Are drepturi de modificare nomenclator',
'can_change_staff' => 'Are drepturi de modificare Echipă Specialiști (Staff)',
'can_change_organisation_profile' => 'Are drepturi de modificare Profilul Organizației în Rețeaua Sunrise',
],
];
18 changes: 11 additions & 7 deletions lang/ro/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
'can_be_case_manager' => 'Poate lua rol de manager de caz',
'case_permissions' => 'Permisiuni cazuri',
'admin_permissions' => 'Permisiuni administrare',
'has_access_to_all_cases' => 'Are acces la toate cazurile din Centru',
'can_search_cases_in_all_centers' => 'Poate căuta cazuri (după CNP) în baza de date a tuturor centrelor instituției',
'can_copy_cases_in_all_centers' => 'Poate copia date identificare beneficiar dintr-o bază de date în alta a instituției',
'has_access_to_statistics' => 'Are acces la rapoarte statistice',
'can_change_nomenclature' => 'Are drepturi de modificare nomenclator',
'can_change_staff' => 'Are drepturi de modificare Echipă Specialiști (Staff)',
'can_change_organisation_profile' => 'Are drepturi de modificare Profilul Organizației în Rețeaua Sunrise',
],

'stats' => [
Expand Down Expand Up @@ -62,4 +55,15 @@
'reset_password' => 'Resetează parola',
'resend_invitation' => 'Retrimite invitația',
],

'action_resend_invitation_confirm' => [
'title' => 'Retrimite invitația',
'success' => 'Invitația a fost trimisata cu succes.',
'failure_title' => 'Eroare la retrimiterea invitației!',
'failure_body' => 'A aparut o eroare la retrimiterea invitației',
],
'action_deactivate_confirm' => [
'title' => 'Deactivează cont',
'success' => 'Cont dezactivat cu succes',
],
];

0 comments on commit b87aff2

Please sign in to comment.