how to check whether a restaurant is verified in laravel - laravel

I am new to laravel and trying to make a panel for food delivery
I have used Laravel default Registration and Login for User Category--Restaurant
and then after user login , the user can Add restaurant details using route (/add_details)
once the user has added restaurant details the user should not be able to go to that route (/add_details)
this will depend on a column in restaurant table (is_verified)
how do i check that
I was thinking of using a Laravel middleware
but then i was stuck how laravel middleware $request variable works
how can i get column value in middleware and verify it
or if any other simple but effective solution
as
i will be using it in sidebar.blade.php as well
so that i can hide the menu

I made a middleware and added it to kernel.php and is using it in routes
Its working fine
but i want to ask is this the right way i have done it
Route::get('/manage_cuisines', 'RestaurantCuisineController#create')->name('manage-cuisines')->middleware('restaurant_verified');
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use \App\User;
use \App\Restaurant;
class CheckRestaurantVerification
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$restaurant = Restaurant::find(User::find(Auth::id())->restaurant_id);
if($restaurant->is_verified == 0)
{
return redirect('home');
}
return $next($request);
}
}

Related

Make login redirect to a different Route based on a condition Laravel 8

I am trying to make the login redirect when loggin in successful not to home when a variable called estado = 2 but to the reset password view, not because the login fails but because when the variable estado of the user equals 2 that means that it has the default password and I want to make the first time the user logs to change his password because of security reasons. (Default password is the same as the username thats why I plan to make mandatory to change the password the first time you log in).
I'm new in Laravel so not sure where I need to do the changes to manage to do that but I think its in the middleware RedirectIfAuthenticated.
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #param string|null ...$guards
* #return mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
}
If I didn't make clear with what I need I can explain it better, english is not my main language so sorry for any issues. After I change the password with the forgot me functionality I make the estado variable change to 1 and I plan to resue it for this so that in future logs in of the user it can log in like normal and go to home.
The line:
return redirect(RouteServiceProvider::HOME);
Home is a constant /home
You can add if condition on this line:
If Auth::user()->estado == 2
return redirect(“your reset password route”);
However a better way to approach this is to create a new middleware and put this condition in it and protect all the authenticated routes with this middleware.

Laravel give user access to specific route when conditions are met

Laravel 5
How to give user access to specific route when certain conditions are met?
For example let user access
Route::get(view('posts/{id}'),'PostsController#show');
when user has over 100 points in his user->points column.
You can use Middleware for this,In Laravel it is very easy to secure your routes by creating your own middlewares.
The following steps are required to do this:
run command php artisan make:middleware Middlewarename and you'll find your middleware inside app/Http/Middleware/yourcustomemiddleware.php
Register your middleware in app/Http/kernel.php file which you just created
Now implement logic in middleware you just created:
YourMiddlewareClassCode:
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::user()->points >= 100)
{
return $next($request);
}
return redirect()->back()->with('flash_message','you are not allowed to access this');
}
Attach middleware to your route:
routes/web.php:
Route::get(view('posts/{id}'),'PostsController#show')->middleware('yourcustommiddleware');
All done now your route is secured.
Summary: this statement return $next($request); in middleware will return the route when condition is matched else it will redirect to the previous route.
Note: I don't know your db structure and also this is just an example to show you that what is middleware and how it works and how you can use it.

Role Checking Before Access

I'm attempting to make a User System from Laravel, and I have a plan to give certain users "BETA Tester" role so that they can access the beta side of the site.
However, I am unsure if this is even possible and how I would even go about doing it.
The sort of plan i'm looking for is 'Navigation To Beta Section Of The Site > Big Log In [SKIP IF ALREADY LOGGED IN] > Check If The User Has The "BETA Tester" role > If Yes Send Them To The Beta Site / If No Tell Them They Do Not Have Access'
Is this possible?
Create a custom middleware class that checks the user's role:
<?php
namespace App\Http\Middleware;
use Closure;
class Checkrole
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
// hasRole being a function defined on your User model that checks
// a user's assigned role(s).
if (auth()->check() && auth()->user()->hasRole('BETA Tester')) {
return $next($request);
}
abort(401, 'You are not allowed to access this page');
}
}

New registered user to be redirected to the password reset screen

I'm quite new to Laravel and have been stumped on a problem for 2 days - I'd be grateful for some guidance.
I'm using the default out-of-the-box User authentication system with Laravel 5.3. A new user is created automatically behind the scenes by an existing Admin user - I will in time hide the user registration page. I have also successfully set up middleware to check if a user is newly registered (by looking for a null 'last_logged_in_date' that I've added to the migration).
All I want to happen is for a new registered user to be redirected to the password reset screen that ships with Laravel (again, in time I will create a dedicated page). I would like this to happen within the middleware file. So far, my middleware looks like this:
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Controllers\Auth;
class CheckIfNewUser
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = $request->user();
if (! is_null($user->last_logged_in_date )) {
return $next($request);
}
// This is where I'm stuck!!!
}
}
I'm not sure what code to enter at the location indicated by the comments above. I've tried sendResetLinkEmail($request); etc and have imported what I though were the correct classes but I always end up with a Call to undefined function App\Http\Middleware\sendResetLinkEmail() message irregardless of what I 'use' at the top of my class.
Where am I going wrong? Thanks!
Well that happens because you have not defined your sendResetLinkEmail($request) function yet. You can do it like this, or you can create a new class with that and then call the class.
Call the trait SendsPasswordResetEmails and then access it with $this since traits are not classes and you cannot access their members directly.
<?php
namespace App\Http\Middleware;
use Closure;
use App\Http\Controllers\Auth;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class CheckIfNewUser
{
use SendsPasswordResetEmails;
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
$user = $request->user();
if (! is_null($user->last_logged_in_date )) {
return $next($request);
}
// This is where I'm stuck!!!
//EDIT
//return $this->SendsPasswordResetEmails->sendResetLinkEmail($request);
return $this->sendResetLinkEmail($request);
}
}

Laravel 5.2 subdomain routing, depending on user role.

I have some problems with subdomain routing in laravel 5.2 and hope you can help me with it.
The point is that I need to redirect a user on certain subdomain, depending on it's usertype.
For example in database I have a usertype (1,2,3 etc...) and basing on that value I need to redirect user on
type1.mysite.com
type2.mysite.com
type3.mysite.com
etc...
But the problem is that I can't get authenticated user in routes.php, it always returns null.
Any ideas on how to solve that problem?
And by the way, to make a subdomain routing, I have to configure apache in some way or it can be done with laravel?
Thanks for the answers!
you need to edit it and specify what we want it to do.
In App\Http\Middleware you should see the newly created file
php artisan make:middleware UserTypeMiddleware
<?php namespace App\Http\Middleware;
use Closure;
class UserTypeMiddleware {
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
//check your user type here
if ($request->user()->type != 1)
{
return redirect('DefinedRoute');
}
return $next($request);
}
}

Resources