訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)


問題描述

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)

我的項目中的路由和中間件有問題。我有一個僅對訂閱者鎖定的主題列表。但是,主題可以標記為公開,允許來賓用戶/公眾查看主題。

我面臨的問題是我要么讓所有主題都對公眾可見,要么沒有。

我有以下訂閱者中間件。

public function handle(Request $request, Closure $next)
{
    if ( $request‑>user() && ! $request‑>user()‑>subscribed('annual_membership') ) {
        return redirect('profile');
    } 

    if( Auth::Guest() ) {
        return redirect('topics')‑>with('error', 'You need to be a registered memeber to view this topic.');
    }

    return $next($request);
}

這是像這樣添加到我的內核中的。

protected $routeMiddleware = [
    // ...
    'subscriber' => \App\Http\Middleware\Subscriber::class,
];

然後我設置了以下路由。

Route::get('voting‑topics', [TopicController::class, 'topics'])‑>name('topics');
Route::get('voting‑topics/{topic}', [TopicController::class, 'topic'])‑>name('topic');

Route::group(['middleware' => ['subscriber']], function () 
{
    Route::get('profile/billing', function (Request $request) {
        return $request‑>user()‑>redirectToBillingPortal();
    });

    Route::get('voting‑topics/{topic}', [TopicController::class, 'topic'])‑>name('topic');
});

還有我的主題控制器。

class TopicController extends Controller
{
    public function topics(Request $request) 
    {
        if ( $request‑>user() && $request‑>user()‑>subscribed('annual_membership') ) {
            return $this‑>subscriberTopics();
        } else {
            return $this‑>publicTopics();
        }
    }


    private function publicTopics() 
    {
        $topics = Topic::orderBy('date', 'asc')‑>where('free_to_view', 1)‑>paginate(50);

        return view('topics.topics', compact('topics'));
    }

    private function subscriberTopics() 
    {
        $topics = Topic::orderBy('date', 'desc')‑>paginate(50);

        return view('topics.topics', compact('topics'));
    }

    public function topic(Request $request, Topic $topic) 
    {
        $topic = Topic::find($topic)‑>first();

        return view('topics.topic', compact(['topic',));
    }
}

主題目錄工作正常。它只顯示標記為公開的主題和訂閱者的所有主題。我的代碼目前只允許訂閱者查看所有主題,訪客可以查看公共主題,這不是我想要的。

如果我刪除中間件路由,相反的情況會發生,如果他們輸入主題的 URL,訪客可以訪問所有主題。

編輯

為了更好地澄清和解釋,我有一個主題列表(基本上就像博客文章)。主題僅供我網站的付費訂閱者查看。但是,有些主題可以標記為免費查看,並且每個人都可以看到。

如果我訪問 example.com/voting‑topics,那麼我只能看到免費主題。(這是正確且有效的)如果我登錄並轉到相同的 URL,我會看到所有主題,因為我已登錄,並且我是付費訂閱者。(這又是正確且有效的)

如果我刪除了中間件中的重複路由,則直接通過 URL 轉到 example.com/voting‑topics/123 或任何其他主題 ID。我可以查看該主題,即使它應該只鎖定給訂閱者。

如果我將重複的路由添加回我的中間件,公眾將無法訪問免費主題。

我需要訪客/普通公眾/非訂閱者僅訪問標記為公開但付費訂閱者可以訪問所有主題的主題。`

我需要訪客/普通公眾/非訂閱者僅訪問標記為公開但付費訂閱者可以訪問所有主題的主題。`

我需要訪客/普通公眾/非訂閱者僅訪問標記為公開但付費訂閱者可以訪問所有主題的主題。`


參考解法

方法 1:

So it seems it was a simple fix after all, rather than do checks in my Routes I needed to do them in my controller.

I removed the Route::get('voting‑topics/{topic}', [TopicController::class, 'topic'])‑>name('topic'); from my middelware and then added the following code in my controller.

if( $topic‑>free_to_view === 1 || Auth::check() && auth()‑>user()‑>subscribed('annual_membership')) {
    return view('topics.topic', compact('topic'));

} else {

    return redirect()‑>back()‑>with('error', 'You need to be a paying member to view this topic');

}

(by CIBCIB)

參考文件

  1. Subscriber middleware route allowing public to view all restricted pages (CC BY‑SA 2.5/3.0/4.0)

#laravel-middleware #laravel-8 #Laravel #laravel-routing






相關問題

使用中間件進行表操作 laravel 5.1 (Using middleware for table manipulation laravel 5.1)

Laravel 中間件“僅”為每條路線觸發 (Laravel middlware 'only' fires for every route)

在 laravel 5.1 中檢查管理員角色 (Checking admin roles in laravel 5.1)

在laravel 5中使用中間件重定向循環 (redirect loop with middleware in laravel 5)

Laravel - 中間件後清空 $request (Laravel - Empty $request after middleware)

$request->user()->role 錯誤 - 試圖獲取非對象的屬性 ($request->user()->role errror - trying to get property of non-object)

檢測到 Laravel 中間件但未執行 (Laravel middleware detected but not executed)

將 Auth 中間件應用於所有 Laravel 路由 (Apply Auth Middleware to All Laravel Routes)

路由組內的功能 [laravel-passport] (Function inside route group [laravel-passport])

當我嘗試訪問主頁時,Laravel 不會重定向到登錄頁面 (Laravel does not redirect to login page when I try to access to home page)

控制器沒有來自中間件的更改請求 (Controller doesnt have the alter request from the middleware)

訂閱者中間件路由允許公眾查看所有受限頁面 (Subscriber middleware route allowing public to view all restricted pages)







留言討論