96 lines
2.8 KiB
PHP
96 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreMailProfileRequest;
|
|
use App\Http\Requests\UpdateMailProfileRequest;
|
|
use App\Models\MailProfile;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class MailProfileController extends Controller
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public function index(): Response
|
|
{
|
|
$this->authorize('viewAny', MailProfile::class);
|
|
$profiles = MailProfile::query()
|
|
->orderBy('priority')
|
|
->orderBy('id')
|
|
->get([
|
|
'id', 'name', 'active', 'host', 'port', 'encryption', 'from_address', 'priority', 'last_success_at', 'last_error_at', 'last_error_message', 'test_status', 'test_checked_at',
|
|
]);
|
|
|
|
return Inertia::render('Admin/MailProfiles/Index', [
|
|
'profiles' => $profiles,
|
|
]);
|
|
}
|
|
|
|
public function store(StoreMailProfileRequest $request)
|
|
{
|
|
$data = $request->validated();
|
|
$profile = new MailProfile;
|
|
foreach ($data as $key => $val) {
|
|
if ($key === 'password') {
|
|
$profile->password = $val; // triggers mutator to encrypt
|
|
} else {
|
|
$profile->{$key} = $val;
|
|
}
|
|
}
|
|
$profile->save();
|
|
|
|
return back()->with('success', 'Mail profile created');
|
|
}
|
|
|
|
public function update(UpdateMailProfileRequest $request, MailProfile $mailProfile)
|
|
{
|
|
$data = $request->validated();
|
|
foreach ($data as $key => $val) {
|
|
if ($key === 'password') {
|
|
if ($val !== null && $val !== '') {
|
|
$mailProfile->password = $val;
|
|
}
|
|
} else {
|
|
$mailProfile->{$key} = $val;
|
|
}
|
|
}
|
|
$mailProfile->save();
|
|
|
|
return back()->with('success', 'Mail profile updated');
|
|
}
|
|
|
|
public function toggle(Request $request, MailProfile $mailProfile)
|
|
{
|
|
$this->authorize('update', $mailProfile);
|
|
$mailProfile->active = ! $mailProfile->active;
|
|
$mailProfile->save();
|
|
|
|
return back()->with('success', 'Status updated');
|
|
}
|
|
|
|
public function test(Request $request, MailProfile $mailProfile)
|
|
{
|
|
$this->authorize('test', $mailProfile);
|
|
$mailProfile->forceFill([
|
|
'test_status' => 'queued',
|
|
'test_checked_at' => now(),
|
|
])->save();
|
|
|
|
\App\Jobs\TestMailProfileConnection::dispatch($mailProfile->id);
|
|
|
|
return back()->with('success', 'Test queued');
|
|
}
|
|
|
|
public function destroy(MailProfile $mailProfile)
|
|
{
|
|
$this->authorize('delete', $mailProfile);
|
|
$mailProfile->delete();
|
|
|
|
return back()->with('success', 'Mail profile deleted');
|
|
}
|
|
}
|