Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/UsersController.php
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | public function status($id) | ||||
if (!$this->guard()->user()->canRead($user)) { | if (!$this->guard()->user()->canRead($user)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
$response = self::statusInfo($user); | $response = self::statusInfo($user); | ||||
if (!empty(request()->input('refresh'))) { | if (!empty(request()->input('refresh'))) { | ||||
$updated = false; | $updated = false; | ||||
$async = false; | |||||
$last_step = 'none'; | $last_step = 'none'; | ||||
foreach ($response['process'] as $idx => $step) { | foreach ($response['process'] as $idx => $step) { | ||||
$last_step = $step['label']; | $last_step = $step['label']; | ||||
if (!$step['state']) { | if (!$step['state']) { | ||||
if (!$this->execProcessStep($user, $step['label'])) { | $exec = $this->execProcessStep($user, $step['label']); | ||||
if (!$exec) { | |||||
if ($exec === null) { | |||||
$async = true; | |||||
} | |||||
break; | break; | ||||
} | } | ||||
$updated = true; | $updated = true; | ||||
} | } | ||||
} | } | ||||
if ($updated) { | if ($updated) { | ||||
$response = self::statusInfo($user); | $response = self::statusInfo($user); | ||||
} | } | ||||
$success = $response['isReady']; | $success = $response['isReady']; | ||||
$suffix = $success ? 'success' : 'error-' . $last_step; | $suffix = $success ? 'success' : 'error-' . $last_step; | ||||
$response['status'] = $success ? 'success' : 'error'; | $response['status'] = $success ? 'success' : 'error'; | ||||
$response['message'] = \trans('app.process-' . $suffix); | $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::userStatuses($user)); | $response = array_merge($response, self::userStatuses($user)); | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 420 Lines • ▼ Show 20 Lines | // 'cost' => $ent->cost, | ||||
} | } | ||||
/** | /** | ||||
* Execute (synchronously) specified step in a user setup process. | * Execute (synchronously) specified step in a user setup process. | ||||
* | * | ||||
* @param \App\User $user User object | * @param \App\User $user User object | ||||
* @param string $step Step identifier (as in self::statusInfo()) | * @param string $step Step identifier (as in self::statusInfo()) | ||||
* | * | ||||
* @return bool True if the execution succeeded, False otherwise | * @return bool|null True if the execution succeeded, False if not, Null when | ||||
* the job has been sent to the worker (result unknown) | |||||
*/ | */ | ||||
public static function execProcessStep(User $user, string $step): bool | public static function execProcessStep(User $user, string $step): ?bool | ||||
{ | { | ||||
try { | try { | ||||
if (strpos($step, 'domain-') === 0) { | if (strpos($step, 'domain-') === 0) { | ||||
list ($local, $domain) = explode('@', $user->email); | list ($local, $domain) = explode('@', $user->email); | ||||
$domain = Domain::where('namespace', $domain)->first(); | $domain = Domain::where('namespace', $domain)->first(); | ||||
return DomainsController::execProcessStep($domain, $step); | return DomainsController::execProcessStep($domain, $step); | ||||
} | } | ||||
switch ($step) { | switch ($step) { | ||||
case 'user-ldap-ready': | case 'user-ldap-ready': | ||||
// User not in LDAP, create it | // User not in LDAP, create it | ||||
$job = new \App\Jobs\User\CreateJob($user->id); | $job = new \App\Jobs\User\CreateJob($user->id); | ||||
$job->handle(); | $job->handle(); | ||||
$user->refresh(); | $user->refresh(); | ||||
return $user->isLdapReady(); | return $user->isLdapReady(); | ||||
case 'user-imap-ready': | case 'user-imap-ready': | ||||
// User not in IMAP? Verify again | // User not in IMAP? Verify again | ||||
// Do it synchronously if the imap admin credentials are available | |||||
// otherwise let the worker do the job | |||||
if (!\config('imap.admin_password')) { | |||||
\App\Jobs\User\VerifyJob::dispatch($user->id); | |||||
return null; | |||||
} | |||||
$job = new \App\Jobs\User\VerifyJob($user->id); | $job = new \App\Jobs\User\VerifyJob($user->id); | ||||
$job->handle(); | $job->handle(); | ||||
$user->refresh(); | $user->refresh(); | ||||
return $user->isImapReady(); | return $user->isImapReady(); | ||||
} | } | ||||
} catch (\Exception $e) { | } catch (\Exception $e) { | ||||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |