diff options
author | Marvin Borner | 2018-05-23 22:23:28 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-23 22:23:28 +0200 |
commit | b66a61addb6c8e66cb26fcf74b532d68891267e4 (patch) | |
tree | 05e9449ff25bdc98f68105f41923ccb9f6ef5095 /main/app/sprinkles/account/src/Authorize | |
parent | 1d4ef435177a5f9b6d1a289800d933e49be0c550 (diff) |
Refactored code, many fixes and improvements in chat backend+frontend
Diffstat (limited to 'main/app/sprinkles/account/src/Authorize')
4 files changed, 43 insertions, 51 deletions
diff --git a/main/app/sprinkles/account/src/Authorize/AccessConditionExpression.php b/main/app/sprinkles/account/src/Authorize/AccessConditionExpression.php index dd5647e..e36f4f4 100644 --- a/main/app/sprinkles/account/src/Authorize/AccessConditionExpression.php +++ b/main/app/sprinkles/account/src/Authorize/AccessConditionExpression.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Account\Authorize; use Monolog\Logger; @@ -69,15 +70,14 @@ class AccessConditionExpression * @param Logger $logger A Monolog logger, used to dump debugging info for authorization evaluations. * @param bool $debug Set to true if you want debugging information printed to the auth log. */ - public function __construct(ParserNodeFunctionEvaluator $nodeVisitor, User $user, Logger $logger, $debug = false) - { - $this->nodeVisitor = $nodeVisitor; - $this->user = $user; - $this->parser = new Parser(new EmulativeLexer); - $this->traverser = new NodeTraverser; + public function __construct(ParserNodeFunctionEvaluator $nodeVisitor, User $user, Logger $logger, $debug = FALSE) { + $this->nodeVisitor = $nodeVisitor; + $this->user = $user; + $this->parser = new Parser(new EmulativeLexer); + $this->traverser = new NodeTraverser; $this->traverser->addVisitor($nodeVisitor); $this->prettyPrinter = new StandardPrettyPrinter; - $this->logger = $logger; + $this->logger = $logger; $this->debug = $debug; } @@ -90,8 +90,7 @@ class AccessConditionExpression * @param array[mixed] $params the parameters to be used when evaluating the expression. * @return bool true if the condition is passed for the given parameters, otherwise returns false. */ - public function evaluateCondition($condition, $params) - { + public function evaluateCondition($condition, $params) { // Set the reserved `self` parameters. // This replaces any values of `self` specified in the arguments, thus preventing them from being overridden in malicious user input. // (For example, from an unfiltered request body). @@ -120,7 +119,7 @@ class AccessConditionExpression $result = eval($expr_eval); if ($this->debug) { - $this->logger->debug("Expression '$expr' evaluates to " . ($result == true ? "true" : "false")); + $this->logger->debug("Expression '$expr' evaluates to " . ($result == TRUE ? "true" : "false")); } return $result; @@ -128,12 +127,12 @@ class AccessConditionExpression if ($this->debug) { $this->logger->debug("Error parsing access condition '$condition':" . $e->getMessage()); } - return false; // Access fails if the access condition can't be parsed. + return FALSE; // Access fails if the access condition can't be parsed. } catch (AuthorizationException $e) { if ($this->debug) { $this->logger->debug("Error parsing access condition '$condition':" . $e->getMessage()); } - return false; + return FALSE; } } } diff --git a/main/app/sprinkles/account/src/Authorize/AuthorizationException.php b/main/app/sprinkles/account/src/Authorize/AuthorizationException.php index 251b67f..33f3d35 100644 --- a/main/app/sprinkles/account/src/Authorize/AuthorizationException.php +++ b/main/app/sprinkles/account/src/Authorize/AuthorizationException.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Account\Authorize; use UserFrosting\Support\Exception\ForbiddenException; diff --git a/main/app/sprinkles/account/src/Authorize/AuthorizationManager.php b/main/app/sprinkles/account/src/Authorize/AuthorizationManager.php index def152b..f9fb196 100644 --- a/main/app/sprinkles/account/src/Authorize/AuthorizationManager.php +++ b/main/app/sprinkles/account/src/Authorize/AuthorizationManager.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Account\Authorize; use Interop\Container\ContainerInterface; @@ -33,8 +34,7 @@ class AuthorizationManager * * @param ContainerInterface $ci The global container object, which holds all your services. */ - public function __construct(ContainerInterface $ci, array $callbacks = []) - { + public function __construct(ContainerInterface $ci, array $callbacks = []) { $this->ci = $ci; $this->callbacks = $callbacks; } @@ -46,8 +46,7 @@ class AuthorizationManager * @param string $name * @param callable $callback */ - public function addCallback($name, $callback) - { + public function addCallback($name, $callback) { $this->callbacks[$name] = $callback; return $this; } @@ -57,8 +56,7 @@ class AuthorizationManager * * @return callable[] */ - public function getCallbacks() - { + public function getCallbacks() { return $this->callbacks; } @@ -69,12 +67,11 @@ class AuthorizationManager * * @param UserFrosting\Sprinkle\Account\Database\Models\User $user * @param string $slug The permission slug to check for access. - * @param array $params[optional] An array of field names => values, specifying any additional data to provide the authorization module + * @param array $params [optional] An array of field names => values, specifying any additional data to provide the authorization module * when determining whether or not this user has access. * @return boolean True if the user has access, false otherwise. */ - public function checkAccess(User $user, $slug, array $params = []) - { + public function checkAccess(User $user, $slug, array $params = []) { $debug = $this->ci->config['debug.auth']; if ($debug) { @@ -87,7 +84,7 @@ class AuthorizationManager if ($debug) { $this->ci->authLogger->debug("User is not logged in. Access denied."); } - return false; + return FALSE; } // The master (root) account has access to everything. @@ -97,7 +94,7 @@ class AuthorizationManager if ($debug) { $this->ci->authLogger->debug("User is the master (root) user. Access granted."); } - return true; + return TRUE; } // Find all permissions that apply to this user (via roles), and check if any evaluate to true. @@ -107,13 +104,13 @@ class AuthorizationManager if ($debug) { $this->ci->authLogger->debug("No matching permissions found. Access denied."); } - return false; + return FALSE; } $permissions = $permissions[$slug]; if ($debug) { - $this->ci->authLogger->debug("Found matching permissions: \n" . print_r($this->getPermissionsArrayDebugInfo($permissions), true)); + $this->ci->authLogger->debug("Found matching permissions: \n" . print_r($this->getPermissionsArrayDebugInfo($permissions), TRUE)); } $nodeVisitor = new ParserNodeFunctionEvaluator($this->callbacks, $this->ci->authLogger, $debug); @@ -125,7 +122,7 @@ class AuthorizationManager if ($debug) { $this->ci->authLogger->debug("User passed conditions '{$permission->conditions}' . Access granted."); } - return true; + return TRUE; } } @@ -133,7 +130,7 @@ class AuthorizationManager $this->ci->authLogger->debug("User failed to pass any of the matched permissions. Access denied."); } - return false; + return FALSE; } /** @@ -142,8 +139,7 @@ class AuthorizationManager * @param array * @return array */ - protected function getPermissionsArrayDebugInfo($permissions) - { + protected function getPermissionsArrayDebugInfo($permissions) { $permissionsInfo = []; foreach ($permissions as $permission) { $permissionData = array_only($permission->toArray(), ['id', 'slug', 'name', 'conditions', 'description']); diff --git a/main/app/sprinkles/account/src/Authorize/ParserNodeFunctionEvaluator.php b/main/app/sprinkles/account/src/Authorize/ParserNodeFunctionEvaluator.php index e8e5cde..e0db07d 100644 --- a/main/app/sprinkles/account/src/Authorize/ParserNodeFunctionEvaluator.php +++ b/main/app/sprinkles/account/src/Authorize/ParserNodeFunctionEvaluator.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Account\Authorize; use Monolog\Logger; @@ -53,17 +54,15 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract * @param Logger $logger A Monolog logger, used to dump debugging info for authorization evaluations. * @param bool $debug Set to true if you want debugging information printed to the auth log. */ - public function __construct($callbacks, $logger, $debug = false) - { + public function __construct($callbacks, $logger, $debug = FALSE) { $this->callbacks = $callbacks; $this->prettyPrinter = new StandardPrettyPrinter; - $this->logger = $logger; + $this->logger = $logger; $this->debug = $debug; $this->params = []; } - public function leaveNode(Node $node) - { + public function leaveNode(Node $node) { // Look for function calls if ($node instanceof \PhpParser\Node\Expr\FuncCall) { $eval = new \PhpParser\Node\Scalar\LNumber; @@ -87,26 +86,26 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract $value = $this->resolveParamPath($argString); $currentArgInfo['type'] = "parameter"; $currentArgInfo['resolved_value'] = $value; - // Resolve arrays - } elseif ($arg->value instanceof \PhpParser\Node\Expr\Array_) { + // Resolve arrays + } else if ($arg->value instanceof \PhpParser\Node\Expr\Array_) { $value = $this->resolveArray($arg); $currentArgInfo['type'] = "array"; - $currentArgInfo['resolved_value'] = print_r($value, true); - // Resolve strings - } elseif ($arg->value instanceof \PhpParser\Node\Scalar\String_) { + $currentArgInfo['resolved_value'] = print_r($value, TRUE); + // Resolve strings + } else if ($arg->value instanceof \PhpParser\Node\Scalar\String_) { $value = $arg->value->value; $currentArgInfo['type'] = "string"; $currentArgInfo['resolved_value'] = $value; - // Resolve numbers - } elseif ($arg->value instanceof \PhpParser\Node\Scalar\DNumber) { + // Resolve numbers + } else if ($arg->value instanceof \PhpParser\Node\Scalar\DNumber) { $value = $arg->value->value; $currentArgInfo['type'] = "float"; $currentArgInfo['resolved_value'] = $value; - } elseif ($arg->value instanceof \PhpParser\Node\Scalar\LNumber) { + } else if ($arg->value instanceof \PhpParser\Node\Scalar\LNumber) { $value = $arg->value->value; $currentArgInfo['type'] = "integer"; $currentArgInfo['resolved_value'] = $value; - // Anything else is simply interpreted as its literal string value + // Anything else is simply interpreted as its literal string value } else { $value = $argString; $currentArgInfo['type'] = "unknown"; @@ -140,8 +139,7 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract } } - public function setParams($params) - { + public function setParams($params) { $this->params = $params; } @@ -151,10 +149,9 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract * @param string $arg the array, represented as a string. * @return array[mixed] the array, as a plain ol' PHP array. */ - private function resolveArray($arg) - { + private function resolveArray($arg) { $arr = []; - $items = (array) $arg->value->items; + $items = (array)$arg->value->items; foreach ($items as $item) { if ($item->key) { $arr[$item->key] = $item->value->value; @@ -172,8 +169,7 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract * @throws Exception the path could not be resolved. Path is malformed or key does not exist. * @return mixed the value of the specified parameter. */ - private function resolveParamPath($path) - { + private function resolveParamPath($path) { $pathTokens = explode(".", $path); $value = $this->params; foreach ($pathTokens as $token) { @@ -181,7 +177,7 @@ class ParserNodeFunctionEvaluator extends NodeVisitorAbstract if (is_array($value) && isset($value[$token])) { $value = $value[$token]; continue; - } elseif (is_object($value) && isset($value->$token)) { + } else if (is_object($value) && isset($value->$token)) { $value = $value->$token; continue; } else { |