Home > Back-end >  How to restrict users to use application
How to restrict users to use application

Time:11-04

I am using laravel framework to develop api’s ,it’s an existing application .there is a requirement if more than 5users registered from 6th user onwards i have to restrict them to use application until they approved by manager or they paid for registration fee then only the user will allow to use the application.

Can anyone give me the idea how to acheive this scenario or suggest me any package in laravel

CodePudding user response:

Solution: You can add 'status' field in your table. Now when your api is registering a user, you can check the No. of users in the database. If more than or equals to 5, you can set the status to 0. Now show the manager list of user with status 0 and when the status changes you can use the application.

Make sure to add condition where status = 1 when user is getting logged in.

I hope it helps!

CodePudding user response:

Well, you can just put a isApproved column to indicate if the user is already approved or just like the email_verified_at that accepts timestamp as a value then create a middleware where you can check if the user is approved or not. then add a method to the user model to check if the user is approve :

User model

class User extends Authenticatable
{
    public function isApproved()
    {
        // check if the account_approved_at column is not null.
        return ! is_null($this->account_approved_at);
    }
}

Middleware

class EnsureUserIsApproved
{
    public function handle(Request $request, Closure $next)
    {
         if(! $request->user()->isApproved()) {
             // can also use abort(403) instead of redirect
             return redirect('route-where-you-want-the-user-to-redirect') 
         }
        
         return $next($request);
    }
} 

You can check this for more information about middleware

  • Related