Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/GroupsController.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\Domain; | use App\Domain; | ||||
use App\Group; | use App\Group; | ||||
use App\Rules\GroupName; | use App\Rules\GroupName; | ||||
use App\User; | use App\User; | ||||
use Carbon\Carbon; | |||||
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 GroupsController extends Controller | class GroupsController extends Controller | ||||
{ | { | ||||
/** @var array Common object properties in the API response */ | |||||
protected static $objectProps = ['email', 'name', 'status']; | |||||
/** | /** | ||||
* Show the form for creating a new group. | * Show the form for creating a new group. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function create() | public function create() | ||||
{ | { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | class GroupsController extends Controller | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function index() | public function index() | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = $this->guard()->user(); | ||||
$result = $user->groups()->orderBy('name')->orderBy('email')->get() | $result = $user->groups()->orderBy('name')->orderBy('email')->get() | ||||
->map(function (Group $group) { | ->map(function ($group) { | ||||
$data = [ | return $this->objectToClient($group); | ||||
'id' => $group->id, | |||||
'email' => $group->email, | |||||
'name' => $group->name, | |||||
]; | |||||
$data = array_merge($data, self::groupStatuses($group)); | |||||
return $data; | |||||
}); | }); | ||||
return response()->json($result); | return response()->json($result); | ||||
} | } | ||||
/** | /** | ||||
* Set the group configuration. | * Set the group configuration. | ||||
* | * | ||||
Show All 39 Lines | public function show($id) | ||||
if (!$this->checkTenant($group)) { | if (!$this->checkTenant($group)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canRead($group)) { | if (!$this->guard()->user()->canRead($group)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$response = $group->toArray(); | $response = $this->objectToClient($group, true); | ||||
$response = array_merge($response, self::groupStatuses($group)); | |||||
$response['statusInfo'] = self::statusInfo($group); | $response['statusInfo'] = self::statusInfo($group); | ||||
// Group configuration, e.g. sender_policy | // Group configuration, e.g. sender_policy | ||||
$response['config'] = $group->getConfig(); | $response['config'] = $group->getConfig(); | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
Show All 11 Lines | public function status($id) | ||||
if (!$this->checkTenant($group)) { | if (!$this->checkTenant($group)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canRead($group)) { | if (!$this->guard()->user()->canRead($group)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$response = self::statusInfo($group); | $response = $this->processStateUpdate($group); | ||||
$response = array_merge($response, self::objectState($group)); | |||||
if (!empty(request()->input('refresh'))) { | |||||
$updated = false; | |||||
$async = false; | |||||
$last_step = 'none'; | |||||
foreach ($response['process'] as $idx => $step) { | |||||
$last_step = $step['label']; | |||||
if (!$step['state']) { | |||||
$exec = $this->execProcessStep($group, $step['label']); | |||||
if (!$exec) { | |||||
if ($exec === null) { | |||||
$async = true; | |||||
} | |||||
break; | |||||
} | |||||
$updated = true; | |||||
} | |||||
} | |||||
if ($updated) { | |||||
$response = self::statusInfo($group); | |||||
} | |||||
$success = $response['isReady']; | |||||
$suffix = $success ? 'success' : 'error-' . $last_step; | |||||
$response['status'] = $success ? 'success' : 'error'; | |||||
$response['message'] = \trans('app.process-' . $suffix); | |||||
if ($async && !$success) { | |||||
$response['processState'] = 'waiting'; | |||||
$response['status'] = 'success'; | |||||
$response['message'] = \trans('app.process-async'); | |||||
} | |||||
} | |||||
$response = array_merge($response, self::groupStatuses($group)); | |||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Group status (extended) information | * Group status (extended) information | ||||
* | * | ||||
* @param \App\Group $group Group object | * @param \App\Group $group Group object | ||||
* | * | ||||
* @return array Status information | * @return array Status information | ||||
*/ | */ | ||||
public static function statusInfo(Group $group): array | public static function statusInfo(Group $group): array | ||||
{ | { | ||||
$process = []; | return self::processStateInfo( | ||||
$steps = [ | $group, | ||||
[ | |||||
'distlist-new' => true, | 'distlist-new' => true, | ||||
'distlist-ldap-ready' => $group->isLdapReady(), | 'distlist-ldap-ready' => $group->isLdapReady(), | ||||
]; | ] | ||||
); | |||||
// Create a process check list | |||||
foreach ($steps as $step_name => $state) { | |||||
$step = [ | |||||
'label' => $step_name, | |||||
'title' => \trans("app.process-{$step_name}"), | |||||
'state' => $state, | |||||
]; | |||||
$process[] = $step; | |||||
} | |||||
$domain = $group->domain(); | |||||
// If that is not a public domain, add domain specific steps | |||||
if ($domain && !$domain->isPublic()) { | |||||
$domain_status = DomainsController::statusInfo($domain); | |||||
$process = array_merge($process, $domain_status['process']); | |||||
} | |||||
$all = count($process); | |||||
$checked = count(array_filter($process, function ($v) { | |||||
return $v['state']; | |||||
})); | |||||
$state = $all === $checked ? 'done' : 'running'; | |||||
// After 180 seconds assume the process is in failed state, | |||||
// this should unlock the Refresh button in the UI | |||||
if ($all !== $checked && $group->created_at->diffInSeconds(Carbon::now()) > 180) { | |||||
$state = 'failed'; | |||||
} | |||||
return [ | |||||
'process' => $process, | |||||
'processState' => $state, | |||||
'isReady' => $all === $checked, | |||||
]; | |||||
} | } | ||||
/** | /** | ||||
* Create a new group record. | * Create a new group record. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse The response | * @return \Illuminate\Http\JsonResponse The response | ||||
▲ Show 20 Lines • Show All 175 Lines • ▼ Show 20 Lines | class GroupsController extends Controller | ||||
/** | /** | ||||
* Prepare group statuses for the UI | * Prepare group statuses for the UI | ||||
* | * | ||||
* @param \App\Group $group Group object | * @param \App\Group $group Group object | ||||
* | * | ||||
* @return array Statuses array | * @return array Statuses array | ||||
*/ | */ | ||||
protected static function groupStatuses(Group $group): array | protected static function objectState(Group $group): array | ||||
{ | { | ||||
return [ | return [ | ||||
'isLdapReady' => $group->isLdapReady(), | 'isLdapReady' => $group->isLdapReady(), | ||||
'isSuspended' => $group->isSuspended(), | 'isSuspended' => $group->isSuspended(), | ||||
'isActive' => $group->isActive(), | 'isActive' => $group->isActive(), | ||||
'isDeleted' => $group->isDeleted() || $group->trashed(), | 'isDeleted' => $group->isDeleted() || $group->trashed(), | ||||
]; | ]; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |