Skip to content
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

Modified NotAuthorizedError default message #681

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

langsharpe
Copy link

record is sometimes an Object, sometimes a Class and sometimes an Array. Use the policy instead in the error message.

e.g.

authorize(Post.first)
before: not allowed to destroy? this Post
after : not allowed to destroy? with PostPolicy
authorize([:project, Post.first])
before: not allowed to destroy? this Array
after : not allowed to destroy? with Project::PostPolicy

This is a slightly different solution to the problem raised by #670. This may not the right solution but it was easy to create without refactoring what 'record' means when it is an Array.

Please let me know if there is anything I can do to help this go through.

record is sometimes an Object, sometimes a Class and sometimes an Array. Use the policy instead in the error message.

e.g.
```
authorize(Post.first)
before: not allowed to destroy? this Post
after : not allowed to destroy? with PostPolicy
```

```
authorize([:project, Post.first])
before: not allowed to destroy? this Array
after : not allowed to destroy? with Project::PostPolicy
```
@Burgestrand
Copy link
Member

Hi! Thanks for the PR!

I personally like this and believe it makes sense. I'd like to get another set of eyes on this, e.g. by @dgmstuart before merging.

@Burgestrand
Copy link
Member

Hi again!

Due to some recent changes (not yet released but available in main) the record is no longer passed as an array, so hopefully most of this problem is resolved.

However, I still believe having the policy name in the default error message would be useful. Would it be overkill to have both? What do you think?

For example (I'm open to suggestions):

not allowed to Project::PostPolicy#destroy? this Post

@langsharpe
Copy link
Author

not allowed to Project::PostPolicy#destroy? this Post

This error message would be fine and fix the issue we have.

The most challenging place to debug is when the error is discovered in production. It would help if you had visibility when viewing the error in an error tracker (e.g. Bugsnag). Knowing which policy is selected is beneficial. The policy can be chosen dynamically, so an error message is the only chance you get to know what was selected.

If you wanted to take it a step further, you could::

This User is not allowed to Project::PostPolicy#destroy? this Post

That would ensure at least the type of user and record is recorded. Which user and record should be determined by other information in the tracked error like URL and logged in user.

Looking forward to the new version!

@Burgestrand
Copy link
Member

Burgestrand commented May 15, 2024

Taking a look at some old issues. Commenting for documentation's sake.

This PR is still relevant, even though the underlying code has changed. We're still not including the policy name in the error message:

message = options.fetch(:message) { "not allowed to #{query} this #{record.class}" }

However, the information is available so it's mostly a matter of updating the message to also include the policy used.

So the PR does need updating, and the underlying idea/approach is good!

@Burgestrand Burgestrand added the needs updating underlying code has changed, needs updating label May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs updating underlying code has changed, needs updating
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants