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\Controller; | ||||
use App\Backends\LDAP; | use App\Backends\LDAP; | ||||
use App\Rules\UserEmailDomain; | |||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\DB; | |||||
use Illuminate\Support\Facades\Validator; | |||||
class DomainsController extends Controller | class DomainsController extends Controller | ||||
{ | { | ||||
/** | /** | ||||
* Return a list of domains owned by the current user | * Return a list of domains owned by the current user | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function index() | public function index() | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = $this->guard()->user(); | ||||
$list = []; | $list = []; | ||||
foreach ($user->domains() as $domain) { | foreach ($user->domains() as $domain) { | ||||
if (!$domain->isPublic()) { | if (!$domain->isPublic()) { | ||||
$data = $domain->toArray(); | $data = $domain->toArray(); | ||||
$data = array_merge($data, self::domainStatuses($domain)); | $data = array_merge($data, self::domainStatuses($domain)); | ||||
$list[] = $data; | $list[] = $data; | ||||
} | } | ||||
} | } | ||||
usort($list, function ($a, $b) { | |||||
return strcmp($a['namespace'], $b['namespace']); | |||||
}); | |||||
return response()->json($list); | return response()->json($list); | ||||
} | } | ||||
/** | /** | ||||
* Show the form for creating a new resource. | * Show the form for creating a new resource. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 Lines | public function setConfig($id) | ||||
} | } | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => \trans('app.domain-setconfig-success'), | 'message' => \trans('app.domain-setconfig-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Store a newly created resource in storage. | * 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) | ||||
{ | { | ||||
return $this->errorResponse(404); | $current_user = $this->guard()->user(); | ||||
$owner = $current_user->wallet()->owner; | |||||
if ($owner->id != $current_user->id) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
// Validate the input | |||||
$v = Validator::make( | |||||
$request->all(), | |||||
[ | |||||
'namespace' => ['required', 'string', new UserEmailDomain()] | |||||
] | |||||
); | |||||
if ($v->fails()) { | |||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | |||||
} | |||||
$namespace = \strtolower(request()->input('namespace')); | |||||
// Domain already exists | |||||
if (Domain::withTrashed()->where('namespace', $namespace)->exists()) { | |||||
$errors = ['namespace' => \trans('validation.domainnotavailable')]; | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
if (empty($request->package) || !($package = \App\Package::withEnvTenantContext()->find($request->package))) { | |||||
$errors = ['package' => \trans('validation.packagerequired')]; | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
if (!$package->isDomain()) { | |||||
$errors = ['package' => \trans('validation.packageinvalid')]; | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
DB::beginTransaction(); | |||||
// TODO: Force-delete domain if it is soft-deleted and belongs to the same user | |||||
// Create the domain | |||||
$domain = Domain::create([ | |||||
'namespace' => $namespace, | |||||
'type' => \App\Domain::TYPE_EXTERNAL, | |||||
]); | |||||
$domain->assignPackage($package, $owner); | |||||
DB::commit(); | |||||
return response()->json([ | |||||
'status' => '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 int $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse|void | * @return \Illuminate\Http\JsonResponse|void | ||||
Show All 22 Lines | public function show($id) | ||||
$response['mx'] = self::getMXConfig($domain->namespace); | $response['mx'] = self::getMXConfig($domain->namespace); | ||||
// Domain configuration, e.g. spf whitelist | // Domain configuration, e.g. spf whitelist | ||||
$response['config'] = $domain->getConfig(); | $response['config'] = $domain->getConfig(); | ||||
// Status info | // Status info | ||||
$response['statusInfo'] = self::statusInfo($domain); | $response['statusInfo'] = self::statusInfo($domain); | ||||
// Entitlements info | |||||
$response['skus'] = \App\Entitlement::objectEntitlementsSummary($domain); | |||||
$response = array_merge($response, self::domainStatuses($domain)); | $response = array_merge($response, self::domainStatuses($domain)); | ||||
// Some basic information about the domain wallet | |||||
$wallet = $domain->wallet(); | |||||
$response['wallet'] = $wallet->toArray(); | |||||
if ($wallet->discount) { | |||||
$response['wallet']['discount'] = $wallet->discount->discount; | |||||
$response['wallet']['discount_description'] = $wallet->discount->description; | |||||
} | |||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Fetch domain status (and reload setup process) | * Fetch domain status (and reload setup process) | ||||
* | * | ||||
* @param int $id Domain identifier | * @param int $id Domain identifier | ||||
* | * | ||||
▲ Show 20 Lines • Show All 234 Lines • Show Last 20 Lines |