phpvms/app/Http/Controllers/Admin/UserController.php

315 lines
8.6 KiB
PHP
Raw Normal View History

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-02-21 12:33:09 +08:00
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Models\Rank;
use App\Models\User;
use App\Models\UserAward;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
2018-02-21 12:33:09 +08:00
use App\Repositories\PirepRepository;
use App\Repositories\RoleRepository;
2018-02-21 12:33:09 +08:00
use App\Repositories\UserRepository;
use App\Services\UserService;
use App\Support\Timezonelist;
use App\Support\Utils;
2018-02-21 12:33:09 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Laracasts\Flash\Flash;
2020-10-07 06:52:34 +08:00
use League\ISO3166\ISO3166;
use Prettus\Repository\Exceptions\RepositoryException;
2017-11-30 08:01:07 +08:00
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 $roleRepo;
2018-08-27 00:40:04 +08:00
private $userRepo;
private $userSvc;
2017-11-30 08:01:07 +08:00
/**
* UserController constructor.
2018-08-27 00:40:04 +08:00
*
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param PirepRepository $pirepRepo
* @param RoleRepository $roleRepo
* @param UserRepository $userRepo
* @param UserService $userSvc
2017-11-30 08:01:07 +08:00
*/
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
PirepRepository $pirepRepo,
RoleRepository $roleRepo,
UserRepository $userRepo,
UserService $userSvc
2017-11-30 08:01:07 +08:00
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->pirepRepo = $pirepRepo;
$this->roleRepo = $roleRepo;
$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)
{
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', [
'users' => $users,
2020-10-07 06:52:34 +08:00
'country' => new 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
*
* @return mixed
2017-11-30 08:01:07 +08:00
*/
public function create()
{
$airlines = $this->airlineRepo->selectBoxList();
$airports = $this->airportRepo->selectBoxList(false);
2020-10-07 06:52:34 +08:00
$countries = collect((new ISO3166())->all())
->mapWithKeys(function ($item, $key) {
return [strtolower($item['alpha2']) => $item['name']];
});
$roles = $this->roleRepo->selectBoxList(false, true);
return view('admin.users.create', [
'user' => null,
'pireps' => null,
'airlines' => $airlines,
'timezones' => Timezonelist::toArray(),
2020-10-07 06:52:34 +08:00
'country' => new ISO3166(),
'countries' => $countries,
'airports' => $airports,
'ranks' => Rank::all()->pluck('name', 'id'),
'roles' => $roles,
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
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-08-27 00:40:04 +08:00
*
* @return mixed
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
*
* @return mixed
2017-11-30 08:01:07 +08:00
*/
public function show($id)
{
2020-10-07 07:26:52 +08:00
return $this->edit($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
*
2020-10-07 07:10:42 +08:00
* @throws RepositoryException
*
* @return mixed
2017-11-30 08:01:07 +08:00
*/
public function edit($id)
{
$user = $this->userRepo
->with(['awards', 'fields', 'rank'])
->findWithoutFail($id);
2017-11-30 08:01:07 +08:00
if (empty($user)) {
Flash::error('User not found');
return redirect(route('admin.users.index'));
}
$pireps = $this->pirepRepo
->whereOrder(['user_id' => $id], 'created_at', 'desc')
->paginate();
2020-10-07 06:52:34 +08:00
$countries = collect((new ISO3166())->all())
2018-01-10 09:08:16 +08:00
->mapWithKeys(function ($item, $key) {
return [strtolower($item['alpha2']) => $item['name']];
});
$airlines = $this->airlineRepo->selectBoxList();
$airports = $this->airportRepo->selectBoxList(false);
$roles = $this->roleRepo->selectBoxList(false, true);
2017-11-30 08:01:07 +08:00
return view('admin.users.edit', [
'user' => $user,
'pireps' => $pireps,
2020-10-07 07:10:42 +08:00
'country' => new ISO3166(),
2018-01-10 09:08:16 +08:00
'countries' => $countries,
'timezones' => Timezonelist::toArray(),
'airports' => $airports,
'airlines' => $airlines,
'ranks' => Rank::all()->pluck('name', 'id'),
'roles' => $roles,
2017-11-30 08:01:07 +08:00
]);
}
/**
* Update the specified User in storage.
2018-08-27 00:40:04 +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 mixed
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');
2017-11-30 08:01:07 +08:00
return redirect(route('admin.users.index'));
}
$req_data = $request->all();
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']);
}
if ($request->filled('avatar_upload')) {
/**
2018-08-27 00:40:04 +08:00
* @var $file \Illuminate\Http\UploadedFile
*/
$file = $request->file('avatar_upload');
$file_path = $file->storeAs(
'avatars',
str_slug($file->getClientOriginalName()),
config('filesystems.public_files')
);
$user->avatar = $file_path;
}
$original_user_state = $user->state;
2018-09-05 08:09:33 +08:00
// Convert transferred hours to minutes
$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);
if ($original_user_state !== $user->state) {
$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
if (!empty($request->input('roles'))) {
DB::table('role_user')->where('user_id', $id)->delete();
foreach ($request->input('roles') as $key => $value) {
$user->attachRole($value);
}
2017-11-30 08:01:07 +08:00
}
Flash::success('User updated successfully.');
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
*
* @return mixed
2017-11-30 08:01:07 +08:00
*/
public function destroy($id)
{
$user = $this->userRepo->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('admin.users.index'));
}
$this->userSvc->removeUser($user);
2017-11-30 08:01:07 +08:00
Flash::success('User deleted successfully.');
2017-11-30 08:01:07 +08:00
return redirect(route('admin.users.index'));
}
/**
* Remove the award from a user
*
* @param \Illuminate\Http\Request $request
* @param mixed $id
* @param mixed $award_id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy_user_award($id, $award_id, Request $request)
{
$userAward = UserAward::where(['user_id' => $id, 'award_id' => $award_id]);
if (empty($userAward)) {
Flash::error('The user award could not be found');
return redirect()->back();
}
$userAward->delete();
return redirect()->back();
}
/**
* Regenerate the user's API key
2018-08-27 00:40:04 +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
*/
public function regen_apikey($id, Request $request)
{
$user = User::find($id);
Log::info('Regenerating API key "'.$user->ident.'"');
$user->api_key = Utils::generateApiKey();
$user->save();
flash('New API key generated!')->success();
2019-09-14 06:20:50 +08:00
return redirect(route('admin.users.edit', [$id]));
}
2017-11-30 08:01:07 +08:00
}