You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
73 lines
2.0 KiB
<?php
|
|
|
|
namespace App\Components\Profile;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class Manager extends Component
|
|
{
|
|
// Profile & roles
|
|
public object $user;
|
|
public array $roles = [];
|
|
public array $allRoles = [];
|
|
public string $mode = 'view'; // 'view' | 'password'
|
|
public string $currentPassword = '';
|
|
public string $password = '';
|
|
public string $password_confirmation = '';
|
|
|
|
protected array $rules = [
|
|
'currentPassword' => ['required', 'string'],
|
|
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
|
];
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->user = Auth::user();
|
|
$this->allRoles = Role::all()->pluck('name')->toArray();
|
|
$this->roles = $this->user->roles->pluck('name')->toArray();
|
|
}
|
|
|
|
public function showPasswordForm(): void
|
|
{
|
|
$this->mode = 'password';
|
|
$this->resetErrorBag();
|
|
$this->resetValidation();
|
|
}
|
|
|
|
public function showView(): void
|
|
{
|
|
$this->mode = 'view';
|
|
$this->reset(['currentPassword', 'password', 'password_confirmation']);
|
|
$this->resetErrorBag();
|
|
}
|
|
|
|
public function updatePassword(): void
|
|
{
|
|
// 1. Validate
|
|
$this->validate();
|
|
|
|
if (! Hash::check($this->currentPassword, $this->user->password)) {
|
|
$this->addError('currentPassword', 'Mật khẩu hiện tại không đúng.');
|
|
return;
|
|
}
|
|
|
|
$this->user->password = Hash::make($this->password);
|
|
$this->user->save();
|
|
|
|
session()->flash('message', 'Đổi mật khẩu thành công.');
|
|
$this->showView();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('components.profile.manager', [
|
|
'user' => $this->user,
|
|
'roles' => $this->roles,
|
|
'allRoles' => collect($this->allRoles),
|
|
'title' => 'My Profile',
|
|
]);
|
|
}
|
|
}
|
|
|