-
Notifications
You must be signed in to change notification settings - Fork 144
feat: add filter permission and group #270
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
Conversation
|
@jlopes90 Like you, I am a beginner in test writing. However, I have tried hard to learn it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! It was definitely something I overlooked when porting it from Myth:Auth. We do need a few things to get it ready, though.
We need docs for this. At the very least, install.md needs updated to include the new filters. Having instructions added to authorization.md in the appropriate section under Authorizing User is needed also.
They also need to be added to src/Config/Registrar.php so they are auto-registered.
Please give a try and getting some tests in place for these. You can look at tests/Authentication/Filters files for examples that should give you a good start on how to do them.
|
A good start, and some good feedback from the team! Thank you for taking this on @jlopes90 - let us know how we can help. |
<?php
namespace CodeIgniter\Shield\Authorization;
use Exception;
class AuthorizationException extends Exception
{
protected $code = 401;
public static function forUnauthorizedGroup(): self
{
return new self(lang('Auth.unauthorizedGroup'));
}
public static function forUnknownGroup(string $group): self
{
return new self(lang('Auth.unknownGroup', [$group]));
}
public static function forUnauthorizedPermission(): self
{
return new self(lang('Auth.unauthorizedPermission'));
}
public static function forUnknownPermission(string $permission): self
{
return new self(lang('Auth.unknownPermission', [$permission]));
}
}'unauthorizedGroup' => 'You do not have sufficient groups to access that page.',
'unauthorizedPermission' => 'You do not have sufficient permissions to access that page.',What about? |
|
I'm not good at documentation or explanation in english. |
|
This is from Myth:Auth. Do we need to change the error message for different filters? |
|
Personal opinion, |
|
I always prefer a very generic authorization error message; it can be used more globally and it doesn't reveal any information to probe attacks. |
<?php
namespace CodeIgniter\Shield\Authorization;
use Exception;
class AuthorizationException extends Exception
{
protected $code = 401;
public static function forUnknownGroup(string $group): self
{
return new self(lang('Auth.unknownGroup', [$group]));
}
public static function forUnknownPermission(string $permission): self
{
return new self(lang('Auth.unknownPermission', [$permission]));
}
public static function forUnauthorized(): self
{
return new self(lang('Auth.notEnoughPrivilege'));
}
}'notEnoughPrivilege' => 'You do not have sufficient permissions to access that page.',What about? |
|
Please go with @datamweb's suggestion, then this phrase can be reused for any authorization fault without belying the actual attempt:
|
'notEnoughPrivilege' => 'You do not have the necessary permission to perform the desired operation.' |
|
@jlopes90 I know how you feel, things are a bit complicated, let's continue together, we will call our friends whenever necessary.
use CodeIgniter\Shield\Filters\GroupFilter ;
use CodeIgniter\Shield\Filters\PermissionFilter;
/**
* Registers the Shield filters.
*/
public static function Filters(): array
{
return [
'aliases' => [
'session' => SessionAuth::class,
...
'group-filter' => GroupFilter::class,
'permission-filter' => PermissionFilter::class,
],
];
} |
I didn't even notice about "registar", it was very helpful. Thanks. The $routes->get('/', 'Home::index', ['filter' => 'group-filter:home.view');or $routes->get('/', 'Home::index', ['filter' => 'group:home.view'); |
I consider Allow other members to decide on this matter. |
Step 2: Writing documents. In fact, since you are not an English speaker, this is a bonus. Because you can express things in simple terms. Well, first try to write down everything you need to say in your own language somewhere. Finally, it is reviewed by the members. So just do it. I am waiting. |
Lines 157 to 176 in bc85d79
Controller FiltersThe Controller Filters you can use to protect your routes the shield provides are:
These filters are already loaded for you by the registrar class located at public $aliases = [
// ...
'session' => \CodeIgniter\Shield\Filters\SessionAuth::class,
'tokens' => \CodeIgniter\Shield\Filters\TokenAuth::class,
'chain' => \CodeIgniter\Shield\Filters\ChainAuth::class,
'auth-rates' => \CodeIgniter\Shield\Filters\AuthRates::class,
'group-filter' => \CodeIgniter\Shield\Filters\GroupFilter::class,
'permission-filter' => \CodeIgniter\Shield\Filters\PermissionFilter::class,
]; |
src/Filters/PermissionFilter.php
Outdated
| * Do whatever processing this filter needs to do. | ||
| * By default it should not return anything during | ||
| * normal execution. However, when an abnormal state | ||
| * is found, it should return an instance of | ||
| * CodeIgniter\HTTP\Response. If it does, script | ||
| * execution will end and that Response will be | ||
| * sent back to the client, allowing for error pages, | ||
| * redirects, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the doc comment.
These are aliases for filters. All of them are filters. So |
|
@jlopes90 The docs seems good. |
|
I have no idea how to resolve the |
@jlopes90 We finally decided to use And see https://github.com/codeigniter4/shield/runs/7880373539?check_suite_focus=true |
|
@jlopes90 Very easy. Just run |
|
|
I sent a PR #390 |
|
Please rebase. |
|
I did |
|
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#updating-your-branch |
|
and now? make new PR? |
|
Cherry pick the commits that are needed and create a new PR. |
|
Sorry, I can't close pull request |
|
I did wrong again, I'm going to recover and do new PR |
Uh oh!
There was an error while loading. Please reload this page.