Module: Sinatra::ELS::Auth

Defined in:
lib/sinatra/els.rb

Instance Method Summary collapse

Instance Method Details

#els_before(opts = {}) ⇒ Object

Set up the before hook to use ELS authentication. Setup ELS options using set :els_opts

params:
  opts       -  Optional hash with a key of either :only, or :except
                and an array of String values which will be cast into
                regular expressions.

example:
  # only use els when patch matches "/api/" or anything ending in "/orders"
  els_before true, :only => ["^\/api\/",".*\/orders$"]

  # do not use els if the "/util/" path is matched
  els_before true, :except

  # use els before everything
  els_before

when before_all === false all before processing will be ignored

An optional hash of path matchers can be supplied



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sinatra/els.rb', line 33

def els_before(opts = {})
  before {
    if opts.key? :only
      # execute authorize if path matches regex(s)
      if request.path_info.match(/#{opts[:only].map{|i|"(#{i})"}.join('|')}/)
        authorize!
      end
    elsif opts.key? :except
      # execute authorize if path doesn't match regex(s)
      if not request.path_info.match(/#{opts[:except].map{|i|"(#{i})"}.join('|')}/)
        authorize!
      end
    else
      authorize!
    end
  }  
end