简体   繁体   English

Laravel-8 中的 Auth::onceUsingID() 和 Auth::setUser() 有什么区别

[英]What is difference between Auth::onceUsingID() and Auth::setUser() in Laravel-8

I want to implement Impersonate functionality into Laravel-8 without using any package.我想在不使用任何 package 的情况下在 Laravel-8 中实现模拟功能。

  • Only super-admin can use this functionality.只有超级管理员才能使用此功能。
  • I used laravel sanctum to authenticate.我使用 laravel sanctum 进行身份验证。
  • to access impersonate functionality user should be super-admin.要访问模拟功能,用户应该是超级管理员。 (is_admin(boolean) flag is set into users table). (is_admin(boolean) 标志设置到 users 表中)。

Here is my middleware:这是我的中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ImpersonateUser
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        $impersonateId = $request->cookie('x-impersonate-id');
        if($request->user()->is_admin && $impersonateId) {
            $user = User::findOrFail($impersonateId);
            if($user->is_admin) {
                return response()->json(["message" => trans("You cannot impersonate an admin account.")], 400);
            }
            Auth::setUser($user);
        }
        return $next($request);
    }
}

My route file:我的路线文件:

    // Impersonate routes.
    Route::middleware(['auth:sanctum', 'impersonate'])->group(function () {
        // checklist routes
        Route::get('checklists', [ChecklistController::class, "index"]);
    });

Whether use Auth::setUser($user) is safe or I have to use Auth::onceUsingId($userId);使用Auth::setUser($user)是安全的还是我必须使用Auth::onceUsingId($userId); ? ?

Auth::onceUsingId($userId); Auth::onceUsingId($userId); not working with auth::sanctum middleware.不使用auth::sanctum中间件。 So Auth::setUser($user) is safe or not?那么Auth::setUser($user)是否安全?

I used laravel to develop backend API only.(SPA)我仅使用 laravel 开发后端 API。(SPA)

They should be the same in terms of safety.它们在安全性方面应该相同。 OnceUsingId() calls setUser() in the background. OnceUsingId() setUser()

From the Illuminate\Auth\SessionGuard classIlluminate\Auth\SessionGuard class

 /** * Log the given user ID into the application without sessions or cookies. * * @param mixed $id * @return \Illuminate\Contracts\Auth\Authenticatable|false */ public function onceUsingId($id) { if (; is_null($user = $this->provider->retrieveById($id))) { $this->setUser($user); return $user; } return false. } /** * Set the current user; * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return $this */ public function setUser(AuthenticatableContract $user) { $this->user = $user; $this->loggedOut = false; $this->fireAuthenticatedEvent($user); return $this; }

Both of these methods come from the SessionGuard though.不过,这两种方法都来自 SessionGuard。 I don't know if Sanctum implements its own version.我不知道 Sanctum 是否实现了自己的版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel中的Auth :: routes()和Route :: auth()有什么区别 - what is the difference between Auth::routes() and Route::auth() in laravel Laravel 5.7 中的 auth 和 access 有什么区别? - What is the difference between auth and access in Laravel 5.7? auth()-&gt; loginUsingId(1)和有什么区别? vs Auth :: loginUsingId(1); - What is the difference between auth()->loginUsingId(1); vs Auth::loginUsingId(1); 无法在 CakePHP 4 中使用 setUser 在 Auth 中设置用户 - Not able to set user in Auth using setUser in CakePHP 4 Laravel 5:Auth :: guard($ this-&gt; getGuard()) - &gt; login($ user)之间的区别; 和auth() - &gt; login($ user); - Laravel 5: The difference between Auth::guard($this->getGuard())->login($user); and auth()->login($user); OAuth 2中的访问令牌和auth_codes之间有什么区别 - What is the difference between access tokens and auth_codes in OAuth 2 “php artisan ui vue --auth”命令和“php artisan ui:auth”有什么区别 - What is the difference between "php artisan ui vue --auth" command and "php artisan ui:auth" CakePHP:Auth-&gt; allowedActions()和Auth-&gt; allow()之间有什么区别? - CakePHP: What's the difference between Auth->allowedActions() and Auth->allow(); “ Web”和“ Auth”中间件之间的区别? - Difference between 'web' and 'auth' middleware? 什么是laravel 5.2 auth中的auth.authenticate视图 - What is auth.authenticate view in laravel 5.2 auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM