-
Notifications
You must be signed in to change notification settings - Fork 18
Home
randx edited this page Aug 13, 2011
·
36 revisions
gem install six
class BookRules
# All authorization works on objects with method 'allowed'.
# no magic behind the scene.
# You can put this method to any class you want
# it should always return array
def self.allowed(author, book)
rules = []
# good practice is to check for object type
return rules unless book.instance_of?(Book)
rules << :read_book if book.published?
rules << :edit_book if author && author.id == book.author_id
# you are free to write any conditions you need
if author && author.id == book.author_id && book.is_approved? # ....etc...
rules << :publis_book
rules
end
end
Six::Guard.instance.add_pack(:book, BookRules)
Six::Guard.instance.allowed? :read_book, nil, nil # false
Six::Guard.instance.allowed? :read_book, nil, published_book # true
Six::Guard.instance.allowed? :edit_book, nil, nil # false
Six::Guard.instance.allowed? :edit_book, author, author.books.first # true