Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/DomainsController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4; | namespace App\Http\Controllers\API\V4; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Http\Controllers\Controller; | use App\Http\Controllers\RelationController; | ||||
use App\Backends\LDAP; | use App\Backends\LDAP; | ||||
use App\Rules\UserEmailDomain; | use App\Rules\UserEmailDomain; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
class DomainsController extends Controller | class DomainsController extends RelationController | ||||
{ | { | ||||
/** @var array Common object properties in the API response */ | /** @var string Resource localization label */ | ||||
protected static $objectProps = ['namespace', 'type']; | protected $label = 'domain'; | ||||
/** @var string Resource model name */ | |||||
protected $model = Domain::class; | |||||
/** | /** @var array Common object properties in the API response */ | ||||
* Return a list of domains owned by the current user | protected $objectProps = ['namespace', 'type']; | ||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function index() | |||||
{ | |||||
$user = $this->guard()->user(); | |||||
$list = $user->domains(true, false) | /** @var array Resource listing order (column names) */ | ||||
->orderBy('namespace') | protected $order = ['namespace']; | ||||
->get() | |||||
->map(function ($domain) { | |||||
return $this->objectToClient($domain); | |||||
}) | |||||
->all(); | |||||
return response()->json($list); | /** @var array Resource relation method arguments */ | ||||
} | protected $relationArgs = [true, false]; | ||||
/** | |||||
* Show the form for creating a new domain. | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function create() | |||||
{ | |||||
return $this->errorResponse(404); | |||||
} | |||||
/** | /** | ||||
* Confirm ownership of the specified domain (via DNS check). | * Confirm ownership of the specified domain (via DNS check). | ||||
* | * | ||||
* @param int $id Domain identifier | * @param int $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse|void | * @return \Illuminate\Http\JsonResponse|void | ||||
*/ | */ | ||||
Show All 21 Lines | public function confirm($id) | ||||
'statusInfo' => self::statusInfo($domain), | 'statusInfo' => self::statusInfo($domain), | ||||
'message' => \trans('app.domain-verify-success'), | 'message' => \trans('app.domain-verify-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Remove the specified domain. | * Remove the specified domain. | ||||
* | * | ||||
* @param int $id Domain identifier | * @param string $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function destroy($id) | public function destroy($id) | ||||
{ | { | ||||
$domain = Domain::withEnvTenantContext()->find($id); | $domain = Domain::withEnvTenantContext()->find($id); | ||||
if (empty($domain)) { | if (empty($domain)) { | ||||
Show All 14 Lines | public function destroy($id) | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => \trans('app.domain-delete-success'), | 'message' => \trans('app.domain-delete-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Show the form for editing the specified domain. | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function edit($id) | |||||
{ | |||||
return $this->errorResponse(404); | |||||
} | |||||
/** | |||||
* Set the domain configuration. | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse|void | |||||
*/ | |||||
public function setConfig($id) | |||||
{ | |||||
$domain = Domain::find($id); | |||||
if (empty($domain)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
// Only owner (or admin) has access to the domain | |||||
if (!$this->guard()->user()->canUpdate($domain)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$errors = $domain->setConfig(request()->input()); | |||||
if (!empty($errors)) { | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
return response()->json([ | |||||
'status' => 'success', | |||||
'message' => \trans('app.domain-setconfig-success'), | |||||
]); | |||||
} | |||||
/** | |||||
* Create a domain. | * Create a domain. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request | * @param \Illuminate\Http\Request $request | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function store(Request $request) | public function store(Request $request) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | public function store(Request $request) | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => __('app.domain-create-success'), | 'message' => __('app.domain-create-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Get the information about the specified domain. | * Get the information about the specified domain. | ||||
* | * | ||||
* @param int $id Domain identifier | * @param string $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse|void | * @return \Illuminate\Http\JsonResponse|void | ||||
*/ | */ | ||||
public function show($id) | public function show($id) | ||||
{ | { | ||||
$domain = Domain::find($id); | $domain = Domain::find($id); | ||||
if (!$this->checkTenant($domain)) { | if (!$this->checkTenant($domain)) { | ||||
Show All 31 Lines | public function show($id) | ||||
$response['wallet']['discount'] = $wallet->discount->discount; | $response['wallet']['discount'] = $wallet->discount->discount; | ||||
$response['wallet']['discount_description'] = $wallet->discount->description; | $response['wallet']['discount_description'] = $wallet->discount->description; | ||||
} | } | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Fetch domain status (and reload setup process) | |||||
* | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function status($id) | |||||
{ | |||||
$domain = Domain::find($id); | |||||
if (!$this->checkTenant($domain)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!$this->guard()->user()->canRead($domain)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$response = $this->processStateUpdate($domain); | |||||
$response = array_merge($response, self::objectState($domain)); | |||||
return response()->json($response); | |||||
} | |||||
/** | |||||
* Update the specified domain. | |||||
* | |||||
* @param \Illuminate\Http\Request $request | |||||
* @param int $id Domain identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function update(Request $request, $id) | |||||
{ | |||||
return $this->errorResponse(404); | |||||
} | |||||
/** | |||||
* Provide DNS MX information to configure specified domain for | * Provide DNS MX information to configure specified domain for | ||||
*/ | */ | ||||
protected static function getMXConfig(string $namespace): array | protected static function getMXConfig(string $namespace): array | ||||
{ | { | ||||
$entries = []; | $entries = []; | ||||
// copy MX entries from an existing domain | // copy MX entries from an existing domain | ||||
if ($master = \config('dns.copyfrom')) { | if ($master = \config('dns.copyfrom')) { | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | class DomainsController extends RelationController | ||||
/** | /** | ||||
* Prepare domain statuses for the UI | * Prepare domain statuses for the UI | ||||
* | * | ||||
* @param \App\Domain $domain Domain object | * @param \App\Domain $domain Domain object | ||||
* | * | ||||
* @return array Statuses array | * @return array Statuses array | ||||
*/ | */ | ||||
protected static function objectState(Domain $domain): array | protected static function objectState($domain): array | ||||
{ | { | ||||
return [ | return [ | ||||
'isLdapReady' => $domain->isLdapReady(), | 'isLdapReady' => $domain->isLdapReady(), | ||||
'isConfirmed' => $domain->isConfirmed(), | 'isConfirmed' => $domain->isConfirmed(), | ||||
'isVerified' => $domain->isVerified(), | 'isVerified' => $domain->isVerified(), | ||||
'isSuspended' => $domain->isSuspended(), | 'isSuspended' => $domain->isSuspended(), | ||||
'isActive' => $domain->isActive(), | 'isActive' => $domain->isActive(), | ||||
'isDeleted' => $domain->isDeleted() || $domain->trashed(), | 'isDeleted' => $domain->isDeleted() || $domain->trashed(), | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
* Domain status (extended) information. | * Domain status (extended) information. | ||||
* | * | ||||
* @param \App\Domain $domain Domain object | * @param \App\Domain $domain Domain object | ||||
* | * | ||||
* @return array Status information | * @return array Status information | ||||
*/ | */ | ||||
public static function statusInfo(Domain $domain): array | public static function statusInfo($domain): array | ||||
{ | { | ||||
// If that is not a public domain, add domain specific steps | // If that is not a public domain, add domain specific steps | ||||
return self::processStateInfo( | return self::processStateInfo( | ||||
$domain, | $domain, | ||||
[ | [ | ||||
'domain-new' => true, | 'domain-new' => true, | ||||
'domain-ldap-ready' => $domain->isLdapReady(), | 'domain-ldap-ready' => $domain->isLdapReady(), | ||||
'domain-verified' => $domain->isVerified(), | 'domain-verified' => $domain->isVerified(), | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |