-
Notifications
You must be signed in to change notification settings - Fork 138
Add filter permission and group #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
540fa25
e12c29d
60c230e
19052f3
f7fbfae
714bdd1
057bc17
9bfc68f
e624cdd
02bafb1
2291828
959dca5
2bde1ae
96920ea
b995529
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Exceptions; | ||
|
||
use CodeIgniter\Shield\Authorization\AuthorizationException; | ||
|
||
class GroupException extends AuthorizationException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Exceptions; | ||
|
||
use CodeIgniter\Shield\Authorization\AuthorizationException; | ||
|
||
class PermissionException extends AuthorizationException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Filters; | ||
|
||
use CodeIgniter\Filters\FilterInterface; | ||
use CodeIgniter\HTTP\RedirectResponse; | ||
use CodeIgniter\HTTP\RequestInterface; | ||
use CodeIgniter\HTTP\Response; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* Group Authorization Filter. | ||
*/ | ||
abstract class AbstractAuthFilter implements FilterInterface | ||
{ | ||
/** | ||
* Ensures the user is logged in and a member of one or | ||
* more groups as specified in the filter. | ||
* | ||
* @param array|null $arguments | ||
* | ||
* @return RedirectResponse|void | ||
*/ | ||
public function before(RequestInterface $request, $arguments = null) | ||
{ | ||
if (empty($arguments)) { | ||
return; | ||
} | ||
|
||
if (! auth()->loggedIn()) { | ||
return redirect()->route('login'); | ||
} | ||
|
||
if ($this->isAuthorized($arguments)) { | ||
return; | ||
} | ||
|
||
// If the previous_url is from this site, then | ||
// we can redirect back to it. | ||
if (strpos(previous_url(), site_url()) === 0) { | ||
return redirect()->back()->with('error', lang('Auth.notEnoughPrivilege')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used this PR in a practical way. I did not see a problem. What bothers me is displaying the error message to the user.
In fact, I'm not sure how Shield users should know what to do to display error message ( |
||
} | ||
|
||
// Otherwise, we'll just send them to the home page. | ||
return redirect()->to('/')->with('error', lang('Auth.notEnoughPrivilege')); | ||
} | ||
|
||
/** | ||
* We don't have anything to do here. | ||
* | ||
* @param Response|ResponseInterface $response | ||
* @param array|null $arguments | ||
*/ | ||
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void | ||
{ | ||
// Nothing required | ||
} | ||
|
||
abstract protected function isAuthorized(array $arguments): bool; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Filters; | ||
|
||
/** | ||
* Group Authorization Filter. | ||
*/ | ||
class GroupFilter extends AbstractAuthFilter | ||
{ | ||
/** | ||
* Ensures the user is logged in and a member of one or | ||
* more groups as specified in the filter. | ||
*/ | ||
protected function isAuthorized(array $arguments): bool | ||
{ | ||
return auth()->user()->inGroup(...$arguments); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Filters; | ||
|
||
/** | ||
* Permission Authorization Filter. | ||
*/ | ||
class PermissionFilter extends AbstractAuthFilter | ||
{ | ||
/** | ||
* Ensures the user is logged in and has one or more | ||
* of the permissions as specified in the filter. | ||
*/ | ||
protected function isAuthorized(array $arguments): bool | ||
{ | ||
foreach ($arguments as $permission) { | ||
if (auth()->user()->can($permission)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.