104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Domain;
|
|
use App\Models\MailAlias;
|
|
use App\Services\WebhookService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AliasController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$query = MailAlias::with(['domain', 'recipients'])
|
|
->where('is_system', false);
|
|
|
|
if ($request->filled('domain')) {
|
|
$query->whereHas('domain', fn($q) => $q->where('domain', $request->domain));
|
|
}
|
|
|
|
$aliases = $query->orderBy('local')->paginate(100);
|
|
|
|
return response()->json([
|
|
'data' => $aliases->map(fn($a) => $this->format($a)),
|
|
'meta' => ['total' => $aliases->total(), 'per_page' => $aliases->perPage(), 'current_page' => $aliases->currentPage()],
|
|
]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$alias = MailAlias::with(['domain', 'recipients'])->where('is_system', false)->findOrFail($id);
|
|
return response()->json(['data' => $this->format($alias)]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$request->tokenCan('aliases:write') || abort(403, 'Scope aliases:write required.');
|
|
|
|
if ($request->isSandbox ?? false) {
|
|
return response()->json(['data' => array_merge(['id' => 9999], $request->only('local', 'domain')), 'sandbox' => true], 201);
|
|
}
|
|
|
|
$data = $request->validate([
|
|
'local' => 'required|string|max:64',
|
|
'domain' => 'required|string',
|
|
'recipients' => 'required|array|min:1',
|
|
'recipients.*'=> 'email',
|
|
'is_active' => 'nullable|boolean',
|
|
]);
|
|
|
|
$domain = Domain::where('domain', $data['domain'])->firstOrFail();
|
|
|
|
$alias = MailAlias::create([
|
|
'domain_id' => $domain->id,
|
|
'local' => $data['local'],
|
|
'type' => 'alias',
|
|
'is_active' => $data['is_active'] ?? true,
|
|
]);
|
|
|
|
foreach ($data['recipients'] as $i => $addr) {
|
|
$alias->recipients()->create(['address' => $addr, 'position' => $i]);
|
|
}
|
|
|
|
if (!($request->isSandbox ?? false)) {
|
|
app(WebhookService::class)->dispatch('alias.created', $this->format($alias->load(['domain', 'recipients'])));
|
|
}
|
|
|
|
return response()->json(['data' => $this->format($alias->load(['domain', 'recipients']))], 201);
|
|
}
|
|
|
|
public function destroy(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->tokenCan('aliases:write') || abort(403, 'Scope aliases:write required.');
|
|
|
|
$alias = MailAlias::where('is_system', false)->findOrFail($id);
|
|
|
|
if ($request->isSandbox ?? false) {
|
|
return response()->json(['sandbox' => true], 204);
|
|
}
|
|
|
|
$formatted = $this->format($alias->load(['domain', 'recipients']));
|
|
$alias->recipients()->delete();
|
|
$alias->delete();
|
|
app(WebhookService::class)->dispatch('alias.deleted', $formatted);
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
private function format(MailAlias $a): array
|
|
{
|
|
return [
|
|
'id' => $a->id,
|
|
'address' => $a->address,
|
|
'local' => $a->local,
|
|
'domain' => $a->domain?->domain,
|
|
'type' => $a->type,
|
|
'is_active' => $a->is_active,
|
|
'recipients' => $a->recipients->pluck('address'),
|
|
'created_at' => $a->created_at->toIso8601String(),
|
|
];
|
|
}
|
|
}
|