mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +00:00
2.x
This commit is contained in:
33
app/Web/Pages/Settings/Profile/Index.php
Normal file
33
app/Web/Pages/Settings/Profile/Index.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Profile;
|
||||
|
||||
use App\Web\Pages\Settings\Profile\Widgets\ProfileInformation;
|
||||
use App\Web\Pages\Settings\Profile\Widgets\TwoFactor;
|
||||
use App\Web\Pages\Settings\Profile\Widgets\UpdatePassword;
|
||||
use App\Web\Traits\PageHasWidgets;
|
||||
use Filament\Pages\Page;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
use PageHasWidgets;
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $slug = 'settings/profile';
|
||||
|
||||
protected static ?string $title = 'Profile';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-user-circle';
|
||||
|
||||
protected static ?int $navigationSort = 2;
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[ProfileInformation::class],
|
||||
[UpdatePassword::class],
|
||||
[TwoFactor::class],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Profile\Widgets;
|
||||
|
||||
use App\Actions\User\UpdateUserProfileInformation;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class ProfileInformation extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static bool $isLazy = false;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public string $name;
|
||||
|
||||
public string $email;
|
||||
|
||||
public string $timezone;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->name = auth()->user()->name;
|
||||
$this->email = auth()->user()->email;
|
||||
$this->timezone = auth()->user()->timezone;
|
||||
}
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
$rules = UpdateUserProfileInformation::rules(auth()->user());
|
||||
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Profile Information')
|
||||
->description('Update your account\'s profile information and email address.')
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label('Name')
|
||||
->rules($rules['name']),
|
||||
TextInput::make('email')
|
||||
->label('Email')
|
||||
->rules($rules['email']),
|
||||
Select::make('timezone')
|
||||
->label('Timezone')
|
||||
->searchable()
|
||||
->options(
|
||||
collect(timezone_identifiers_list())
|
||||
->mapWithKeys(fn ($timezone) => [$timezone => $timezone])
|
||||
)
|
||||
->rules($rules['timezone']),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('save')
|
||||
->label('Save')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
app(UpdateUserProfileInformation::class)->update(auth()->user(), $this->all());
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Profile updated!')
|
||||
->send();
|
||||
}
|
||||
}
|
138
app/Web/Pages/Settings/Profile/Widgets/TwoFactor.php
Normal file
138
app/Web/Pages/Settings/Profile/Widgets/TwoFactor.php
Normal file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Profile\Widgets;
|
||||
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Infolists\Components\Actions\Action;
|
||||
use Filament\Infolists\Components\Section;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Infolists\Components\ViewEntry;
|
||||
use Filament\Infolists\Concerns\InteractsWithInfolists;
|
||||
use Filament\Infolists\Contracts\HasInfolists;
|
||||
use Filament\Infolists\Infolist;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
|
||||
use Laravel\Fortify\Actions\GenerateNewRecoveryCodes;
|
||||
|
||||
class TwoFactor extends Widget implements HasForms, HasInfolists
|
||||
{
|
||||
use InteractsWithForms;
|
||||
use InteractsWithInfolists;
|
||||
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected static bool $isLazy = false;
|
||||
|
||||
protected static string $view = 'web.components.infolist';
|
||||
|
||||
public bool $enabled = false;
|
||||
|
||||
public bool $showCodes = false;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
if (auth()->user()->two_factor_secret) {
|
||||
$this->enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function infolist(Infolist $infolist): Infolist
|
||||
{
|
||||
return $infolist->schema([
|
||||
Section::make()
|
||||
->heading('Two Factor Authentication')
|
||||
->description('Here you can activate 2FA to secure your account')
|
||||
->schema([
|
||||
TextEntry::make('disabled')
|
||||
->hiddenLabel()
|
||||
->state('Two factor authentication is disabled.')
|
||||
->visible(! $this->enabled),
|
||||
ViewEntry::make('qr_code')
|
||||
->hiddenLabel()
|
||||
->view('web.components.container', [
|
||||
'content' => $this->enabled ? auth()->user()->twoFactorQrCodeSvg() : null,
|
||||
])
|
||||
->visible($this->enabled && $this->showCodes),
|
||||
TextEntry::make('qr_code_manual')
|
||||
->label('If you are unable to scan the QR code, please use the 2FA secret instead.')
|
||||
->state($this->enabled ? decrypt(auth()->user()->two_factor_secret) : null)
|
||||
->copyable()
|
||||
->visible($this->enabled && $this->showCodes),
|
||||
TextEntry::make('recovery_codes_text')
|
||||
->hiddenLabel()
|
||||
->color('warning')
|
||||
->state('Store these recovery codes in a secure password manager. They can be used to recover access to your account if your two factor authentication device is lost.')
|
||||
->visible($this->enabled),
|
||||
ViewEntry::make('recovery_codes')
|
||||
->hiddenLabel()
|
||||
->extraAttributes(['class' => 'rounded-lg border border-gray-100 p-2 dark:border-gray-700'])
|
||||
->view('web.components.container', [
|
||||
'content' => $this->enabled ? implode('</br>', json_decode(decrypt(auth()->user()->two_factor_recovery_codes), true)) : null,
|
||||
])
|
||||
->visible($this->enabled),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('two-factor')
|
||||
->color($this->enabled ? 'danger' : 'primary')
|
||||
->label($this->enabled ? 'Disable' : 'Enable')
|
||||
->action(function () {
|
||||
if ($this->enabled) {
|
||||
$this->disableTwoFactor();
|
||||
} else {
|
||||
$this->enableTwoFactor();
|
||||
}
|
||||
}),
|
||||
Action::make('regenerate')
|
||||
->color('gray')
|
||||
->label('Regenerate Recovery Codes')
|
||||
->visible($this->enabled)
|
||||
->action(fn () => $this->regenerateRecoveryCodes()),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function enableTwoFactor(): void
|
||||
{
|
||||
app(EnableTwoFactorAuthentication::class)(auth()->user());
|
||||
|
||||
$this->enabled = true;
|
||||
$this->showCodes = true;
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Two factor authentication enabled')
|
||||
->send();
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}
|
||||
|
||||
public function disableTwoFactor(): void
|
||||
{
|
||||
app(DisableTwoFactorAuthentication::class)(auth()->user());
|
||||
|
||||
$this->enabled = false;
|
||||
$this->showCodes = false;
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Two factor authentication disabled')
|
||||
->send();
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}
|
||||
|
||||
public function regenerateRecoveryCodes(): void
|
||||
{
|
||||
app(GenerateNewRecoveryCodes::class)(auth()->user());
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Recovery codes generated')
|
||||
->send();
|
||||
|
||||
$this->dispatch('$refresh');
|
||||
}
|
||||
}
|
73
app/Web/Pages/Settings/Profile/Widgets/UpdatePassword.php
Normal file
73
app/Web/Pages/Settings/Profile/Widgets/UpdatePassword.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Profile\Widgets;
|
||||
|
||||
use App\Actions\User\UpdateUserPassword;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class UpdatePassword extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static bool $isLazy = false;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public string $current_password = '';
|
||||
|
||||
public string $password = '';
|
||||
|
||||
public string $password_confirmation = '';
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
$rules = UpdateUserPassword::rules();
|
||||
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Update Password')
|
||||
->description('Ensure your account is using a long, random password to stay secure.')
|
||||
->schema([
|
||||
TextInput::make('current_password')
|
||||
->label('Current Password')
|
||||
->password()
|
||||
->rules($rules['current_password']),
|
||||
TextInput::make('password')
|
||||
->label('New Password')
|
||||
->password()
|
||||
->rules($rules['password']),
|
||||
TextInput::make('password_confirmation')
|
||||
->label('Confirm Password')
|
||||
->password()
|
||||
->rules($rules['password_confirmation']),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('save')
|
||||
->label('Save')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
app(UpdateUserPassword::class)->update(auth()->user(), $this->all());
|
||||
|
||||
$this->current_password = '';
|
||||
$this->password = '';
|
||||
$this->password_confirmation = '';
|
||||
|
||||
Notification::make()
|
||||
->success()
|
||||
->title('Password updated!')
|
||||
->send();
|
||||
}
|
||||
}
|
66
app/Web/Pages/Settings/Projects/Index.php
Normal file
66
app/Web/Pages/Settings/Projects/Index.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects;
|
||||
|
||||
use App\Actions\Projects\CreateProject;
|
||||
use App\Models\Project;
|
||||
use App\Web\Traits\PageHasWidgets;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
use PageHasWidgets;
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $slug = 'settings/projects';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
||||
|
||||
protected static ?int $navigationSort = 4;
|
||||
|
||||
protected static ?string $title = 'Projects';
|
||||
|
||||
public static function getNavigationItemActiveRoutePattern(): string
|
||||
{
|
||||
return static::getRouteName().'*';
|
||||
}
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('viewAny', Project::class) ?? false;
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[Widgets\ProjectsList::class],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Create Project')
|
||||
->icon('heroicon-o-plus')
|
||||
->authorize('create', Project::class)
|
||||
->using(function (array $data) {
|
||||
return app(CreateProject::class)->create(auth()->user(), $data);
|
||||
})
|
||||
->form(function (Form $form) {
|
||||
return $form->schema([
|
||||
TextInput::make('name')
|
||||
->name('name')
|
||||
->rules(CreateProject::rules()['name']),
|
||||
])->columns(1);
|
||||
})
|
||||
->createAnother(false)
|
||||
->modalWidth(MaxWidth::Large),
|
||||
];
|
||||
}
|
||||
}
|
79
app/Web/Pages/Settings/Projects/Settings.php
Normal file
79
app/Web/Pages/Settings/Projects/Settings.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects;
|
||||
|
||||
use App\Actions\Projects\DeleteProject;
|
||||
use App\Models\Project;
|
||||
use App\Web\Pages\Settings\Projects\Widgets\AddUser;
|
||||
use App\Web\Pages\Settings\Projects\Widgets\ProjectUsersList;
|
||||
use App\Web\Pages\Settings\Projects\Widgets\UpdateProject;
|
||||
use App\Web\Traits\PageHasWidgets;
|
||||
use Exception;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Pages\Page;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
|
||||
class Settings extends Page
|
||||
{
|
||||
use PageHasWidgets;
|
||||
|
||||
protected static ?string $slug = 'settings/projects/{project}';
|
||||
|
||||
protected static ?string $title = 'Project Settings';
|
||||
|
||||
protected static bool $shouldRegisterNavigation = false;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('update', request()->route('project')) ?? false;
|
||||
}
|
||||
|
||||
public Project $project;
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
UpdateProject::class,
|
||||
['project' => $this->project],
|
||||
],
|
||||
[
|
||||
AddUser::class,
|
||||
['project' => $this->project],
|
||||
],
|
||||
[
|
||||
ProjectUsersList::class,
|
||||
['project' => $this->project],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getTitle(): string|Htmlable
|
||||
{
|
||||
return 'Project Settings';
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make()
|
||||
->record($this->project)
|
||||
->label('Delete Project')
|
||||
->modalHeading('Delete Project')
|
||||
->modalDescription('Are you sure you want to delete this project? This action will delete all associated data and cannot be undone.')
|
||||
->using(function (Project $record) {
|
||||
try {
|
||||
app(DeleteProject::class)->delete(auth()->user(), $record);
|
||||
|
||||
$this->redirectRoute('filament.app.resources.projects.index');
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
}
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
73
app/Web/Pages/Settings/Projects/Widgets/AddUser.php
Normal file
73
app/Web/Pages/Settings/Projects/Widgets/AddUser.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class AddUser extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public Project $project;
|
||||
|
||||
public ?int $user;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
}
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Add User')
|
||||
->schema([
|
||||
Select::make('user')
|
||||
->name('user')
|
||||
->options(fn () => User::query()->pluck('name', 'id'))
|
||||
->searchable()
|
||||
->rules(\App\Actions\Projects\AddUser::rules($this->project)['user']),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('add')
|
||||
->label('Add')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->authorize('update', $this->project);
|
||||
|
||||
$this->validate();
|
||||
|
||||
app(\App\Actions\Projects\AddUser::class)
|
||||
->add($this->project, [
|
||||
'user' => $this->user,
|
||||
]);
|
||||
|
||||
Notification::make()
|
||||
->title('User added!')
|
||||
->success()
|
||||
->send();
|
||||
|
||||
$this->user = null;
|
||||
}
|
||||
|
||||
public function updated(): void
|
||||
{
|
||||
$this->dispatch('userAdded');
|
||||
}
|
||||
}
|
53
app/Web/Pages/Settings/Projects/Widgets/ProjectUsersList.php
Normal file
53
app/Web/Pages/Settings/Projects/Widgets/ProjectUsersList.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ProjectUsersList extends Widget
|
||||
{
|
||||
protected $listeners = ['userAdded' => '$refresh'];
|
||||
|
||||
public Project $project;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
}
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return User::query()->whereHas('projects', function (Builder $query) {
|
||||
$query->where('project_id', $this->project->id);
|
||||
});
|
||||
}
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
Tables\Columns\TextColumn::make('id')->width('20%'),
|
||||
Tables\Columns\TextColumn::make('name')->width('20%'),
|
||||
Tables\Columns\TextColumn::make('email')->width('20%'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table->actions([
|
||||
Tables\Actions\DeleteAction::make()
|
||||
->label('Remove')
|
||||
->modalHeading('Remove user from project')
|
||||
->visible(function ($record) {
|
||||
return $this->authorize('update', $this->project)->allowed() && $record->id !== auth()->id();
|
||||
})
|
||||
->using(function ($record) {
|
||||
$this->project->users()->detach($record);
|
||||
}),
|
||||
])->paginated(false);
|
||||
}
|
||||
}
|
47
app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php
Normal file
47
app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Web\Pages\Settings\Projects\Settings;
|
||||
use Filament\Tables\Actions\Action;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ProjectsList extends Widget
|
||||
{
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return Project::query();
|
||||
}
|
||||
|
||||
protected static ?string $heading = '';
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('created_at_by_timezone')
|
||||
->label('Created At')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table
|
||||
->recordUrl(fn (Project $record) => Settings::getUrl(['project' => $record]))
|
||||
->actions([
|
||||
Action::make('settings')
|
||||
->label('Settings')
|
||||
->icon('heroicon-o-cog-6-tooth')
|
||||
->authorize(fn ($record) => auth()->user()->can('update', $record))
|
||||
->url(fn (Project $record) => Settings::getUrl(['project' => $record])),
|
||||
]);
|
||||
}
|
||||
}
|
61
app/Web/Pages/Settings/Projects/Widgets/SelectProject.php
Normal file
61
app/Web/Pages/Settings/Projects/Widgets/SelectProject.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Widgets\Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class SelectProject extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public int|string|null $project;
|
||||
|
||||
protected function getFormSchema(): array
|
||||
{
|
||||
$options = Project::query()
|
||||
->where(function (Builder $query) {
|
||||
if (auth()->user()->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
$query->where('user_id', auth()->id())
|
||||
->orWhereHas('users', fn ($query) => $query->where('user_id', auth()->id()));
|
||||
})
|
||||
->get()
|
||||
->mapWithKeys(fn ($project) => [$project->id => $project->name])
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
Select::make('project')
|
||||
->name('project')
|
||||
->model($this->project)
|
||||
->label('Project')
|
||||
->searchable()
|
||||
->options($options)
|
||||
->searchPrompt('Select a project...')
|
||||
->extraAttributes(['class' => '-mx-2 pointer-choices'])
|
||||
->selectablePlaceholder(false)
|
||||
->live(),
|
||||
];
|
||||
}
|
||||
|
||||
public function updatedProject($value): void
|
||||
{
|
||||
$project = Project::query()->findOrFail($value);
|
||||
$this->authorize('view', $project);
|
||||
auth()->user()->update(['current_project_id' => $value]);
|
||||
|
||||
$this->redirect('/app');
|
||||
}
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->project = auth()->user()->current_project_id;
|
||||
}
|
||||
}
|
66
app/Web/Pages/Settings/Projects/Widgets/UpdateProject.php
Normal file
66
app/Web/Pages/Settings/Projects/Widgets/UpdateProject.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class UpdateProject extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public Project $project;
|
||||
|
||||
public string $name;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->name = $project->name;
|
||||
}
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Project Information')
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->name('name')
|
||||
->label('Name')
|
||||
->rules(\App\Actions\Projects\UpdateProject::rules($this->project)['name'])
|
||||
->placeholder('Enter the project name'),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('save')
|
||||
->label('Save')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->authorize('update', $this->project);
|
||||
|
||||
$this->validate();
|
||||
|
||||
app(\App\Actions\Projects\UpdateProject::class)
|
||||
->update($this->project, [
|
||||
'name' => $this->name,
|
||||
]);
|
||||
|
||||
Notification::make()
|
||||
->title('Project updated successfully!')
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
}
|
70
app/Web/Pages/Settings/ServerProviders/Actions/Create.php
Normal file
70
app/Web/Pages/Settings/ServerProviders/Actions/Create.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\ServerProviders\Actions;
|
||||
|
||||
use App\Actions\ServerProvider\CreateServerProvider;
|
||||
use App\Enums\ServerProvider;
|
||||
use Exception;
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Notifications\Notification;
|
||||
|
||||
class Create
|
||||
{
|
||||
public static function form(): array
|
||||
{
|
||||
return [
|
||||
Select::make('provider')
|
||||
->options(
|
||||
collect(config('core.server_providers'))
|
||||
->filter(fn ($provider) => $provider != ServerProvider::CUSTOM)
|
||||
->mapWithKeys(fn ($provider) => [$provider => $provider])
|
||||
)
|
||||
->live()
|
||||
->reactive()
|
||||
->rules(CreateServerProvider::rules()['provider']),
|
||||
TextInput::make('name')
|
||||
->rules(CreateServerProvider::rules()['name']),
|
||||
TextInput::make('token')
|
||||
->label('API Key')
|
||||
->validationAttribute('API Key')
|
||||
->visible(fn ($get) => in_array($get('provider'), [
|
||||
ServerProvider::DIGITALOCEAN,
|
||||
ServerProvider::LINODE,
|
||||
ServerProvider::VULTR,
|
||||
ServerProvider::HETZNER,
|
||||
]))
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['token']),
|
||||
TextInput::make('key')
|
||||
->label('Access Key')
|
||||
->visible(function ($get) {
|
||||
return $get('provider') == ServerProvider::AWS;
|
||||
})
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['key']),
|
||||
TextInput::make('secret')
|
||||
->label('Secret')
|
||||
->visible(fn ($get) => $get('provider') == ServerProvider::AWS)
|
||||
->rules(fn ($get) => CreateServerProvider::providerRules($get())['secret']),
|
||||
Checkbox::make('global')
|
||||
->label('Is Global (Accessible in all projects)'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function action(array $data): void
|
||||
{
|
||||
try {
|
||||
app(CreateServerProvider::class)->create(auth()->user(), $data);
|
||||
} catch (Exception $e) {
|
||||
Notification::make()
|
||||
->title($e->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
27
app/Web/Pages/Settings/ServerProviders/Actions/Edit.php
Normal file
27
app/Web/Pages/Settings/ServerProviders/Actions/Edit.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\ServerProviders\Actions;
|
||||
|
||||
use App\Actions\ServerProvider\EditServerProvider;
|
||||
use App\Models\ServerProvider;
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
|
||||
class Edit
|
||||
{
|
||||
public static function form(): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('name')
|
||||
->label('Name')
|
||||
->rules(EditServerProvider::rules()['name']),
|
||||
Checkbox::make('global')
|
||||
->label('Is Global (Accessible in all projects)'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function action(ServerProvider $provider, array $data): void
|
||||
{
|
||||
app(EditServerProvider::class)->edit($provider, auth()->user(), $data);
|
||||
}
|
||||
}
|
51
app/Web/Pages/Settings/ServerProviders/Index.php
Normal file
51
app/Web/Pages/Settings/ServerProviders/Index.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\ServerProviders;
|
||||
|
||||
use App\Enums\ServerProvider;
|
||||
use App\Web\Traits\PageHasWidgets;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
use PageHasWidgets;
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $slug = 'settings/server-providers';
|
||||
|
||||
protected static ?string $title = 'Server Providers';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-server-stack';
|
||||
|
||||
protected static ?int $navigationSort = 5;
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('viewAny', ServerProvider::class) ?? false;
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[Widgets\ServerProvidersList::class],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Connect')
|
||||
->modalHeading('Connect to a Server Provider')
|
||||
->modalSubmitActionLabel('Connect')
|
||||
->createAnother(false)
|
||||
->form(Actions\Create::form())
|
||||
->authorize('create', ServerProvider::class)
|
||||
->modalWidth(MaxWidth::Medium)
|
||||
->using(fn (array $data) => Actions\Create::action($data)),
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\ServerProviders\Widgets;
|
||||
|
||||
use App\Actions\ServerProvider\DeleteServerProvider;
|
||||
use App\Models\ServerProvider;
|
||||
use App\Web\Pages\Settings\ServerProviders\Actions\Edit;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
use Filament\Tables\Actions\DeleteAction;
|
||||
use Filament\Tables\Actions\EditAction;
|
||||
use Filament\Tables\Columns\ImageColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ServerProvidersList extends Widget
|
||||
{
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return ServerProvider::getByProjectId(auth()->user()->current_project_id);
|
||||
}
|
||||
|
||||
protected static ?string $heading = '';
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
ImageColumn::make('image_url')
|
||||
->label('Provider')
|
||||
->size(24),
|
||||
TextColumn::make('name')
|
||||
->default(fn ($record) => $record->profile)
|
||||
->label('Name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('id')
|
||||
->label('Global')
|
||||
->badge()
|
||||
->color(fn ($record) => $record->project_id ? 'gray' : 'success')
|
||||
->formatStateUsing(function (ServerProvider $record) {
|
||||
return $record->project_id ? 'No' : 'Yes';
|
||||
}),
|
||||
TextColumn::make('created_at_by_timezone')
|
||||
->label('Created At')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table->actions([
|
||||
EditAction::make('edit')
|
||||
->label('Edit')
|
||||
->modalHeading('Edit Server Provider')
|
||||
->mutateRecordDataUsing(function (array $data, ServerProvider $record) {
|
||||
return [
|
||||
'name' => $record->profile,
|
||||
'global' => $record->project_id === null,
|
||||
];
|
||||
})
|
||||
->form(Edit::form())
|
||||
->authorize(fn (ServerProvider $record) => auth()->user()->can('update', $record))
|
||||
->using(fn (array $data, ServerProvider $record) => Edit::action($record, $data))
|
||||
->modalWidth(MaxWidth::Medium),
|
||||
DeleteAction::make('delete')
|
||||
->label('Delete')
|
||||
->modalHeading('Delete Server Provider')
|
||||
->authorize(fn (ServerProvider $record) => auth()->user()->can('delete', $record))
|
||||
->using(function (array $data, ServerProvider $record) {
|
||||
app(DeleteServerProvider::class)->delete($record);
|
||||
}),
|
||||
]);
|
||||
}
|
||||
}
|
73
app/Web/Pages/Settings/Users/Index.php
Normal file
73
app/Web/Pages/Settings/Users/Index.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Users;
|
||||
|
||||
use App\Actions\User\CreateUser;
|
||||
use App\Models\User;
|
||||
use App\Web\Traits\PageHasWidgets;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
|
||||
class Index extends Page
|
||||
{
|
||||
use PageHasWidgets;
|
||||
|
||||
protected static ?string $navigationGroup = 'Settings';
|
||||
|
||||
protected static ?string $slug = 'users';
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-users';
|
||||
|
||||
protected static ?int $navigationSort = 3;
|
||||
|
||||
protected static ?string $title = 'Users';
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return auth()->user()?->can('viewAny', User::class) ?? false;
|
||||
}
|
||||
|
||||
public function getWidgets(): array
|
||||
{
|
||||
return [
|
||||
[Widgets\UsersList::class],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Create User')
|
||||
->authorize('create', User::class)
|
||||
->action(function (array $data) {
|
||||
$user = app(CreateUser::class)->create($data);
|
||||
$this->dispatch('$refresh');
|
||||
|
||||
return $user;
|
||||
})
|
||||
->form(function (Form $form) {
|
||||
$rules = CreateUser::rules();
|
||||
|
||||
return $form
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->rules($rules['name']),
|
||||
TextInput::make('email')
|
||||
->rules($rules['email']),
|
||||
TextInput::make('password')
|
||||
->rules($rules['password']),
|
||||
Select::make('role')
|
||||
->rules($rules['role'])
|
||||
->options(collect(config('core.user_roles'))->mapWithKeys(fn ($role) => [$role => $role])),
|
||||
])
|
||||
->columns(1);
|
||||
})
|
||||
->modalWidth(MaxWidth::Large),
|
||||
];
|
||||
}
|
||||
}
|
113
app/Web/Pages/Settings/Users/Widgets/UsersList.php
Normal file
113
app/Web/Pages/Settings/Users/Widgets/UsersList.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Users\Widgets;
|
||||
|
||||
use App\Actions\User\UpdateProjects;
|
||||
use App\Actions\User\UpdateUser;
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Filament\Forms\Components\CheckboxList;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
use Filament\Tables\Actions\Action;
|
||||
use Filament\Tables\Actions\DeleteAction;
|
||||
use Filament\Tables\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class UsersList extends Widget
|
||||
{
|
||||
protected $listeners = ['$refresh'];
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return User::query();
|
||||
}
|
||||
|
||||
protected static ?string $heading = '';
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('email')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('timezone'),
|
||||
TextColumn::make('created_at_by_timezone')
|
||||
->label('Created At')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('role'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table
|
||||
->actions([
|
||||
EditAction::make('edit')
|
||||
->authorize(fn ($record) => auth()->user()->can('update', $record))
|
||||
->using(function ($record, array $data) {
|
||||
app(UpdateUser::class)->update($record, $data);
|
||||
})
|
||||
->form(function (Form $form, $record) {
|
||||
return $form
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->rules(UpdateUser::rules($record)['name']),
|
||||
TextInput::make('email')
|
||||
->rules(UpdateUser::rules($record)['email']),
|
||||
Select::make('timezone')
|
||||
->searchable()
|
||||
->options(
|
||||
collect(timezone_identifiers_list())
|
||||
->mapWithKeys(fn ($timezone) => [$timezone => $timezone])
|
||||
)
|
||||
->rules(UpdateUser::rules($record)['timezone']),
|
||||
Select::make('role')
|
||||
->options(
|
||||
collect(config('core.user_roles'))
|
||||
->mapWithKeys(fn ($role) => [$role => $role])
|
||||
)
|
||||
->rules(UpdateUser::rules($record)['role']),
|
||||
])
|
||||
->columns(1);
|
||||
})
|
||||
->modalWidth(MaxWidth::Large),
|
||||
Action::make('update-projects')
|
||||
->label('Projects')
|
||||
->icon('heroicon-o-rectangle-stack')
|
||||
->authorize(fn ($record) => auth()->user()->can('update', $record))
|
||||
->action(function ($record, array $data) {
|
||||
app(UpdateProjects::class)->update($record, $data);
|
||||
Notification::make()
|
||||
->title('Projects Updated')
|
||||
->success()
|
||||
->send();
|
||||
})
|
||||
->form(function (Form $form, $record) {
|
||||
return $form
|
||||
->schema([
|
||||
CheckboxList::make('projects')
|
||||
->options(Project::query()->pluck('name', 'id')->toArray())
|
||||
->searchable()
|
||||
->default($record->projects->pluck('id')->toArray())
|
||||
->rules(UpdateProjects::rules()['projects.*']),
|
||||
])
|
||||
->columns(1);
|
||||
})
|
||||
->modalSubmitActionLabel('Save')
|
||||
->modalWidth(MaxWidth::Large),
|
||||
DeleteAction::make()
|
||||
->authorize(fn ($record) => auth()->user()->can('delete', $record)),
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user