Class: DandelionS1

Inherits:
RackRscript
  • Object
show all
Defined in:
lib/dandelion_s1.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DandelionS1

Returns a new instance of DandelionS1.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dandelion_s1.rb', line 12

def initialize(opts={})

  h = {root: 'www', static: [], passwords: {'user' => 'us3r'}}.merge(opts)
  
  @passwords = h[:passwords]
  access_list = h[:access]
  @app_root = Dir.pwd

  #@access_list = {'/do/r/hello3' => 'user'}
      
  if access_list then
    
    h2 = SimpleConfig.new(access_list).to_h
    conf_access = h2[:body] || h2
    @access_list = conf_access.inject({}) \
                                {|r,x| k,v = x; r.merge(k.to_s => v.split)}
  
  end
  
  h3 = %i(log pkg_src rsc_host rsc_package_src root static debug)\
      .inject({}) {|r,x| r.merge(x => h[x])}
  
  super(h3)
  @log.debug '@access_list: ' + @access_list.inspect if @log    
  @log.debug 'end of initialize' if @log
  
end

Instance Method Details

#call(e) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dandelion_s1.rb', line 40

def call(e)

  request = e['REQUEST_PATH']
  @log.debug 'request: ' + request.inspect if @log
  
  return super(e) if request == '/login'
  r = @access_list.detect {|k,v| request =~ Regexp.new(k)} if @access_list
  private_user = r ? r.last : nil
  
  req = Rack::Request.new(e)
  user = req.session[:username]

  #@log.debug 'user: ' + user.inspect if @log
  #@log.debug '@e: ' + e.inspect if @log
  return jumpto '/login2?referer=' + e['PATH_INFO'] unless user
  
  if private_user.nil? then
    super(e)
  elsif (private_user.is_a? String and private_user == user) \
      or (private_user.is_a? Array and private_user.any? {|x| x == user})
    super(e)
  else
    jumpto '/unauthorised'
  end

end