Hello everyone. This is my first post and question about my first project in PhalconPHP. So be nice and gentle with me :)
I've begun to make an MVC structured project. It's going to have a log in and the app will be merging CSV files of different formats into a database. Nothing too taxing. I have reached the point where I have now created a Security()
plugin based on an example written in "Getting Started with Phalcon" by Stephan A. Miller (which has been very helpful in .. well getting started). It's all working to this point apart from one thing that has me confused and I'm presuming I've just hit a gap in knowledge here. So I am hoping that I can get an answer. I apologise if I am repeating something that's already been posted but my searches were unable to find anything.
In my routes.php file I have:
<?php
/**
* Routes File
*/
$di->set('router', function () {
$router = new \Phalcon\Mvc\Router();
$router->add("/", [
'controller' => 'pages',
'action' => 'index',
]);
$router->notFound(['controller' => 'pages', 'action'=> 'index']);
return $router;
});
I intend to have a general info landing page here along with other useful information pages withing a pages controller. Before I implimented the ACL, https://MyProject.dev/ would render /pages/index/ fine. After ACL implimentation I can still navigate to /pages/index/ and see the page but navigating to root/ shows the log in page and my message saying I don't have permission. All other urls private and public work fine as they should because I've not started to add more routes.
Here is my Security.php file (which I plan on refactoring later to cut down the length of it, this is more from the book than anything but I have modified it slightly).
<?php
use Phalcon\Events\Event,
Phalcon\Mvc\User\Plugin,
Phalcon\Mvc\Dispatcher,
Phalcon\Acl;
class Security extends Plugin {
public function __construct($dependencyInjector) {
$this->_dependencyInjector = $dependencyInjector;
}
public function getAcl()
{
if(!isset($this->persistent->acl))
{
$acl = new Phalcon\Acl\Adapter\Memory();
$acl->setDefaultAction(Phalcon\Acl::DENY);
$roles = [
'users' => new Phalcon\Acl\Role('Users'),
//'admin' => new Phalcon\Acl\Role('Admin'), // comming back to this in a refactor
'guests' => new Phalcon\Acl\Role('Guests')
];
foreach ($roles as $role)
{
$acl->addRole($role);
}
$private = [
'dashboard' => ['index']
];
foreach ($private as $resource => $actions)
{
$acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
}
$public = [
'users' => ['*'], // will be moving to private
'pages' => ['*']
];
foreach ($public as $resource => $actions)
{
$acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
}
foreach ($roles as $role)
{
foreach ($public as $resource => $actions)
{
foreach ($actions as $action)
{
$acl->allow($role->getName(), $resource, $action);
}
}
}
foreach ($private as $resource => $actions)
{
foreach($actions as $action)
{
$acl->allow('Users', $resource, $action);
}
}
$this->persistent->acl = $acl;
}
return $this->persistent->acl;
}
public function beforeDispatch(Event $event, Dispatcher $dispatcher)
{
$user = $this->session->get('user_id');
if(empty($user))
{
$role = "Guests";
} else {
$role = 'Users';
}
$controller = $dispatcher->getControllerName();
$action = $dispatcher->getActionName();
$acl = $this->getAcl();
$user_is_allowed = ($acl->isAllowed($role, $controller, $action) == Acl::ALLOW ? true : false);
if(!$user_is_allowed)
{
$this->flash->error("Please Log in to access this area.");
$this->dispatcher->forward(['controller' => 'pages', 'action' => 'login']);
return false;
}
}
}
$user_is_allowed
is coming back as false for https://MyProject.dev/ and therefore it fires $this->dispatcher->forward(['controller' => 'pages', 'action' => 'login']);
which is not what I want. Please help. Thanks. xx