0
votes

I have some problems with my auth middleware. I want to protect my routes with it, but in some controllers it dosent work really. If I check the routes as logout user, I got different error messasges instead of blocking the access and redirect to the login page. I use the middleware always over the __construct() in the controllers.

An example.

Controller

public function show(Dialog $dialog)
{
    return $this->template($dialog, self::DISPLAY_MODE_DIALOG);
}

protected function template(Dialog $dialog, $displayMode, $messageLimit = 10)
{
    $user = Auth::user();

    // Check if the profile is currently controlled by the current user
    $isControlled = $dialog->moderator()->appProfile()->isControlledBy($user->id);
    $messageList  = $dialog->latestMessages($messageLimit);

    return view('dialog.chat.template')
        ->with(compact('dialog', 'messageList', 'isControlled', 'displayMode'));
}

Model

public function isAccessibleBy(User $user)
{
    $profile = $this->moderator()->appProfile();
    return  $profile->isOwner($user->id)
                    || $profile->isControlledBy($user->id);
}

Error

Argument 1 passed to App\Model\Dialog\Dialog::isAccessibleBy() must be an instance of App\Model\Account\User, null given, called in C:\projekte\php\newchat\app\Model\Dialog\Dialog.php on line 332 and defined

Now I realize why this happens, but i think the middleware should dont show this messages an protecte the route.

http://laravel.com/docs/5.0/middleware

"However, if the user is authenticated, the middleware will allow the request to proceed further into the application."

1
Could you extend your question with the constructor in your controller?Iamzozo
I always use the middleware like this . code public function __construct() { $this->middleware('auth); } codeToyRobotic
You are using model binding in your routes, so its get called before your controller, before your middleware.Iamzozo
Is there a way to fix it ?ToyRobotic
The quickest way if your check the user in isAccessibleBy(User $user = null)... Then if($user) ... set accessible, return false if the user is nullIamzozo

1 Answers

0
votes

I fix my Problem with the Solution from ryanmortier. The Way from LPMadness dosent work for me.

https://github.com/laravel/framework/issues/6118