Authorization check method must return `Authorization\Policy\ResultInterface` or `bool`

Hey everyone,

I’m just trying to implement my authentication and it works well and i can only access the right houses.

I tried to break the authentication by changing the URL from this

http://localhost/hausundhof/houses/view/6

to this.

http://localhost/hausundhof/houses/view/7

Now i get the following error message.

Authorization check method must return Authorization\Policy\ResultInterface or bool.

That seems like not the right Error.

# UserPolicy.php
<?php
declare(strict_types=1);

namespace App\Policy;

use App\Model\Entity\User;
use Authorization\IdentityInterface;

class UserPolicy
{
    public function canView(IdentityInterface $user, User $resource)
    {
        return $user->getIdentifier() === $resource->id;
    }
}

# UserController.php
 public function view($id = null)
    {
        $user = $this->Users->get($id, contain: ['Roles', 'Houses', 'TicketSnaps', 'Tickets']);
        $this->Authorization->authorize($user, 'view');
        $this->set(compact('user'));
    }

Mache ich da irgendetwas falsch??

Merci :pray:

Why are you showing us code from your User controller and policy when it’s a House that you’re trying to access via your URL?

Hey Zuluru,

sorry for that, i was just completely lost becuse i tried to authorize the user with

$this->Authorization->authorize($house);

and then built the whole logic that the user sees only to his account connected houses what got very complicated. I just realized i can built that with.

$query = $this->Authorization->applyScope($query);

What a amazing way to write with so less code.

:pray: