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

Nil values on enums fails to authorize #800

Open
patodevilla opened this issue Aug 13, 2022 · 0 comments
Open

Nil values on enums fails to authorize #800

patodevilla opened this issue Aug 13, 2022 · 0 comments

Comments

@patodevilla
Copy link

Steps to reproduce

Adding a can rule to authorize when a value is nil fails to authorize.

class Person < ActiveRecord::Base
  enum my_enum: {
    'something' => 0,
    'other' => 1
  }
end

class TestAbility
  include CanCan::Ability

  def initialize
    # can [:create], Person, my_enum: nil # also fails
    can [:create], Person, my_enum: [nil]
  end
end

TestAbility.new().can? :create, Person.new()

The bug is caused by this line because nil becomes ''

This would fix it

array_values = Array(value).map{|v| v.nil? ? nil : v.to_s }
!(array_values & [attribute, raw_attribute]).empty?

Expected behavior

Should return true

Actual behavior

Returns false

System configuration

Rails version:
6.1.0

Ruby version:
2.7.4

CanCanCan version
3.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant