Support the ongoing development of Laravel.io →
posted 9 years ago
Security
Last updated 2 years ago.
0

Hi,

This can be solved pretty easily by using Middleware (http://laravel.com/docs/5.0/middleware)

First lets create the middleware, you can call it whatever, let's say AdminMiddleware:

php artisan make:middleware AdminMiddleware

Now that we have our middleware, we need to edit it and specify what we want it to do.

In App\Http\Middleware you should see the newly created file

<?php namespace App\Http\Middleware;

use Closure;

class AdminMiddleware {

	/**
	 * Handle an incoming request.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Closure  $next
	 * @return mixed
	 */
	public function handle($request, Closure $next)
	{
		if ($request->user()->type != 'A')
		{
			return redirect('home');
		}

		return $next($request);
	}

}

What we are doing here, is taking the user and checking to see if the type is A if not.. redirect home.

Now that we have that, we need to use it in the routes.php file.

Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function()
{
    Route::get('/admin', function()
    {
        // can only access this if type == A
    });

});

Hopefully that helps!

Last updated 9 years ago.
0

willstumpf an absolute legend thanks, so much works perfectly!

Really appreciate you help. :)

0

drc83 said:

willstumpf an absolute legend thanks, so much works perfectly!

Really appreciate you help. :)

How did you manage to implement checking of user type?

    Auth::user()->type;
Last updated 9 years ago.
0

orsic said:

drc83 said:

willstumpf an absolute legend thanks, so much works perfectly!

Really appreciate you help. :)

How did you manage to implement checking of user type?

   Auth::user()->type;

I'm using "zizaco/entrust" for user types

0

for me the redirect make a problem, This webpage has a redirect loop

0

Sign in to participate in this thread!

Eventy

Your banner here too?

drc83 drc83 Joined 16 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.