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\RelationController; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Group; | use App\Group; | ||||
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 Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
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 RelationController | ||||
{ | { | ||||
/** @const array List of user setting keys available for modification in UI */ | /** @const array List of user setting keys available for modification in UI */ | ||||
public const USER_SETTINGS = [ | public const USER_SETTINGS = [ | ||||
'billing_address', | 'billing_address', | ||||
'country', | 'country', | ||||
'currency', | 'currency', | ||||
'external_email', | 'external_email', | ||||
'first_name', | 'first_name', | ||||
'last_name', | 'last_name', | ||||
'organization', | 'organization', | ||||
'phone', | 'phone', | ||||
]; | ]; | ||||
/** | /** | ||||
* On user create it is filled with a user or group object to force-delete | * On user create it is filled with a user or group object to force-delete | ||||
* before the creation of a new user record is possible. | * before the creation of a new user record is possible. | ||||
* | * | ||||
* @var \App\User|\App\Group|null | * @var \App\User|\App\Group|null | ||||
*/ | */ | ||||
protected $deleteBeforeCreate; | protected $deleteBeforeCreate; | ||||
/** @var array Common object properties in the API response */ | /** @var string Resource localization label */ | ||||
protected static $objectProps = ['email']; | protected $label = 'user'; | ||||
/** | |||||
* Delete a user. | |||||
* | |||||
* @param int $id User identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse The response | |||||
*/ | |||||
public function destroy($id) | |||||
{ | |||||
$user = User::withEnvTenantContext()->find($id); | |||||
if (empty($user)) { | /** @var string Resource model name */ | ||||
return $this->errorResponse(404); | protected $model = User::class; | ||||
} | |||||
// User can't remove himself until he's the controller | /** @var array Common object properties in the API response */ | ||||
if (!$this->guard()->user()->canDelete($user)) { | protected $objectProps = ['email']; | ||||
return $this->errorResponse(403); | |||||
} | |||||
$user->delete(); | |||||
return response()->json([ | |||||
'status' => 'success', | |||||
'message' => \trans('app.user-delete-success'), | |||||
]); | |||||
} | |||||
/** | /** | ||||
* Listing of users. | * Listing of users. | ||||
* | * | ||||
* 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 | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public function index() | ||||
'count' => count($result), | 'count' => count($result), | ||||
'hasMore' => $hasMore, | 'hasMore' => $hasMore, | ||||
]; | ]; | ||||
return response()->json($result); | return response()->json($result); | ||||
} | } | ||||
/** | /** | ||||
* Set user config. | |||||
* | |||||
* @param int $id The user | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse | |||||
*/ | |||||
public function setConfig($id) | |||||
{ | |||||
$user = User::find($id); | |||||
if (empty($user)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!$this->guard()->user()->canUpdate($user)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$errors = $user->setConfig(request()->input()); | |||||
if (!empty($errors)) { | |||||
return response()->json(['status' => 'error', 'errors' => $errors], 422); | |||||
} | |||||
return response()->json([ | |||||
'status' => 'success', | |||||
'message' => \trans('app.user-setconfig-success'), | |||||
]); | |||||
} | |||||
/** | |||||
* 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 string $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::withEnvTenantContext()->find($id); | $user = User::find($id); | ||||
if (empty($user)) { | if (!$this->checkTenant($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); | ||||
} | } | ||||
$response = $this->userResponse($user); | $response = $this->userResponse($user); | ||||
$response['skus'] = \App\Entitlement::objectEntitlementsSummary($user); | $response['skus'] = \App\Entitlement::objectEntitlementsSummary($user); | ||||
$response['config'] = $user->getConfig(); | $response['config'] = $user->getConfig(); | ||||
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::withEnvTenantContext()->find($id); | |||||
if (empty($user)) { | |||||
return $this->errorResponse(404); | |||||
} | |||||
if (!$this->guard()->user()->canRead($user)) { | |||||
return $this->errorResponse(403); | |||||
} | |||||
$response = $this->processStateUpdate($user); | |||||
$response = array_merge($response, self::objectState($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): array | ||||
{ | { | ||||
$process = self::processStateInfo( | $process = self::processStateInfo( | ||||
$user, | $user, | ||||
[ | [ | ||||
'user-new' => true, | 'user-new' => true, | ||||
'user-ldap-ready' => $user->isLdapReady(), | 'user-ldap-ready' => $user->isLdapReady(), | ||||
'user-imap-ready' => $user->isImapReady(), | 'user-imap-ready' => $user->isImapReady(), | ||||
] | ] | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | class UsersController extends RelationController | ||||
* Create a response data array for specified user. | * Create a response data array for specified user. | ||||
* | * | ||||
* @param \App\User $user User object | * @param \App\User $user User object | ||||
* | * | ||||
* @return array Response data | * @return array Response data | ||||
*/ | */ | ||||
public static function userResponse(User $user): array | public static function userResponse(User $user): array | ||||
{ | { | ||||
$response = self::objectToClient($user, true); | $response = array_merge($user->toArray(), self::objectState($user)); | ||||
// Settings | // Settings | ||||
$response['settings'] = []; | $response['settings'] = []; | ||||
foreach ($user->settings()->whereIn('key', self::USER_SETTINGS)->get() as $item) { | foreach ($user->settings()->whereIn('key', self::USER_SETTINGS)->get() as $item) { | ||||
$response['settings'][$item->key] = $item->value; | $response['settings'][$item->key] = $item->value; | ||||
} | } | ||||
// Aliases | // Aliases | ||||
Show All 34 Lines | class UsersController extends RelationController | ||||
/** | /** | ||||
* Prepare user statuses for the UI | * Prepare user statuses for the UI | ||||
* | * | ||||
* @param \App\User $user User object | * @param \App\User $user User object | ||||
* | * | ||||
* @return array Statuses array | * @return array Statuses array | ||||
*/ | */ | ||||
protected static function objectState(User $user): array | protected static function objectState($user): array | ||||
{ | { | ||||
return [ | return [ | ||||
'isImapReady' => $user->isImapReady(), | 'isImapReady' => $user->isImapReady(), | ||||
'isLdapReady' => $user->isLdapReady(), | 'isLdapReady' => $user->isLdapReady(), | ||||
'isSuspended' => $user->isSuspended(), | 'isSuspended' => $user->isSuspended(), | ||||
'isActive' => $user->isActive(), | 'isActive' => $user->isActive(), | ||||
'isDeleted' => $user->isDeleted() || $user->trashed(), | 'isDeleted' => $user->isDeleted() || $user->trashed(), | ||||
]; | ]; | ||||
▲ Show 20 Lines • Show All 292 Lines • Show Last 20 Lines |