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\User; | use App\User; | ||||
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 | ||||
{ | { | ||||
/** | /** | ||||
* 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)) { | if ($owner = User::find($owner)) { | ||||
$result = $owner->users(false)->orderBy('email')->get(); | $result = $owner->users(false)->orderBy('email')->get(); | ||||
} | } | ||||
} elseif (strpos($search, '@')) { | } elseif (strpos($search, '@')) { | ||||
// Search by email | // Search by email | ||||
if ($user = User::findByEmail($search, false)) { | $user = User::where('email', $search)->first(); | ||||
if ($user) { | |||||
$result->push($user); | $result->push($user); | ||||
} else { | } else { | ||||
// Search by an alias | |||||
$user_ids = UserAlias::where('alias', $search)->get()->pluck('user_id'); | |||||
if ($user_ids->isEmpty()) { | |||||
// Search by an external email | // Search by an external email | ||||
// TODO: This is not optimal (external email should be in users table) | $user_ids = UserSetting::where('key', 'external_email') | ||||
$user_ids = UserSetting::where('key', 'external_email')->where('value', $search) | ->where('value', $search)->get()->pluck('user_id'); | ||||
->get()->pluck('user_id'); | } | ||||
// TODO: Sort order | if (!$user_ids->isEmpty()) { | ||||
$result = User::find($user_ids); | $result = User::whereIn('id', $user_ids)->orderBy('email')->get(); | ||||
} | |||||
} | } | ||||
} elseif (is_numeric($search)) { | } elseif (is_numeric($search)) { | ||||
// Search by user ID | // Search by user ID | ||||
if ($user = User::find($search)) { | if ($user = User::find($search)) { | ||||
$result->push($user); | $result->push($user); | ||||
} | } | ||||
} elseif (!empty($search)) { | } elseif (!empty($search)) { | ||||
// Search by domain | // Search by domain | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |