<?php

    namespace Icing\Policy;

    use Authorization\Identity;
    use Cake\Controller\Controller;
    use Icing\Model\Entity\User;

    class ControllerHookPolicy
    {
        public function __call(string $name, array $arguments)
        {
            /** @var ?Identity $user */
            [$user, $controller] = $arguments;

            // admin has special permissions
            if ($user instanceof User && $user->is('admin')) {
                return true;
            }

            $action = null;
            if ($controller instanceof Controller) {
                $action = $controller->getRequest()->getParam('action');
            }

            if (method_exists($controller, 'isAuthorized')) {
                return $controller->isAuthorized($user, $action);
            }

            return true;
        }
    }

