Skip to content

Commit

Permalink
[#13648] - Reformatting of the components
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed May 15, 2019
1 parent fce18bd commit dc55ac5
Show file tree
Hide file tree
Showing 11 changed files with 901 additions and 1,710 deletions.
70 changes: 36 additions & 34 deletions phalcon/Acl/Adapter/Memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -592,18 +592,19 @@ class Memory extends Adapter
*/
public function isAllowed(var roleName, var componentName, string access, array parameters = null) -> bool
{
var eventsManager, accessList, accessKey, haveAccess = null, rolesNames,
funcAccess = null, componentObject = null, roleObject = null,
funcList, reflectionFunction, reflectionParameters, parameterNumber,
parametersForFunction, numberOfRequiredParameters,
userParametersSizeShouldBe, reflectionClass, parameterToCheck,
reflectionParameter;
bool hasRole = false, hasComponent = false;
var accessKey, accessList, componentObject = null, haveAccess = null,
eventsManager, funcAccess = null, funcList, numberOfRequiredParameters,
reflectionFunction, reflectionParameters, parameterNumber,
parameterToCheck, parametersForFunction, reflectionClass,
reflectionParameter, rolesNames, roleObject = null,
userParametersSizeShouldBe;

bool hasComponent = false, hasRole = false;

if typeof roleName == "object" {
if roleName instanceof RoleAware {
let roleObject = roleName;
let roleName = roleObject->getRoleName();
let roleObject = roleName,
roleName = roleObject->getRoleName();
} elseif roleName instanceof RoleInterface {
let roleName = roleName->getName();
} else {
Expand All @@ -615,8 +616,8 @@ class Memory extends Adapter

if typeof componentName == "object" {
if componentName instanceof ComponentAware {
let componentObject = componentName;
let componentName = componentObject->getComponentName();
let componentObject = componentName,
componentName = componentObject->getComponentName();
} elseif componentName instanceof ComponentInterface {
let componentName = componentName->getName();
} else {
Expand All @@ -626,16 +627,17 @@ class Memory extends Adapter
}
}

let this->activeRole = roleName;
let this->activeComponent = componentName;
let this->activeAccess = access;
let this->activeKey = null;
let this->activeFunction = null;
let this->activeFunctionCustomArgumentsCount = 0;
let this->activeRole = roleName,
this->activeComponent = componentName,
this->activeAccess = access,
this->activeKey = null,
this->activeKey = null,
this->activeFunction = null,
accessList = this->access,
eventsManager = <EventsManager> this->eventsManager,
funcList = this->func;

let accessList = this->access;
let eventsManager = <EventsManager> this->eventsManager;
let funcList = this->func;
let this->activeFunctionCustomArgumentsCount = 0;

if typeof eventsManager == "object" {
if eventsManager->fire("acl:beforeCheckAccess", this) === false {
Expand Down Expand Up @@ -672,8 +674,8 @@ class Memory extends Adapter
eventsManager->fire("acl:afterCheckAccess", this);
}

let this->activeKey = accessKey;
let this->activeFunction = funcAccess;
let this->activeKey = accessKey,
this->activeFunction = funcAccess;

if haveAccess == null {
/**
Expand All @@ -689,9 +691,9 @@ class Memory extends Adapter
* If we have funcAccess then do all the checks for it
*/
if is_callable(funcAccess) {
let reflectionFunction = new \ReflectionFunction(funcAccess);
let reflectionParameters = reflectionFunction->getParameters();
let parameterNumber = count(reflectionParameters);
let reflectionFunction = new \ReflectionFunction(funcAccess),
reflectionParameters = reflectionFunction->getParameters(),
parameterNumber = count(reflectionParameters);

/**
* No parameters, just return haveAccess and call function without
Expand All @@ -701,28 +703,28 @@ class Memory extends Adapter
return haveAccess == Acl::ALLOW && call_user_func(funcAccess);
}

let parametersForFunction = [];
let numberOfRequiredParameters = reflectionFunction->getNumberOfRequiredParameters();
let userParametersSizeShouldBe = parameterNumber;
let parametersForFunction = [],
numberOfRequiredParameters = reflectionFunction->getNumberOfRequiredParameters(),
userParametersSizeShouldBe = parameterNumber;

for reflectionParameter in reflectionParameters {
let reflectionClass = reflectionParameter->getClass();
let parameterToCheck = reflectionParameter->getName();
let reflectionClass = reflectionParameter->getClass(),
parameterToCheck = reflectionParameter->getName();

if reflectionClass !== null {
// roleObject is this class
if roleObject !== null && reflectionClass->isInstance(roleObject) && !hasRole {
let hasRole = true;
let parametersForFunction[] = roleObject;
let hasRole = true,
parametersForFunction[] = roleObject;
let userParametersSizeShouldBe--;

continue;
}

// componentObject is this class
if componentObject !== null && reflectionClass->isInstance(componentObject) && !hasComponent {
let hasComponent = true;
let parametersForFunction[] = componentObject;
let hasComponent = true,
parametersForFunction[] = componentObject;
let userParametersSizeShouldBe--;

continue;
Expand Down
248 changes: 248 additions & 0 deletions phalcon/Firewall/Adapter.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Firewall;

use Phalcon\Acl;
use Phalcon\Acl\RoleAware;
use Phalcon\Cache\Adapter\AdapterInterface as CacheAdapterInterface;
use Phalcon\DiInterface;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Mvc\Dispatcher;

/**
* Phalcon\Firewall\Adapter
*
* Adapter for Phalcon\Firewall adapters
*/
abstract class Adapter implements AdapterInterface, EventsAwareInterface
{
/**
* Storing active identity object implementing Phalcon/Acl/RoleAware
*/
protected activeIdentity { get };

/**
* Storing active user role
*/
protected activeRole { get };

/**
* Should role always be resolved using role callback or just once?
* @var bool
*/
protected alwaysResolvingRole = false { set };

/**
* Cache for caching access
* @var <CacheAdapterInterface>
*/
protected cache;

/**
* Default access
* @var int
*/
protected defaultAccess = Acl::DENY { get, set };

/**
* Events manager
* @var mixed
*/
protected eventsManager;

/**
* Internal cache for caching access during request time
* @var mixed
*/
protected internalCache;

/**
* Anonymous function for getting user identity - this function must
* return string, array or object implementing Phalcon\Acl\RoleAware
* @var mixed
*/
protected roleCallback;

/**
* Returns the internal event manager
*/
public function getEventsManager() -> <ManagerInterface>
{
return this->eventsManager;
}

/**
* Gets role callback to fetch role name
*/
public function getRoleCallback() -> <\Closure>
{
return this->roleCallback;
}

/**
* Gets always resolving role option
*/
public function isAlwaysResolvingRole() -> bool
{
return this->alwaysResolvingRole;
}

/**
* Sets the events manager
*/
public function setEventsManager(<ManagerInterface> eventsManager) -> <AdapterInterface>
{
let this->eventsManager = eventsManager;

return this;
}

/**
* Sets role callback to fetch role name
*/
public function setRoleCallback(var callback) -> <AdapterInterface>
{
if !is_callable(callback){
throw new Exception("Role callback must be function.");
}
let this->roleCallback = callback;

return this;
}

protected function callRoleCallback(<DiInterface> container) -> void
{
var roleCallback, identity;

let roleCallback = this->roleCallback,
identity = {roleCallback}(container);

if empty identity {
throw new Exception("Function defined as roleCallback must return something.");
}

if typeof identity == "object" {
if !(identity instanceof RoleAware) {
throw new Exception("Role passed as object must implement 'Phalcon\\Acl\\RoleAware'");
}
let this->activeIdentity = identity,
this->activeRole = identity->getRoleName();
} else {
let this->activeRole = identity;
}
}

/**
* Gets access from cache
*/
protected function getAccessFromCache(
string! key,
array originalValues = null,
string roleCacheKey = null
) -> bool | null
{
var access;

fetch access, this->internalCache[key];

return access;
}

/**
* Handles a user exception
*/
protected function handleException(<\Exception> exception)
{
var eventsManager;
let eventsManager = <ManagerInterface> this->eventsManager;
if typeof eventsManager == "object" {
if eventsManager->fire("firewall:beforeException", this, exception) === false {
return false;
}
}
}

/**
* Fires event or throwing exception
*/
protected function fireEventOrThrowException(
var role,
string actionName,
string controllerName,
bool access
)
{
var eventsManager, roleName;

let eventsManager = this->eventsManager;
if access {
if typeof eventsManager == "object" {
eventsManager->fire("firewall:afterCheck",this);
}
} else {
if typeof role == "array" {
let roleName = implode(", ",role);
} else {
let roleName = role;
}

return this->throwFirewallException(
"Role name " . roleName . " doesn't have access to action " .
actionName . " in controller " . controllerName,
403
);
}
}

/**
* Saves access in cache and internal cache
*/
protected function saveAccessInCache(string! key, bool access) -> void
{
var cache;

let this->internalCache[key] = access;

let cache = this->cache;

if cache != null {
cache->save("_PHF_", this->internalCache);
}
}

public function setCache(<CacheAdapterInterface> cache) -> <AdapterInterface>
{
let this->cache = cache;

if this->internalCache === null {
let this->internalCache = cache->get("_PHF_");
}

return this;
}

/**
* Throws an internal exception
*/
protected function throwFirewallException(string message, int exceptionCode = 0)
{
var exception;

let exception = new Exception(message, exceptionCode);

if this->handleException(exception) === false {
return false;
}

throw exception;
}
}
Loading

0 comments on commit dc55ac5

Please sign in to comment.