Skip to content

Commit 136f2bf

Browse files
committed
added middleware
1 parent a860aa8 commit 136f2bf

File tree

20 files changed

+169
-12
lines changed

20 files changed

+169
-12
lines changed

src/Darryldecode/Backend/BackendServiceProvider.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
use Darryldecode\Backend\Base\Registrar\ComponentLoader;
44
use Darryldecode\Backend\Base\Registrar\Registrar;
55
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Routing\Router;
67
use Illuminate\Support\ServiceProvider;
78

89
class BackendServiceProvider extends ServiceProvider {
910

1011
/**
1112
* Register the service provider.
12-
*
13+
* @param Router $router \Illuminate\Contracts\Http\Kernel
1314
*/
14-
public function boot()
15+
public function boot(Router $router)
1516
{
1617
$this->loadViewsFrom(__DIR__.'/Base/Views', 'backend');
1718
$this->bootBackend();
1819

20+
$router->middleware(
21+
'backend.guest',
22+
'Darryldecode\Backend\Base\Middleware\RedirectIfAuthenticated'
23+
);
24+
25+
$router->middleware(
26+
'backend.authenticated',
27+
'Darryldecode\Backend\Base\Middleware\Authenticate'
28+
);
29+
1930
$this->publishes([
2031
__DIR__.'/Public/backend/cb' => public_path('darryldecode/backend/cb'),
2132
__DIR__.'/Public/backend/vendor' => public_path('darryldecode/backend/vendor'),

src/Darryldecode/Backend/Base/Controllers/BaseController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@
1212

1313
abstract class BaseController extends Controller {
1414

15+
/**
16+
* @var \Darryldecode\Backend\Components\User\Models\User
17+
*/
18+
protected $user;
19+
20+
public function __construct()
21+
{
22+
$app = app();
23+
$this->app = $app;
24+
$this->user = $app['auth']->user();
25+
}
1526
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Middleware;
4+
5+
use Closure;
6+
use Darryldecode\Backend\Utility\Helpers;
7+
use Illuminate\Contracts\Auth\Guard;
8+
9+
class Authenticate
10+
{
11+
/**
12+
* The Guard implementation.
13+
*
14+
* @var Guard
15+
*/
16+
protected $auth;
17+
18+
/**
19+
* Create a new filter instance.
20+
*
21+
* @param Guard $auth
22+
*/
23+
public function __construct(Guard $auth)
24+
{
25+
$this->auth = $auth;
26+
}
27+
28+
/**
29+
* Handle an incoming request.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @param \Closure $next
33+
* @return mixed
34+
*/
35+
public function handle($request, Closure $next)
36+
{
37+
if ($this->auth->guest()) {
38+
if ($request->ajax()) {
39+
return response('Unauthorized.', 401);
40+
} else {
41+
return Helpers::redirectLogin();
42+
}
43+
}
44+
45+
return $next($request);
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Darryldecode\Backend\Base\Middleware;
4+
5+
use Closure;
6+
use Darryldecode\Backend\Utility\Helpers;
7+
use Illuminate\Contracts\Auth\Guard;
8+
9+
class RedirectIfAuthenticated
10+
{
11+
/**
12+
* The Guard implementation.
13+
*
14+
* @var Guard
15+
*/
16+
protected $auth;
17+
18+
/**
19+
* Create a new filter instance.
20+
*
21+
* @param Guard $auth
22+
*/
23+
public function __construct(Guard $auth)
24+
{
25+
$this->auth = $auth;
26+
}
27+
28+
/**
29+
* Handle an incoming request.
30+
*
31+
* @param \Illuminate\Http\Request $request
32+
* @param \Closure $next
33+
* @return mixed
34+
*/
35+
public function handle($request, Closure $next)
36+
{
37+
if ($this->auth->check()) {
38+
return Helpers::redirectDashboard();
39+
}
40+
41+
return $next($request);
42+
}
43+
}

src/Darryldecode/Backend/Base/Views/includes/navigation.blade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<span class="icon-bar"></span>
88
<span class="icon-bar"></span>
99
</button>
10-
<a class="navbar-brand" href="#">BACKEND</a>
10+
<a class="navbar-brand" href="{{url(config('backend.backend.base_url').'/dashboard')}}">BACKEND</a>
1111
</div>
1212

1313
<!-- Collect the nav links, forms, and other content for toggling -->
@@ -28,6 +28,9 @@
2828
</ul>
2929

3030
</li>
31+
<li>
32+
<a href="{{url(config('backend.backend.base_url').'/logout')}}">Logout</a>
33+
</li>
3134
</ul>
3235
</div><!-- /.navbar-collapse -->
3336
</div>

src/Darryldecode/Backend/Components/Auth/Controllers/AuthController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@
44

55
use Darryldecode\Backend\Base\Controllers\BaseController;
66

7+
use Darryldecode\Backend\Utility\Helpers;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Auth;
910

1011
class AuthController extends BaseController {
1112

1213
public function __construct()
1314
{
14-
$this->middleware('guest', ['except' => 'getLogout']);
15+
parent::__construct();
16+
$this->middleware('backend.guest',array('except'=>'getLogout'));
1517
}
1618

1719
/**
1820
* displays the login page
1921
*
22+
* @param Request $request
2023
* @return \Illuminate\View\View
2124
*/
22-
public function getLogin()
25+
public function getLogin(Request $request)
2326
{
2427
return view('authManager::login');
2528
}

src/Darryldecode/Backend/Components/Auth/Controllers/PasswordController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PasswordController extends BaseController {
2222

2323
public function __construct()
2424
{
25-
$this->middleware('guest');
25+
parent::__construct();
2626
}
2727

2828
/**

src/Darryldecode/Backend/Components/Auth/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
});
1717

1818
// logout route
19-
Route::get(config('backend.backend.logout_route'), 'AuthController@getLogout');
19+
Route::get('logout', 'AuthController@getLogout');
2020

2121
// Password reset link request routes...
2222
Route::get('password/email', 'PasswordController@getEmail');

src/Darryldecode/Backend/Components/ContentBuilder/Controllers/ContentController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ContentController extends BaseController {
2929
*/
3030
public function __construct(Request $request, Response $response)
3131
{
32+
parent::__construct();
33+
$this->middleware('backend.authenticated');
3234
$this->request = $request;
3335
$this->response = $response;
3436
}

src/Darryldecode/Backend/Components/ContentBuilder/Controllers/ContentTaxonomyController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ContentTaxonomyController extends BaseController {
2929
*/
3030
public function __construct(Request $request, Response $response)
3131
{
32+
parent::__construct();
33+
$this->middleware('backend.authenticated');
3234
$this->request = $request;
3335
$this->response = $response;
3436
}

0 commit comments

Comments
 (0)