2017-01-12 19:40:20 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Http\Controllers\Controller;
|
2017-01-12 23:42:39 -08:00
|
|
|
use App\Http\Transformers\UsersTransformer;
|
2017-01-13 04:50:20 -08:00
|
|
|
use App\Models\Company;
|
2017-01-12 23:42:39 -08:00
|
|
|
use App\Models\User;
|
2017-08-03 19:50:18 -07:00
|
|
|
use App\Helpers\Helper;
|
2017-08-22 20:32:39 -07:00
|
|
|
use App\Http\Requests\SaveUserRequest;
|
2017-08-26 15:21:38 -07:00
|
|
|
use App\Models\Asset;
|
2017-10-24 19:18:20 -07:00
|
|
|
use App\Http\Transformers\AssetsTransformer;
|
2017-01-12 19:40:20 -08:00
|
|
|
|
|
|
|
class UsersController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2017-01-13 04:50:20 -08:00
|
|
|
public function index(Request $request)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
2017-01-12 23:42:39 -08:00
|
|
|
$this->authorize('view', User::class);
|
2017-01-13 04:50:20 -08:00
|
|
|
|
|
|
|
$users = User::select([
|
|
|
|
'users.id',
|
|
|
|
'users.employee_num',
|
|
|
|
'users.two_factor_enrolled',
|
|
|
|
'users.jobtitle',
|
|
|
|
'users.email',
|
2017-10-23 14:21:51 -07:00
|
|
|
'users.phone',
|
2017-01-13 04:50:20 -08:00
|
|
|
'users.username',
|
|
|
|
'users.location_id',
|
|
|
|
'users.manager_id',
|
|
|
|
'users.first_name',
|
|
|
|
'users.last_name',
|
|
|
|
'users.created_at',
|
|
|
|
'users.notes',
|
|
|
|
'users.company_id',
|
2017-03-03 18:28:13 -08:00
|
|
|
'users.last_login',
|
2017-01-13 04:50:20 -08:00
|
|
|
'users.deleted_at',
|
2017-05-23 02:46:55 -07:00
|
|
|
'users.department_id',
|
2017-10-24 09:51:07 -07:00
|
|
|
'users.activated',
|
|
|
|
'users.avatar',
|
|
|
|
|
2017-05-23 02:46:55 -07:00
|
|
|
])->with('manager', 'groups', 'userloc', 'company', 'department','throttle','assets','licenses','accessories','consumables')
|
2017-02-03 22:20:11 -08:00
|
|
|
->withCount('assets','licenses','accessories','consumables');
|
2017-01-13 04:50:20 -08:00
|
|
|
$users = Company::scopeCompanyables($users);
|
|
|
|
|
|
|
|
|
|
|
|
if ($request->has('search')) {
|
|
|
|
$users = $users->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2017-09-06 17:11:43 -07:00
|
|
|
|
|
|
|
if (($request->has('deleted')) && ($request->input('deleted')=='true')) {
|
|
|
|
$users = $users->GetDeleted();
|
|
|
|
}
|
|
|
|
|
2017-02-08 08:48:41 -08:00
|
|
|
if ($request->has('company_id')) {
|
2017-06-12 17:39:03 -07:00
|
|
|
$users = $users->where('company_id', '=', $request->input('company_id'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('location_id')) {
|
|
|
|
$users = $users->where('location_id', '=', $request->input('location_id'));
|
2017-02-08 08:48:41 -08:00
|
|
|
}
|
2017-10-17 21:43:57 -07:00
|
|
|
|
|
|
|
if ($request->has('group_id')) {
|
2017-10-24 04:39:47 -07:00
|
|
|
$users = $users->ByGroup($request->get('group_id'));
|
2017-10-17 21:43:57 -07:00
|
|
|
}
|
2017-02-08 08:48:41 -08:00
|
|
|
|
2017-05-23 01:09:03 -07:00
|
|
|
if ($request->has('department_id')) {
|
|
|
|
$users = $users->where('department_id','=',$request->input('department_id'));
|
|
|
|
}
|
|
|
|
|
2017-01-13 04:50:20 -08:00
|
|
|
$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;
|
2017-05-23 02:46:55 -07:00
|
|
|
case 'department':
|
|
|
|
$users = $users->OrderDepartment($order);
|
|
|
|
break;
|
2017-01-13 04:50:20 -08:00
|
|
|
default:
|
|
|
|
$allowed_columns =
|
|
|
|
[
|
|
|
|
'last_name','first_name','email','jobtitle','username','employee_num',
|
|
|
|
'assets','accessories', 'consumables','licenses','groups','activated','created_at',
|
2017-10-19 17:15:21 -07:00
|
|
|
'two_factor_enrolled','two_factor_optin','last_login', 'assets_count', 'licenses_count',
|
2017-10-23 14:21:51 -07:00
|
|
|
'consumables_count', 'accessories_count', 'phone'
|
2017-01-13 04:50:20 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
$sort = in_array($request->get('sort'), $allowed_columns) ? $request->get('sort') : 'first_name';
|
|
|
|
$users = $users->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
2017-05-23 14:30:07 -07:00
|
|
|
$total = $users->count();
|
2017-01-13 04:50:20 -08:00
|
|
|
$users = $users->skip($offset)->take($limit)->get();
|
2017-05-23 14:30:07 -07:00
|
|
|
return (new UsersTransformer)->transformUsers($users, $total);
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-08-22 20:32:39 -07:00
|
|
|
public function store(SaveUserRequest $request)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
2017-08-03 19:50:18 -07:00
|
|
|
$this->authorize('view', User::class);
|
|
|
|
$user = new User;
|
|
|
|
$user->fill($request->all());
|
2017-08-08 14:41:58 -07:00
|
|
|
$user->password = bcrypt($request->input('password'));
|
2017-08-03 19:50:18 -07:00
|
|
|
|
|
|
|
if ($user->save()) {
|
2017-10-24 19:17:30 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new UsersTransformer)->transformUser($user), trans('admin/users/message.success.create')));
|
2017-08-03 19:50:18 -07:00
|
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $user->getErrors()));
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2017-01-12 23:42:39 -08:00
|
|
|
$this->authorize('view', User::class);
|
|
|
|
$user = User::findOrFail($id);
|
|
|
|
return (new UsersTransformer)->transformUser($user);
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-08-22 20:32:39 -07:00
|
|
|
public function update(SaveUserRequest $request, $id)
|
2017-01-12 19:40:20 -08:00
|
|
|
{
|
2017-08-03 19:50:18 -07:00
|
|
|
$this->authorize('edit', User::class);
|
|
|
|
$user = User::findOrFail($id);
|
|
|
|
$user->fill($request->all());
|
|
|
|
|
2017-08-08 14:41:58 -07:00
|
|
|
if ($request->has('password')) {
|
|
|
|
$user->password = bcrypt($request->input('password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-03 19:50:18 -07:00
|
|
|
if ($user->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new UsersTransformer)->transformUser($user), trans('admin/users/message.success.update')));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $user->getErrors()));
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2017-08-03 19:50:18 -07:00
|
|
|
$this->authorize('delete', User::class);
|
|
|
|
$user = User::findOrFail($id);
|
|
|
|
$this->authorize('delete', $user);
|
|
|
|
|
|
|
|
|
|
|
|
if ($user->assets()->count() > 0) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.error.delete_has_assets')));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->delete()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.delete')));
|
|
|
|
}
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.error.delete')));
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|
2017-08-26 15:21:38 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return JSON containing a list of assets assigned to a user.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v3.0]
|
|
|
|
* @param $userId
|
|
|
|
* @return string JSON
|
|
|
|
*/
|
|
|
|
public function assets($id)
|
|
|
|
{
|
|
|
|
$this->authorize('view', User::class);
|
|
|
|
$assets = Asset::where('assigned_to', '=', $id)->with('model')->get();
|
2017-10-24 19:18:20 -07:00
|
|
|
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
|
|
|
|
// return response()->json($assets);
|
2017-08-26 15:21:38 -07:00
|
|
|
}
|
2017-01-12 19:40:20 -08:00
|
|
|
}
|