cancan icon indicating copy to clipboard operation
cancan copied to clipboard

CanCan adds " AND (1=0)" to Sql Query

Open swistaczek opened this issue 12 years ago • 8 comments

Hi, I found out that CanCan adds " AND (1=0)" to SqlQuery and break results, please look at this example:

class Partner::UsersController < Partner::BaseController
  load_and_authorize_resource class: PartnerUser
...
SELECT `users`.* FROM `users` INNER JOIN `partner_users` ON `users`.`id` = `partner_users`.`user_id` WHERE `partner_users`.`partner_id` = 1 AND (1=0)"

If you have any suggestions I would be glad, Thanks.

swistaczek avatar Aug 30 '12 20:08 swistaczek

Please attach your ability file and the PartnerUser model.

thedelchop avatar Sep 24 '12 01:09 thedelchop

I had the same issue with

cancan 2.0.0.alpha

downgrading to latest stable 1.6.8 fixed that issue. I had a almost blank ability:

class Ability
  include CanCan::Ability  
  def initialize(user)
    user ||= User.new
    can :read, Empfehlungscode
  end       
end      
# 2.0.0.alpha
>> Empfehlungscode.accessible_by(Ability.new(User.first)).to_sql
=> "SELECT `empfehlungscodes`.* FROM `empfehlungscodes`  WHERE (1=0)"

# 1.6.8
=> ""SELECT `empfehlungscodes`.* FROM `empfehlungscodes` "

zealot128 avatar Sep 28 '12 11:09 zealot128

Friends,

I have the same problem, any suggestion to resolve it?

Thanks

Andrew

vixlima avatar Nov 01 '12 14:11 vixlima

Hi guys,

I was facing the same issue and solved it.

When the user is not allowed to :index and you try something like Model.accessible_by(current_ability).all it will add the WHERE (1=0) so as not to allow the user to list any item from the model.

Solution add

can [:index], [Model]

And it will no longer add the WHERE (1=0).

I hope that helps... All the best!

viniciusnz avatar Nov 11 '12 18:11 viniciusnz

I have also faced same issue.

I have installed two versions(1.6.7 and 1.6.8) and configured cancan 1.6.7 in GemFile as gem "cancan", "1.6.7"

If user is allowed only :show action and cancan will append WHERE (1=0) when we try Model.accessible_by(current_ability, :show).

There is no problem only on first request after restarting server.

jmaniv avatar Aug 13 '13 11:08 jmaniv

I'm really confused by the behaviour of the :index ability. Where is it documented? I can't find it mentioned in the wiki.

callumlocke avatar Oct 09 '13 19:10 callumlocke

@callumlocke - By default, cancan adds some functionality based on the CRUD routes. Defining the :index ability has no behavior by itself, but :read is aliased to [:index, :show].

https://github.com/ryanb/cancan/blob/master/lib/cancan/ability.rb#L303-309

Whenever you call Model#accessible_by(ability) without defining a permission explicitly, it defaults to :read. Defining :index will allow you to have more fine grained control.

For instance, you may want to allow certain users to view individual phone numbers that aren't deleted, but never be able to list (or index) all phone numbers. You could do that like this:

ability.can :show,
  PhoneNumber,
  deleted => false

ability.cannot :index,
  PhoneNumber

# Somewhere in controllers
phone = PhoneNumber.new
ability.can? :show, phone #=> true

PhoneNumber.accessible_by(ability, :index) #=> []

twessler avatar Jan 17 '14 17:01 twessler

Does anyone already solved this issue? I'm having quite a similar bug here that I really believe that cancan is causing it. I have a few scopes to filter queries, something like:

User.scoped_doc_like(result, key).pluck(:user_doc).uniq

This query works 98% of the time, but sometimes, randomly it adds 1=0 at the end of the query.

SELECT users.* FROM users WHERE 1=0

None of the replies above solved my issue, I tried to add read and index to all users in cancan, with no success, though I'm having this problem logged as admin, and admin can :manage, :all. So I'm kinda lost about what is happening!

I also thought that this was an empty hash being passed to the lambda scope, but even with emptiness validation it fails.

the-harry avatar Aug 19 '19 14:08 the-harry