Archived
1
0
Fork 0

initial commit

This commit is contained in:
Henrik Hautakoski 2017-09-01 17:10:27 +02:00
commit e869a1cab4
107 changed files with 9029 additions and 0 deletions

47
app/plugins/AclPlugin.php Normal file
View file

@ -0,0 +1,47 @@
<?php
use Phalcon\Acl;
use Phalcon\Events\Event;
use Phalcon\Mvc\Dispatcher;
class AclPlugin extends Phalcon\Mvc\User\Plugin
{
public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher)
{
// We only have two roles for now, authenticated users and guests.
if ($this->auth->hasIdentity()) {
$role = \Acl\Acl::ROLE_USER;
} else {
$role = \Acl\Acl::ROLE_GUEST;
}
// Support annotations for actions to define custom resources.
$controllerClass = $dispatcher->getControllerClass();
$activeMethod = $dispatcher->getActiveMethod();
$annotation = $this->annotations->getMethod($controllerClass, $activeMethod);
// ACL annotation found. use that.
if ($annotation->has('Acl')) {
$resource = $annotation->get('Acl')->getArgument('resource');
}
// Otherwise, default to controller name.
else {
$resource = $dispatcher->getControllerName();
}
// Now, check and redirect user to login page if
// this role does not have access to this resource.
if ($this->acl->isAllowed($role, $resource, 'Read') == Acl::DENY) {
// Forward to login page.
$dispatcher->forward(array(
'controller' => 'auth',
'action' => 'index',
));
// Return false to stop the dispatch loop.
return false;
}
}
}

View file

@ -0,0 +1,49 @@
<?php
use Phalcon\Events\Event;
use Phalcon\Mvc\User\Plugin;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Dispatcher\Exception as DispatcherException;
/**
* Class ExceptionHandlerPlugin
*
* Plugin for forwarding user to 404 (not found) page
* if a request could not be dispatched.
*/
class ExceptionHandlerPlugin extends Plugin
{
protected $_route_notfound = array(
'controller' => 'error',
'action' => 'show404'
);
protected $_route_error = array(
'controller' => 'error',
'action' => 'error',
);
/**
* @param Event $event
* @param Dispatcher $dispatcher
* @param Exception $exception
* @return bool
*/
public function beforeException(Event $event, Dispatcher $dispatcher, Exception $exception)
{
// Figure out if this was a exception from dispatcher and that exception
// was that an controller or action was not found.
if ($exception instanceof DispatcherException) {
switch ($exception->getCode()) {
case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND :
case Dispatcher::EXCEPTION_ACTION_NOT_FOUND :
// in this case, forward to 404 page.
$dispatcher->forward($this->_route_notfound);
return false;
}
}
$dispatcher->forward($this->_route_error);
return false;
}
}