Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/GroupsController.php
Show First 20 Lines • Show All 69 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->groupToClient($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 = self::groupToClient($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 20 Lines • Show All 312 Lines • ▼ Show 20 Lines | protected static function groupStatuses(Group $group): array | ||||
'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(), | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
* Convert a group object to an array for the UI. | |||||
* | |||||
* @param \App\Group $group Group object | |||||
* @param bool $full Add all group properties, e.g. members | |||||
* | |||||
* @return array Statuses array | |||||
*/ | |||||
public static function groupToClient(Group $group, bool $full = false): array | |||||
{ | |||||
if ($full) { | |||||
$response = $group->toArray(); | |||||
} else { | |||||
$response = [ | |||||
'id' => $group->id, | |||||
'email' => $group->email, | |||||
'name' => $group->name, | |||||
'status' => $group->status, | |||||
]; | |||||
} | |||||
$response = array_merge($response, self::groupStatuses($group)); | |||||
return $response; | |||||
} | |||||
/** | |||||
* Validate an email address for use as a group email | * Validate an email address for use as a group email | ||||
* | * | ||||
* @param string $email Email address | * @param string $email Email address | ||||
* @param \App\User $user The group owner | * @param \App\User $user The group owner | ||||
* | * | ||||
* @return ?string Error message on validation error | * @return ?string Error message on validation error | ||||
*/ | */ | ||||
public static function validateGroupEmail($email, \App\User $user): ?string | public static function validateGroupEmail($email, \App\User $user): ?string | ||||
▲ Show 20 Lines • Show All 90 Lines • Show Last 20 Lines |