Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal exception when disabling view on null in Plugin/Security.php #38

Open
ronindesign opened this issue Jan 28, 2018 · 1 comment
Open

Comments

@ronindesign
Copy link

Following docs and setting my app/config/services.php as follows:

/**
 * Set dispatcher adding UserPlugin (crada/phalcon-user-plugin)
 */
$di->setShared(
  'dispatcher',
  function () use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    
    $security = new SecurityPlugin();
    $eventsManager->attach('dispatch', $security);
    
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    
    return $dispatcher;
  }
);

/**
 * Set auth service to UserPlugin/Auth (crada/phalcon-user-plugin)
 */
$di->setShared(
  'auth',
    function () {
      return new Auth();
    }
);

/**
 * Set ACL service to UserPlugin/Acl (crada/phalcon-user-plugin)
 */
$di->setShared(
  'acl',
    function () {
      return new Acl();
    }
);

/**
 * Set mail service to UserPlugin/Mail (crada/phalcon-user-plugin)
 */
$di->setShared(
  'mail',
    function () {
      return new Mail();
    }
);

Results in exception:

( ! ) Fatal error: Uncaught Error: Call to a member function disable() on null in /var/www/project/vendor/crada/phalcon-user-plugin/lib/Plugin/Security.php on line 115
--
1 | 0.0000 | 399984 | {main}( ) | .../index.php:0
2 | 0.0013 | 432208 | handle ( ) | .../index.php:42
3 | 0.0015 | 453872 | dispatch ( ) | .../index.php:42
4 | 0.0015 | 454336 | fire ( ) | .../index.php:42
5 | 0.0015 | 455344 | fireQueue ( ) | .../index.php:42
6 | 0.0015 | 455792 | Phalcon\UserPlugin\Plugin\Security->beforeDispatchLoop( ) | .../index.php:42

Both the following changes in vendor/crada/phalcon-user-plugin/lib/Plugin/Security.php stopped the fatal error (not a solution, just targets error location):

/**
     * beforeDispatchLoop.
     *
     * @param Event      $event
     * @param Dispatcher $dispatcher
     *
     * @return \Phalcon\Http\ResponseInterface
     */
    public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher)
    {
        $auth = $this->getAuth($dispatcher);
        $view = $this->getView($dispatcher);

        // ...

        if (true === $needsIdentity) {
            if (!is_array($identity)) {
                $this->flash->notice('Private area. Please login.');

                //$this->view->disable(); // Commenting this out stops fatal error

                return $this->response->redirect($config->pup->redirect->failure)->send();
            }
        }

        $view->setVar('identity', $identity);
    }

I noticed Security::setView doesn't seem to be called anywhere, so tried changing $this->view->disable() to $view->disable():

/**
     * beforeDispatchLoop.
     *
     * @param Event      $event
     * @param Dispatcher $dispatcher
     *
     * @return \Phalcon\Http\ResponseInterface
     */
    public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher)
    {
        $auth = $this->getAuth($dispatcher);
        $view = $this->getView($dispatcher);

        // ...

        if (true === $needsIdentity) {
            if (!is_array($identity)) {
                $this->flash->notice('Private area. Please login.');

                $view->disable(); // Changing this to $view fixes fatal error

                return $this->response->redirect($config->pup->redirect->failure)->send();
            }
        }

        $view->setVar('identity', $identity);
    }
@ronindesign ronindesign changed the title Fatal exception when disabling view on null in Plugin/Security.php Fatal exception when disabling view on null in Plugin/Security.php Jan 28, 2018
@ronindesign
Copy link
Author

The following fix from @mikguo on #27 solved my issue by adding line:
$security->setView($di->getShared('view'))->setAuth($di->getShared('auth'));

Resulting in updated app/config/services.php:

/**
 * Set auth service to UserPlugin/Auth (crada/phalcon-user-plugin)
 */
$di->setShared(
  'auth',
    function () {
      return new Auth();
    }
);

/**
 * Set ACL service to UserPlugin/Acl (crada/phalcon-user-plugin)
 */
$di->setShared(
  'acl',
    function () {
      return new Acl();
    }
);

/**
 * Set mail service to UserPlugin/Mail (crada/phalcon-user-plugin)
 */
$di->setShared(
  'mail',
    function () {
      return new Mail();
    }
);

/**
 * Set dispatcher adding UserPlugin (crada/phalcon-user-plugin)
 */
$di->setShared(
  'dispatcher',
  function () use ($di) {
    $eventsManager = $di->getShared('eventsManager');
    
    $security = new SecurityPlugin();
    $security->setView($di->getShared('view'))->setAuth($di->getShared('auth'));
    $eventsManager->attach('dispatch', $security);
    
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    
    return $dispatcher;
  }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant