Merge branch 'develop' of github.com:invoiceninja/invoiceninja into develop

This commit is contained in:
Hillel Coren 2018-02-20 21:36:32 +02:00
commit a4523ba872
6 changed files with 157 additions and 31 deletions

View File

@ -0,0 +1,21 @@
<?php
namespace App\Events;
use Illuminate\Queue\SerializesModels;
class SubdomainWasRemoved extends Event
{
use SerializesModels;
public $account;
/**
* Create a new event instance.
*
* @param $account
*/
public function __construct($account)
{
$this->account = $account;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Events\SubdomainWasRemoved;
use App\Events\SubdomainWasUpdated; use App\Events\SubdomainWasUpdated;
use App\Events\UserSettingsChanged; use App\Events\UserSettingsChanged;
use App\Events\UserSignedUp; use App\Events\UserSignedUp;
@ -809,8 +810,12 @@ class AccountController extends BaseController
} }
} }
(bool) $fireUpdateSubdomainEvent = false;
if ($account->subdomain !== $request->subdomain) { if ($account->subdomain !== $request->subdomain) {
event(new SubdomainWasUpdated($account)); $fireUpdateSubdomainEvent = true;
event(new SubdomainWasRemoved($account));
} }
$account->fill($request->all()); $account->fill($request->all());
@ -819,6 +824,11 @@ class AccountController extends BaseController
$account->iframe_url = $request->iframe_url; $account->iframe_url = $request->iframe_url;
$account->save(); $account->save();
if ($fireUpdateSubdomainEvent) {
event(new SubdomainWasUpdated($account));
}
return redirect('settings/' . ACCOUNT_CLIENT_PORTAL) return redirect('settings/' . ACCOUNT_CLIENT_PORTAL)
->with('message', trans('texts.updated_settings')); ->with('message', trans('texts.updated_settings'));
} }

View File

@ -2,6 +2,7 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\SubdomainWasRemoved;
use App\Events\SubdomainWasUpdated; use App\Events\SubdomainWasUpdated;
use App\Ninja\DNS\Cloudflare; use App\Ninja\DNS\Cloudflare;
@ -19,4 +20,11 @@ class DNSListener
if(env("CLOUDFLARE_DNS_ENABLED")) if(env("CLOUDFLARE_DNS_ENABLED"))
Cloudflare::addDNSRecord($event->account); Cloudflare::addDNSRecord($event->account);
} }
public function removeDNSRecord(SubdomainWasRemoved $event)
{
if(env("CLOUDFLARE_DNS_ENABLED"))
Cloudflare::removeDNSRecord($event->account);
}
} }

View File

@ -18,13 +18,90 @@ class Cloudflare
if($account->subdomain != "") if($account->subdomain != "")
{ {
$curl = curl_init();
$jsonEncodedData = json_encode(['type' => 'A', 'name' => $account->subdomain, 'content' => env('CLOUDFLARE_TARGET_IP_ADDRESS', ''), 'proxied' => true]); $jsonEncodedData = json_encode(['type' => 'A', 'name' => $account->subdomain, 'content' => env('CLOUDFLARE_TARGET_IP_ADDRESS', ''), 'proxied' => true]);
$requestType = 'POST';
$url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records';
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
if ($response['status'] != 200)
Utils::logError('Unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $response['result']['result']);
}
}
}
public static function removeDNSRecord(Account $account) {
$zones = json_decode(env('CLOUDFLARE_ZONE_IDS',''), true);
foreach($zones as $zone)
{
if($account->subdomain != "")
{
$dnsRecordId = self::getDNSRecord($zone, $account->subdomain);
$jsonEncodedData = json_encode([]);
$requestType = 'DELETE';
$url = 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records/'. $dnsRecordId .'';
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
if ($response['status'] != 200)
Utils::logError('Unable to delete subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $response['result']['result']);
}
}
}
public static function getDNSRecord($zone, $aRecord)
{
//harvest the zone_name
$url = 'https://api.cloudflare.com/client/v4/zones/'. $zone .'/dns_records?type=A&per_page=1';
$requestType = 'GET';
$jsonEncodedData = json_encode([]);
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
if ($response['status'] != 200)
Utils::logError('Unable to get the zone name for ' . $aRecord . ' @ Cloudflare - ' . $response['result']['result']);
$zoneName = $response['result']['result'][0]['zone_name'];
//get the A record
$url = 'https://api.cloudflare.com/client/v4/zones/'. $zone .'/dns_records?type=A&name='. $aRecord .'.'. $zoneName .' ';
$response = self::curlCloudFlare($requestType, $url, $jsonEncodedData);
if ($response['status'] != 200)
Utils::logError('Unable to get the record ID for ' . $aRecord . ' @ Cloudflare - ' . $response['result']['result']);
return $response['result']['result'][0]['id'];
}
private static function curlCloudFlare($requestType, $url, $jsonEncodedData)
{
$curl = curl_init();
$opts = [ $opts = [
CURLOPT_URL => 'https://api.cloudflare.com/client/v4/zones/' . $zone . '/dns_records', CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_CUSTOMREQUEST => $requestType,
CURLOPT_POST => 1, CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $jsonEncodedData, CURLOPT_POSTFIELDS => $jsonEncodedData,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', CURLOPT_HTTPHEADER => ['Content-Type: application/json',
@ -37,17 +114,16 @@ class Cloudflare
curl_setopt_array($curl, $opts); curl_setopt_array($curl, $opts);
$result = curl_exec($curl); $result = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$data['status'] = $status;
$data['result'] = \json_decode($result, true);
curl_close($curl); curl_close($curl);
if ($status != 200) return $data;
Utils::logError('unable to update subdomain ' . $account->subdomain . ' @ Cloudflare - ' . $result);
}
}
} }

View File

@ -17,6 +17,9 @@ class ProjectTransformer extends EntityTransformer
* @SWG\Property(property="archived_at", type="integer", example=1451160233, readOnly=true) * @SWG\Property(property="archived_at", type="integer", example=1451160233, readOnly=true)
* @SWG\Property(property="is_deleted", type="boolean", example=false, readOnly=true) * @SWG\Property(property="is_deleted", type="boolean", example=false, readOnly=true)
* @SWG\Property(property="task_rate", type="number", format="float", example=10) * @SWG\Property(property="task_rate", type="number", format="float", example=10)
* @SWG\Property(property="due_date", type="string", format="date", example="2016-01-01")
* @SWG\Property(property="private_notes", type="string", format="Sample notes", example=10)
* @SWG\Property(property="budgeted_hours", type="number", format="float", example=10)
*/ */
public function transform(Project $project) public function transform(Project $project)
{ {
@ -28,6 +31,9 @@ class ProjectTransformer extends EntityTransformer
'archived_at' => $this->getTimestamp($project->deleted_at), 'archived_at' => $this->getTimestamp($project->deleted_at),
'is_deleted' => (bool) $project->is_deleted, 'is_deleted' => (bool) $project->is_deleted,
'task_rate' => (float) $project->task_rate, 'task_rate' => (float) $project->task_rate,
'due_date' => $project->due_date,
'private_notes' => $project->private_notes,
'budgeted_hours' => (float) $project->budgeted_hours,
]); ]);
} }
} }

View File

@ -225,9 +225,14 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\InvoiceListener@jobFailed' 'App\Listeners\InvoiceListener@jobFailed'
], ],
//DNS //DNS Add A record to Cloudflare
'App\Events\SubdomainWasUpdated' => [ 'App\Events\SubdomainWasUpdated' => [
'App\Listeners\DNSListener@addDNSRecord' 'App\Listeners\DNSListener@addDNSRecord'
],
//DNS Remove A record from Cloudflare
'App\Events\SubdomainWasRemoved' => [
'App\Listeners\DNSListener@removeDNSRecord'
] ]
/* /*