Skip to content

Commit

Permalink
Enhanced admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Apr 26, 2024
1 parent 42ad5e0 commit 0bf8a25
Show file tree
Hide file tree
Showing 15 changed files with 258 additions and 77 deletions.
3 changes: 2 additions & 1 deletion app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Actions\Fortify;

use App\Enums\Role;
use App\Enums\Weekday;
use App\Models\Organization;
use App\Models\User;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected function createTeam(User $user): void

$organization->users()->attach(
$user, [
'role' => 'owner',
'role' => Role::Owner->value,
]
);

Expand Down
3 changes: 2 additions & 1 deletion app/Actions/Jetstream/CreateOrganization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Actions\Jetstream;

use App\Enums\Role;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function create(User $user, array $input): Organization

$organization->users()->attach(
$user, [
'role' => 'owner',
'role' => Role::Owner->value,
]
);

Expand Down
15 changes: 15 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use App\Filament\Resources\UserResource\RelationManagers\OrganizationsRelationManager;
use App\Filament\Resources\UserResource\RelationManagers\OwnedOrganizationsRelationManager;
use App\Models\User;
use Exception;
use Filament\Forms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;

class UserResource extends Resource
{
Expand Down Expand Up @@ -70,6 +72,19 @@ public static function table(Table $table): Table
//
])
->actions([
Impersonate::make()->before(function (User $record): void {
if ($record->currentTeam === null) {
$organization = $record->organizations()->where('personal_team', '=', true)->first();
if ($organization === null) {
$organization = $record->organizations()->first();
}
if ($organization === null) {
throw new Exception('User has no organization');
}
$record->currentTeam()->associate($organization);
$record->save();
}
}),
Tables\Actions\EditAction::make(),
])
->bulkActions([
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use STS\FilamentImpersonate\Pages\Actions\Impersonate;

class EditUser extends EditRecord
{
Expand All @@ -15,6 +16,7 @@ class EditUser extends EditRecord
protected function getHeaderActions(): array
{
return [
Impersonate::make()->record($this->getRecord()),
Actions\DeleteAction::make(),
];
}
Expand Down
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public function canAccessPanel(Panel $panel): bool
return in_array($this->email, config('auth.super_admins', []), true) && $this->hasVerifiedEmail();
}

public function canBeImpersonated(): bool
{
return $this->is_placeholder === false;
}

/**
* @return BelongsToMany<Organization>
*/
Expand Down
6 changes: 5 additions & 1 deletion app/Service/PermissionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ private function getPermissionsByUser(Organization $organization, User $user): a
?->membership
?->role;

if ($role === null) {
return [];
}

/** @var Role|null $roleObj */
$roleObj = Jetstream::findRole($role);

return $role !== null ? ($roleObj?->permissions ?? []) : [];
return $roleObj?->permissions ?? [];
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"nwidart/laravel-modules": "dev-feature/fixed_path",
"pxlrbt/filament-environment-indicator": "^2.0",
"spatie/temporary-directory": "^2.2",
"stechstudio/filament-impersonate": "^3.8",
"tightenco/ziggy": "^2.1.0",
"tpetry/laravel-postgresql-enhanced": "^0.38.0",
"wikimedia/composer-merge-plugin": "^2.1.0"
Expand Down
155 changes: 132 additions & 23 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0bf8a25

Please sign in to comment.