Policy for `App\Model\Table\UsersTable` has not been defined

Fresh install with CakePHP 5.1 walked through the CMS tutorial and got everything working. Started added the Authorization features and now get an error on all pages index functions. Just to get working with basics the User Policy I’ve added.

src\Policy\UserPolicy.php

public function canIndex(IdentityInterface $user, user $resource)
    {
        // All logged in users can index.
        return true;
    }

Looking at vendor\cakephp\authorization\src\Policy\OrmResolver.php
protected function findPolicy(string $class, string $name, string $namespace): mixed

$name is being passed “UsersTable” for index functions whereas all other functions are being passed “User” and I assume the reason for the error not finding the correct policy file.

Your error logs should have details of exactly what the error is, to help you diagnose the problem. Debugging should never be a matter of pure guesswork.

More about Policies: Policies - 3.x

# Create an entity policy
bin/cake bake policy --type entity Article

# Create a table policy
bin/cake bake policy --type table Articles

Thanks for that! Didn’t see the --type table option going thru everything

1 Like

I higly recommend this plugin. Make your life easy a handle the permissions from a config file.

addind just controllers, actions and custom rules.

Is nice to go though the tutorial to know how it works, But cakeDC did this amazing plugin which will meake your life much easy.

True, it is a good plugin, saved me a lot of time :partying_face:
But in some situations, I needed to implement the Policy, see the problem here: