将用户限制为Laravel中的一次和多次登录

问题描述 投票:0回答:1

我是Laravel的初学者。我想根据用户角色来限制用户会话/登录。因此,我的登录控制器中有此方法。

我有两个角色

  1. 基本
  2. 情侣
public function authenticated(Request $request, User $user)
{
    if(Auth::check()) { //check if the user is logged in or not
        $user = Auth::user();

        if ($user->isBasic()) {
            $previous_session = $user->session_id;

            if ($previous_session) {
                \Session::getHandler()->destroy($previous_session);
            }

            Auth::user()->session_id = \Session::getId();
            Auth::user()->save();
            return redirect(route('home'));
        } elseif ($user->isCouple()) { //check if the user is logged in or not
            $previous_session = $user->session_id;
            $login = $user->no_of_logins;

            if ($previous_session) {
                if($login > 2) {
                    \Session::getHandler()->destroy($previous_session);
                    Auth::user()->no_of_logins = $user->decrement('no_of_logins');
                }
            }

            Auth::user()->session_id = \Session::getId();
            Auth::user()->no_of_logins = $user->increment('no_of_logins');
            Auth::user()->save();
            return redirect(route('home'));
        }
    }
}
}

elseif语句对我不起作用。我不知道该怎么办。

在我的桌子上,我有这个

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->enum('role', ['subscriber', 'admin', 'basic', 'couple', 'family'])->default('subscriber');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('avatar')->nullable();
        $table->integer('no_of_logins')->default(0);
        $table->string('session_id')->nullable();
        $table->string('password')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}
php laravel
1个回答
0
投票

我能够通过创建会话表来解决它

php artisan session:table

composer dump-autoload

php artisan migrate

然后在我的控制器中

public function authenticated(Request $request,User $user)
    {

        if(Auth::check())

        { //check if the user is logged in or not
            $user = Auth::user();
          //  $login = Session::where('user_id', Auth::id())->count();
          $login = DB::table('sessions')->where('user_id', Auth::id())->count();
           // dd($login);
            if ($user->isBasic()) 
            {
                if ($login > 0)
                {
                    Auth::logout();    
                    session()->flash('logout', "You are Logged in on other devices");
                    return redirect('login');
                }

                return redirect(route('welcome'));
            }

            elseif ($user->isCouple()) 
            {
                if ($login > 1)
                {
                    Auth::logout();    
                    session()->flash('logout', "You are Logged in on other devices");
                    return redirect('login');
                }

                return redirect(route('welcome'));
            }

            elseif ($user->isFamily()) 
            {
                if ($login > 5)
                {
                    Auth::logout();    
                    session()->flash('logout', "You are Logged in on other devices");
                    return redirect('login');
                }

                return redirect(route('welcome'));

                }
            }

            else 
            {
                return redirect(route('welcome'));
            }
        } 
© www.soinside.com 2019 - 2024. All rights reserved.