开发者

Rails CanCan, failing unless I have a Rails.logger.info --- why?

开发者 https://www.devze.com 2023-03-23 16:59 出处:网络
If I have this: can [:manage], GroupMember do |group_member| wall_member.try(:user_id) == current_user.id

If I have this:

can [:manage], GroupMember do |group_member|
  wall_member.try(:user_id) == current_user.id
  Rails.logger.info 'XXXX'
end

CanCan works properly but if I remove the logger, it fails:

can [:manage], GroupMember do |group_member|
  wall_m开发者_StackOverflow社区ember.try(:user_id) == current_user.id
end

Any ideas what's going on here with CanCan? or my code? :) thanks


From the fine manual:

If the conditions hash does not give you enough control over defining abilities, you can use a block along with any Ruby code you want.

can :update, Project do |project|
  project.groups.include?(user.group)
end

If the block returns true then the user has that :update ability for that project, otherwise he will be denied access. The downside to using a block is that it cannot be used to generate conditions for database queries.

Your first block:

can [:manage], GroupMember do |group_member|
  wall_member.try(:user_id) == current_user.id
  Rails.logger.info 'XXXX'
end

Will always return a true value because Rails.logger.info 'XXXX' returns "XXXX\n" (info is just a wrapper for add and you have to read the source to see what add returns as it isn't very well documented). Without the Rails.logger.info call, the block returns just:

wall_member.try(:user_id) == current_user.id

and that must be false for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消