Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/SkusController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4; | namespace App\Http\Controllers\API\V4; | ||||
use App\Http\Controllers\Controller; | use App\Http\Controllers\Controller; | ||||
use App\Sku; | use App\Sku; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Auth; | |||||
class SkusController extends Controller | class SkusController extends Controller | ||||
{ | { | ||||
/** | /** | ||||
* Show the form for creating a new sku. | * Show the form for creating a new sku. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
Show All 25 Lines | class SkusController extends Controller | ||||
*/ | */ | ||||
public function edit($id) | public function edit($id) | ||||
{ | { | ||||
// TODO | // TODO | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
/** | /** | ||||
* Display a listing of the sku. | * Get a list of active SKUs. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function index() | public function index() | ||||
{ | { | ||||
// Note: Order by title for consistent ordering in tests | // Note: Order by title for consistent ordering in tests | ||||
$skus = Sku::select()->orderBy('title')->get(); | $skus = Sku::where('active', true)->orderBy('title')->get(); | ||||
// Note: we do not limit the result to active SKUs only. | |||||
// It's because we might need users assigned to old SKUs, | |||||
// we need to display these old SKUs on the entitlements list | |||||
$response = []; | $response = []; | ||||
foreach ($skus as $sku) { | foreach ($skus as $sku) { | ||||
if ($data = $this->skuElement($sku)) { | if ($data = $this->skuElement($sku)) { | ||||
$response[] = $data; | $response[] = $data; | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | class SkusController extends Controller | ||||
*/ | */ | ||||
public function update(Request $request, $id) | public function update(Request $request, $id) | ||||
{ | { | ||||
// TODO | // TODO | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
/** | /** | ||||
* Get a list of SKUs available to the user. | |||||
* | |||||
* @param int $id User identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function userSkus($id) | |||||
{ | |||||
$user = \App\User::find($id); | |||||
if (empty($user)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!Auth::guard()->user()->canRead($user)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$type = request()->input('type'); | |||||
$response = []; | |||||
// Note: Order by title for consistent ordering in tests | |||||
$skus = Sku::orderBy('title')->get(); | |||||
foreach ($skus as $sku) { | |||||
if (!$sku->handler_class::isAvailable($sku, $user)) { | |||||
continue; | |||||
} | |||||
if ($data = $this->skuElement($sku)) { | |||||
if ($type && $type != $data['type']) { | |||||
continue; | |||||
} | |||||
$response[] = $data; | |||||
} | |||||
} | |||||
usort($response, function ($a, $b) { | |||||
return ($b['prio'] <=> $a['prio']); | |||||
}); | |||||
return response()->json($response); | |||||
} | |||||
/** | |||||
* Convert SKU information to metadata used by UI to | * Convert SKU information to metadata used by UI to | ||||
* display the form control | * display the form control | ||||
* | * | ||||
* @param \App\Sku $sku SKU object | * @param \App\Sku $sku SKU object | ||||
* | * | ||||
* @return array|null Metadata | * @return array|null Metadata | ||||
*/ | */ | ||||
protected function skuElement($sku): ?array | protected function skuElement($sku): ?array | ||||
{ | { | ||||
$type = $sku->handler_class::entitleableClass(); | $data = array_merge($sku->toArray(), $sku->handler_class::metadata($sku)); | ||||
// ignore incomplete handlers | // ignore incomplete handlers | ||||
if (!$type) { | if (empty($data['type'])) { | ||||
return null; | return null; | ||||
} | } | ||||
$type = explode('\\', $type); | |||||
$type = strtolower(end($type)); | |||||
$handler = explode('\\', $sku->handler_class); | |||||
$handler = strtolower(end($handler)); | |||||
$data = $sku->toArray(); | |||||
$data['type'] = $type; | |||||
$data['handler'] = $handler; | |||||
$data['readonly'] = false; | |||||
$data['enabled'] = false; | |||||
$data['prio'] = $sku->handler_class::priority(); | |||||
// Use localized value, toArray() does not get them right | // Use localized value, toArray() does not get them right | ||||
$data['name'] = $sku->name; | $data['name'] = $sku->name; | ||||
$data['description'] = $sku->description; | $data['description'] = $sku->description; | ||||
unset($data['handler_class']); | unset($data['handler_class'], $data['created_at'], $data['updated_at']); | ||||
switch ($handler) { | |||||
case 'activesync': | |||||
$data['required'] = ['groupware']; | |||||
break; | |||||
case 'auth2f': | |||||
$data['forbidden'] = ['activesync']; | |||||
break; | |||||
case 'storage': | |||||
// Quota range input | |||||
$data['readonly'] = true; // only the checkbox will be disabled, not range | |||||
$data['enabled'] = true; | |||||
$data['range'] = [ | |||||
'min' => $data['units_free'], | |||||
'max' => $sku->handler_class::MAX_ITEMS, | |||||
'unit' => $sku->handler_class::ITEM_UNIT, | |||||
]; | |||||
break; | |||||
case 'mailbox': | |||||
// Mailbox is always enabled and cannot be unset | |||||
$data['readonly'] = true; | |||||
$data['enabled'] = true; | |||||
break; | |||||
} | |||||
return $data; | return $data; | ||||
} | } | ||||
} | } |