Skip to content

Commit a327eae

Browse files
committed
refactor: add redirectToDeniedUrl() for able inherit from AbstractAuthFilter
1 parent e7f5c3e commit a327eae

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/Filters/AbstractAuthFilter.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
use CodeIgniter\HTTP\RedirectResponse;
99
use CodeIgniter\HTTP\RequestInterface;
1010
use CodeIgniter\HTTP\ResponseInterface;
11-
use CodeIgniter\Shield\Config\Auth;
1211

1312
/**
1413
* Group Authorization Filter.
1514
*/
1615
abstract class AbstractAuthFilter implements FilterInterface
1716
{
18-
protected string $filterName;
19-
2017
/**
2118
* Ensures the user is logged in and a member of one or
2219
* more groups as specified in the filter.
@@ -45,15 +42,7 @@ public function before(RequestInterface $request, $arguments = null)
4542
return;
4643
}
4744

48-
switch ($this->filterName) {
49-
case 'group':
50-
return redirect()->to(config(Auth::class)->afterGroupDeniedRedirect())
51-
->with('error', lang('Auth.notEnoughPrivilege'));
52-
53-
case 'permission':
54-
return redirect()->to(config(Auth::class)->afterPermissionDeniedRedirect())
55-
->with('error', lang('Auth.notEnoughPrivilege'));
56-
}
45+
return $this->redirectToDeniedUrl();
5746
}
5847

5948
/**
@@ -67,4 +56,6 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
6756
}
6857

6958
abstract protected function isAuthorized(array $arguments): bool;
59+
60+
abstract protected function redirectToDeniedUrl(): RedirectResponse;
7061
}

src/Filters/GroupFilter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7+
use CodeIgniter\HTTP\RedirectResponse;
8+
use CodeIgniter\Shield\Config\Auth;
9+
710
/**
811
* Group Authorization Filter.
912
*/
1013
class GroupFilter extends AbstractAuthFilter
1114
{
12-
protected string $filterName = 'group';
13-
1415
/**
1516
* Ensures the user is logged in and a member of one or
1617
* more groups as specified in the filter.
@@ -19,4 +20,14 @@ protected function isAuthorized(array $arguments): bool
1920
{
2021
return auth()->user()->inGroup(...$arguments);
2122
}
23+
24+
/**
25+
* If there is no necessary access to the group,
26+
* it will redirect the user to the set URL with an error message.
27+
*/
28+
protected function redirectToDeniedUrl(): RedirectResponse
29+
{
30+
return redirect()->to(config(Auth::class)->groupDeniedRedirect())
31+
->with('error', lang('Auth.notEnoughPrivilege'));
32+
}
2233
}

src/Filters/PermissionFilter.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7+
use CodeIgniter\HTTP\RedirectResponse;
8+
use CodeIgniter\Shield\Config\Auth;
9+
710
/**
811
* Permission Authorization Filter.
912
*/
1013
class PermissionFilter extends AbstractAuthFilter
1114
{
12-
protected string $filterName = 'permission';
13-
1415
/**
1516
* Ensures the user is logged in and has one or more
1617
* of the permissions as specified in the filter.
@@ -25,4 +26,14 @@ protected function isAuthorized(array $arguments): bool
2526

2627
return false;
2728
}
29+
30+
/**
31+
* If there is no necessary access to the permission,
32+
* it will redirect the user to the set URL with an error message.
33+
*/
34+
protected function redirectToDeniedUrl(): RedirectResponse
35+
{
36+
return redirect()->to(config(Auth::class)->permissionDeniedRedirect())
37+
->with('error', lang('Auth.notEnoughPrivilege'));
38+
}
2839
}

0 commit comments

Comments
 (0)