Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/DomainsController.php
Show All 33 Lines | public function index() | ||||
usort($list, function ($a, $b) { | usort($list, function ($a, $b) { | ||||
return strcmp($a['namespace'], $b['namespace']); | 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 domain. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function create() | public function create() | ||||
{ | { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
Show All 26 Lines | public function confirm($id) | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'statusInfo' => self::statusInfo($domain), | 'statusInfo' => self::statusInfo($domain), | ||||
'message' => \trans('app.domain-verify-success'), | 'message' => \trans('app.domain-verify-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Remove the specified resource from storage. | * Remove the specified domain. | ||||
* | * | ||||
* @param int $id | * @param int $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function destroy($id) | public function destroy($id) | ||||
{ | { | ||||
$domain = Domain::withEnvTenantContext()->find($id); | |||||
if (empty($domain)) { | |||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canDelete($domain)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
// It is possible to delete domain only if there are no users/aliases/groups using it. | |||||
if (!$domain->isEmpty()) { | |||||
$response = ['status' => 'error', 'message' => \trans('app.domain-notempty-error')]; | |||||
return response()->json($response, 422); | |||||
} | |||||
$domain->delete(); | |||||
return response()->json([ | |||||
'status' => 'success', | |||||
'message' => \trans('app.domain-delete-success'), | |||||
]); | |||||
} | |||||
/** | /** | ||||
* Show the form for editing the specified resource. | * Show the form for editing the specified domain. | ||||
* | * | ||||
* @param int $id | * @param int $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function edit($id) | public function edit($id) | ||||
{ | { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | public function store(Request $request) | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
$namespace = \strtolower(request()->input('namespace')); | $namespace = \strtolower(request()->input('namespace')); | ||||
// Domain already exists | // Domain already exists | ||||
if (Domain::withTrashed()->where('namespace', $namespace)->exists()) { | if ($domain = Domain::withTrashed()->where('namespace', $namespace)->first()) { | ||||
// Check if the domain is soft-deleted and belongs to the same user | |||||
$deleteBeforeCreate = $domain->trashed() && ($wallet = $domain->wallet()) | |||||
&& $wallet->owner && $wallet->owner->id == $owner->id; | |||||
if (!$deleteBeforeCreate) { | |||||
$errors = ['namespace' => \trans('validation.domainnotavailable')]; | $errors = ['namespace' => \trans('validation.domainnotavailable')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
} | |||||
if (empty($request->package) || !($package = \App\Package::withEnvTenantContext()->find($request->package))) { | if (empty($request->package) || !($package = \App\Package::withEnvTenantContext()->find($request->package))) { | ||||
$errors = ['package' => \trans('validation.packagerequired')]; | $errors = ['package' => \trans('validation.packagerequired')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
if (!$package->isDomain()) { | if (!$package->isDomain()) { | ||||
$errors = ['package' => \trans('validation.packageinvalid')]; | $errors = ['package' => \trans('validation.packageinvalid')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
DB::beginTransaction(); | DB::beginTransaction(); | ||||
// TODO: Force-delete domain if it is soft-deleted and belongs to the same user | // Force-delete the existing domain if it is soft-deleted and belongs to the same user | ||||
if (!empty($deleteBeforeCreate)) { | |||||
$domain->forceDelete(); | |||||
} | |||||
// Create the domain | // Create the domain | ||||
$domain = Domain::create([ | $domain = Domain::create([ | ||||
'namespace' => $namespace, | 'namespace' => $namespace, | ||||
'type' => \App\Domain::TYPE_EXTERNAL, | 'type' => \App\Domain::TYPE_EXTERNAL, | ||||
]); | ]); | ||||
$domain->assignPackage($package, $owner); | $domain->assignPackage($package, $owner); | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | public function status($id) | ||||
} | } | ||||
$response = array_merge($response, self::domainStatuses($domain)); | $response = array_merge($response, self::domainStatuses($domain)); | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Update the specified resource in storage. | * Update the specified domain. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request | * @param \Illuminate\Http\Request $request | ||||
* @param int $id | * @param int $id Domain identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function update(Request $request, $id) | public function update(Request $request, $id) | ||||
{ | { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |