我有一个 Rails 3 应用程序,我在其中使用 CanCan 进行授权,但我正在寻找更适合我需要的东西。我的授权不是非常基于角色的,而是更多的情景。
该应用程序用于管理大学/宿舍的日常晚餐俱乐部,人们轮流为其余的人做晚餐。因此,厨师对他负责的晚餐俱乐部拥有更多权限(例如更改菜单),并且参与者比未参加该单一晚餐俱乐部的人拥有更多权限(例如添加客人)。
因此,我需要一些授权系统,它取决于用户与晚餐俱乐部或厨房之间的关系(一个厨房有很多晚餐俱乐部),而不是用户角色。
如果有帮助,我目前的康康舞能力在这里吗(如您所见,它们非常复杂)
# In ability.rb
def initialize(user)
# edit (like changing time or menu) for a dinner club
can :edit, DinnerClub do |dinner_club|
dinner_club.is_chef?(user) &&
!dinner_club.canceled?
end
# open or close a dinner club
can [:open, :close], DinnerClub do |dinner_club|
!dinner_club.passed? &&
dinner_club.is_chef?(user)
end
# cancel or resume a dinner club
can [:cancel, :resume], DinnerClub do |dinner_club|
!dinner_club.passed? &&
dinner_club.is_chef?(user)
end
# register or unregister for at specific dinner club
can [:register, :unregister], DinnerClub do |dinner_club|
dinner_club.open? &&
(dinner_club.kitchen_id == user.kitchen_id || dinner_club.kitchen.open_registrations?) &&
!dinner_club.is_chef?(user)
end
# add guests to a dinner club
can [:add_guests], DinnerClub do |dinner_club|
dinner_club.open? &&
(dinner_club.kitchen_id == user.kitchen_id || dinner_club.kitchen.open_registrations?) &&
dinner_club.registered?(user)
end
# take the dinner club from another use if the dinner club is canceled
can [:take], DinnerClub do |dinner_club|
dinner_club.canceled? &&
(dinner_club.kitchen_id == user.kitchen_id || dinner_club.kitchen.open_registrations?) &&
!dinner_club.passed? &&
!dinner_club.is_chef?(user)
end
# create a new dinner club
can [:create], DinnerClub do |dinner_club|
(dinner_club.kitchen_id == user.kitchen_id || dinner_club.kitchen.open_registrations?)
end
# comment on existing dinner clubs
can [:comment], DinnerClub do |dinner_club|
dinner_club.registered?(user)
end
# can see dinner clubs for this kitchen
can :read, Kitchen do |kitchen|
(kitchen.id == user.kitchen_id || kitchen.open_registrations?)
end
# can manage the kitchen, like changing name and configuration options
can :manage, Kitchen, admin_id: user.id
end