Skip to content

Commit 56ab89e

Browse files
authored
Merge pull request #790 from datamweb/feat-add-denied-for-filter
feat: add redirect denied for filter
2 parents 0cd5f3f + 1546bec commit 56ab89e

File tree

7 files changed

+88
-13
lines changed

7 files changed

+88
-13
lines changed

UPGRADING.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ following steps will be done.
2525
$this->helpers = array_merge($this->helpers, ['setting']);
2626
```
2727

28+
#### Config\Auth
29+
30+
The following items have been added. Copy the properties in **src/Config/Auth.php**.
31+
32+
- `permission_denied` and `group_denied` are added to `Config\Auth::$redirects`.
33+
- `permissionDeniedRedirect()` and `groupDeniedRedirect()` are added.
34+
35+
### Fix Custom Filter If extends `AbstractAuthFilter`
36+
37+
If you have written a custom filter that extends `AbstractAuthFilter`, now you need to add and implement the `redirectToDeniedUrl()` method to your custom filter.
38+
The following example is related to the above explanation for **group** filter.
39+
40+
```php
41+
/**
42+
* If the user does not belong to the group, redirect to the configured URL with an error message.
43+
*/
44+
protected function redirectToDeniedUrl(): RedirectResponse
45+
{
46+
return redirect()->to(config('Auth')->groupDeniedRedirect())
47+
->with('error', lang('Auth.notEnoughPrivilege'));
48+
}
49+
```
50+
2851
## Version 1.0.0-beta.6 to 1.0.0-beta.7
2952

3053
### The minimum CodeIgniter version

src/Config/Auth.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ class Auth extends BaseConfig
6565
* to apply any logic you may need.
6666
*/
6767
public array $redirects = [
68-
'register' => '/',
69-
'login' => '/',
70-
'logout' => 'login',
71-
'force_reset' => '/',
68+
'register' => '/',
69+
'login' => '/',
70+
'logout' => 'login',
71+
'force_reset' => '/',
72+
'permission_denied' => '/',
73+
'group_denied' => '/',
7274
];
7375

7476
/**
@@ -475,6 +477,28 @@ public function forcePasswordResetRedirect(): string
475477
return $this->getUrl($url);
476478
}
477479

480+
/**
481+
* Returns the URL the user should be redirected to
482+
* if permission denied.
483+
*/
484+
public function permissionDeniedRedirect(): string
485+
{
486+
$url = setting('Auth.redirects')['permission_denied'];
487+
488+
return $this->getUrl($url);
489+
}
490+
491+
/**
492+
* Returns the URL the user should be redirected to
493+
* if group denied.
494+
*/
495+
public function groupDeniedRedirect(): string
496+
{
497+
$url = setting('Auth.redirects')['group_denied'];
498+
499+
return $this->getUrl($url);
500+
}
501+
478502
/**
479503
* Accepts a string which can be an absolute URL or
480504
* a named route or just a URI path, and returns the

src/Filters/AbstractAuthFilter.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use CodeIgniter\Filters\FilterInterface;
88
use CodeIgniter\HTTP\RedirectResponse;
99
use CodeIgniter\HTTP\RequestInterface;
10-
use CodeIgniter\HTTP\Response;
1110
use CodeIgniter\HTTP\ResponseInterface;
1211

1312
/**
@@ -43,20 +42,27 @@ public function before(RequestInterface $request, $arguments = null)
4342
return;
4443
}
4544

46-
// Otherwise, we'll just send them to the home page.
47-
return redirect()->to('/')->with('error', lang('Auth.notEnoughPrivilege'));
45+
return $this->redirectToDeniedUrl();
4846
}
4947

5048
/**
5149
* We don't have anything to do here.
5250
*
53-
* @param Response|ResponseInterface $response
54-
* @param array|null $arguments
51+
* @param array|null $arguments
5552
*/
5653
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
5754
{
5855
// Nothing required
5956
}
6057

58+
/**
59+
* Ensures the user is logged in and has one or more
60+
* of the permissions as specified in the filter.
61+
*/
6162
abstract protected function isAuthorized(array $arguments): bool;
63+
64+
/**
65+
* Returns redirect response when the user does not have access authorizations.
66+
*/
67+
abstract protected function redirectToDeniedUrl(): RedirectResponse;
6268
}

src/Filters/GroupFilter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7+
use CodeIgniter\HTTP\RedirectResponse;
8+
79
/**
810
* Group Authorization Filter.
911
*/
@@ -17,4 +19,13 @@ protected function isAuthorized(array $arguments): bool
1719
{
1820
return auth()->user()->inGroup(...$arguments);
1921
}
22+
23+
/**
24+
* If the user does not belong to the group, redirect to the configured URL with an error message.
25+
*/
26+
protected function redirectToDeniedUrl(): RedirectResponse
27+
{
28+
return redirect()->to(config('Auth')->groupDeniedRedirect())
29+
->with('error', lang('Auth.notEnoughPrivilege'));
30+
}
2031
}

src/Filters/PermissionFilter.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7+
use CodeIgniter\HTTP\RedirectResponse;
8+
79
/**
810
* Permission Authorization Filter.
911
*/
@@ -23,4 +25,13 @@ protected function isAuthorized(array $arguments): bool
2325

2426
return false;
2527
}
28+
29+
/**
30+
* If the user does not have the permission, redirect to the configured URL with an error message.
31+
*/
32+
protected function redirectToDeniedUrl(): RedirectResponse
33+
{
34+
return redirect()->to(config('Auth')->permissionDeniedRedirect())
35+
->with('error', lang('Auth.notEnoughPrivilege'));
36+
}
2637
}

tests/Authentication/Filters/GroupFilterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function testFilterIncorrectGroupNoPrevious(): void
6969
->get('protected-route');
7070

7171
// Should redirect to home page since previous_url is not set
72-
$result->assertRedirectTo(site_url('/'));
72+
$result->assertRedirectTo(config('Auth')->groupDeniedRedirect());
7373
// Should have error message
74-
$result->assertSessionHas('error');
74+
$result->assertSessionHas('error', lang('Auth.notEnoughPrivilege'));
7575
}
7676
}

tests/Authentication/Filters/PermissionFilterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function testFilterIncorrectGroupNoPrevious(): void
6969
->get('protected-route');
7070

7171
// Should redirect to home page since previous_url is not set
72-
$result->assertRedirectTo(site_url('/'));
72+
$result->assertRedirectTo(config('Auth')->permissionDeniedRedirect());
7373
// Should have error message
74-
$result->assertSessionHas('error');
74+
$result->assertSessionHas('error', lang('Auth.notEnoughPrivilege'));
7575
}
7676
}

0 commit comments

Comments
 (0)