Skip to content

Commit 3036133

Browse files
authored
Merge pull request #102 from codeigniter4/routes-except
Ensure 'except' routes are not in routes at all. Fixes #72
2 parents f7dafd4 + db78708 commit 3036133

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function routes(RouteCollection &$routes, array $config = [])
100100

101101
$routes->group('/', ['namespace' => 'CodeIgniter\Shield\Controllers'], static function ($routes) use ($authRoutes, $config) {
102102
foreach ($authRoutes as $name => $row) {
103-
if (! isset($config['except']) || (isset($config['except']) && ! array_key_exists($name, $config['except']))) {
103+
if (! isset($config['except']) || (isset($config['except']) && ! in_array($name, $config['except'], true))) {
104104
foreach ($row as $params) {
105105
$options = isset($params[3])
106106
? ['as' => $params[3]]

tests/Unit/AuthRoutesTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\Support\TestCase;
6+
7+
/**
8+
* @internal
9+
*/
10+
final class AuthRoutesTest extends TestCase
11+
{
12+
public function testRoutes()
13+
{
14+
$collection = single_service('routes');
15+
$auth = service('auth');
16+
17+
$auth->routes($collection);
18+
19+
$routes = $collection->getRoutes('get');
20+
21+
$this->assertArrayHasKey('register', $routes);
22+
$this->assertArrayHasKey('login', $routes);
23+
$this->assertArrayHasKey('login/magic-link', $routes);
24+
$this->assertArrayHasKey('logout', $routes);
25+
$this->assertArrayHasKey('auth/a/show', $routes);
26+
}
27+
28+
public function testRoutesExcept()
29+
{
30+
$collection = single_service('routes');
31+
$auth = service('auth');
32+
33+
$auth->routes($collection, ['except' => ['login']]);
34+
35+
$routes = $collection->getRoutes('get');
36+
37+
$this->assertArrayNotHasKey('login', $routes);
38+
$this->assertArrayHasKey('register', $routes);
39+
$this->assertArrayHasKey('login/magic-link', $routes);
40+
$this->assertArrayHasKey('logout', $routes);
41+
$this->assertArrayHasKey('auth/a/show', $routes);
42+
}
43+
}

0 commit comments

Comments
 (0)