Skip to content

Commit 540fa25

Browse files
jlopes90lonnieezell
authored andcommitted
add filter permission and group
1 parent ddab236 commit 540fa25

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/Filters/GroupFilter.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
use CodeIgniter\Shield\Exceptions\GroupException;
14+
15+
/**
16+
* Group Authorization Filter.
17+
*/
18+
class GroupFilter implements FilterInterface
19+
{
20+
/**
21+
* Do whatever processing this filter needs to do.
22+
* By default it should not return anything during
23+
* normal execution. However, when an abnormal state
24+
* is found, it should return an instance of
25+
* CodeIgniter\HTTP\Response. If it does, script
26+
* execution will end and that Response will be
27+
* sent back to the client, allowing for error pages,
28+
* redirects, etc.
29+
*
30+
* @param array|null $arguments
31+
*
32+
* @return RedirectResponse|void
33+
*/
34+
public function before(RequestInterface $request, $arguments = null)
35+
{
36+
if (empty($arguments)) {
37+
return;
38+
}
39+
40+
if (! auth()->loggedIn()) {
41+
return redirect()->to('login');
42+
}
43+
44+
if (auth()->user()->inGroup(...$arguments)) {
45+
return;
46+
}
47+
48+
throw GroupException::forUnauthorized();
49+
}
50+
51+
/**
52+
* We don't have anything to do here.
53+
*
54+
* @param Response|ResponseInterface $response
55+
* @param array|null $arguments
56+
*/
57+
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
58+
{
59+
// Nothing required
60+
}
61+
}

src/Filters/PermissionFilter.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
use CodeIgniter\Shield\Exceptions\PermissionException;
14+
15+
/**
16+
* Permission Authorization Filter.
17+
*/
18+
class PermissionFilter implements FilterInterface
19+
{
20+
/**
21+
* Do whatever processing this filter needs to do.
22+
* By default it should not return anything during
23+
* normal execution. However, when an abnormal state
24+
* is found, it should return an instance of
25+
* CodeIgniter\HTTP\Response. If it does, script
26+
* execution will end and that Response will be
27+
* sent back to the client, allowing for error pages,
28+
* redirects, etc.
29+
*
30+
* @param array|null $arguments
31+
*
32+
* @return RedirectResponse|void
33+
*/
34+
public function before(RequestInterface $request, $arguments = null)
35+
{
36+
if (empty($arguments)) {
37+
return;
38+
}
39+
40+
if (! auth()->loggedIn()) {
41+
return redirect()->to('login');
42+
}
43+
44+
foreach ($arguments as $permission) {
45+
if (! auth()->user()->can($permission)) {
46+
throw PermissionException::forUnauthorized();
47+
}
48+
}
49+
}
50+
51+
/**
52+
* We don't have anything to do here.
53+
*
54+
* @param Response|ResponseInterface $response
55+
* @param array|null $arguments
56+
*/
57+
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
58+
{
59+
// Nothing required
60+
}
61+
}

0 commit comments

Comments
 (0)