mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-27 14:39:49 -08:00
147 lines
4.2 KiB
PHP
147 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Transformers\UsersTransformer;
|
|
use App\Models\Company;
|
|
use App\Models\User;
|
|
|
|
class UsersController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
|
|
$users = User::select([
|
|
'users.id',
|
|
'users.employee_num',
|
|
'users.two_factor_enrolled',
|
|
'users.jobtitle',
|
|
'users.email',
|
|
'users.username',
|
|
'users.location_id',
|
|
'users.manager_id',
|
|
'users.first_name',
|
|
'users.last_name',
|
|
'users.created_at',
|
|
'users.notes',
|
|
'users.company_id',
|
|
'users.last_login',
|
|
'users.deleted_at',
|
|
'users.department_id',
|
|
'users.activated'
|
|
])->with('manager', 'groups', 'userloc', 'company', 'department','throttle','assets','licenses','accessories','consumables')
|
|
->withCount('assets','licenses','accessories','consumables');
|
|
$users = Company::scopeCompanyables($users);
|
|
|
|
|
|
if ($request->has('search')) {
|
|
$users = $users->TextSearch($request->input('search'));
|
|
}
|
|
|
|
if ($request->has('company_id')) {
|
|
$users = $users->where('company_id','=',$request->input('company_id'));
|
|
}
|
|
|
|
if ($request->has('department_id')) {
|
|
$users = $users->where('department_id','=',$request->input('department_id'));
|
|
}
|
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
$offset = request('offset', 0);
|
|
$limit = request('limit', 50);
|
|
|
|
switch ($request->input('sort')) {
|
|
case 'manager':
|
|
$users = $users->OrderManager($order);
|
|
break;
|
|
case 'location':
|
|
$users = $users->OrderLocation($order);
|
|
break;
|
|
case 'department':
|
|
$users = $users->OrderDepartment($order);
|
|
break;
|
|
default:
|
|
$allowed_columns =
|
|
[
|
|
'last_name','first_name','email','jobtitle','username','employee_num',
|
|
'assets','accessories', 'consumables','licenses','groups','activated','created_at',
|
|
'two_factor_enrolled','two_factor_optin','last_login'
|
|
];
|
|
|
|
$sort = in_array($request->get('sort'), $allowed_columns) ? $request->get('sort') : 'first_name';
|
|
$users = $users->orderBy($sort, $order);
|
|
break;
|
|
}
|
|
$userCount = $users->count();
|
|
$users = $users->skip($offset)->take($limit)->get();
|
|
return (new UsersTransformer)->transformUsers($users, $userCount);
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$this->authorize('view', User::class);
|
|
$user = User::findOrFail($id);
|
|
return (new UsersTransformer)->transformUser($user);
|
|
}
|
|
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
* @since [v4.0]
|
|
* @param int $id
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
//
|
|
}
|
|
}
|