CanCanCommunity / cancancan

The authorization Gem for Ruby on Rails.
MIT License
5.58k stars 638 forks source link

Ability with conditions array behaving strangely for single objects #430

Closed mgorman42 closed 7 years ago

mgorman42 commented 7 years ago

have a ability

can :manage, StopBlacklist, phone_code: phonecodes That I know is getting hit for a given user

phonecodes is a list of phone_codes the user can mange stop blacklists for and phone_code is the column on the table for stop blacklist

when I run accessible_by it returns the array of stop blacklist the user should be able delete as expected

StopBlacklist.accessible_by(current_ability, :delete)

but when I run can? on a specific one in that list it run me false

can? :delete, StopBlacklist.find(##)

I even tried to do the following

can? :delete, StopBlacklist.accessible_by(current_ability, :delete)

and it returned false

I am using Cancancan version (1.17.0) and Rails 4.0

Is there a reason using an array for the condition breaks checking on a single object?

EDIT

failing example

if phonecodes for the active user is [12345, 54321]

and there is a StopBlacklist with the values

id: 25 cell: 15555555555 phone_code: 12345

I would expect

current_ability.can? :delete, StopBlacklist.find 25

to return true, but it is returning false

but if I do

StopBlacklist.accessible_by(current_ability, :delete)

I find the StopBlacklist object with id 25 in the returned list

coorasse commented 7 years ago

Please follow the issue template and provide a failing example. Thank you!

mgorman42 commented 7 years ago

I added a more specific example, thanks for the response.

mgorman42 commented 7 years ago

Okay, while making the gist I think I figured out what is going on. it looks like the issue was that the array was an array of integers [12345, 54321]. but the value on the object was a string "12345", and the comparison wasn't working right, going to re-factor the phonecodes array to be strings

Thanks for your help.

coorasse commented 7 years ago

Glad I could help