mirror of
https://github.com/Aviortheking/CA_LARAVEL.git
synced 2025-06-06 06:59:56 +00:00
fix migration && add seeder && add is_admin
This commit is contained in:
parent
cae091e1ec
commit
7f4c5d47df
@ -1,40 +1,125 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Providers\RouteServiceProvider;
|
||||
|
||||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
|
||||
|
||||
class LoginController extends Controller
|
||||
|
||||
{
|
||||
|
||||
/*
|
||||
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
| Login Controller
|
||||
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
|
|
||||
|
||||
| This controller handles authenticating users for the application and
|
||||
|
||||
| redirecting them to your home screen. The controller uses a trait
|
||||
|
||||
| to conveniently provide its functionality to your applications.
|
||||
|
||||
|
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
use AuthenticatesUsers;
|
||||
|
||||
/**
|
||||
* Where to redirect users after login.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
|
||||
* Where to redirect users after login.
|
||||
|
||||
*
|
||||
* @return void
|
||||
|
||||
* @var string
|
||||
|
||||
*/
|
||||
|
||||
protected $redirectTo = '/home';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Create a new controller instance.
|
||||
|
||||
*
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
|
||||
{
|
||||
|
||||
$this->middleware('guest')->except('logout');
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function login(Request $request)
|
||||
|
||||
{
|
||||
|
||||
$input = $request->all();
|
||||
|
||||
|
||||
|
||||
$this->validate($request, [
|
||||
|
||||
'email' => 'required|email',
|
||||
|
||||
'password' => 'required',
|
||||
|
||||
]);
|
||||
|
||||
|
||||
|
||||
if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password'])))
|
||||
|
||||
{
|
||||
|
||||
if (auth()->user()->is_admin == 1) {
|
||||
|
||||
return redirect()->route('admin.home');
|
||||
|
||||
}else{
|
||||
|
||||
return redirect()->route('home');
|
||||
|
||||
}
|
||||
|
||||
}else{
|
||||
|
||||
return redirect()->route('login')
|
||||
|
||||
->with('error','Email-Address And Password Are Wrong.');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
36
app/Http/Controllers/HomeController.php
Normal file
36
app/Http/Controllers/HomeController.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application dashboard.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Support\Renderable
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('home');
|
||||
}
|
||||
|
||||
public function adminHome()
|
||||
|
||||
{
|
||||
|
||||
return view('adminHome');
|
||||
|
||||
}
|
||||
}
|
@ -51,16 +51,27 @@ class Kernel extends HttpKernel
|
||||
* @var array
|
||||
*/
|
||||
protected $routeMiddleware = [
|
||||
|
||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||
|
||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||
|
||||
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
|
||||
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
|
||||
|
||||
'can' => \Illuminate\Auth\Middleware\Authorize::class,
|
||||
|
||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
|
||||
|
||||
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
||||
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
|
||||
'is_admin' => \App\Http\Middleware\IsAdmin::class,
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
|
47
app/Http/Middleware/IsAdmin.php
Normal file
47
app/Http/Middleware/IsAdmin.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
|
||||
|
||||
use Closure;
|
||||
|
||||
|
||||
|
||||
class IsAdmin
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
* Handle an incoming request.
|
||||
|
||||
*
|
||||
|
||||
* @param \Illuminate\Http\Request $request
|
||||
|
||||
* @param \Closure $next
|
||||
|
||||
* @return mixed
|
||||
|
||||
*/
|
||||
|
||||
public function handle($request, Closure $next)
|
||||
|
||||
{
|
||||
|
||||
if(auth()->user()->is_admin == 1){
|
||||
|
||||
return $next($request);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return redirect(‘home’)->with(‘error’,"You don't have admin access.");
|
||||
|
||||
}
|
||||
|
||||
}
|
77
app/Http/User.php
Normal file
77
app/Http/User.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
namespace App;
|
||||
|
||||
|
||||
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
|
||||
|
||||
class User extends Authenticatable
|
||||
|
||||
{
|
||||
|
||||
use Notifiable;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
*
|
||||
|
||||
* @var array
|
||||
|
||||
*/
|
||||
|
||||
protected $fillable = [
|
||||
|
||||
'name', 'email', 'password', 'is_admin'
|
||||
|
||||
];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* The attributes that should be hidden for arrays.
|
||||
|
||||
*
|
||||
|
||||
* @var array
|
||||
|
||||
*/
|
||||
|
||||
protected $hidden = [
|
||||
|
||||
'password', 'remember_token',
|
||||
|
||||
];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* The attributes that should be cast to native types.
|
||||
|
||||
*
|
||||
|
||||
* @var array
|
||||
|
||||
*/
|
||||
|
||||
protected $casts = [
|
||||
|
||||
'email_verified_at' => 'datetime',
|
||||
|
||||
];
|
||||
|
||||
}
|
@ -1,36 +1,73 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
|
||||
|
||||
class CreateUsersTable extends Migration
|
||||
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('name');
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
|
||||
* Run the migrations.
|
||||
|
||||
*
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
public function down()
|
||||
|
||||
public function up()
|
||||
|
||||
{
|
||||
Schema::dropIfExists('users');
|
||||
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
|
||||
$table->bigIncrements('id');
|
||||
|
||||
$table->string('name');
|
||||
|
||||
$table->string('email');
|
||||
|
||||
// $table->timestamp('email_verified_at')->nullable();
|
||||
|
||||
$table->boolean('is_admin')->nullable();
|
||||
|
||||
$table->string('password');
|
||||
|
||||
$table->rememberToken();
|
||||
|
||||
$table->timestamps();
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Reverse the migrations.
|
||||
|
||||
*
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
public function down()
|
||||
|
||||
{
|
||||
|
||||
Schema::dropIfExists('users');
|
||||
|
||||
}
|
||||
|
||||
}
|
67
database/seeds/CreateUsersSeeder.php
Normal file
67
database/seeds/CreateUsersSeeder.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
use App\User;
|
||||
|
||||
|
||||
|
||||
class CreateUsersSeeder extends Seeder
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
* Run the database seeds.
|
||||
|
||||
*
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
public function run()
|
||||
|
||||
{
|
||||
|
||||
$user = [
|
||||
|
||||
[
|
||||
|
||||
'name'=>'Admin',
|
||||
|
||||
'email'=>'admin@admin.fr',
|
||||
|
||||
'is_admin'=>'1',
|
||||
|
||||
'password'=> bcrypt('123456'),
|
||||
|
||||
],
|
||||
|
||||
[
|
||||
|
||||
'name'=>'User',
|
||||
|
||||
'email'=>'user@itsolutionstuff.com',
|
||||
|
||||
'is_admin'=>'0',
|
||||
|
||||
'password'=> bcrypt('123456'),
|
||||
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
|
||||
|
||||
foreach ($user as $key => $value) {
|
||||
|
||||
User::create($value);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
31
resources/views/adminHome.blade.php
Normal file
31
resources/views/adminHome.blade.php
Normal file
@ -0,0 +1,31 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
|
||||
|
||||
@section('content')
|
||||
|
||||
<div class="container">
|
||||
|
||||
<div class="row justify-content-center">
|
||||
|
||||
<div class="col-md-8">
|
||||
|
||||
<div class="card">
|
||||
|
||||
<div class="card-header">Dashboard</div>
|
||||
|
||||
<div class="card-body">
|
||||
|
||||
You are Admin.
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
@endsection
|
@ -21,3 +21,4 @@ Route::post('contact-us', ['as' => 'contactus.store', 'uses' => 'ContactUSContro
|
||||
Auth::routes();
|
||||
|
||||
Route::get('/home', 'HomeController@index')->name('home');
|
||||
Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin');
|
Loading…
x
Reference in New Issue
Block a user