Skip to content

Commit 88e1e10

Browse files
author
Sandip Patel
committed
admin auth updates
1 parent 67413de commit 88e1e10

8 files changed

+222
-2
lines changed

src/Middleware/AdminAuthenticated.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PCB\Laravel\Middleware;
4+
5+
use Auth;
6+
use Closure;
7+
8+
class AdminAuthenticated
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
if( Auth::check() )
20+
{
21+
// allow admin to proceed with url
22+
if ( Auth::user()->hasAdminAccess() ) {
23+
return redirect(route(config('modules.auth.admin_redirect_route')));
24+
}
25+
}
26+
27+
abort(404);
28+
}
29+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PCB\Laravel\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Support\Facades\Auth;
7+
8+
class RedirectIfAuthenticated
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @param string|null $guard
16+
* @return mixed
17+
*/
18+
public function handle($request, Closure $next, $guard = null)
19+
{
20+
if( Auth::check() )
21+
{
22+
// allow admin to proceed with url
23+
if ( Auth::user()->hasAdminAccess() ) {
24+
return redirect(route(config('modules.auth.admin_redirect_route')));
25+
}
26+
27+
// if user is client take him to his dash
28+
else if ( Auth::user()->isUser() ) {
29+
return redirect(route(config('modules.auth.user_redirect_route')));
30+
}
31+
}
32+
33+
return $next($request);
34+
}
35+
}

src/Middleware/UserAuthenticated.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PCB\Laravel\Middleware;
4+
5+
use Auth;
6+
use Closure;
7+
8+
class UserAuthenticated
9+
{
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
if( Auth::check() )
20+
{
21+
// if admin take him to admin dash
22+
if ( Auth::user()->hasAdminAccess() ) {
23+
return redirect( route('admin_dashboard') );
24+
}
25+
26+
// if user is client take him to his dash
27+
else if ( Auth::user()->isUser() ) {
28+
return $next($request);
29+
}
30+
}
31+
32+
abort(404);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('users', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('name');
19+
$table->string('password');
20+
$table->string('email')->unique();
21+
$table->string('role')->default('user')->index();
22+
$table->rememberToken();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('users');
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreatePasswordResetsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('password_resets', function (Blueprint $table) {
17+
$table->string('email')->index();
18+
$table->string('token');
19+
$table->timestamp('created_at')->nullable();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('password_resets');
31+
}
32+
}

src/Model/BaseUser.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PCB\Laravel\Model;
4+
5+
use Illuminate\Notifications\Notifiable;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
8+
class BaseUser extends Authenticatable
9+
{
10+
use Notifiable;
11+
12+
protected $hidden = [ 'password', 'remember_token' ];
13+
protected $fillable = ['id', 'username', 'password', 'email', 'name', 'role'];
14+
15+
public static $roles = [
16+
'user' => 'User',
17+
'admin' => 'Admin',
18+
'super_admin' => 'Super Admin'
19+
];
20+
21+
public function isUser()
22+
{
23+
return $this->role === 'user';
24+
}
25+
public function isAdmin()
26+
{
27+
return $this->role == 'admin';
28+
}
29+
public function isSuperAdmin()
30+
{
31+
return $this->role === 'super_admin';
32+
}
33+
public function hasAdminAccess()
34+
{
35+
return ($this->role === 'admin' || $this->role === 'super_admin');
36+
}
37+
}

src/ServiceProvider.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ServiceProvider extends RouteServiceProvider
2323
*/
2424
public function boot()
2525
{
26-
$plugins = config('modules', []);
26+
$plugins = config('modules.enabled', []);
2727

2828
foreach ($plugins as $plugin => $desc)
2929
{
@@ -50,8 +50,15 @@ public function boot()
5050
}
5151
}
5252

53+
// Register Migrations
54+
$this->loadMigrationsFrom(__DIR__. '/Migrations');
5355
$this->publishes([
5456
__DIR__. '/config.php' => config_path('modules.php')
5557
]);
58+
59+
// Register Middlewares
60+
$this->aliasMiddleware('guest', Middleware\RedirectIfAuthenticated::class);
61+
$this->aliasMiddleware('admin', Middleware\AdminAuthenticated::class);
62+
$this->aliasMiddleware('user', Middleware\UserAuthenticated::class);
5663
}
5764
}

src/config.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,15 @@
77
*/
88

99
return [
10-
10+
11+
// Authentication related settings
12+
'auth' => [
13+
'user_redirect_route' => 'user_dashboard',
14+
'admin_redirect_route' => 'admin_dashboard'
15+
],
16+
17+
// List of enabled modules
18+
'enabled' => [
19+
20+
]
1121
];

0 commit comments

Comments
 (0)