Okay, I was a little lost at the phrase, “custom finder,” when in reference to the “resolver configuration” when referring to the CakePHP 5 docs. I figured out by resolver configuration what was meant was the Application.php’s getAuthorizationService() method setup. I’m not sure if what Zuluru was suggesting is the route I took, but considering I’m coming from a CakePHP 2 application and attempting to merge the business logic back into CakePHP 5, the closest thing (for my application) would be to take advantage of a controller method of isAuthorized(). Based on Zuluru’s mention of a custom finder (assuming resolver, as defined in the book), I adjusted my code as follows (after having already installed and setup the authentication and authorization plugins):
Application.php:
// ...
use App\Policy\ControllerResolver;
// ...
// ...
// ...
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
{
// $resolver = new OrmResolver();
$resolver = new ControllerResolver();
return new AuthorizationService($resolver);
}
Per the (Authorization) book, I created a src\Policy\ControllerResolver.php file, and a src\PolicyControllerHookPolicy.php file - each of which are exact copies of what was in the book for the custom resolver examples. In my UserController.php file, I provided the following adjustments:
public function add()
{
$this->Authorization->authorize($this);
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
$groups = $this->Users->Groups->find('list')->all();
$this->set(compact('groups'));
}
public function isAuthorized() {
return true;
}
The logic for determining access rights will be added to isAuthorized (in each controller); thus far I’d only gotten so far as to add a simple die(‘We are in isAuthorized’) call to verify I made it through the proper paths of authorization, but I at least know I can access models within the controller without much hassle.
NOTE: Take note as to what parameter is passed to Authorization->authorize(); It’s $this, which is the (current) Controller object. We are not explicitly sending a user identity object.
Also, the current version of the book, for CakePHP 5, points to the Authorization version 2 documentation. The interface for that provides a selection to choose other versions, but 2 is the highest. There is a version 3, however (if you change the URL manually). I’m not 100% sure which one CakePHP v5 uses by default, though my recently configured composer file seems to show version 3 as a minimum expectation.
If this is the wrong way to do what I was proposing, please feel free to correct me! I somehow managed to skip the page in the documentation about custom resolvers prior to Zuluru’s response, so I very much appreciate the reply. I now need to go back and clean up all of the various implementations I’ve intermingled in my controller.