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

Fixes namespace handling for policy_scope #391

Closed
wants to merge 3 commits into from

Conversation

ramaboo
Copy link
Contributor

@ramaboo ramaboo commented May 5, 2016

As discussed here #351 this fixes the policy_scope finder to work with namespaces.

@@ -88,7 +88,8 @@ def policy_scope(user, scope)
# @raise [NotDefinedError] if the policy scope cannot be found
# @return [Scope{#resolve}] instance of scope class which can resolve to a scope
def policy_scope!(user, scope)
PolicyFinder.new(scope).scope!.new(user, scope).resolve
model = scope.is_a?(Array) ? scope.last : scope
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will break if the scope is a Rails 3 relation. See #402

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... It's been so long since I used Rails 3 I had not considered this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very unfortunate behavior. I think the only option might be to check #model_name, or drop support for Rails 3 (not my personal favorite choice, since I still maintain a Rails 3 application, but it would be understandable since even Rails doesn't support Rails 3 anymore).

@asia653
Copy link

asia653 commented Aug 1, 2016

What's the status on this?

@ramaboo
Copy link
Contributor Author

ramaboo commented Aug 1, 2016

Not sure we have been using this in production at Doorman.co for months now without issue. I would vote to drop rails 3 support as rails 5 has been released and supporting 3 versions is just crazy.

@ce07c3
Copy link

ce07c3 commented Nov 12, 2016

@ramaboo yes, please drop Rails 3

@chevinbrown
Copy link

chevinbrown commented Mar 8, 2017

+1
Edit:
It's 2017, please.

@brunowego
Copy link

+1

1 similar comment
@stephen-puiszis
Copy link

👍

@chevinbrown
Copy link

@ramaboo Can close & merge this PR with a readme update that explains the version w/ rails 3 support and that future versions will drop R3 support?

@medir
Copy link

medir commented Aug 29, 2017

+1

@medir
Copy link

medir commented Aug 29, 2017

After reading the discussion #351 I found very interesting the @mysmallidea implementation and give the idea to apply the same fix to the scope class.

class ApplicationPolicy
  #index? show?....

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope.is_a?(Array) ? scope.last : scope #This fix the problem
    end

    def resolve
      scope
    end
  end
end

class Backoffice::EventPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      if user.is_admin?
        scope.all
      else
        scope.not_published
      end
    end
  end
end

class Backoffice::EventsController < ApplicationController
  def index
    authorize [:backoffice, Event]
    @events = policy_scope([:backoffice, Event]).page params[:page]
  end
end

@chevinbrown
Copy link

@medir, we've had to do something similar in order to make namespaces work. Had a #TODO in that module for quite a while...Can confirm your solution is good for all our use cases.

@rafaelsales
Copy link

Man.. this is really getting in my way - can we bring this thread back to life?

@Linuus Linuus self-assigned this Apr 24, 2018
@Linuus
Copy link
Collaborator

Linuus commented May 11, 2018

@ramaboo A long overdue reply here :) So, this fixes the issue that the whole array is passed to the policy scope initializer by passing only the last item, right? So we need to make the same fix for the Policy class as well, as discussed in #351

I don't mean that you should add that fix in this PR, I just want to make clear that I understand what's going on here. I will wait with the merge here until I have a PR to fix the same issue for policies.

As this is a breaking change it will have to go into 2.0.

@laertispappas
Copy link

Is anyone going to merge this fix?

@Linuus
Copy link
Collaborator

Linuus commented May 11, 2018

Please read the comment I made before yours.

@laertispappas
Copy link

@Linuus Ack sorry

@Linuus
Copy link
Collaborator

Linuus commented Jun 10, 2018

@ramaboo Could you rebase this on master and fix the conflicts?

@@ -88,7 +88,8 @@ def policy_scope(user, scope)
# @raise [NotDefinedError] if the policy scope cannot be found
# @return [Scope{#resolve}] instance of scope class which can resolve to a scope
def policy_scope!(user, scope)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The policy_scope method on line 78 needs to be updated as well.

@Linuus
Copy link
Collaborator

Linuus commented Jun 17, 2018

Since I've not heard anything here, I added a PR which incorporates these changes and fixes the same issue for policy.

See #529

@Linuus Linuus closed this in #529 Jun 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet