-
app/Jobs/InstallCertificate.php
Open in GitHubuse App\Models\Certificate; use App\Models\TlsSetting; use App\Models\User; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Throwable; class InstallCertificate implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public function __construct(public Certificate $certificate, public ?User $user = null) { } public function handle() { $site = $this->certificate->site; $site->uploadAsUser($this->certificate->certificatePath(), $this->certificate->certificate, throw: true); $site->uploadAsUser($this->certificate->privateKeyPath(), $this->certificate->private_key, throw: true); // Get rid of the certificate and private key in database $this->certificate->forceFill([ 'private_key' => null, 'certificate' => null, 'uploaded_at' => now(), ])->save(); dispatch(new UpdateSiteTlsSetting($site, TlsSetting::Custom, $this->certificate)); } public function failed(Throwable $exception) { $this->certificate->site->forceFill([ 'pending_tls_update_since' => null, ])->save(); $this->certificate->delete(); $this->certificate->site->server ->exceptionHandler() ->notify($this->user) ->about($exception) ->withReference(__('Install a custom SSL certificate for :site', ['site' => $this->certificate->site->address])) ->send(); } }
-
app/Http/Controllers/SiteSslController.php
Open in GitHubuse App\Jobs\InstallCertificate; use App\Models\Server; use App\Models\Site; use Illuminate\Http\Request; use ProtoneMedia\Splade\Facades\Toast; class SiteSslController extends Controller { // ... public function update(Request $request, Server $server, Site $site) { // ... if ($newCertificate) { dispatch(new InstallCertificate($newCertificate, $this->user())); Toast::info(__('The certificate will be uploaded to the server and installed. This may take a few minutes.')); } elseif ($site->tls_setting !== $newTlsSetting) { dispatch(new UpdateSiteTlsSetting($site, $newTlsSetting, $newCertificate, $this->user())); Toast::info(__('The site SSL settings will be updated. It might take a few minutes before the changes are applied.')); } else { Toast::warning(__('No changes were made.')); $site->pending_tls_update_since = null; } // ... } }