Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # #Does the user have all roles given? #You can use hash values
- # :any=>[role,role] and :all=>[role,role]cto change how this operates. If both
- # :all and :any are present it processes vit as Does user have at least one of
- # the roles in :any roles and has every role in of :all roles #If user has the
- # roles "admin" this method returns true regardless of #the arguments passed.
- # Use :check_admin => false to disable this.
- def roles?(*roles)
- roles.flatten!
- options={}
- options.merge! roles.delete_at(-1) if roles[-1].instance_of?(Hash)
- unless options.fetch(:check_admin, nil)
- return true if role?('admin')
- end
- return false unless options.fetch(:any, []).any do |r| has_role?(r) end
- return false unless options.fetch(:all, []).all do |r| has_role?(r) end
- return true if roles.empty?
- return roles.all do |r| has_role?(r) end
- end
- ##Notes
- #The ruby method {}.fetch("key","default") is similar to the code below
- #Assume: array={}
- if array.has_key?("key")
- return array["key"]
- else
- return "default"
- end
Add Comment
Please, Sign In to add comment