File: /var/www/admin.fixgini.com/app/Livewire/Admin/Index.php
<?php
namespace App\Livewire\Admin;
use App\Models\Admin;
use Livewire\Component;
use Livewire\WithPagination;
use App\Mail\AdminNewAccount;
use App\Models\OtpVerification;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
class Index extends Component
{
public $name, $lastname, $email, $phone, $role, $permissions, $password, $status;
public function mount()
{
$this->generatepassword();
$this->permissions = [];
}
public function generatePassword()
{
// Define the character pool
$uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$lowercase = 'abcdefghijklmnopqrstuvwxyz';
$numbers = '0123456789';
$specialCharacters = '!@#$%^&*()_+-=[]{}|;:,.<>?';
// Shuffle and pick random characters
$password = substr(str_shuffle($uppercase), 0, 3) .
substr(str_shuffle($lowercase), 0, 3) .
substr(str_shuffle($numbers), 0, 3) .
substr(str_shuffle($specialCharacters), 0, 3);
// Shuffle the combined password to randomize the order
$this->password = str_shuffle($password);
}
use WithPagination;
public $search = '';
public $sortField = 'name';
public $sortDirection = 'asc';
public $perPage = 10;
public function sortBy($field)
{
$this->sortDirection = $this->sortField === $field
? ($this->sortDirection === 'asc' ? 'desc' : 'asc')
: 'asc';
$this->sortField = $field;
}
public function updatingSearch()
{
$this->resetPage();
}
public $adminId;
public function editAdmin($id)
{
$admin = Admin::findOrFail($id);
$this->adminId = $admin->id;
$this->name = $admin->name;
$this->lastname = $admin->lastname;
$this->email = $admin->email;
$this->phone = $admin->phone;
$this->status = $admin->status;
$this->role = $admin->role;
$this->permissions = $admin->permissions;
$this->password = $admin->password;
$this->dispatch('show-modal');
}
public function saveAdminUser()
{
try {
$validationRules = [
'name' => 'required|string',
'lastname' => 'required|string',
'role' => 'required|string',
'permissions' => 'required|array|min:1',
'permissions.*' => 'distinct',
'password' => 'nullable|string',
];
// Conditionally add rules for 'email' and 'phone' based on whether adminId exists
if ($this->adminId) {
$validationRules['email'] = [
'required',
'email',
'exists:admin_users,email',
];
$validationRules['phone'] = 'required|numeric|exists:admin_users,phone';
} else {
$validationRules['email'] = [
'required',
'email',
'unique:admin_users,email',
];
$validationRules['phone'] = 'required|numeric|unique:admin_users,phone';
}
$this->validate($validationRules);
if ($this->adminId) {
// Update existing admin
$admin = Admin::findOrFail($this->adminId);
$admin->update([
'name' => $this->name,
'lastname' => $this->lastname,
'email' => $this->email,
'phone' => $this->phone,
'role' => $this->role,
'permissions' => $this->permissions,
'status' => $this->status,
]);
session()->flash('success', 'Admin updated successfully.');
return redirect()->to('admin/users');
} else {
// Create new admin
$admin = Admin::create([
'name' => $this->name,
'lastname' => $this->lastname,
'email' => $this->email,
'phone' => $this->phone,
'role' => $this->role,
'created_by' => Auth::user()->name,
'permissions' => $this->permissions,
'password' => Hash::make($this->password),
]);
// store password to user email
OtpVerification::updateOrCreate(
['email' => $admin->email],
['otp' => $this->password]
);
Mail::to($this->email)->send(new AdminNewAccount($admin, $this->password));
session()->flash('success', 'Admin created successfully.');
return redirect()->to('admin/users');
}
} catch (\Throwable $th) {
Log::error($th->getMessage());
session()->flash('error', 'An error occurred: ' . $th->getMessage());
return redirect()->to('admin/users');
}
}
public function confirmDeletion($adminId)
{
try {
$admin = Admin::findOrFail($adminId);
$admin->delete();
session()->flash('success', 'Admin User deleted successfully.');
} catch (\Throwable $e) {
session()->flash('error', 'An error occurred while deleting the admin.');
}
}
public function editAdminForm()
{
$admin = Admin::find($this->adminId);
$admin->update([
'adminId' => $this->adminId,
'name' => $this->name,
'lastname' => $this->lastname,
'email' => $this->email,
'phone' => $this->phone,
'role' => $this->role,
'status' => $this->status,
'permissions' => $this->permissions,
'password' => $this->password,
]);
session()->flash('message', 'Admin updated successfully.');
$this->redirect('/admin/users');
}
public function sendTempoPassword()
{
$this->generatepassword();
$admin = Admin::where('id', $this->adminId)->first();
OtpVerification::updateOrCreate(
['email' => $admin->email],
['otp' => $this->password]
);
$admin->update([
'password' => Hash::make($this->password),
]);
Mail::to($this->email)->send(new AdminNewAccount($admin, $this->password));
$this->reset();
$this->dispatch('hide-modal');
session()->flash('success', 'Temporary password sent successfully.');
$this->redirect('/admin/users', navigate: true);
}
public function render()
{
return view('livewire.admin.index', [
'admins' => Admin::query()
->orWhere('name', 'like', '%' . $this->search . '%')
->orWhere('lastname', 'like', '%' . $this->search . '%')
->orWhere('email', 'like', '%' . $this->search . '%')
->orderBy($this->sortField, $this->sortDirection)
->latest()
->simplePaginate($this->perPage),
]);
}
}