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

Firewall improvement #14450

Merged
merged 14 commits into from
Oct 5, 2019
3 changes: 3 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
- Fixed `Phalcon\Http\Message\ServerRequestFactory::load` to correctly detect the protocol passed from `$_SERVER` [#14432](https://github.com/phalcon/cphalcon/issues/14432)
- Fixed `Phalcon\Cli\Router\Route` added missing `Phalcon\Cli\Router\RouteInterface`
- Fixed incorrect return types on `Phalcon\Mvc\View\Engine\AbstractEngine::partial` and `Phalcon\Mvc\View\Engine\EngineInterface::partial` [#14429](https://github.com/phalcon/cphalcon/issues/14429)
- Fixed `Phalcon\Firewall\Adapter\AbstractAdapter::getRoleCallback` and `Phalcon\Firewall\Adapter\AbstractAdapter::setRoleCallback` to correctly accept and return a `Closure` [#14450](https://github.com/phalcon/cphalcon/issues/14450)
- Fixed `Phalcon\Firewall\Adapter\AdapterInterface::getRoleCallback` and `Phalcon\Firewall\Adapter\AbstractAdapter::setRoleCallback` to correctly accept and return a `Closure` [#14450](https://github.com/phalcon/cphalcon/issues/14450)
- Fixed `Phalcon\Events\Event::__constructor` to correctly accept an `object` as the `source` parameter [#14449](https://github.com/phalcon/cphalcon/issues/14449)

## Removed
- Removed `Phalcon\Application\AbstractApplication::handle()` as it does not serve any purpose and causing issues with type hinting. [#14407](https://github.com/phalcon/cphalcon/pull/14407)
Expand Down
18 changes: 17 additions & 1 deletion phalcon/Events/Event.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@

namespace Phalcon\Events;

use Phalcon\Events\Exception;

/**
* Phalcon\Events\Event
*
* This class offers contextual information of a fired event in the
* EventsManager
*
*```php
* Phalcon\Events\Event;
*
* $event = new Event("db:afterQuery", $this, ["data" => "mydata"], true);
* if ($event->isCancelable()) {
* $event->stop();
* }
* ```
*/
class Event implements EventInterface
{
Expand Down Expand Up @@ -58,8 +69,13 @@ class Event implements EventInterface
*
* @param object source
*/
public function __construct(string! type, source, var data = null, bool cancelable = true)
public function __construct(string! type, object source, var data = null, bool cancelable = true)
{
if unlikely typeof source != "object" {
throw new Exception(
"The source of " . type . " event must be an object, got " . (typeof source)
);
}
let this->type = type,
this->source = source,
this->data = data,
Expand Down
24 changes: 16 additions & 8 deletions phalcon/Firewall/Adapter/AbstractAdapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,8 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
/**
* Sets role callback to fetch role name
*/
public function setRoleCallback(var callback) -> <AdapterInterface>
public function setRoleCallback(<Closure> callback) -> <AdapterInterface>
{
if !is_callable(callback) {
throw new Exception("Role callback must be function.");
}
let this->roleCallback = callback;

return this;
Expand All @@ -153,16 +150,27 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
{
var roleCallback, identity;

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

if unlikely !roleCallback {
throw new Exception(
"You must set the roleCallback"
);
}

let identity = {roleCallback}(container);

if empty identity {
throw new Exception("Function defined as roleCallback must return something.");
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'");
throw new Exception(
"Role passed as object must implement 'Phalcon\\Acl\\RoleAware'"
);
}
let this->activeIdentity = identity,
this->activeRole = identity->getRoleName();
Expand Down
5 changes: 3 additions & 2 deletions phalcon/Firewall/Adapter/AdapterInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Phalcon\Firewall\Adapter;

use Closure;
use Phalcon\Mvc\DispatcherInterface;
use Phalcon\Cache\Adapter\AdapterInterface as CacheAdapterInterface;

Expand All @@ -26,7 +27,7 @@ interface AdapterInterface
/**
* Gets role callback to fetch role name
*/
public function getRoleCallback();
public function getRoleCallback() -> <Closure>;

/**
* Gets always resolving role option
Expand All @@ -51,5 +52,5 @@ interface AdapterInterface
/**
* Sets role callback to fetch role name
*/
public function setRoleCallback(var callback) -> <AdapterInterface>;
public function setRoleCallback(<Closure> callback) -> <AdapterInterface>;
}