Skip to content

Commit b995529

Browse files
committed
Refactor to a common abstract auth filter.
1 parent 96920ea commit b995529

File tree

3 files changed

+71
-85
lines changed

3 files changed

+71
-85
lines changed

src/Filters/AbstractAuthFilter.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeIgniter\Shield\Filters;
6+
7+
use CodeIgniter\Filters\FilterInterface;
8+
use CodeIgniter\HTTP\RedirectResponse;
9+
use CodeIgniter\HTTP\RequestInterface;
10+
use CodeIgniter\HTTP\Response;
11+
use CodeIgniter\HTTP\ResponseInterface;
12+
13+
/**
14+
* Group Authorization Filter.
15+
*/
16+
abstract class AbstractAuthFilter implements FilterInterface
17+
{
18+
/**
19+
* Ensures the user is logged in and a member of one or
20+
* more groups as specified in the filter.
21+
*
22+
* @param array|null $arguments
23+
*
24+
* @return RedirectResponse|void
25+
*/
26+
public function before(RequestInterface $request, $arguments = null)
27+
{
28+
if (empty($arguments)) {
29+
return;
30+
}
31+
32+
if (! auth()->loggedIn()) {
33+
return redirect()->route('login');
34+
}
35+
36+
if ($this->isAuthorized($arguments)) {
37+
return;
38+
}
39+
40+
// If the previous_url is from this site, then
41+
// we can redirect back to it.
42+
if (strpos(previous_url(), site_url()) === 0) {
43+
return redirect()->back()->with('error', lang('Auth.notEnoughPrivilege'));
44+
}
45+
46+
// Otherwise, we'll just send them to the home page.
47+
return redirect()->to('/')->with('error', lang('Auth.notEnoughPrivilege'));
48+
}
49+
50+
/**
51+
* We don't have anything to do here.
52+
*
53+
* @param Response|ResponseInterface $response
54+
* @param array|null $arguments
55+
*/
56+
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
57+
{
58+
// Nothing required
59+
}
60+
61+
abstract protected function isAuthorized(array $arguments): bool;
62+
}

src/Filters/GroupFilter.php

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,17 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7-
use CodeIgniter\Filters\FilterInterface;
8-
use CodeIgniter\HTTP\RedirectResponse;
9-
use CodeIgniter\HTTP\RequestInterface;
10-
use CodeIgniter\HTTP\Response;
11-
use CodeIgniter\HTTP\ResponseInterface;
12-
137
/**
148
* Group Authorization Filter.
159
*/
16-
class GroupFilter implements FilterInterface
10+
class GroupFilter extends AbstractAuthFilter
1711
{
1812
/**
1913
* Ensures the user is logged in and a member of one or
2014
* more groups as specified in the filter.
21-
*
22-
* @param array|null $arguments
23-
*
24-
* @return RedirectResponse|void
25-
*/
26-
public function before(RequestInterface $request, $arguments = null)
27-
{
28-
if (empty($arguments)) {
29-
return;
30-
}
31-
32-
if (! auth()->loggedIn()) {
33-
return redirect()->route('login');
34-
}
35-
36-
if (auth()->user()->inGroup(...$arguments)) {
37-
return;
38-
}
39-
40-
// If the previous_url is from this site, then
41-
// we can redirect back to it.
42-
if (strpos(previous_url(), site_url()) === 0) {
43-
return redirect()->back()->with('error', lang('Auth.notEnoughPrivilege'));
44-
}
45-
46-
// Otherwise, we'll just send them to the home page.
47-
return redirect()->to('/')->with('error', lang('Auth.notEnoughPrivilege'));
48-
}
49-
50-
/**
51-
* We don't have anything to do here.
52-
*
53-
* @param Response|ResponseInterface $response
54-
* @param array|null $arguments
5515
*/
56-
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
16+
protected function isAuthorized(array $arguments): bool
5717
{
58-
// Nothing required
18+
return auth()->user()->inGroup(...$arguments);
5919
}
6020
}

src/Filters/PermissionFilter.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,23 @@
44

55
namespace CodeIgniter\Shield\Filters;
66

7-
use CodeIgniter\Filters\FilterInterface;
8-
use CodeIgniter\HTTP\RedirectResponse;
9-
use CodeIgniter\HTTP\RequestInterface;
10-
use CodeIgniter\HTTP\Response;
11-
use CodeIgniter\HTTP\ResponseInterface;
12-
137
/**
148
* Permission Authorization Filter.
159
*/
16-
class PermissionFilter implements FilterInterface
10+
class PermissionFilter extends AbstractAuthFilter
1711
{
1812
/**
19-
* Ensures the user is logged in and has one or
20-
* more permissions as specified in the filter.
21-
*
22-
* @param array|null $arguments
23-
*
24-
* @return RedirectResponse|void
13+
* Ensures the user is logged in and has one or more
14+
* of the permissions as specified in the filter.
2515
*/
26-
public function before(RequestInterface $request, $arguments = null)
16+
protected function isAuthorized(array $arguments): bool
2717
{
28-
if (empty($arguments)) {
29-
return;
30-
}
31-
32-
if (! auth()->loggedIn()) {
33-
return redirect()->route('login');
34-
}
35-
3618
foreach ($arguments as $permission) {
3719
if (auth()->user()->can($permission)) {
38-
return;
20+
return true;
3921
}
4022
}
4123

42-
// If the previous_url is from this site, then
43-
// we can redirect back to it.
44-
if (strpos(previous_url(), site_url()) === 0) {
45-
return redirect()->back()->with('error', lang('Auth.notEnoughPrivilege'));
46-
}
47-
48-
// Otherwise, we'll just send them to the home page.
49-
return redirect()->to('/')->with('error', lang('Auth.notEnoughPrivilege'));
50-
}
51-
52-
/**
53-
* We don't have anything to do here.
54-
*
55-
* @param Response|ResponseInterface $response
56-
* @param array|null $arguments
57-
*/
58-
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
59-
{
60-
// Nothing required
24+
return false;
6125
}
6226
}

0 commit comments

Comments
 (0)