0

I have 3 types of users Admin, Vendor, Customer in my laravel E-commerce.

Now I have to restrict routes for those vendors who are not approved by admin. Can someone please tell me the process, please?

here are the two tables users and shops.

users have those attributes

id, name, password, email, phone

and shops have

shop_name, address, shop_phone, owner_id (this is a foreign key of users.id), is_approved(bool)
  • users hasOne shop relation are given here.

Now I need to know How can I restrict all auth routes who are not approved means is_approved==0 for shops.

Can Anyone please help me?

tariqul anik
  • 314
  • 6
  • 24
  • You can define your own middleware, and then assign it to those routes like auth. See this: https://stackoverflow.com/a/30643915/6908226 – iazaran Aug 31 '20 at 21:28
  • 1
    Please include the routes as it is hard to grasp what you are trying to do without em. What will happen in the case where an user has two shops? – mrhn Aug 31 '20 at 21:30
  • i want to add a middleware in construct. if it is possible that it it is vendor but user()->shops()->is_activated == 0 then it return to login page. not going to next. like handle() . Is it possible? – tariqul anik Aug 31 '20 at 21:36
  • Yes it is possible, but it needs to use similar way again. Instead of this `$this->middleware('newMiddleware');` use this: `$this->middleware(function ($request, $next) { // ... });` See this section: https://laravel.com/docs/7.x/controllers#controller-middleware – iazaran Aug 31 '20 at 21:47

1 Answers1

1

Create a middleware:

php artisan make:middleware CheckIsApproved

A new middleware class will be created in app/Http/Middleware/CheckIsApproved.php fille.

Then in thehandle method of the middle, you can do the check:

public function handle ($request, Closure $next)
{
    if (auth()->user()->shop->is_approved) {
        return $next($request);
    }

    return back()->with('error', 'Unauthorized');
}

(You may need to customize the condition in theif according to the relationship)

In the file app/Http/Kernel.php, register the middleware: search $routeMiddleware property and add:

protected $routeMiddleware = [
    //...
    'isApproved' => \App\Http\Middleware\CheckIsApproved::class,
];

Then, in your route definition, you can specify the middleware:

Route::get('/some-route', 'SomeRouteController@show')->middleware('isApproved');

Check the docs for more insight

Prince Dorcis
  • 955
  • 7
  • 7
  • 1
    @Iagbox: Not exactly. Here, the `shop` method on the `User` model is supposed to define a relationship between the `User` and the `Shop` models. – Prince Dorcis Sep 01 '20 at 01:24
  • 2
    You are right. I mean `auth()->user()->shop->is_approved`. I've corrected the answer. – Prince Dorcis Sep 01 '20 at 05:10
  • 1
    nice, just stopping the next question being some error about that that they can't resolve :) thank you for putting up an answer to their question btw, always appreciated – lagbox Sep 01 '20 at 05:13
  • 1
    Thanks for your answer brother. It's work for me. Thanks a lot. :) – tariqul anik Sep 02 '20 at 18:30