Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/UsersController.php
Show First 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
* The user-entitlements billed to the current user wallet(s) | * The user-entitlements billed to the current user wallet(s) | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function index() | public function index() | ||||
{ | { | ||||
$user = $this->guard()->user(); | $user = $this->guard()->user(); | ||||
$result = $user->users()->orderBy('email')->get(); | $result = $user->users()->orderBy('email')->get()->map(function ($user) { | ||||
$data = $user->toArray(); | |||||
$data = array_merge($data, self::userStatuses($user)); | |||||
return $data; | |||||
}); | |||||
return response()->json($result); | return response()->json($result); | ||||
} | } | ||||
/** | /** | ||||
* Get the authenticated User | * Get the authenticated User | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | // 'cost' => $ent->cost, | ||||
* User status (extended) information | * User status (extended) information | ||||
* | * | ||||
* @param \App\User $user User object | * @param \App\User $user User object | ||||
* | * | ||||
* @return array Status information | * @return array Status information | ||||
*/ | */ | ||||
public static function statusInfo(User $user): array | public static function statusInfo(User $user): array | ||||
{ | { | ||||
$status = 'new'; | |||||
$process = []; | $process = []; | ||||
$steps = [ | $steps = [ | ||||
'user-new' => true, | 'user-new' => true, | ||||
'user-ldap-ready' => 'isLdapReady', | 'user-ldap-ready' => $user->isLdapReady(), | ||||
'user-imap-ready' => 'isImapReady', | 'user-imap-ready' => $user->isImapReady(), | ||||
]; | |||||
// Create a process check list | |||||
foreach ($steps as $step_name => $state) { | |||||
$step = [ | |||||
'label' => $step_name, | |||||
'title' => \trans("app.process-{$step_name}"), | |||||
'state' => $state, | |||||
]; | ]; | ||||
if ($user->isDeleted()) { | $process[] = $step; | ||||
$status = 'deleted'; | |||||
} elseif ($user->isSuspended()) { | |||||
$status = 'suspended'; | |||||
} elseif ($user->isActive()) { | |||||
$status = 'active'; | |||||
} | } | ||||
list ($local, $domain) = explode('@', $user->email); | list ($local, $domain) = explode('@', $user->email); | ||||
$domain = Domain::where('namespace', $domain)->first(); | $domain = Domain::where('namespace', $domain)->first(); | ||||
// If that is not a public domain, add domain specific steps | // If that is not a public domain, add domain specific steps | ||||
if ($domain && !$domain->isPublic()) { | if ($domain && !$domain->isPublic()) { | ||||
$steps['domain-new'] = true; | $domain_status = DomainsController::statusInfo($domain); | ||||
$steps['domain-ldap-ready'] = 'isLdapReady'; | $process = array_merge($process, $domain_status['process']); | ||||
$steps['domain-verified'] = 'isVerified'; | |||||
$steps['domain-confirmed'] = 'isConfirmed'; | |||||
} | } | ||||
// Create a process check list | $all = count($process); | ||||
foreach ($steps as $step_name => $func) { | $checked = count(array_filter($process, function ($v) { | ||||
$object = strpos($step_name, 'user-') === 0 ? $user : $domain; | return $v['state']; | ||||
})); | |||||
$step = [ | |||||
'label' => $step_name, | |||||
'title' => __("app.process-{$step_name}"), | |||||
'state' => is_bool($func) ? $func : $object->{$func}(), | |||||
]; | |||||
if ($step_name == 'domain-confirmed' && !$step['state']) { | |||||
$step['link'] = "/domain/{$domain->id}"; | |||||
} | |||||
$process[] = $step; | |||||
} | |||||
return [ | return [ | ||||
'process' => $process, | 'process' => $process, | ||||
'status' => $status, | 'isReady' => $all === $checked, | ||||
]; | ]; | ||||
} | } | ||||
/** | /** | ||||
* Create a new user record. | * Create a new user record. | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | protected function userResponse(User $user): array | ||||
$response['aliases'] = []; | $response['aliases'] = []; | ||||
foreach ($user->aliases as $item) { | foreach ($user->aliases as $item) { | ||||
$response['aliases'][] = $item->alias; | $response['aliases'][] = $item->alias; | ||||
} | } | ||||
// Status info | // Status info | ||||
$response['statusInfo'] = self::statusInfo($user); | $response['statusInfo'] = self::statusInfo($user); | ||||
$response = array_merge($response, self::userStatuses($user)); | |||||
// Information about wallets and accounts for access checks | // Information about wallets and accounts for access checks | ||||
$response['wallets'] = $user->wallets->toArray(); | $response['wallets'] = $user->wallets->toArray(); | ||||
$response['accounts'] = $user->accounts->toArray(); | $response['accounts'] = $user->accounts->toArray(); | ||||
$response['wallet'] = $user->wallet()->toArray(); | $response['wallet'] = $user->wallet()->toArray(); | ||||
return $response; | return $response; | ||||
} | } | ||||
/** | /** | ||||
* Prepare user statuses for the UI | |||||
* | |||||
* @param \App\User $user User object | |||||
* | |||||
* @return array Statuses array | |||||
*/ | |||||
protected static function userStatuses(User $user): array | |||||
{ | |||||
return [ | |||||
'isImapReady' => $user->isImapReady(), | |||||
'isLdapReady' => $user->isLdapReady(), | |||||
'isSuspended' => $user->isSuspended(), | |||||
'isActive' => $user->isActive(), | |||||
'isDeleted' => $user->isDeleted() || $user->trashed(), | |||||
]; | |||||
} | |||||
/** | |||||
* Validate user input | * Validate user input | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
* @param \App\User|null $user User identifier | * @param \App\User|null $user User identifier | ||||
* @param array $settings User settings (from the request) | * @param array $settings User settings (from the request) | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse The response on error | * @return \Illuminate\Http\JsonResponse The response on error | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 137 Lines • Show Last 20 Lines |