Initial user management in admin panel

This commit is contained in:
Nabeel Shahzad 2017-11-29 18:01:07 -06:00
parent f8cf430db8
commit f52b4bb4d9
17 changed files with 491 additions and 5 deletions

View File

@ -0,0 +1,184 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CreateUserRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Repositories\UserRepository;
use DB;
use Hash;
use Illuminate\Http\Request;
use Illuminate\Foundation\Http\FormRequest;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
use App\Models\Airport;
use App\Models\Airline;
use App\Models\Rank;
use App\Models\Role;
class UserController extends BaseController
{
/** @var UserRepository */
private $userRepo;
/**
* UserController constructor.
*
* @param UserRepository $userRepo
*/
public function __construct(
UserRepository $userRepo
) {
$this->userRepo = $userRepo;
}
/**
* Display a listing of the User.
*
* @param Request $request
* @return Response
*/
public function index(Request $request)
{
$this->userRepo->pushCriteria(new RequestCriteria($request));
$Users = $this->userRepo->all();
return view('admin.users.index', [
'users' => $Users,
]);
}
/**
* 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
*/
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)
{
$users = $this->userRepo->findWithoutFail($id);
if (empty($users)) {
Flash::error('User not found');
return redirect(route('admin.users.index'));
}
return view('admin.users.show', [
'users' => $users,
]);
}
/**
* 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'));
}
return view('admin.users.edit', [
'user' => $user,
'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();
if($req_data['password'] == '') {
unset($req_data['password']);
} else {
$req_data['password'] = Hash::make($req_data['password']);
}
$user = $this->userRepo->update($req_data, $id);
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'));
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
class CreateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return User::$rules;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return User::$rules;
}
}

View File

@ -63,7 +63,8 @@ class User extends Authenticatable
'airline_id', 'airline_id',
'home_airport_id', 'home_airport_id',
'curr_airport_id', 'curr_airport_id',
'rank_id' 'rank_id',
'active',
]; ];
/** /**
@ -85,6 +86,15 @@ class User extends Authenticatable
'timezone' => 'integer', 'timezone' => 'integer',
]; ];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
];
public function pilot_id() public function pilot_id()
{ {
return $this->airline->icao.str_pad($this->id, 3, '0', STR_PAD_LEFT); return $this->airline->icao.str_pad($this->id, 3, '0', STR_PAD_LEFT);

View File

@ -0,0 +1,22 @@
<?php
namespace App\Repositories;
use App\Models\User;
class UserRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'email'
];
/**
* Configure the Model
**/
public function model()
{
return User::class;
}
}

View File

@ -20,6 +20,7 @@ users:
flight_time: 43200 flight_time: 43200
created_at: now created_at: now
updated_at: now updated_at: now
active: 1
- id: 2 - id: 2
name: Carla Walters name: Carla Walters
email: carla.walters68@example.com email: carla.walters68@example.com
@ -32,6 +33,7 @@ users:
flight_time: 43200 flight_time: 43200
created_at: now created_at: now
updated_at: now updated_at: now
active: 1
- id: 3 - id: 3
name: Raymond Pearson name: Raymond Pearson
email: raymond.pearson56@example.com email: raymond.pearson56@example.com
@ -44,6 +46,7 @@ users:
flight_time: 43200 flight_time: 43200
created_at: now created_at: now
updated_at: now updated_at: now
active: 0
role_user: role_user:
- user_id: 1 - user_id: 1

View File

@ -7,7 +7,7 @@
<div class="form-group col-sm-6"> <div class="form-group col-sm-6">
{!! Form::label('subfleet_id', 'Subfleet:') !!} {!! Form::label('subfleet_id', 'Subfleet:') !!}
{!! Form::select('subfleet_id', $subfleets, null, ['class' => 'form-control', 'placeholder' => 'Select Subfleet']) !!} {!! Form::select('subfleet_id', $subfleets, null, ['class' => 'form-control select2', 'placeholder' => 'Select Subfleet']) !!}
</div> </div>
<!-- Registration Field --> <!-- Registration Field -->

View File

@ -18,7 +18,7 @@
- -
@endif @endif
</td> </td>
<td><a href="{!! route('admin.aircraft.show', [$ac->id]) !!}">{!! $ac->name !!}</a></td> <td><a href="{!! route('admin.aircraft.edit', [$ac->id]) !!}">{!! $ac->name !!}</a></td>
<td>{!! $ac->registration !!}</td> <td>{!! $ac->registration !!}</td>
<td style="text-align: center;"> <td style="text-align: center;">
@if($ac->active == 1) @if($ac->active == 1)

View File

@ -8,7 +8,9 @@
<div class="sidebar-wrapper"> <div class="sidebar-wrapper">
<div class="logo" style="background: #067ec1; margin: 0px; text-align: center; min-height: 74px;"> <div class="logo" style="background: #067ec1; margin: 0px; text-align: center; min-height: 74px;">
<img src="/assets/frontend/img/logo_blue_bg.svg" width="110px" style=""> <a href="{!! url('/dashboard') !!}">
<img src="/assets/frontend/img/logo_blue_bg.svg" width="110px" style="">
</a>
</div> </div>
<ul class="nav"> <ul class="nav">

View File

@ -0,0 +1,24 @@
@extends('admin.app')
@section('content')
<section class="content-header">
<h1>
Airlines
</h1>
</section>
<div class="content">
@include('adminlte-templates::common.errors')
<div class="box box-primary">
<div class="box-body">
<div class="row">
{!! Form::open(['route' => 'admin.airlines.store']) !!}
@include('admin.airlines.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,12 @@
@extends('admin.app')
@section('title', "Edit \"$user->name\"")
@section('content')
<div class="card border-blue-bottom">
<div class="content">
@include('adminlte-templates::common.errors')
{!! Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'patch']) !!}
@include('admin.users.fields')
{!! Form::close() !!}
</div>
</div>
@endsection

View File

@ -0,0 +1,62 @@
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-sm-6">
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('password', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group col-sm-6">
{!! Form::label('home_airport_id', 'Home Airport:') !!}
{!! Form::select('home_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
<br /><br />
{!! Form::label('curr_airport_id', 'Current Airport:') !!}
{!! Form::select('curr_airport_id', $airports, null , ['class' => 'form-control select2']) !!}
</div>
</div>
<div class="row">
<div class="form-group col-sm-4">
{!! Form::label('airline_id', 'Airline:') !!}
{!! Form::select('airline_id', $airlines, null, ['class' => 'form-control select2', 'placeholder' => 'Select Airline']) !!}
</div>
<div class="form-group col-sm-4">
{!! Form::label('rank_id', 'Rank:') !!}
{!! Form::select('rank_id', $ranks, null, ['class' => 'form-control select2', 'placeholder' => 'Select Rank']) !!}
</div>
<div class="form-group col-sm-4">
{!! Form::label('roles', 'Roles:') !!}
{!! Form::select('roles[]', $roles, $user->roles->pluck('id'),
['class' => 'form-control select2', 'placeholder' => 'Select Roles', 'multiple']) !!}
</div>
</div>
<div class="row">
<div class="form-group col-sm-6">
{!! Form::label('active', 'Active:') !!}
<label class="checkbox-inline">
{!! Form::hidden('active', 0, false) !!}
{!! Form::checkbox('active', 1, null) !!}
</label>
</div>
<!-- Submit Field -->
<div class="form-group col-sm-6">
<div class="pull-right">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
<a href="{!! route('admin.users.index') !!}" class="btn btn-default">Cancel</a>
</div>
</div>
</div>

View File

@ -0,0 +1,17 @@
@extends('admin.app')
@section('title', 'Users')
@section('actions')
<li>
<a href="{!! route('admin.users.create') !!}">
<i class="ti-plus"></i>
Add New</a>
</li>
@endsection
@section('content')
<div class="card">
@include('admin.users.table')
</div>
@endsection

View File

@ -0,0 +1,19 @@
@extends('admin.app')
@section('content')
<section class="content-header">
<h1>
Airlines
</h1>
</section>
<div class="content">
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
@include('admin.airlines.show_fields')
<a href="{!! route('admin.airlines.index') !!}" class="btn btn-default">Back</a>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,40 @@
<!-- Code Field -->
<div class="form-group">
{!! Form::label('icao', 'ICAO:') !!}
<p>{!! $airlines->icao !!}</p>
</div>
<div class="form-group">
{!! Form::label('iata', 'IATA:') !!}
<p>{!! $airlines->iata !!}</p>
</div>
<!-- Name Field -->
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
<p>{!! $airlines->name !!}</p>
</div>
<div class="form-group">
{!! Form::label('logo', 'Logo URL:') !!}
<p>{!! $airlines->logo !!}</p>
</div>
<!-- Active Field -->
<div class="form-group">
{!! Form::label('active', 'Active:') !!}
<p>{!! $airlines->active !!}</p>
</div>
<!-- Created At Field -->
<div class="form-group">
{!! Form::label('created_at', 'Created At:') !!}
<p>{!! $airlines->created_at !!}</p>
</div>
<!-- Updated At Field -->
<div class="form-group">
{!! Form::label('updated_at', 'Updated At:') !!}
<p>{!! $airlines->updated_at !!}</p>
</div>

View File

@ -0,0 +1,30 @@
<table class="table table-hover table-responsive" id="airlines-table">
<thead>
<th>Name</th>
<th>Email</th>
<th class="text-center">Active</th>
<th></th>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{!! $user->name !!}</td>
<td>{!! $user->email !!}</td>
<td class="text-center">
@if($user->active == 1)
<span class="label label-success">Active</span>
@else
<span class="label label-default">Inactive</span>
@endif
</td>
<td class="text-right">
{!! Form::open(['route' => ['admin.users.destroy', $user->id], 'method' => 'delete']) !!}
<a href="{!! route('admin.users.edit', [$user->id]) !!}"
class='btn btn-sm btn-success btn-icon'><i class="fa fa-pencil-square-o"></i></a>
{!! Form::button('<i class="fa fa-times"></i>', ['type' => 'submit', 'class' => 'btn btn-sm btn-danger btn-icon', 'onclick' => "return confirm('Are you sure?')"]) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>

View File

@ -12,7 +12,6 @@ Route::group([
Route::match(['get', 'put'], 'airports/fuel', 'AirportController@fuel'); Route::match(['get', 'put'], 'airports/fuel', 'AirportController@fuel');
Route::resource('airports', 'AirportController'); Route::resource('airports', 'AirportController');
#Route::resource('aircraftclasses', 'AircraftClassController');
Route::resource('fares', 'FareController'); Route::resource('fares', 'FareController');
# subfleet # subfleet
@ -39,6 +38,8 @@ Route::group([
Route::resource('pireps', 'PirepController'); Route::resource('pireps', 'PirepController');
Route::resource('pirepfields', 'PirepFieldController'); Route::resource('pirepfields', 'PirepFieldController');
Route::resource('users', 'UserController');
# defaults # defaults
Route::get('', ['uses' => 'DashboardController@index']); Route::get('', ['uses' => 'DashboardController@index']);
Route::get('/', ['uses' => 'DashboardController@index']); Route::get('/', ['uses' => 'DashboardController@index']);