mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-09 20:52:56 +01:00
Cloudflare DNS Zone Updates (#1733)
* Add subdomain to cloudflare * Fire subdomain event when saving client portal settings * Refactor to include zones * JSON decode to array * Proxy requests
This commit is contained in:
parent
ee917ba95a
commit
19881bd41f
@ -99,3 +99,9 @@ WEPAY_THEME='{"name":"Invoice Ninja","primary_color":"0b4d78","secondary_color":
|
|||||||
|
|
||||||
BLUEVINE_PARTNER_UNIQUE_ID=
|
BLUEVINE_PARTNER_UNIQUE_ID=
|
||||||
BLUEVINE_PARTNER_TOKEN=
|
BLUEVINE_PARTNER_TOKEN=
|
||||||
|
|
||||||
|
CLOUDFLARE_DNS_ENABLED=false
|
||||||
|
CLOUDFLARE_API_KEY=
|
||||||
|
CLOUDFLARE_EMAIL=
|
||||||
|
CLOUDFLARE_TARGET_IP_ADDRESS=
|
||||||
|
CLOUDFLARE_ZONE_IDS={}
|
21
app/Events/SubdomainWasUpdated.php
Normal file
21
app/Events/SubdomainWasUpdated.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Events;
|
||||||
|
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class SubdomainWasUpdated extends Event
|
||||||
|
{
|
||||||
|
use SerializesModels;
|
||||||
|
public $account;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new event instance.
|
||||||
|
*
|
||||||
|
* @param $account
|
||||||
|
*/
|
||||||
|
public function __construct($account)
|
||||||
|
{
|
||||||
|
$this->account = $account;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Events\SubdomainWasUpdated;
|
||||||
use App\Events\UserSettingsChanged;
|
use App\Events\UserSettingsChanged;
|
||||||
use App\Events\UserSignedUp;
|
use App\Events\UserSignedUp;
|
||||||
use App\Http\Requests\SaveClientPortalSettings;
|
use App\Http\Requests\SaveClientPortalSettings;
|
||||||
@ -768,7 +769,12 @@ class AccountController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function saveClientPortalSettings(SaveClientPortalSettings $request)
|
public function saveClientPortalSettings(SaveClientPortalSettings $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
$account = $request->user()->account;
|
$account = $request->user()->account;
|
||||||
|
|
||||||
|
if($account->subdomain !== $request->subdomain)
|
||||||
|
event(new SubdomainWasUpdated($account));
|
||||||
|
|
||||||
$account->fill($request->all());
|
$account->fill($request->all());
|
||||||
$account->client_view_css = $request->client_view_css;
|
$account->client_view_css = $request->client_view_css;
|
||||||
$account->subdomain = $request->subdomain;
|
$account->subdomain = $request->subdomain;
|
||||||
|
22
app/Listeners/DNSListener.php
Normal file
22
app/Listeners/DNSListener.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners;
|
||||||
|
|
||||||
|
use App\Events\SubdomainWasUpdated;
|
||||||
|
use App\Ninja\DNS\Cloudflare;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DNSListener.
|
||||||
|
*/
|
||||||
|
class DNSListener
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param DNSListener $event
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function addDNSRecord(SubdomainWasUpdated $event)
|
||||||
|
{
|
||||||
|
if(env("CLOUDFLARE_DNS_ENABLED"))
|
||||||
|
Cloudflare::addDNSRecord($event->account);
|
||||||
|
}
|
||||||
|
}
|
50
app/Ninja/DNS/Cloudflare.php
Normal file
50
app/Ninja/DNS/Cloudflare.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Ninja\DNS;
|
||||||
|
|
||||||
|
use App\Libraries\Utils;
|
||||||
|
use App\Models\Account;
|
||||||
|
|
||||||
|
class Cloudflare
|
||||||
|
{
|
||||||
|
|
||||||
|
public static function addDNSRecord(Account $account){
|
||||||
|
|
||||||
|
$zones = json_decode(env('CLOUDFLARE_ZONE_IDS',''), true);
|
||||||
|
|
||||||
|
foreach($zones as $zone)
|
||||||
|
{
|
||||||
|
|
||||||
|
$curl = curl_init();
|
||||||
|
$jsonEncodedData = json_encode(['type'=>'A', 'name'=>$account->subdomain, 'content'=>env('CLOUDFLARE_TARGET_IP_ADDRESS',''),'proxied'=>true]);
|
||||||
|
|
||||||
|
$opts = [
|
||||||
|
CURLOPT_URL => 'https://api.cloudflare.com/client/v4/zones/'.$zone.'/dns_records',
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||||
|
CURLOPT_POST => 1,
|
||||||
|
CURLOPT_POSTFIELDS => $jsonEncodedData,
|
||||||
|
CURLOPT_HTTPHEADER => [ 'Content-Type: application/json',
|
||||||
|
'Content-Length: '.strlen($jsonEncodedData),
|
||||||
|
'X-Auth-Email: '.env('CLOUDFLARE_EMAIL', ''),
|
||||||
|
'X-Auth-Key: '.env('CLOUDFLARE_API_KEY', '')
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
curl_setopt_array($curl, $opts);
|
||||||
|
|
||||||
|
$result = curl_exec($curl);
|
||||||
|
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
curl_close($curl);
|
||||||
|
|
||||||
|
if ($status != 200)
|
||||||
|
Utils::logError('unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - '.$result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -200,6 +200,11 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
'Illuminate\Queue\Events\JobExceptionOccurred' => [
|
'Illuminate\Queue\Events\JobExceptionOccurred' => [
|
||||||
'App\Listeners\InvoiceListener@jobFailed'
|
'App\Listeners\InvoiceListener@jobFailed'
|
||||||
|
],
|
||||||
|
|
||||||
|
//DNS
|
||||||
|
'App\Events\SubdomainWasUpdated' => [
|
||||||
|
'App\Listeners\DNSListener@addDNSRecord'
|
||||||
]
|
]
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user