2017-11-30 08:01:07 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2018-01-08 05:09:22 +08:00
|
|
|
use App\Facades\Utils;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Requests\CreateUserRequest;
|
|
|
|
use App\Http\Requests\UpdateUserRequest;
|
|
|
|
use App\Models\Rank;
|
|
|
|
use App\Models\Role;
|
2018-01-08 05:09:22 +08:00
|
|
|
use App\Models\User;
|
2018-03-01 05:37:24 +08:00
|
|
|
use App\Repositories\AirlineRepository;
|
|
|
|
use App\Repositories\AirportRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
|
|
|
use App\Repositories\UserRepository;
|
|
|
|
use App\Services\UserService;
|
2017-11-30 08:01:07 +08:00
|
|
|
use DB;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Flash;
|
2017-11-30 08:01:07 +08:00
|
|
|
use Hash;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Jackiedo\Timezonelist\Facades\Timezonelist;
|
2017-12-23 06:32:21 +08:00
|
|
|
use Log;
|
2018-01-08 05:09:22 +08:00
|
|
|
use Prettus\Repository\Exceptions\RepositoryException;
|
2017-11-30 08:01:07 +08:00
|
|
|
use Response;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class UserController
|
|
|
|
*/
|
|
|
|
class UserController extends Controller
|
2017-11-30 08:01:07 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $airlineRepo;
|
|
|
|
private $airportRepo;
|
|
|
|
private $pirepRepo;
|
|
|
|
private $userRepo;
|
|
|
|
private $userSvc;
|
2017-11-30 08:01:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-01 05:37:24 +08:00
|
|
|
* @param AirlineRepository $airlineRepo
|
|
|
|
* @param AirportRepository $airportRepo
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param PirepRepository $pirepRepo
|
|
|
|
* @param UserRepository $userRepo
|
|
|
|
* @param UserService $userSvc
|
2017-11-30 08:01:07 +08:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-03-01 05:37:24 +08:00
|
|
|
AirlineRepository $airlineRepo,
|
|
|
|
AirportRepository $airportRepo,
|
2018-01-08 05:09:22 +08:00
|
|
|
PirepRepository $pirepRepo,
|
2017-12-23 06:32:21 +08:00
|
|
|
UserRepository $userRepo,
|
|
|
|
UserService $userSvc
|
2017-11-30 08:01:07 +08:00
|
|
|
) {
|
2018-03-01 05:37:24 +08:00
|
|
|
$this->airlineRepo = $airlineRepo;
|
|
|
|
$this->airportRepo = $airportRepo;
|
2018-01-08 05:09:22 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-23 06:32:21 +08:00
|
|
|
$this->userSvc = $userSvc;
|
2017-11-30 08:01:07 +08:00
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-11-30 08:01:07 +08:00
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-01-08 05:09:22 +08:00
|
|
|
try {
|
|
|
|
$users = $this->userRepo->searchCriteria($request, false)
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->paginate();
|
|
|
|
} catch (RepositoryException $e) {
|
|
|
|
}
|
2017-11-30 08:01:07 +08:00
|
|
|
|
|
|
|
return view('admin.users.index', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'users' => $users,
|
2018-01-31 02:37:51 +08:00
|
|
|
'country' => new \League\ISO3166\ISO3166(),
|
2017-11-30 08:01:07 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new User.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-11-30 08:01:07 +08:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2018-03-01 05:37:24 +08:00
|
|
|
$airlines = $this->airlineRepo->selectBoxList();
|
2018-09-09 03:07:31 +08:00
|
|
|
$airports = $this->airportRepo->selectBoxList(false);
|
2018-03-01 05:37:24 +08:00
|
|
|
|
|
|
|
return view('admin.users.create', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => null,
|
|
|
|
'pireps' => null,
|
|
|
|
'airlines' => $airlines,
|
2018-03-01 05:37:24 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'country' => new \League\ISO3166\ISO3166(),
|
|
|
|
'airports' => $airports,
|
|
|
|
'ranks' => Rank::all()->pluck('name', 'id'),
|
|
|
|
'roles' => Role::all()->pluck('name', 'id'),
|
2017-11-30 08:01:07 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created User in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-11-30 08:01:07 +08:00
|
|
|
* @param CreateUserRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-08 05:09:22 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return Response
|
2017-11-30 08:01:07 +08:00
|
|
|
*/
|
|
|
|
public function store(CreateUserRequest $request)
|
|
|
|
{
|
|
|
|
$input = $request->all();
|
2018-02-21 12:33:09 +08:00
|
|
|
$user = $this->userRepo->create($input);
|
2017-11-30 08:01:07 +08:00
|
|
|
|
|
|
|
Flash::success('User saved successfully.');
|
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified User.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2017-11-30 08:01:07 +08:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2018-01-04 05:27:56 +08:00
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
2017-11-30 08:01:07 +08:00
|
|
|
|
2018-01-04 05:27:56 +08:00
|
|
|
if (empty($user)) {
|
2017-11-30 08:01:07 +08:00
|
|
|
Flash::error('User not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
2018-01-08 05:09:22 +08:00
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->whereOrder(['user_id' => $id], 'created_at', 'desc')
|
|
|
|
->paginate();
|
|
|
|
|
2018-03-01 05:37:24 +08:00
|
|
|
$airlines = $this->airlineRepo->selectBoxList();
|
2018-09-09 03:07:31 +08:00
|
|
|
$airports = $this->airportRepo->selectBoxList(false);
|
2018-03-01 05:37:24 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return view('admin.users.show', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => $user,
|
|
|
|
'pireps' => $pireps,
|
|
|
|
'airlines' => $airlines,
|
2018-01-04 05:27:56 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'country' => new \League\ISO3166\ISO3166(),
|
|
|
|
'airports' => $airports,
|
|
|
|
'ranks' => Rank::all()->pluck('name', 'id'),
|
|
|
|
'roles' => Role::all()->pluck('name', 'id'),
|
2017-11-30 08:01:07 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified User.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2017-11-30 08:01:07 +08:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
2018-01-08 05:09:22 +08:00
|
|
|
$pireps = $this->pirepRepo
|
|
|
|
->whereOrder(['user_id' => $id], 'created_at', 'desc')
|
|
|
|
->paginate();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$countries = collect((new \League\ISO3166\ISO3166())->all())
|
2018-01-10 09:08:16 +08:00
|
|
|
->mapWithKeys(function ($item, $key) {
|
|
|
|
return [strtolower($item['alpha2']) => $item['name']];
|
|
|
|
});
|
|
|
|
|
2018-03-01 05:37:24 +08:00
|
|
|
$airlines = $this->airlineRepo->selectBoxList();
|
2018-09-09 03:07:31 +08:00
|
|
|
$airports = $this->airportRepo->selectBoxList(false);
|
2018-03-01 05:37:24 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return view('admin.users.edit', [
|
2018-03-20 09:50:40 +08:00
|
|
|
'user' => $user,
|
|
|
|
'pireps' => $pireps,
|
2018-01-10 09:08:16 +08:00
|
|
|
'countries' => $countries,
|
2017-12-07 06:07:25 +08:00
|
|
|
'timezones' => Timezonelist::toArray(),
|
2018-03-20 09:50:40 +08:00
|
|
|
'airports' => $airports,
|
|
|
|
'airlines' => $airlines,
|
|
|
|
'ranks' => Rank::all()->pluck('name', 'id'),
|
|
|
|
'roles' => Role::all()->pluck('name', 'id'),
|
2017-11-30 08:01:07 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified User in storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param int $id
|
2017-11-30 08:01:07 +08:00
|
|
|
* @param UpdateUserRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return Response
|
2017-11-30 08:01:07 +08:00
|
|
|
*/
|
|
|
|
public function update($id, UpdateUserRequest $request)
|
|
|
|
{
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$req_data = $request->all();
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!$request->filled('password')) {
|
2017-11-30 08:01:07 +08:00
|
|
|
unset($req_data['password']);
|
|
|
|
} else {
|
|
|
|
$req_data['password'] = Hash::make($req_data['password']);
|
|
|
|
}
|
|
|
|
|
2018-04-03 00:04:32 +08:00
|
|
|
if ($request->filled('avatar_upload')) {
|
|
|
|
/**
|
2018-08-27 00:40:04 +08:00
|
|
|
* @var $file \Illuminate\Http\UploadedFile
|
2018-04-03 00:04:32 +08:00
|
|
|
*/
|
|
|
|
$file = $request->file('avatar_upload');
|
|
|
|
$file_path = $file->storeAs(
|
|
|
|
'avatars',
|
|
|
|
str_slug($file->getClientOriginalName()),
|
|
|
|
config('filesystems.public_files')
|
|
|
|
);
|
|
|
|
|
|
|
|
$user->avatar = $file_path;
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:32:21 +08:00
|
|
|
$original_user_state = $user->state;
|
|
|
|
|
2018-09-05 08:09:33 +08:00
|
|
|
// Convert transferred hours to minutes
|
2018-09-09 03:07:31 +08:00
|
|
|
$req_data['transfer_time'] *= 60;
|
2018-09-05 08:09:33 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
$user = $this->userRepo->update($req_data, $id);
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($original_user_state !== $user->state) {
|
2017-12-23 06:32:21 +08:00
|
|
|
$this->userSvc->changeUserState($user, $original_user_state);
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Delete all of the roles and then re-attach the valid ones
|
2018-03-20 09:50:40 +08:00
|
|
|
DB::table('role_user')->where('user_id', $id)->delete();
|
2017-11-30 08:01:07 +08:00
|
|
|
foreach ($request->input('roles') as $key => $value) {
|
|
|
|
$user->attachRole($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Flash::success('User updated successfully.');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified User from storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
*
|
2017-11-30 08:01:07 +08:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$user = $this->userRepo->findWithoutFail($id);
|
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
Flash::error('User not found');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->userRepo->delete($id);
|
|
|
|
|
|
|
|
Flash::success('User deleted successfully.');
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-11-30 08:01:07 +08:00
|
|
|
return redirect(route('admin.users.index'));
|
|
|
|
}
|
2018-01-08 05:09:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate the user's API key
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-01-08 05:09:22 +08:00
|
|
|
*/
|
|
|
|
public function regen_apikey($id, Request $request)
|
|
|
|
{
|
|
|
|
$user = User::find($id);
|
2019-07-17 01:54:14 +08:00
|
|
|
Log::info('Regenerating API key "'.$user->ident.'"');
|
2018-01-08 05:09:22 +08:00
|
|
|
|
|
|
|
$user->api_key = Utils::generateApiKey();
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
flash('New API key generated!')->success();
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-08 05:09:22 +08:00
|
|
|
return redirect(route('admin.users.edit', ['id' => $id]));
|
|
|
|
}
|
2017-11-30 08:01:07 +08:00
|
|
|
}
|