Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/DomainsController.php
- This file was added.
<?php | |||||
namespace App\Http\Controllers\API; | |||||
use App\Domain; | |||||
use App\Http\Controllers\Controller; | |||||
use Illuminate\Http\Request; | |||||
use Illuminate\Support\Facades\Auth; | |||||
class DomainsController extends Controller | |||||
{ | |||||
/** | |||||
* Display a listing of the resource. | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function index() | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Show the form for creating a new resource. | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function create() | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Confirm ownership of the specified domain (via DNS check). | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function confirm($id) | |||||
{ | |||||
$domain = Domain::findOrFail($id); | |||||
// Only owner (or admin) has access to the domain | |||||
if (!self::hasAccess($domain)) { | |||||
return abort(403); | |||||
} | |||||
if (!$domain->confirm()) { | |||||
return response()->json(['status' => 'error']); | |||||
} | |||||
return response()->json(['status' => 'success']); | |||||
} | |||||
/** | |||||
* Remove the specified resource from storage. | |||||
* | |||||
* @param int $id | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function destroy($id) | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Show the form for editing the specified resource. | |||||
* | |||||
* @param int $id | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function edit($id) | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Store a newly created resource in storage. | |||||
* | |||||
* @param \Illuminate\Http\Request $request | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function store(Request $request) | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Get the information about the specified domain. | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function show($id) | |||||
{ | |||||
$domain = Domain::findOrFail($id); | |||||
// Only owner (or admin) has access to the domain | |||||
if (!self::hasAccess($domain)) { | |||||
return abort(403); | |||||
} | |||||
$response = $domain->toArray(); | |||||
// Add hash information to the response | |||||
$response['hash_text'] = $domain->hash(Domain::HASH_TEXT); | |||||
$response['hash_cname'] = $domain->hash(Domain::HASH_CNAME); | |||||
$response['hash_code'] = $domain->hash(Domain::HASH_CODE); | |||||
// Add DNS/MX configuration for the domain | |||||
$response['dns'] = self::getDNSConfig($domain); | |||||
$response['config'] = self::getMXConfig($domain->namespace); | |||||
$response['confirmed'] = $domain->isConfirmed(); | |||||
return response()->json($response); | |||||
} | |||||
/** | |||||
* Update the specified resource in storage. | |||||
* | |||||
* @param \Illuminate\Http\Request $request | |||||
* @param int $id | |||||
* | |||||
* @return \Illuminate\Http\Response | |||||
*/ | |||||
public function update(Request $request, $id) | |||||
{ | |||||
// | |||||
} | |||||
/** | |||||
* Provide DNS MX information to configure specified domain for | |||||
*/ | |||||
protected static function getMXConfig(string $namespace): array | |||||
{ | |||||
$entries = []; | |||||
// copy MX entries from an existing domain | |||||
if ($master = \config('dns.copyfrom')) { | |||||
// TODO: cache this lookup | |||||
foreach ((array) dns_get_record($master, DNS_MX) as $entry) { | |||||
$entries[] = sprintf( | |||||
"@\t%s\t%s\tMX\t%d %s.", | |||||
\config('dns.ttl', $entry['ttl']), | |||||
$entry['class'], | |||||
$entry['pri'], | |||||
$entry['target'] | |||||
); | |||||
} | |||||
} elseif ($static = \config('dns.static')) { | |||||
$entries[] = strtr($static, array('\n' => "\n", '%s' => $namespace)); | |||||
} | |||||
// display SPF settings | |||||
if ($spf = \config('dns.spf')) { | |||||
$entries[] = ';'; | |||||
foreach (['TXT', 'SPF'] as $type) { | |||||
$entries[] = sprintf( | |||||
"@\t%s\tIN\t%s\t\"%s\"", | |||||
\config('dns.ttl'), | |||||
$type, | |||||
$spf | |||||
); | |||||
} | |||||
} | |||||
return $entries; | |||||
} | |||||
/** | |||||
* Provide sample DNS config for domain confirmation | |||||
*/ | |||||
protected static function getDNSConfig(Domain $domain): array | |||||
{ | |||||
$serial = date('Ymd01'); | |||||
$hash_txt = $domain->hash(Domain::HASH_TEXT); | |||||
$hash_cname = $domain->hash(Domain::HASH_CNAME); | |||||
$hash = $domain->hash(Domain::HASH_CODE); | |||||
return [ | |||||
"@ IN SOA ns1.dnsservice.com. hostmaster.{$domain->namespace}. (", | |||||
" {$serial} 10800 3600 604800 86400 )", | |||||
";", | |||||
"@ IN A <some-ip>", | |||||
"www IN A <some-ip>", | |||||
";", | |||||
"{$hash_cname}.{$domain->namespace}. IN CNAME {$hash}.{$domain->namespace}.", | |||||
"@ 3600 TXT \"{$hash_txt}\"", | |||||
]; | |||||
} | |||||
/** | |||||
* Check if the current user has access to the domain | |||||
* | |||||
* @param \App\Domain Domain | |||||
* | |||||
* @return bool True if current user has access, False otherwise | |||||
*/ | |||||
protected static function hasAccess(Domain $domain): bool | |||||
{ | |||||
$user = Auth::guard()->user(); | |||||
$entitlement = $domain->entitlement()->first(); | |||||
// TODO: Admins | |||||
return $entitlement && $entitlement->owner_id == $user->id; | |||||
} | |||||
} |