pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

通過 Laravel 中的用戶 ID 強制注銷特定用戶

Force logout of specific user by user id in Laravel(通過 Laravel 中的用戶 ID 強制注銷特定用戶)
本文介紹了通過 Laravel 中的用戶 ID 強制注銷特定用戶的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我使用 Laravel 5.2,我想知道如何強制用戶通過 id 注銷.我正在構建一個管理面板,可以選擇停用當前登錄到 Web 應用程序的特定用戶.Laravel 為當前用戶提供了這個選項.

I use Laravel 5.2, and I want to know how to force a user to log out by id. I'm building an admin panel with the option to deactivate a specific user that is currently logged in to the web application. Laravel gives you this option for a current user.

Auth::logout()

但我不想注銷當前用戶,因為我是經過身份驗證的用戶.因此,我需要通過特定用戶的 id 強制退出該用戶.就像我們使用特定 id 登錄用戶一樣.

But I don't want to log out the current user, as I am an authenticated user. So I need to force log out of a specific user by its id. Just like when we log in a user with a specific id.

Auth::loginUsingId($id);

有沒有類似的東西?

Auth::logoutUsingId($id);

推薦答案

目前,沒有直接的方法可以做到這一點;作為 StatefulGuard 合約及其 SessionGuard 實現不像 logoutUsingId()Auth/SessionGuard.php#L507" rel="noreferrer">登錄.

Currently, there's no straightforward way to do this; As the StatefulGuard contract and its SessionGuard implementation don't offer a logoutUsingId() as they do for login.

您需要在用戶表中添加一個新字段,并在您希望特定用戶注銷時將其設置為 true.然后使用中間件檢查當前用戶是否需要強制注銷.

You need to add a new field to your users table and set it to true when you want a specific user to be logged out. Then use a middleware to check if the current user needs a force logout.

這是一個快速實現.

讓我們為users表遷移類添加一個新字段:

Let's add a new field to users table migration class:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            // ...
            $table->boolean('logout')->default(false);
            // other fields...
        });
    }

    // ...
}

確保在更改遷移后運行 php artisan migrate:refresh [--seed].

Make sure you run php artisan migrate:refresh [--seed] after changing the migration.

讓我們創建一個新的中間件:

php artisan make:middleware LogoutUsers

以下是檢查用戶是否需要被踢出的邏輯:

Here's the logic to check if a user needs to be kicked out:

<?php

namespace AppHttpMiddleware;

use Auth;
use Closure;

class LogoutUsers
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        // You might want to create a method on your model to
        // prevent direct access to the `logout` property. Something
        // like `markedForLogout()` maybe.
        if (! empty($user->logout)) {
            // Not for the next time!
            // Maybe a `unmarkForLogout()` method is appropriate here.
            $user->logout = false;
            $user->save();

            // Log her out
            Auth::logout();

            return redirect()->route('login');
        }

        return $next($request);
    }
}

3.在HTTP內核中注冊中間件

打開app/Http/Kernel.php 并添加中間件的 FQN:

3. Register the middleware in HTTP kernel

Open up the app/Http/Kernel.php and add your middleware's FQN:

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        AppHttpMiddlewareVerifyCsrfToken::class,
        IlluminateRoutingMiddlewareSubstituteBindings::class,
        AppHttpMiddlewareLogoutUsers::class, // <= Here
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

這是未經測試的代碼,但它應該給你一個想法.將幾個 API 方法添加到您的 User 模型以配合此功能是一個很好的做法:

It's untested code, but it should give you the idea. It'd be a good practice to add a couple of API methods to your User model to accompany with this functionality:

  • markedForLogout() :檢查用戶的 logout 標志.
  • markForLogout() :將用戶的 logout 標志設置為 true.
  • unmarkForLogout() :將用戶的 logout 標志設置為 false.
  • markedForLogout() : Checks user's logout flag.
  • markForLogout() : Sets user's logout flag to true.
  • unmarkForLogout() : Sets user's logout flag to false.

然后在管理方面(我想這是您的情況),您只需要在特定用戶模型上調用 markForLogout() 即可在下一個請求中將其踢出.或者,如果模型對象不可用,您可以使用查詢構建器來設置標志:

Then on the administration side (I suppose it's your case), you just need to call markForLogout() on the specific user model to kick him out on the next request. Or you can utilize the query builder to set the flag, if the model object is not available:

User::where('id', $userId)
    ->update(['logout' => true]);

它可以是一個 markForLogoutById($id) 方法.

It can be a markForLogoutById($id) method.

相關討論
[提案] 通過 ID 注銷用戶
刪除登錄用戶時的多個語句

這篇關于通過 Laravel 中的用戶 ID 強制注銷特定用戶的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 吸污车_吸粪车_抽粪车_电动三轮吸粪车_真空吸污车_高压清洗吸污车-远大汽车制造有限公司 | 注塑_注塑加工_注塑模具_塑胶模具_注塑加工厂家_深圳环科 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 定制防伪标签_防伪标签印刷_防伪标签厂家-510品保防伪网 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 同步带轮_同步带_同步轮_iHF合发齿轮厂家-深圳市合发齿轮机械有限公司 | 蓄电池在线监测系统|SF6在线监控泄露报警系统-武汉中电通电力设备有限公司 | 小小作文网_中小学优秀作文范文大全 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 高速龙门架厂家_监控杆_多功能灯杆_信号灯杆_锂电池太阳能路灯-鑫世源照明 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 江苏远邦专注皮带秤,高精度皮带秤,电子皮带秤研发生产 | 防水试验机_防水测试设备_防水试验装置_淋雨试验箱-广州岳信试验设备有限公司 | NBA直播_NBA直播免费观看直播在线_NBA直播免费高清无插件在线观看-24直播网 | 江苏皓越真空设备有限公司| 齿辊分级破碎机,高低压压球机,立式双动力磨粉机-郑州长城冶金设备有限公司 | 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 皮带式输送机械|链板式输送机|不锈钢输送机|网带输送机械设备——青岛鸿儒机械有限公司 | CE认证_FCC认证_CCC认证_MFI认证_UN38.3认证-微测检测 CNAS实验室 | 管形母线,全绝缘铜管母线厂家-山东佰特电气科技有限公司 | 石家庄小程序开发_小程序开发公司_APP开发_网站制作-石家庄乘航网络科技有限公司 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 发光字|标识设计|标牌制作|精神堡垒 - 江苏苏通广告有限公司 | 进口便携式天平,外校_十万分之一分析天平,奥豪斯工业台秤,V2000防水秤-重庆珂偌德科技有限公司(www.crdkj.com) | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 皮带机-带式输送机价格-固定式胶带机生产厂家-河南坤威机械 | 上海乾拓贸易有限公司-日本SMC电磁阀_德国FESTO电磁阀_德国FESTO气缸 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | atcc网站,sigma试剂价格,肿瘤细胞现货,人结肠癌细胞株购买-南京科佰生物 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 集装袋吨袋生产厂家-噸袋廠傢-塑料编织袋-纸塑复合袋-二手吨袋-太空袋-曹县建烨包装 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 尾轮组_头轮组_矿用刮板_厢式刮板机_铸石刮板机厂家-双驰机械 | 冷藏车厂家|冷藏车价格|小型冷藏车|散装饲料车厂家|程力专用汽车股份有限公司销售十二分公司 |