您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

UpdatePassword.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Livewire;
  3. use Filament\Facades\Filament;
  4. use Filament\Forms;
  5. use Filament\Forms\Concerns\InteractsWithForms;
  6. use Filament\Forms\Contracts\HasForms;
  7. use Filament\Forms\Form;
  8. use Filament\Notifications\Notification;
  9. use Filament\Support\Exceptions\Halt;
  10. use Illuminate\Contracts\Auth\Authenticatable;
  11. use Illuminate\Contracts\View\View;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Support\Facades\Hash;
  14. use Illuminate\Validation\Rules\Password;
  15. use Livewire\Component;
  16. use RuntimeException;
  17. /**
  18. * @property Form $form
  19. */
  20. class UpdatePassword extends Component implements HasForms
  21. {
  22. use InteractsWithForms;
  23. /**
  24. * @var array<string, mixed> | null
  25. */
  26. public ?array $data = [];
  27. public function mount(): void
  28. {
  29. $this->fillForm();
  30. }
  31. public function getUser(): Authenticatable | Model
  32. {
  33. $user = Filament::auth()->user();
  34. if (! $user instanceof Model) {
  35. throw new RuntimeException('The authenticated user object must be an Eloquent model to allow profile information to be updated.');
  36. }
  37. return $user;
  38. }
  39. public function fillForm(): void
  40. {
  41. $data = $this->getUser()->attributesToArray();
  42. $data = $this->mutateFormDataBeforeFill($data);
  43. $this->form->fill($data);
  44. }
  45. /**
  46. * @param array<string, mixed> $data
  47. * @return array<string, mixed>
  48. */
  49. protected function mutateFormDataBeforeFill(array $data): array
  50. {
  51. return $data;
  52. }
  53. /**
  54. * @param array<string, mixed> $data
  55. * @return array<string, mixed>
  56. */
  57. protected function mutateFormDataBeforeSave(array $data): array
  58. {
  59. return $data;
  60. }
  61. public function save(): void
  62. {
  63. try {
  64. $data = $this->form->getState();
  65. $data = $this->mutateFormDataBeforeSave($data);
  66. $this->handleRecordUpdate($this->getUser(), $data);
  67. } catch (Halt $exception) {
  68. return;
  69. }
  70. if (session() !== null) {
  71. session()->put([
  72. 'password_hash_' . Filament::getAuthGuard() => Filament::auth()->user()?->getAuthPassword(),
  73. ]);
  74. }
  75. $this->getSavedNotification()?->send();
  76. $this->fillForm();
  77. }
  78. /**
  79. * @param array<string, mixed> $data
  80. */
  81. protected function handleRecordUpdate(Model $record, array $data): Model
  82. {
  83. $record->update($data);
  84. return $record;
  85. }
  86. protected function getSavedNotification(): ?Notification
  87. {
  88. $title = $this->getSavedNotificationTitle();
  89. if (blank($title)) {
  90. return null;
  91. }
  92. return Notification::make()
  93. ->success()
  94. ->title($this->getSavedNotificationTitle())
  95. ->body($this->getSavedNotificationBody());
  96. }
  97. protected function getSavedNotificationTitle(): ?string
  98. {
  99. return __('filament-companies::default.notifications.profile_information_updated.title');
  100. }
  101. protected function getSavedNotificationBody(): ?string
  102. {
  103. return __('filament-companies::default.notifications.profile_information_updated.body');
  104. }
  105. public function form(Form $form): Form
  106. {
  107. return $form
  108. ->schema([
  109. Forms\Components\TextInput::make('current_password')
  110. ->label(__('filament-companies::default.fields.current_password'))
  111. ->password()
  112. ->currentPassword()
  113. ->revealable()
  114. ->validationMessages([
  115. 'current_password' => __('filament-companies::default.errors.password_does_not_match'),
  116. ])
  117. ->autocomplete('current-password')
  118. ->required(),
  119. Forms\Components\TextInput::make('password')
  120. ->label(__('filament-companies::default.labels.new_password'))
  121. ->password()
  122. ->revealable()
  123. ->autocomplete('new-password')
  124. ->rule(Password::default())
  125. ->required()
  126. ->dehydrated(static fn ($state): bool => filled($state))
  127. ->dehydrateStateUsing(static fn ($state): string => Hash::make($state))
  128. ->same('password_confirmation'),
  129. Forms\Components\TextInput::make('password_confirmation')
  130. ->label(__('filament-companies::default.labels.password_confirmation'))
  131. ->password()
  132. ->revealable()
  133. ->autocomplete('new-password')
  134. ->required()
  135. ->dehydrated(false),
  136. ])
  137. ->operation('edit')
  138. ->model($this->getUser())
  139. ->statePath('data');
  140. }
  141. public function render(): View
  142. {
  143. return view('livewire.update-password-form');
  144. }
  145. }