Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/UsersController.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\Rules\UserEmailDomain; | use App\Rules\UserEmailDomain; | ||||
use App\Rules\UserEmailLocal; | use App\Rules\UserEmailLocal; | ||||
use App\Sku; | use App\Sku; | ||||
use App\User; | use App\User; | ||||
use Carbon\Carbon; | |||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Auth; | use Illuminate\Support\Facades\Auth; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
use Illuminate\Support\Str; | use Illuminate\Support\Str; | ||||
class UsersController extends Controller | class UsersController extends Controller | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | // 'cost' => $ent->cost, | ||||
'count' => isset($response['skus'][$sku->id]) ? $response['skus'][$sku->id]['count'] + 1 : 1, | 'count' => isset($response['skus'][$sku->id]) ? $response['skus'][$sku->id]['count'] + 1 : 1, | ||||
]; | ]; | ||||
} | } | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Fetch user status (and reload setup process) | |||||
* | |||||
* @param int $id User identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function status($id) | |||||
{ | |||||
$user = User::find($id); | |||||
if (empty($user)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!$this->guard()->user()->canRead($user)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$response = self::statusInfo($user); | |||||
if (!empty(request()->input('refresh'))) { | |||||
$updated = false; | |||||
$last_step = 'none'; | |||||
foreach ($response['process'] as $idx => $step) { | |||||
$last_step = $step['label']; | |||||
if (!$step['state']) { | |||||
if (!$this->execProcessStep($user, $step['label'])) { | |||||
break; | |||||
} | |||||
$updated = true; | |||||
} | |||||
} | |||||
if ($updated) { | |||||
$response = self::statusInfo($user); | |||||
} | |||||
$success = $response['isReady']; | |||||
$suffix = $success ? 'success' : 'error-' . $last_step; | |||||
$response['status'] = $success ? 'success' : 'error'; | |||||
$response['message'] = \trans('app.process-' . $suffix); | |||||
} | |||||
$response = array_merge($response, self::userStatuses($user)); | |||||
return response()->json($response); | |||||
} | |||||
/** | |||||
* 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 | ||||
{ | { | ||||
Show All 10 Lines | public static function statusInfo(User $user): array | ||||
'label' => $step_name, | 'label' => $step_name, | ||||
'title' => \trans("app.process-{$step_name}"), | 'title' => \trans("app.process-{$step_name}"), | ||||
'state' => $state, | 'state' => $state, | ||||
]; | ]; | ||||
$process[] = $step; | $process[] = $step; | ||||
} | } | ||||
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()) { | ||||
$domain_status = DomainsController::statusInfo($domain); | $domain_status = DomainsController::statusInfo($domain); | ||||
$process = array_merge($process, $domain_status['process']); | $process = array_merge($process, $domain_status['process']); | ||||
} | } | ||||
$all = count($process); | $all = count($process); | ||||
$checked = count(array_filter($process, function ($v) { | $checked = count(array_filter($process, function ($v) { | ||||
return $v['state']; | 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 && $user->created_at->diffInSeconds(Carbon::now()) > 180) { | |||||
$state = 'failed'; | |||||
} | |||||
return [ | return [ | ||||
'process' => $process, | 'process' => $process, | ||||
'processState' => $state, | |||||
'isReady' => $all === $checked, | '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 322 Lines • ▼ Show 20 Lines | protected function validateUserRequest(Request $request, $user, &$settings = []) | ||||
if (!empty($errors)) { | if (!empty($errors)) { | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
// Update user settings | // Update user settings | ||||
$settings = $request->only(array_keys($rules)); | $settings = $request->only(array_keys($rules)); | ||||
unset($settings['password'], $settings['aliases'], $settings['email']); | unset($settings['password'], $settings['aliases'], $settings['email']); | ||||
} | } | ||||
/** | |||||
* Execute (synchronously) specified step in a user setup process. | |||||
* | |||||
* @param \App\User $user User object | |||||
* @param string $step Step identifier (as in self::statusInfo()) | |||||
* | |||||
* @return bool True if the execution succeeded, False otherwise | |||||
*/ | |||||
public static function execProcessStep(User $user, string $step): bool | |||||
{ | |||||
try { | |||||
if (strpos($step, 'domain-') === 0) { | |||||
list ($local, $domain) = explode('@', $user->email); | |||||
$domain = Domain::where('namespace', $domain)->first(); | |||||
return DomainsController::execProcessStep($domain, $step); | |||||
} | |||||
switch ($step) { | |||||
case 'user-ldap-ready': | |||||
// User not in LDAP, create it | |||||
$job = new \App\Jobs\UserCreate($user); | |||||
$job->handle(); | |||||
return $user->isLdapReady(); | |||||
case 'user-imap-ready': | |||||
// User not in IMAP? Verify again | |||||
$job = new \App\Jobs\UserVerify($user); | |||||
$job->handle(); | |||||
return $user->isImapReady(); | |||||
} | |||||
} catch (\Exception $e) { | |||||
\Log::error($e); | |||||
} | |||||
return false; | |||||
} | |||||
} | } |