Skip to content

Commit

Permalink
Merge pull request #20 from joanrodas/dev
Browse files Browse the repository at this point in the history
Add config options + refactor code
  • Loading branch information
joanrodas authored Nov 29, 2023
2 parents d6698af + a75863a commit c92f9eb
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 341 deletions.
1 change: 1 addition & 0 deletions PluboRoutes/Endpoint/DeleteEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down
2 changes: 1 addition & 1 deletion PluboRoutes/Endpoint/Endpoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down Expand Up @@ -56,7 +57,6 @@ public function __construct(string $namespace, string $path, callable $config, c
$this->path = $path;
$this->config = $config;
$this->permission_callback = $permission_callback ?? '__return_true';
$this->args = [];
}

/**
Expand Down
1 change: 1 addition & 0 deletions PluboRoutes/Endpoint/EndpointInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down
1 change: 1 addition & 0 deletions PluboRoutes/Endpoint/GetEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down
1 change: 1 addition & 0 deletions PluboRoutes/Endpoint/PostEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down
1 change: 1 addition & 0 deletions PluboRoutes/Endpoint/PutEndpoint.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Endpoint;

/**
Expand Down
23 changes: 15 additions & 8 deletions PluboRoutes/Helpers/RegexHelper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Helpers;

abstract class RegexHelper
Expand All @@ -7,36 +8,42 @@ abstract class RegexHelper
const NUMBER = '([0-9]+)';
const WORD = '([a-zA-Z]+)';
const TEXT = '([A-za-z0-9-%]+)';
const ALPHANUMERIC = '([a-zA-Z0-9]+)';
const HEXADECIMAL = '([a-fA-F0-9]+)';
const UUID = '([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})';
const FILE_PATH = '([\/\w\.-]+)';
const DATE = '(\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]))';
const YEAR = '(\d{4})';
const MONTH = '(0[1-9]|1[0-2])';
const DAY = '(0[1-9]|[12][0-9]|3[01])';
const IP = '(([0-9]{1,3}\.){3}[0-9]{1,3})';
const JWT = '((?:[\w-]*\.){2}[\w-]*)';
const SLUG = '([a-z0-9-]+)';
const EMAIL = '([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}';

const AVAILABLE_REGEX = [
'digit' => self::DIGIT,
'number' => self::NUMBER,
'word' => self::WORD,
'text' => self::TEXT,
'alphanumeric' => self::ALPHANUMERIC,
'hex' => self::HEXADECIMAL,
'uuid' => self::UUID,
'file' => self::FILE_PATH,
'date' => self::DATE,
'slug' => self::SLUG,
'digit' => self::DIGIT,
'year' => self::YEAR,
'month' => self::MONTH,
'day' => self::DAY,
'ip' => self::IP,
'jwt' => self::JWT,
'email' => self::EMAIL,
'ip' => self::IP
'slug' => self::SLUG,
];

/**
* Get translated Regex path for an endpoint route.
*
* @param string $path
*/
public static function getRegexMatches(string $regex_path)
public function getRegexMatches(string $regex_path)
{
preg_match_all('#\{(.+?)\}#', $regex_path, $matches);
return $matches;
Expand All @@ -48,7 +55,7 @@ public static function getRegexMatches(string $regex_path)
* @param string $path
* @return string
*/
public static function cleanPath(string $path)
public function cleanPath(string $path)
{
return ltrim(trim($path), '/');
}
Expand All @@ -59,5 +66,5 @@ public static function cleanPath(string $path)
* @param mixed $type
* @return string
*/
abstract public static function getRegex($type): string;
abstract public function getRegex($type): string;
}
3 changes: 2 additions & 1 deletion PluboRoutes/Helpers/RegexHelperEndpoints.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace PluboRoutes\Helpers;

class RegexHelperEndpoints extends RegexHelper
{
public static function getRegex($type): string
public function getRegex($type): string
{
$regex_code = array_key_exists($type[1], self::AVAILABLE_REGEX) ? self::AVAILABLE_REGEX[$type[1]] : $type[1];
return "(?P<$type[0]>$regex_code)";
Expand Down
3 changes: 2 additions & 1 deletion PluboRoutes/Helpers/RegexHelperRoutes.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace PluboRoutes\Helpers;

class RegexHelperRoutes extends RegexHelper
{
public static function getRegex($type): string
public function getRegex($type): string
{
return array_key_exists($type, self::AVAILABLE_REGEX) ? self::AVAILABLE_REGEX[$type] : $type;
}
Expand Down
150 changes: 150 additions & 0 deletions PluboRoutes/PermissionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace PluboRoutes;

use PluboRoutes\Route\Route;
use PluboRoutes\Route\RouteInterface;

/**
* The PermissionChecker class is responsible for checking roles, capabilities and custom permissions
*
*/
class PermissionChecker
{

/**
* The matched route found by the router.
*
* @var Route
*/
private $matched_route;

/**
* The matched args found by the router.
*
* @var array
*/
private $matched_args;

/**
* The matched args found by the router.
*
* @var \WP_User
*/
private $current_user;

/**
* Constructor.
*
* @param RouteInterface $route
* @param array $args
*/
public function __construct(RouteInterface $route, array $args)
{
$this->matched_route = $route;
$this->matched_args = $args;
$this->current_user = wp_get_current_user();
}

/**
* Check permissions for the matched route.
*/
public function checkPermissions()
{
$permission_callback = $this->matched_route->getPermissionCallback();
if (!$permission_callback || !is_callable($permission_callback)) {
return;
}
$has_access = $permission_callback($this->matched_args);
if (!$has_access) {
$this->forbidAccess();
}

if ($this->checkLoggedIn()) {
$this->checkRoles();
$this->checkCapabilities();
}
}

/**
* Check if the user is logged in and has access based on route settings.
*
* @return bool Whether the user is logged in.
*/
private function checkLoggedIn()
{
$is_logged_in = $this->current_user->exists();

if (
!$this->matched_route->guestHasAccess() && !$is_logged_in
|| !$this->matched_route->memberHasAccess() && $is_logged_in
) {
$this->forbidAccess();
}

return $is_logged_in;
}

/**
* Check if the user has the required roles for the matched route.
*/
private function checkRoles()
{
$allowed_roles = $this->matched_route->getRoles();
if ($this->matched_route->hasRolesCallback()) {
$allowed_roles = $allowed_roles($this->matched_args);
}
if ($allowed_roles !== false && !array_intersect((array)$this->current_user->roles, (array)$allowed_roles)) {
$this->forbidAccess();
}
}

/**
* Check if the user has the required capabilities for the matched route.
*/
private function checkCapabilities()
{
$allowed_caps = $this->getAllowedCapabilities();
if ($allowed_caps === false) {
return;
}

$is_allowed = false;
foreach ((array)$allowed_caps as $allowed_cap) {
if ($this->current_user->has_cap($allowed_cap)) {
$is_allowed = true;
break;
}
}
if (!$is_allowed) {
$this->forbidAccess();
}
}

/**
* Get the allowed capabilities for the matched route.
*
* @return mixed
*/
private function getAllowedCapabilities()
{
$allowed_caps = $this->matched_route->getCapabilities();
if ($this->matched_route->hasCapabilitiesCallback()) {
$allowed_caps = $allowed_caps($this->matched_args);
}
return $allowed_caps;
}

/**
* Forbid access based on route settings.
*/
private function forbidAccess()
{
if ($this->matched_route->hasRedirect()) {
wp_redirect(esc_url_raw($this->matched_route->getRedirect()), $this->matched_route->getNotAllowedStatus());
exit;
}
status_header($this->matched_route->getNotAllowedStatus());
exit();
}
}
12 changes: 12 additions & 0 deletions PluboRoutes/Route/ActionRoute.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Route;

/**
Expand Down Expand Up @@ -50,4 +51,15 @@ public function hasCallback()
{
return is_callable($this->action);
}

/**
* Get the status.
*
* @return int
*/
public function getStatus()
{
$status = $this->config['status'] ?? 200;
return (int)$status;
}
}
12 changes: 12 additions & 0 deletions PluboRoutes/Route/PageRoute.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Route;

/**
Expand Down Expand Up @@ -70,4 +71,15 @@ public function getPageId()
{
return $this->page_id;
}

/**
* Get the status.
*
* @return int
*/
public function getStatus()
{
$status = $this->config['status'] ?? 200;
return (int)$status;
}
}
3 changes: 2 additions & 1 deletion PluboRoutes/Route/RedirectRoute.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace PluboRoutes\Route;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public function isExternal()
}

/**
* Check if the action is a callable.
* Get the status.
*
* @return int
*/
Expand Down
Loading

0 comments on commit c92f9eb

Please sign in to comment.