mirror of
				https://github.com/pelican-dev/panel.git
				synced 2025-11-04 12:06:52 +01:00 
			
		
		
		
	* add spatie/permissions * add policies * add role resource * add root admin role handling * replace some "root_admin" with function * add model specific permissions * make permission selection nicer * fix user creation * fix tests * add back subuser checks in server policy * add custom model for role * assign new users to role if root_admin is set * add api for roles * fix phpstan * add permissions for settings page * remove "restore" and "forceDelete" permissions * add user count to list * prevent deletion if role has users * update user list * fix server policy * remove old `root_admin` column * small refactor * fix tests * forgot can checks here * forgot use * disable editing own roles & disable assigning root admin * don't allow to rename root admin role * remove php bombing exception handler * fix role assignment when creating a user * fix disableOptionWhen * fix missing `root_admin` attribute on react frontend * add permission check for bulk delete * rename viewAny to viewList * improve canAccessPanel check * fix admin not displaying for non-root admins * make sure non root admins can't edit root admins * fix import * fix settings page permission check * fix server permissions for non-subusers * fix settings page permission check v2 * small cleanup * cleanup config file * move consts from resouce into enum & model * Update database/migrations/2024_08_01_114538_remove_root_admin_column.php Co-authored-by: Lance Pioch <lancepioch@gmail.com> * fix config * fix phpstan * fix phpstan 2.0 --------- Co-authored-by: Lance Pioch <lancepioch@gmail.com>
		
			
				
	
	
		
			134 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Filament\Resources\UserResource\Pages;
 | 
						|
 | 
						|
use App\Filament\Resources\UserResource;
 | 
						|
use App\Models\Role;
 | 
						|
use App\Models\User;
 | 
						|
use App\Services\Users\UserCreationService;
 | 
						|
use Filament\Actions\CreateAction;
 | 
						|
use Filament\Forms\Components\CheckboxList;
 | 
						|
use Filament\Forms\Components\Grid;
 | 
						|
use Filament\Forms\Components\TextInput;
 | 
						|
use Filament\Notifications\Notification;
 | 
						|
use Filament\Resources\Pages\ListRecords;
 | 
						|
use Filament\Tables\Actions\BulkActionGroup;
 | 
						|
use Filament\Tables\Actions\DeleteBulkAction;
 | 
						|
use Filament\Tables\Actions\EditAction;
 | 
						|
use Filament\Tables\Columns\IconColumn;
 | 
						|
use Filament\Tables\Columns\ImageColumn;
 | 
						|
use Filament\Tables\Columns\TextColumn;
 | 
						|
use Filament\Tables\Table;
 | 
						|
 | 
						|
class ListUsers extends ListRecords
 | 
						|
{
 | 
						|
    protected static string $resource = UserResource::class;
 | 
						|
 | 
						|
    public function table(Table $table): Table
 | 
						|
    {
 | 
						|
        return $table
 | 
						|
            ->searchable(false)
 | 
						|
            ->columns([
 | 
						|
                ImageColumn::make('picture')
 | 
						|
                    ->visibleFrom('lg')
 | 
						|
                    ->label('')
 | 
						|
                    ->extraImgAttributes(['class' => 'rounded-full'])
 | 
						|
                    ->defaultImageUrl(fn (User $user) => 'https://gravatar.com/avatar/' . md5(strtolower($user->email))),
 | 
						|
                TextColumn::make('external_id')
 | 
						|
                    ->searchable()
 | 
						|
                    ->hidden(),
 | 
						|
                TextColumn::make('uuid')
 | 
						|
                    ->label('UUID')
 | 
						|
                    ->hidden()
 | 
						|
                    ->searchable(),
 | 
						|
                TextColumn::make('username')
 | 
						|
                    ->searchable(),
 | 
						|
                TextColumn::make('email')
 | 
						|
                    ->searchable()
 | 
						|
                    ->icon('tabler-mail'),
 | 
						|
                IconColumn::make('use_totp')
 | 
						|
                    ->label('2FA')
 | 
						|
                    ->visibleFrom('lg')
 | 
						|
                    ->icon(fn (User $user) => $user->use_totp ? 'tabler-lock' : 'tabler-lock-open-off')
 | 
						|
                    ->boolean()->sortable(),
 | 
						|
                TextColumn::make('roles_count')
 | 
						|
                    ->counts('roles')
 | 
						|
                    ->icon('tabler-users-group')
 | 
						|
                    ->label('Roles')
 | 
						|
                    ->formatStateUsing(fn (User $user, $state) => $state . ($user->isRootAdmin() ? ' (Root Admin)' : '')),
 | 
						|
                TextColumn::make('servers_count')
 | 
						|
                    ->counts('servers')
 | 
						|
                    ->icon('tabler-server')
 | 
						|
                    ->label('Servers'),
 | 
						|
                TextColumn::make('subusers_count')
 | 
						|
                    ->visibleFrom('sm')
 | 
						|
                    ->label('Subusers')
 | 
						|
                    ->counts('subusers')
 | 
						|
                    ->icon('tabler-users'),
 | 
						|
                // ->formatStateUsing(fn (string $state, $record): string => (string) ($record->servers_count + $record->subusers_count))
 | 
						|
            ])
 | 
						|
            ->actions([
 | 
						|
                EditAction::make(),
 | 
						|
            ])
 | 
						|
            ->checkIfRecordIsSelectableUsing(fn (User $user) => auth()->user()->id !== $user->id && !$user->servers_count)
 | 
						|
            ->bulkActions([
 | 
						|
                BulkActionGroup::make([
 | 
						|
                    DeleteBulkAction::make()
 | 
						|
                        ->authorize(fn () => auth()->user()->can('delete user')),
 | 
						|
                ]),
 | 
						|
            ]);
 | 
						|
    }
 | 
						|
    protected function getHeaderActions(): array
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            CreateAction::make('create')
 | 
						|
                ->label('Create User')
 | 
						|
                ->createAnother(false)
 | 
						|
                ->form([
 | 
						|
                    Grid::make()
 | 
						|
                        ->schema([
 | 
						|
                            TextInput::make('username')
 | 
						|
                                ->alphaNum()
 | 
						|
                                ->required()
 | 
						|
                                ->maxLength(255),
 | 
						|
                            TextInput::make('email')
 | 
						|
                                ->email()
 | 
						|
                                ->required()
 | 
						|
                                ->unique()
 | 
						|
                                ->maxLength(255),
 | 
						|
                            TextInput::make('password')
 | 
						|
                                ->hintIcon('tabler-question-mark')
 | 
						|
                                ->hintIconTooltip('Providing a user password is optional. New user email will prompt users to create a password the first time they login.')
 | 
						|
                                ->password(),
 | 
						|
                            CheckboxList::make('roles')
 | 
						|
                                ->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id)
 | 
						|
                                ->relationship('roles', 'name')
 | 
						|
                                ->dehydrated()
 | 
						|
                                ->label('Admin Roles')
 | 
						|
                                ->columnSpanFull()
 | 
						|
                                ->bulkToggleable(false),
 | 
						|
                        ]),
 | 
						|
                ])
 | 
						|
                ->successRedirectUrl(route('filament.admin.resources.users.index'))
 | 
						|
                ->action(function (array $data) {
 | 
						|
                    $roles = $data['roles'];
 | 
						|
                    $roles = collect($roles)->map(fn ($role) => Role::findById($role));
 | 
						|
                    unset($data['roles']);
 | 
						|
 | 
						|
                    /** @var UserCreationService $creationService */
 | 
						|
                    $creationService = resolve(UserCreationService::class);
 | 
						|
                    $user = $creationService->handle($data);
 | 
						|
 | 
						|
                    $user->syncRoles($roles);
 | 
						|
 | 
						|
                    Notification::make()
 | 
						|
                        ->title('User Created!')
 | 
						|
                        ->success()
 | 
						|
                        ->send();
 | 
						|
 | 
						|
                    return redirect()->route('filament.admin.resources.users.index');
 | 
						|
                }),
 | 
						|
        ];
 | 
						|
    }
 | 
						|
}
 |