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

237 lines
6.3 KiB
PHP
Raw Normal View History

2017-11-30 08:01:07 +08:00
<?php
namespace App\Http\Controllers\Admin;
use App\Facades\Utils;
use App\Models\User;
2017-11-30 08:01:07 +08:00
use DB;
use Hash;
use Illuminate\Support\Facades\Auth;
use Log;
2017-11-30 08:01:07 +08:00
use Flash;
use Prettus\Repository\Exceptions\RepositoryException;
2017-11-30 08:01:07 +08:00
use Response;
use Jackiedo\Timezonelist\Facades\Timezonelist;
use Illuminate\Http\Request;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Repositories\PirepRepository;
use App\Repositories\UserRepository;
use App\Services\UserService;
2017-11-30 08:01:07 +08:00
use App\Models\Airport;
use App\Models\Airline;
use App\Models\Rank;
use App\Models\Role;
class UserController extends BaseController
{
private $pirepRepo,
$userRepo,
$userSvc;
2017-11-30 08:01:07 +08:00
/**
* UserController constructor.
*
* @param UserRepository $userRepo
*/
public function __construct(
PirepRepository $pirepRepo,
UserRepository $userRepo,
UserService $userSvc
2017-11-30 08:01:07 +08:00
) {
$this->pirepRepo = $pirepRepo;
$this->userSvc = $userSvc;
2017-11-30 08:01:07 +08:00
$this->userRepo = $userRepo;
}
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,
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.
* @return Response
*/
public function create()
{
return view('admin.user.create', [
'airlines' => Airline::all()->pluck('name', 'id'),
]);
}
/**
* Store a newly created User in storage.
* @param CreateUserRequest $request
* @return Response
* @throws \Prettus\Validator\Exceptions\ValidatorException
2017-11-30 08:01:07 +08:00
*/
public function store(CreateUserRequest $request)
{
$input = $request->all();
$User = $this->userRepo->create($input);
Flash::success('User saved successfully.');
return redirect(route('admin.users.index'));
}
/**
* Display the specified User.
* @param int $id
* @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');
return redirect(route('admin.users.index'));
}
$pireps = $this->pirepRepo
->whereOrder(['user_id' => $id], 'created_at', 'desc')
->paginate();
2017-11-30 08:01:07 +08:00
return view('admin.users.show', [
2018-01-04 05:27:56 +08:00
'user' => $user,
'pireps' => $pireps,
2018-01-04 05:27:56 +08:00
'airlines' => Airline::all(),
'timezones' => Timezonelist::toArray(),
2018-01-31 02:39:24 +08:00
'country' => new \League\ISO3166\ISO3166(),
2018-01-04 05:27:56 +08:00
'airports' => Airport::all()->pluck('icao', 'id'),
'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.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$user = $this->userRepo->findWithoutFail($id);
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();
2018-01-10 09:08:16 +08:00
$countries = collect((new \League\ISO3166\ISO3166)->all())
->mapWithKeys(function ($item, $key) {
return [strtolower($item['alpha2']) => $item['name']];
});
2017-11-30 08:01:07 +08:00
return view('admin.users.edit', [
'user' => $user,
'pireps' => $pireps,
2018-01-10 09:08:16 +08:00
'countries' => $countries,
'timezones' => Timezonelist::toArray(),
2017-11-30 08:01:07 +08:00
'airports' => Airport::all()->pluck('icao', 'id'),
'airlines' => Airline::all()->pluck('name', 'id'),
'ranks' => Rank::all()->pluck('name', 'id'),
'roles' => Role::all()->pluck('name', 'id'),
]);
}
/**
* Update the specified User in storage.
*
* @param int $id
* @param UpdateUserRequest $request
*
* @return Response
*/
public function update($id, UpdateUserRequest $request)
{
$user = $this->userRepo->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('admin.users.index'));
}
$req_data = $request->all();
2017-12-07 00:07:10 +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']);
}
$original_user_state = $user->state;
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);
}
# Delete all of the roles and then re-attach the valid ones
2017-11-30 08:01:07 +08:00
DB::table('role_user')->where('user_id',$id)->delete();
foreach ($request->input('roles') as $key => $value) {
$user->attachRole($value);
}
Flash::success('User updated successfully.');
return redirect(route('admin.users.index'));
}
/**
* Remove the specified User from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$user = $this->userRepo->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('admin.users.index'));
}
$this->userRepo->delete($id);
Flash::success('User deleted successfully.');
return redirect(route('admin.users.index'));
}
/**
* Regenerate the user's API key
*/
public function regen_apikey($id, Request $request)
{
$user = User::find($id);
Log::info('Regenerating API key "' . $user->pilot_id . '"');
$user->api_key = Utils::generateApiKey();
$user->save();
flash('New API key generated!')->success();
return redirect(route('admin.users.edit', ['id' => $id]));
}
2017-11-30 08:01:07 +08:00
}