Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion WebFiori/Framework/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ private function validateId($id): bool {
$valid = true;

for ($x = 0 ; $x < $len ; $x++) {
$valid = $valid && $id[$x] != ';' && $id[$x] != ' ' && $id[$x] != '-' && $id[$x] != ',';
$valid = $valid && $id[$x] != ';' && $id[$x] != ' ' && $id[$x] != ',';
}

return $valid;
Expand Down
16 changes: 16 additions & 0 deletions WebFiori/Framework/Middleware/MiddlewareRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ public function remove(string $name): void {
public function reset(): void {
$this->middlewareList = [];
}
/**
* Finds a registered middleware by its class name.
*
* @param string $className The fully qualified class name.
*
* @return AbstractMiddleware|null The first registered middleware that is an instance of the given class, or null.
*/
public function findByClass(string $className): ?AbstractMiddleware {
foreach ($this->middlewareList as $mw) {
if ($mw instanceof $className) {
return $mw;
}
}

return null;
}
/**
* Returns all registered middleware.
*
Expand Down
2 changes: 1 addition & 1 deletion WebFiori/Framework/Privilege.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function setID(string $code): bool {
for ($x = 0 ; $x < $len ; $x++) {
$ch = $xid[$x];

if (!($ch == '_' || ($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' && $ch <= 'Z') || ($ch >= '0' && $ch <= '9'))) {
if (!($ch == '_' || $ch == '.' || $ch == '-' || ($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' && $ch <= 'Z') || ($ch >= '0' && $ch <= '9'))) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion WebFiori/Framework/PrivilegesGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static function isValidID(string $id) : bool {
for ($x = 0 ; $x < $len ; $x++) {
$ch = $xid[$x];

if (!($ch == '_' || ($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' && $ch <= 'Z') || ($ch >= '0' && $ch <= '9'))) {
if (!($ch == '_' || $ch == '.' || $ch == '-' || ($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' && $ch <= 'Z') || ($ch >= '0' && $ch <= '9'))) {
return false;
}
}
Expand Down
73 changes: 65 additions & 8 deletions WebFiori/Framework/Router/RouterUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,14 +645,16 @@ private function resolveDependencies(array $middlewareList): array {
$current = array_shift($queue);

foreach ($current->getDependencies() as $depName) {
if (isset($byName[$depName])) {
$resolvedName = $this->resolveDepToName($depName, $byName);

if ($resolvedName !== null && isset($byName[$resolvedName])) {
continue;
}

$dep = MiddlewareManager::getMiddleware($depName);
$dep = $this->lookupMiddleware($depName);

if ($dep !== null) {
$byName[$depName] = $dep;
if ($dep !== null && !isset($byName[$dep->getName()])) {
$byName[$dep->getName()] = $dep;
$queue[] = $dep;
}
}
Expand Down Expand Up @@ -701,11 +703,13 @@ private function sortByDependencies(array $middlewareList): array {
}

foreach ($mw->getDependencies() as $dep) {
if (isset($byName[$dep])) {
$graph[$dep][] = $name;
$resolvedDep = $this->resolveDepToName($dep, $byName);

if ($resolvedDep !== null && isset($byName[$resolvedDep])) {
$graph[$resolvedDep][] = $name;

if (!isset($inDegree[$dep])) {
$inDegree[$dep] = 0;
if (!isset($inDegree[$resolvedDep])) {
$inDegree[$resolvedDep] = 0;
}
$inDegree[$name]++;
}
Expand Down Expand Up @@ -757,4 +761,57 @@ private function sortByDependencies(array $middlewareList): array {

return $sorted;
}
/**
* Resolves a dependency identifier to a middleware name.
*
* If the identifier is already a known name in the map, returns it directly.
* If it looks like a class name (class_exists), finds the registered middleware
* that is an instance of that class and returns its name.
*
* @param string $dep The dependency identifier (name or class).
* @param array $byName Map of name => AbstractMiddleware.
*
* @return string|null The resolved middleware name, or null if unresolvable.
*/
private function resolveDepToName(string $dep, array $byName): ?string {
if (isset($byName[$dep])) {
return $dep;
}

if (class_exists($dep)) {
foreach ($byName as $mw) {
if ($mw instanceof $dep) {
return $mw->getName();
}
}

$mw = MiddlewareManager::getInstance()->findByClass($dep);

if ($mw !== null) {
return $mw->getName();
}
}

return null;
}
/**
* Looks up a middleware by name or class from the registry.
*
* @param string $dep The dependency identifier (name or class).
*
* @return AbstractMiddleware|null
*/
private function lookupMiddleware(string $dep): ?AbstractMiddleware {
$mw = MiddlewareManager::getMiddleware($dep);

if ($mw !== null) {
return $mw;
}

if (class_exists($dep)) {
return MiddlewareManager::getInstance()->findByClass($dep);
}

return null;
}
}
4 changes: 3 additions & 1 deletion WebFiori/Framework/Router/ServiceRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ private static function scanNamespace(string $namespace, string $dir, string $pa
if (!empty($attrs)) {
$attr = $attrs[0]->newInstance();

if (!empty($attr->name)) {
if (!empty($attr->path)) {
$name = $attr->path;
} else if (!empty($attr->name)) {
if (str_contains($attr->name, '/')) {
continue; // Invalid: name must not contain slashes
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ServiceRouterFixtures/AuthLoginService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace WebFiori\Tests\ServiceRouterFixtures;

use WebFiori\Http\Annotations\RestController;
use WebFiori\Http\WebService;

#[RestController(name: 'login', path: 'auth/login')]
class AuthLoginService extends WebService {
public function __construct() {
parent::__construct('login');
}

public function processRequest() {
}
}
15 changes: 15 additions & 0 deletions tests/ServiceRouterFixtures/UserProfileService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace WebFiori\Tests\ServiceRouterFixtures;

use WebFiori\Http\Annotations\RestController;
use WebFiori\Http\WebService;

#[RestController(path: 'v2/users/profile')]
class UserProfileService extends WebService {
public function __construct() {
parent::__construct('user-profile');
}

public function processRequest() {
}
}
6 changes: 3 additions & 3 deletions tests/WebFiori/Framework/Tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public function test07() {
$this->assertTrue(Access::hasGroup('USERS_Group'));
$this->assertEquals(0, count(Access::privileges()));
$this->assertFalse(Access::newPrivilege('not_exist', 'new_pr'));
$this->assertFalse(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new-pr'));
$this->assertTrue(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new-pr'));
$this->assertFalse(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new,pr'));
$this->assertFalse(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new;pr'));
$this->assertFalse(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new pr'));
$this->assertTrue(Access::newPrivilege('USERS_MANAGEMENT_GROUP', 'new_pr'));
$this->assertTrue(Access::newPrivilege('USERS_Group', 'new_pr_2'));
$this->assertEquals(2, count(Access::privileges()));
$this->assertEquals(3, count(Access::privileges()));
$this->assertEquals(1, count(Access::privileges('USERS_Group')));
$this->assertEquals(1, count(Access::privileges('USERS_MANAGEMENT_GROUP')));
$this->assertEquals(2, count(Access::privileges('USERS_MANAGEMENT_GROUP')));
$this->assertEquals(0, count(Access::privileges('ADMINS_Group')));
$this->assertTrue(Access::hasPrivilege('new_pr'));
$this->assertFalse(Access::hasPrivilege('new_pr','USERS_Group'));
Expand Down
Loading
Loading