Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/UsersController.php
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
* Delete a user. | * Delete a user. | ||||
* | * | ||||
* @param int $id User identifier | * @param int $id User identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse The response | * @return \Illuminate\Http\JsonResponse The response | ||||
*/ | */ | ||||
public function destroy($id) | public function destroy($id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenantContext()->find($id); | ||||
if (empty($user)) { | if (empty($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
// User can't remove himself until he's the controller | // User can't remove himself until he's the controller | ||||
if (!$this->guard()->user()->canDelete($user)) { | if (!$this->guard()->user()->canDelete($user)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
Show All 31 Lines | class UsersController extends Controller | ||||
* Display information on the user account specified by $id. | * Display information on the user account specified by $id. | ||||
* | * | ||||
* @param int $id The account to show information for. | * @param int $id The account to show information for. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function show($id) | public function show($id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenantContext()->find($id); | ||||
if (empty($user)) { | if (empty($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canRead($user)) { | if (!$this->guard()->user()->canRead($user)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
Show All 19 Lines | class UsersController extends Controller | ||||
* Fetch user status (and reload setup process) | * Fetch user status (and reload setup process) | ||||
* | * | ||||
* @param int $id User identifier | * @param int $id User identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function status($id) | public function status($id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenantContext()->find($id); | ||||
if (empty($user)) { | if (empty($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
if (!$this->guard()->user()->canRead($user)) { | if (!$this->guard()->user()->canRead($user)) { | ||||
return $this->errorResponse(403); | return $this->errorResponse(403); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | public static function statusInfo(User $user): array | ||||
'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::withEnvTenantContext()->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); | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | public function store(Request $request) | ||||
} | } | ||||
$this->deleteBeforeCreate = null; | $this->deleteBeforeCreate = null; | ||||
if ($error_response = $this->validateUserRequest($request, null, $settings)) { | if ($error_response = $this->validateUserRequest($request, null, $settings)) { | ||||
return $error_response; | return $error_response; | ||||
} | } | ||||
if (empty($request->package) || !($package = \App\Package::find($request->package))) { | if (empty($request->package) || !($package = \App\Package::withEnvTenantContext()->find($request->package))) { | ||||
$errors = ['package' => \trans('validation.packagerequired')]; | $errors = ['package' => \trans('validation.packagerequired')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
if ($package->isDomain()) { | if ($package->isDomain()) { | ||||
$errors = ['package' => \trans('validation.packageinvalid')]; | $errors = ['package' => \trans('validation.packageinvalid')]; | ||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | return response()->json(['status' => 'error', 'errors' => $errors], 422); | ||||
} | } | ||||
Show All 34 Lines | class UsersController extends Controller | ||||
* | * | ||||
* @param \Illuminate\Http\Request $request The API request. | * @param \Illuminate\Http\Request $request The API request. | ||||
* @param string $id User identifier | * @param string $id User identifier | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse The response | * @return \Illuminate\Http\JsonResponse The response | ||||
*/ | */ | ||||
public function update(Request $request, $id) | public function update(Request $request, $id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenantContext()->find($id); | ||||
if (empty($user)) { | if (empty($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
$current_user = $this->guard()->user(); | $current_user = $this->guard()->user(); | ||||
// TODO: Decide what attributes a user can change on his own profile | // TODO: Decide what attributes a user can change on his own profile | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
*/ | */ | ||||
protected function updateEntitlements(User $user, $rSkus) | protected function updateEntitlements(User $user, $rSkus) | ||||
{ | { | ||||
if (!is_array($rSkus)) { | if (!is_array($rSkus)) { | ||||
return; | return; | ||||
} | } | ||||
// list of skus, [id=>obj] | // list of skus, [id=>obj] | ||||
$skus = Sku::all()->mapWithKeys( | $skus = Sku::withEnvTenantContext()->get()->mapWithKeys( | ||||
function ($sku) { | function ($sku) { | ||||
return [$sku->id => $sku]; | return [$sku->id => $sku]; | ||||
} | } | ||||
); | ); | ||||
// existing entitlement's SKUs | // existing entitlement's SKUs | ||||
$eSkus = []; | $eSkus = []; | ||||
▲ Show 20 Lines • Show All 197 Lines • ▼ Show 20 Lines | class UsersController extends Controller | ||||
* @return bool|null True if the execution succeeded, False if not, Null when | * @return bool|null True if the execution succeeded, False if not, Null when | ||||
* the job has been sent to the worker (result unknown) | * 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::withEnvTenantContext()->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); | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public static function validateEmail(string $email, \App\User $user, &$deleted = null): ?string | ||||
list($login, $domain) = explode('@', Str::lower($email)); | list($login, $domain) = explode('@', Str::lower($email)); | ||||
if (strlen($login) === 0 || strlen($domain) === 0) { | if (strlen($login) === 0 || strlen($domain) === 0) { | ||||
return \trans('validation.entryinvalid', ['attribute' => 'email']); | return \trans('validation.entryinvalid', ['attribute' => 'email']); | ||||
} | } | ||||
// Check if domain exists | // Check if domain exists | ||||
$domain = Domain::where('namespace', $domain)->first(); | $domain = Domain::withEnvTenantContext()->where('namespace', $domain)->first(); | ||||
if (empty($domain)) { | if (empty($domain)) { | ||||
return \trans('validation.domaininvalid'); | return \trans('validation.domaininvalid'); | ||||
} | } | ||||
// Validate login part alone | // Validate login part alone | ||||
$v = Validator::make( | $v = Validator::make( | ||||
['email' => $login], | ['email' => $login], | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public static function validateAlias(string $email, \App\User $user): ?string | ||||
list($login, $domain) = explode('@', Str::lower($email)); | list($login, $domain) = explode('@', Str::lower($email)); | ||||
if (strlen($login) === 0 || strlen($domain) === 0) { | if (strlen($login) === 0 || strlen($domain) === 0) { | ||||
return \trans('validation.entryinvalid', ['attribute' => 'alias']); | return \trans('validation.entryinvalid', ['attribute' => 'alias']); | ||||
} | } | ||||
// Check if domain exists | // Check if domain exists | ||||
$domain = Domain::where('namespace', $domain)->first(); | $domain = Domain::withEnvTenantContext()->where('namespace', $domain)->first(); | ||||
if (empty($domain)) { | if (empty($domain)) { | ||||
return \trans('validation.domaininvalid'); | return \trans('validation.domaininvalid'); | ||||
} | } | ||||
// Validate login part alone | // Validate login part alone | ||||
$v = Validator::make( | $v = Validator::make( | ||||
['alias' => $login], | ['alias' => $login], | ||||
Show All 39 Lines |