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

Pundit Authorization localization not working #3487

Open
4 of 11 tasks
jmarsh24 opened this issue Dec 4, 2024 · 6 comments
Open
4 of 11 tasks

Pundit Authorization localization not working #3487

jmarsh24 opened this issue Dec 4, 2024 · 6 comments
Assignees
Labels
Bug Something isn't working

Comments

@jmarsh24
Copy link

jmarsh24 commented Dec 4, 2024

Describe the bug

When caching is turned on and the user get redirected because the policy prevents them from seeing the resource, the flash message doesn't not respect the users local.

Reproduction repository for the bug

jmarsh24/localized_dashboard#20

Steps to use in the reproduction repository

Steps to reproduce the behavior:

  1. Open the repo
  2. visit https://localhost:3000/en/avo/resources/posts
  3. visit https://localhost:3000/de/avo/resources/posts
  4. the flash message not respect the localization

Expected behavior & Actual behavior

I would expect the localization to be respected. so german should see Germany and english should see and english flash message

System configuration

Avo version:
3.14
Rails version:
8.0.0
Ruby version:
3.3.5
License type:

  • Community
  • Pro
  • Advanced

Are you using Avo monkey patches, overriding views or view components?

  • Yes. If so, please post code samples.
  • No

Screenshots or screen recordings

https://share.cleanshot.com/jhFrlDr8

Additional context

Impact

  • High impact (It makes my app un-usable.)
  • Medium impact (I'm annoyed, but I'll live.)
  • Low impact (It's really a tiny thing that I could live with.)

Urgency

  • High urgency (I can't continue development without it.)
  • Medium urgency (I found a workaround, but I'd love to have it fixed.)
  • Low urgency (It can wait. I just wanted you to know about it.)
@Paul-Bob
Copy link
Contributor

Paul-Bob commented Dec 4, 2024

Hi @jmarsh24 thanks for opening this issue, the not authorized flash is generated here https://github.com/avo-hq/avo/blob/main/app/controllers/avo/base_application_controller.rb#L236

I'm not sure what is causing it to not render on the correct locale. Let me know if you want to give this a try, we can guide you through the source code

@Paul-Bob Paul-Bob moved this to To Do in Issues Dec 4, 2024
@Paul-Bob Paul-Bob added the Bug Something isn't working label Dec 4, 2024
@jmarsh24
Copy link
Author

jmarsh24 commented Dec 4, 2024

@Paul-Bob Thanks for looking into this. I'm not sure what fix is being proposed. If caching is turned off, the flash message is correctly translated but when caching is turned on then the flash message seemed to be cached and doesn't change dependent upon the current locale.

@jmarsh24
Copy link
Author

jmarsh24 commented Dec 4, 2024

@Paul-Bob I added the translation key to the minimum reproducible repository and the issue persists even without caching.

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Dec 4, 2024

What I meant was, since you've marked this as "High urgency (I can't continue development without it)" and we can't prioritize it right now, I'd be happy to walk you through the Avo source code if you want to give it a shot

The first step would be to debug avo on the reproduction repository around here and try to understand why the correct locale is not being applied

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Dec 4, 2024

@jmarsh24 nevermind, I'll give it a look

@Paul-Bob Paul-Bob self-assigned this Dec 4, 2024
@Paul-Bob Paul-Bob moved this from To Do to In Progress in Issues Dec 4, 2024
@Paul-Bob
Copy link
Contributor

Paul-Bob commented Dec 4, 2024

The problem is that when an error pops up, I18n resets the locale to the default because it unexpectedly jumps out of the with_locale block.

I’m not sure about a solid native fix just yet, but I can suggest a workaround in the meantime

  1. rails generate avo:eject --controller application_controller
module Avo
  class ApplicationController < BaseApplicationController
    def render_unauthorized(exception)
      # localize is your custom method from app/controllers/concerns/avo_localization.rb
      # when following the recipe from here https://docs.avohq.io/3.0/guides/multi-language-urls.html use set_avo_locale isntead of localize
      localize do
        super
      end
    end
  end
end

@Paul-Bob Paul-Bob moved this from In Progress to In Review in Issues Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
Status: In Review
Development

No branches or pull requests

2 participants