cms-laravel/app/Livewire/UserManagement/Edit.php

162 lines
6.6 KiB
PHP

<?php
namespace App\Livewire\UserManagement;
use Livewire\Component;
use App\Services\ApiService;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Log;
class Edit extends Component
{
public $username = '';
public $firstname = '';
public $lastname = '';
public $email = '';
public $role = '';
public $status = '';
public $password = '';
public $userInfo = null;
public $mounted = false;
public $loading = false;
public $isGenerated = false;
public $id;
protected $apiService;
public function boot(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function mount($id)
{
// Check role-based access
$userInfo = Session::get('userInfo');
if (!$userInfo || $userInfo['role'] != 1) {
return redirect()->route('404');
}
$this->id = $id;
try {
$response = $this->apiService->get("admin/{$id}");
if ($response && isset($response['data'])) {
$this->userInfo = $response['data'];
$this->username = $this->userInfo['username'] ?? '';
$this->firstname = $this->userInfo['firstname'] ?? '';
$this->lastname = $this->userInfo['lastname'] ?? '';
$this->email = $this->userInfo['email'] ?? '';
$this->role = $this->userInfo['role'] ?? '';
$this->status = $this->userInfo['status'] ?? '';
$this->password = $this->userInfo['generated_password'] ?? '*******************';
$this->isGenerated = !empty($this->userInfo['generated_password']);
$this->mounted = true;
Log::info('User data loaded for edit', ['source' => 'UserManagementEdit']);
}
} catch (\Exception $e) {
Session::flash('error', 'Something went wrong loading data: ' . $e->getMessage());
Log::error('Failed to load user data', ['error' => $e->getMessage(), 'source' => 'UserManagementEdit']);
if ($e->getCode() == 404) {
return redirect()->route('404');
}
$this->mounted = false;
}
}
public function update()
{
$this->loading = true;
try {
$this->validate([
'username' => ['required', 'max:128', 'regex:/^[a-zA-Z0-9_@.ñÑ ]+$/'],
'firstname' => ['required', 'max:128', 'regex:/^[A-Za-z ñÑ-]+$/'],
'lastname' => ['required', 'max:128', 'regex:/^[A-Za-z ñÑ-]+$/'],
'email' => ['required', 'email', 'max:128', 'regex:/^[A-Za-z0-9@_.ñÑ ]+$/'],
'role' => ['required'],
'status' => ['required'],
'password' => ['required'],
], [
'username.required' => 'Username is required!',
'username.max' => 'Maximum character is 128.',
'username.regex' => 'Invalid username.',
'firstname.required' => 'First Name is required!',
'firstname.max' => 'Maximum character is 128.',
'firstname.regex' => 'Invalid First Name.',
'lastname.required' => 'Last Name is required!',
'lastname.max' => 'Maximum character is 128.',
'lastname.regex' => 'Invalid Last Name.',
'email.required' => 'Email is required!',
'email.email' => 'Invalid Email Address.',
'email.max' => 'Maximum character is 128.',
'email.regex' => 'Invalid Email Address.',
'role.required' => 'Role is required!',
'status.required' => 'Status is required!',
'password.required' => 'Default Password is required!',
]);
$params = [
'username' => strtolower(trim($this->username)),
'firstname' => trim($this->firstname),
'lastname' => trim($this->lastname),
'email' => $this->email,
'role' => $this->role,
'status' => $this->status,
];
if ($this->password !== '*******************') {
$params['password'] = $this->password;
}
$response = $this->apiService->put("admin/{$this->userInfo['admin_uuid']}", $params);
if ($response && isset($response['status']) && $response['status'] === 200) {
$message = $this->isGenerated ? 'User account updated successfully. Please send the new temporary password to the user.' : 'User account updated successfully.';
Session::flash('success', $message);
Log::info('User updated successfully', ['source' => 'UserManagementEdit']);
return redirect()->route('user-management');
}
} catch (\Illuminate\Validation\ValidationException $e) {
$this->errors = $e->errors();
Session::flash('error', 'Something went wrong updating record: ' . implode(', ', array_merge(...array_values($e->errors()))));
Log::error('Validation failed', ['errors' => $e->errors(), 'source' => 'UserManagementEdit']);
} catch (\Exception $e) {
Session::flash('error', 'Something went wrong updating record: ' . $e->getMessage());
Log::error('Failed to update user', ['error' => $e->getMessage(), 'source' => 'UserManagementEdit']);
} finally {
$this->loading = false;
}
}
public function generatePassword()
{
$this->loading = true;
try {
$response = $this->apiService->post('generatePassword', ['admin_uuid' => $this->userInfo['admin_uuid']]);
if ($response && isset($response['data']['password'])) {
$this->password = $response['data']['password'];
$this->isGenerated = true;
Session::flash('success', 'Password generated successfully.');
Log::info('Password generated', ['source' => 'UserManagementEdit']);
}
} catch (\Exception $e) {
Session::flash('error', 'Something went wrong generating password: ' . $e->getMessage());
Log::error('Failed to generate password', ['error' => $e->getMessage(), 'source' => 'UserManagementEdit']);
} finally {
$this->loading = false;
}
}
public function copyPassword()
{
// Client-side copy handled in Blade
$this->dispatchBrowserEvent('copy-password', ['password' => $this->password]);
}
public function render()
{
return view('livewire.user-management.edit')->layout('layouts.app', ['title' => 'Update User']);
}
}