Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/Admin/UsersController.php
<?php | <?php | ||||
namespace App\Http\Controllers\API\V4\Admin; | namespace App\Http\Controllers\API\V4\Admin; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Group; | use App\Group; | ||||
use App\Sku; | use App\Sku; | ||||
use App\User; | use App\User; | ||||
use App\UserAlias; | use App\UserAlias; | ||||
use App\UserSetting; | use App\UserSetting; | ||||
use Illuminate\Http\Request; | use Illuminate\Http\Request; | ||||
use Illuminate\Support\Facades\Validator; | use Illuminate\Support\Facades\Validator; | ||||
class UsersController extends \App\Http\Controllers\API\V4\UsersController | class UsersController extends \App\Http\Controllers\API\V4\UsersController | ||||
{ | { | ||||
/** | /** | ||||
* Delete a user. | |||||
* | |||||
* @param int $id User identifier | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse The response | |||||
*/ | |||||
public function destroy($id) | |||||
{ | |||||
return $this->errorResponse(404); | |||||
} | |||||
/** | |||||
* Searching of user accounts. | * Searching of user accounts. | ||||
* | * | ||||
* @return \Illuminate\Http\JsonResponse | * @return \Illuminate\Http\JsonResponse | ||||
*/ | */ | ||||
public function index() | public function index() | ||||
{ | { | ||||
$search = trim(request()->input('search')); | $search = trim(request()->input('search')); | ||||
$owner = trim(request()->input('owner')); | $owner = trim(request()->input('owner')); | ||||
$result = collect([]); | $result = collect([]); | ||||
if ($owner) { | if ($owner) { | ||||
if ($owner = User::find($owner)) { | $owner = User::where('id', $owner) | ||||
$result = $owner->users(false)->orderBy('email')->get(); | ->withEnvTenant() | ||||
->whereNull('role') | |||||
->first(); | |||||
if ($owner) { | |||||
$result = $owner->users(false)->whereNull('role')->orderBy('email')->get(); | |||||
} | } | ||||
} elseif (strpos($search, '@')) { | } elseif (strpos($search, '@')) { | ||||
// Search by email | // Search by email | ||||
$result = User::withTrashed()->where('email', $search) | $result = User::withTrashed()->where('email', $search) | ||||
->orderBy('email')->get(); | ->withEnvTenant() | ||||
->whereNull('role') | |||||
->orderBy('email') | |||||
->get(); | |||||
if ($result->isEmpty()) { | if ($result->isEmpty()) { | ||||
// Search by an alias | // Search by an alias | ||||
$user_ids = UserAlias::where('alias', $search)->get()->pluck('user_id'); | $user_ids = UserAlias::where('alias', $search)->get()->pluck('user_id'); | ||||
// Search by an external email | // Search by an external email | ||||
$ext_user_ids = UserSetting::where('key', 'external_email') | $ext_user_ids = UserSetting::where('key', 'external_email') | ||||
->where('value', $search)->get()->pluck('user_id'); | ->where('value', $search) | ||||
->get() | |||||
->pluck('user_id'); | |||||
$user_ids = $user_ids->merge($ext_user_ids)->unique(); | $user_ids = $user_ids->merge($ext_user_ids)->unique(); | ||||
// Search by a distribution list email | // Search by a distribution list email | ||||
if ($group = Group::withTrashed()->where('email', $search)->first()) { | if ($group = Group::withTrashed()->where('email', $search)->first()) { | ||||
$user_ids = $user_ids->merge([$group->wallet()->user_id])->unique(); | $user_ids = $user_ids->merge([$group->wallet()->user_id])->unique(); | ||||
} | } | ||||
if (!$user_ids->isEmpty()) { | if (!$user_ids->isEmpty()) { | ||||
$result = User::withTrashed()->whereIn('id', $user_ids) | $result = User::withTrashed()->whereIn('id', $user_ids) | ||||
->orderBy('email')->get(); | ->withEnvTenant() | ||||
->whereNull('role') | |||||
->orderBy('email') | |||||
->get(); | |||||
} | } | ||||
} | } | ||||
} elseif (is_numeric($search)) { | } elseif (is_numeric($search)) { | ||||
// Search by user ID | // Search by user ID | ||||
if ($user = User::withTrashed()->find($search)) { | $user = User::withTrashed()->where('id', $search) | ||||
->withEnvTenant() | |||||
->whereNull('role') | |||||
->first(); | |||||
if ($user) { | |||||
$result->push($user); | $result->push($user); | ||||
} | } | ||||
} elseif (!empty($search)) { | } elseif (!empty($search)) { | ||||
// Search by domain | // Search by domain | ||||
if ($domain = Domain::withTrashed()->where('namespace', $search)->first()) { | $domain = Domain::withTrashed()->where('namespace', $search) | ||||
if ($wallet = $domain->wallet()) { | ->withEnvTenant() | ||||
$result->push($wallet->owner()->withTrashed()->first()); | ->first(); | ||||
if ($domain) { | |||||
if ( | |||||
($wallet = $domain->wallet()) | |||||
&& ($owner = $wallet->owner()->withTrashed()->withEnvTenant()->first()) | |||||
&& empty($owner->role) | |||||
) { | |||||
$result->push($owner); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
// Process the result | // Process the result | ||||
$result = $result->map(function ($user) { | $result = $result->map(function ($user) { | ||||
$data = $user->toArray(); | $data = $user->toArray(); | ||||
$data = array_merge($data, self::userStatuses($user)); | $data = array_merge($data, self::userStatuses($user)); | ||||
Show All 14 Lines | class UsersController extends \App\Http\Controllers\API\V4\UsersController | ||||
* | * | ||||
* @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 reset2FA(Request $request, $id) | public function reset2FA(Request $request, $id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenant()->find($id); | ||||
if (empty($user)) { | if (empty($user) || !$this->guard()->user()->canUpdate($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
$sku = Sku::where('title', '2fa')->first(); | $sku = Sku::where('title', '2fa')->first(); | ||||
// Note: we do select first, so the observer can delete | // Note: we do select first, so the observer can delete | ||||
// 2FA preferences from Roundcube database, so don't | // 2FA preferences from Roundcube database, so don't | ||||
// be tempted to replace first() with delete() below | // be tempted to replace first() with delete() below | ||||
$entitlement = $user->entitlements()->where('sku_id', $sku->id)->first(); | $entitlement = $user->entitlements()->where('sku_id', $sku->id)->first(); | ||||
$entitlement->delete(); | $entitlement->delete(); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => __('app.user-reset-2fa-success'), | 'message' => __('app.user-reset-2fa-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Create a new user record. | |||||
* | |||||
* @param \Illuminate\Http\Request $request The API request. | |||||
* | |||||
* @return \Illuminate\Http\JsonResponse The response | |||||
*/ | |||||
public function store(Request $request) | |||||
{ | |||||
return $this->errorResponse(404); | |||||
} | |||||
/** | |||||
* Suspend the user | * Suspend the user | ||||
* | * | ||||
* @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 suspend(Request $request, $id) | public function suspend(Request $request, $id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenant()->find($id); | ||||
if (empty($user)) { | if (empty($user) || !$this->guard()->user()->canUpdate($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
$user->suspend(); | $user->suspend(); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => __('app.user-suspend-success'), | 'message' => __('app.user-suspend-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Un-Suspend the user | * Un-Suspend the user | ||||
* | * | ||||
* @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 unsuspend(Request $request, $id) | public function unsuspend(Request $request, $id) | ||||
{ | { | ||||
$user = User::find($id); | $user = User::withEnvTenant()->find($id); | ||||
if (empty($user)) { | if (empty($user) || !$this->guard()->user()->canUpdate($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
$user->unsuspend(); | $user->unsuspend(); | ||||
return response()->json([ | return response()->json([ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => __('app.user-unsuspend-success'), | 'message' => __('app.user-unsuspend-success'), | ||||
]); | ]); | ||||
} | } | ||||
/** | /** | ||||
* Update user data. | * Update user data. | ||||
* | * | ||||
* @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::withEnvTenant()->find($id); | ||||
if (empty($user)) { | if (empty($user) || !$this->guard()->user()->canUpdate($user)) { | ||||
return $this->errorResponse(404); | return $this->errorResponse(404); | ||||
} | } | ||||
// For now admins can change only user external email address | // For now admins can change only user external email address | ||||
$rules = []; | $rules = []; | ||||
if (array_key_exists('external_email', $request->input())) { | if (array_key_exists('external_email', $request->input())) { | ||||
Show All 23 Lines |