Guest User

Untitled

a guest
Apr 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # #Does the user have all roles given? #You can use hash values
  2. # :any=>[role,role] and :all=>[role,role]cto change how this operates. If both
  3. # :all and :any are present it processes vit as Does user have at least one of
  4. # the roles in :any roles and has every role in of :all roles #If user has the
  5. # roles "admin" this method returns true regardless of #the arguments passed.
  6. # Use :check_admin => false to disable this.
  7. def roles?(*roles)
  8. roles.flatten!
  9.  
  10. options={}
  11. options.merge! roles.delete_at(-1) if roles[-1].instance_of?(Hash)
  12.  
  13. unless options.fetch(:check_admin, nil)
  14. return true if role?('admin')
  15. end
  16. return false unless options.fetch(:any, []).any do |r| has_role?(r) end
  17. return false unless options.fetch(:all, []).all do |r| has_role?(r) end
  18. return true if roles.empty?
  19. return roles.all do |r| has_role?(r) end
  20. end
  21.  
  22. ##Notes
  23. #The ruby method {}.fetch("key","default") is similar to the code below
  24. #Assume: array={}
  25. if array.has_key?("key")
  26. return array["key"]
  27. else
  28. return "default"
  29. end
Add Comment
Please, Sign In to add comment