rack_warden 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +2 -0
- data/Gemfile +13 -10
- data/README.md +115 -53
- data/config.ru +1 -0
- data/lib/rack_warden.rb +33 -5
- data/lib/rack_warden/app.rb +73 -58
- data/lib/rack_warden/core_patches.rb +20 -0
- data/lib/rack_warden/env.rb +27 -0
- data/lib/rack_warden/frameworks.rb +34 -36
- data/lib/rack_warden/frameworks/rack.rb +36 -0
- data/lib/rack_warden/frameworks/rails.rb +29 -9
- data/lib/rack_warden/frameworks/sinatra.rb +15 -11
- data/lib/rack_warden/helpers.rb +197 -29
- data/lib/rack_warden/mail.rb +26 -0
- data/lib/rack_warden/models.rb +79 -40
- data/lib/rack_warden/models/user.rb +180 -22
- data/lib/rack_warden/routes.rb +159 -83
- data/lib/rack_warden/sinatra/decompile.rb +127 -0
- data/lib/rack_warden/sinatra/json.rb +131 -0
- data/lib/rack_warden/sinatra/namespace.rb +285 -0
- data/lib/rack_warden/sinatra/respond_with.rb +277 -0
- data/lib/rack_warden/version.rb +1 -1
- data/lib/rack_warden/views/rw_account_widget.html.erb +8 -0
- data/lib/rack_warden/views/rw_activation.email.erb +3 -0
- data/lib/rack_warden/views/rw_admin.html.erb +7 -5
- data/lib/rack_warden/views/rw_dbinfo.html.erb +5 -4
- data/lib/rack_warden/views/rw_error.html.erb +1 -0
- data/lib/rack_warden/views/rw_flash_widget.html.erb +12 -0
- data/lib/rack_warden/views/rw_index.html.erb +1 -1
- data/lib/rack_warden/views/rw_layout.html.erb +13 -19
- data/lib/rack_warden/views/rw_layout_admin.html.erb +6 -6
- data/lib/rack_warden/views/rw_login.html.erb +18 -5
- data/lib/rack_warden/views/rw_new_user.html.erb +22 -6
- data/lib/rack_warden/views/rw_protected.xml.erb +10 -0
- data/lib/rack_warden/views/rw_session.html.erb +34 -0
- data/lib/rack_warden/warden.rb +161 -30
- data/rack_warden.gemspec +16 -13
- metadata +84 -29
@@ -0,0 +1,20 @@
|
|
1
|
+
# Simple conversion to html (intended for yaml output)
|
2
|
+
class String
|
3
|
+
def to_html
|
4
|
+
self.gsub(/\n|\r/, '<br>').gsub(/ /, ' ')
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class Hash
|
9
|
+
# Extract key-value pairs from self, given list of objects.
|
10
|
+
# If last object given is hash, it will be the collector for the extracted pairs.
|
11
|
+
# Extracted pairs are deleted from the original hash (self).
|
12
|
+
# Returns the extracted pairs as a hash or as the supplied collector hash.
|
13
|
+
# Attempts to ignore case.
|
14
|
+
def extract(*args)
|
15
|
+
other_hash = args.last.is_a?(Hash) ? args.pop : {}
|
16
|
+
other_hash.tap do |other|
|
17
|
+
self.delete_if {|k,v| (args.include?(k) || args.include?(k.to_s) || args.include?(k.to_s.downcase) || args.include?(k.to_sym)) || args.include?(k.to_s.downcase.to_sym) ? other[k]=v : nil}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RackWarden
|
2
|
+
module Env
|
3
|
+
|
4
|
+
def cookies
|
5
|
+
self['rack.cookies']
|
6
|
+
end
|
7
|
+
|
8
|
+
def remember_token
|
9
|
+
cookies[App.settings.remember_token_cookie_name]
|
10
|
+
end
|
11
|
+
|
12
|
+
def remember_token=(string)
|
13
|
+
App.logger.debug "RW env.remember_token= #{string} (#{App.settings.remember_token_cookie_name})"
|
14
|
+
cookies[App.settings.remember_token_cookie_name]= string
|
15
|
+
end
|
16
|
+
|
17
|
+
def rack_warden
|
18
|
+
self['rack_warden_instance']
|
19
|
+
end
|
20
|
+
|
21
|
+
def rack_warden=(object)
|
22
|
+
App.logger.debug "RW env['rack_warden_instance']= #{object === Class ? object.name : object.class.name}"
|
23
|
+
self['rack_warden_instance'] = object
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -1,47 +1,45 @@
|
|
1
1
|
module RackWarden
|
2
2
|
module Frameworks
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
nil
|
15
|
-
end
|
16
|
-
|
17
|
-
# Extend target with target (like saying 'extend self').
|
18
|
-
def extended(target)
|
19
|
-
target.extend target
|
3
|
+
|
4
|
+
# Module methods.
|
5
|
+
class << self
|
6
|
+
attr_accessor :selected_framework
|
7
|
+
|
8
|
+
# Select the framework of the parent app.
|
9
|
+
def select_framework(env)
|
10
|
+
App.logger.debug "RW framework constants: #{constants}"
|
11
|
+
self.constants.dup.tap{|_constants| _constants.delete(:Base)}.each do |c|
|
12
|
+
@selected_framework = self.const_get(c).framework_selector(env)
|
13
|
+
break if @selected_framework
|
20
14
|
end
|
15
|
+
@selected_framework ||= Rack
|
16
|
+
App.logger.info "RW selected framework #{@selected_framework}"
|
17
|
+
@selected_framework
|
21
18
|
end
|
22
19
|
|
20
|
+
# Extend target with target (like saying 'extend self' within target).
|
21
|
+
def extended(target)
|
22
|
+
target.extend target
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
attr_accessor :parent_app_instance, :parent_app_class, :parent_app, :rack_warden_app_instance, :rack_warden_app_class
|
27
|
+
### Methods extended into framework module ###
|
27
28
|
|
28
|
-
|
29
|
-
# and runs the framework selector logic.
|
30
|
-
# Returns the framework module or nil.
|
31
|
-
def framework_selector(env)
|
32
|
-
#puts "RW testing framework #{self}"
|
33
|
-
#puts "BASE.framework_selector #{self} env: #{env.eval 'self'} locals: #{env.eval 'local_variables'}"
|
34
|
-
@initialization_args = env.eval 'initialization_args'
|
35
|
-
@parent_app_instance = env.eval 'parent_app_instance'
|
36
|
-
@parent_app_class = @parent_app_instance.class
|
37
|
-
@parent_app = @parent_app_instance.is_a?(Class) ? @parent_app_instance : @parent_app_class
|
38
|
-
@rack_warden_app_instance = env.eval 'self'
|
39
|
-
@rack_warden_app_class = @rack_warden_app_instance.class
|
40
|
-
selector && self
|
41
|
-
end
|
29
|
+
attr_accessor :parent_app_instance, :parent_app_class, :parent_app
|
42
30
|
|
43
|
-
|
44
|
-
|
31
|
+
# Sets framework module with variables from env (the scope of the parent app's initializer),
|
32
|
+
# and runs the framework selector logic.
|
33
|
+
# Returns the framework module or nil.
|
34
|
+
def framework_selector(app)
|
35
|
+
App.logger.debug "RW framework_selector #{self}"
|
36
|
+
@parent_app_instance = app #env.eval 'parent_app_instance'
|
37
|
+
@parent_app_class = @parent_app_instance.class
|
38
|
+
@parent_app = @parent_app_instance.is_a?(Class) ? @parent_app_instance : @parent_app_class
|
39
|
+
selector && self
|
45
40
|
end
|
41
|
+
|
42
|
+
### End methods extended into framework module ###
|
43
|
+
|
46
44
|
end
|
47
45
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RackWarden
|
2
|
+
module Frameworks
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
extend Frameworks
|
6
|
+
|
7
|
+
def selector
|
8
|
+
App.logger.debug "RW Rack.selector parent_app.ancestors #{parent_app.ancestors}"
|
9
|
+
parent_app.ancestors.find{|x| x.to_s=='Rack::URLMap'}
|
10
|
+
end
|
11
|
+
|
12
|
+
def views_path
|
13
|
+
[File.join(Dir.pwd, "views/rack_warden"), File.join(Dir.pwd,"views")]
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
# def require_login(*args)
|
18
|
+
# App.logger.debug "RW class.require_login self #{self}, args #{args}"
|
19
|
+
# before(*args) do
|
20
|
+
# require_login
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_framework
|
26
|
+
App.logger.debug "RW setup_framework for rack app #{parent_app}"
|
27
|
+
parent_app.include(RackWarden::UniversalHelpers)
|
28
|
+
App.logger.info "RW registering class methods with #{parent_app}"
|
29
|
+
parent_app.extend ClassMethods
|
30
|
+
App.set :rack_authentication, '.*'
|
31
|
+
#parent_app.require_login(RackWarden::App.require_login) if RackWarden::App.require_login != false
|
32
|
+
end
|
33
|
+
|
34
|
+
end # Rack
|
35
|
+
end # Frameworks
|
36
|
+
end # RackWarden
|
@@ -2,28 +2,48 @@ module RackWarden
|
|
2
2
|
module Frameworks
|
3
3
|
module Rails
|
4
4
|
|
5
|
-
extend
|
5
|
+
extend Frameworks
|
6
6
|
|
7
7
|
def selector
|
8
|
-
|
9
|
-
parent_app.ancestors.find{|x| x.to_s[/Rails|ActionDispatch/]} or defined?(Rails)
|
8
|
+
App.logger.debug "RW Rails.selector parent_app.ancestors #{parent_app.ancestors}"
|
9
|
+
parent_app.ancestors.find{|x| x.to_s[/Rails|ActionDispatch/]} or defined?(::Rails)
|
10
10
|
end
|
11
11
|
|
12
12
|
def views_path
|
13
13
|
[File.join(Dir.pwd, "app/views/rack_warden"), File.join(Dir.pwd, "app/views")]
|
14
14
|
end
|
15
15
|
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def require_login(*args)
|
19
|
+
App.logger.debug "RW running #{self}.require_login(#{args.inspect})"
|
20
|
+
#before_filter(*[:require_login, args].flatten.compact)
|
21
|
+
before_filter(*args) do |controller|
|
22
|
+
controller.send :require_login
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
16
28
|
def setup_framework
|
17
|
-
|
18
|
-
|
29
|
+
App.logger.debug "RW setup_framework for rails"
|
30
|
+
m = Module.new.include(RackWarden::UniversalHelpers)
|
31
|
+
m.send :protected, *(m.instance_methods)
|
32
|
+
ActionController::Base.send(:include, m)
|
33
|
+
|
34
|
+
ActionController::Base.helper_method UniversalHelpers.instance_methods
|
19
35
|
|
20
36
|
# Define class method 'require_login' on framework controller.
|
21
|
-
|
22
|
-
|
23
|
-
|
37
|
+
# Note that rails before-filters are also class methods, thus the need to differentiate method names (is this correct?).
|
38
|
+
App.logger.info "RW defining ActionController::Base.require_login"
|
39
|
+
# ActionController::Base.define_singleton_method :require_login do |*args|
|
40
|
+
# conditions_hash = args[0] || Hash.new
|
41
|
+
# before_filter(:require_login, conditions_hash)
|
42
|
+
# end
|
43
|
+
ActionController::Base.extend ClassMethods
|
24
44
|
|
25
45
|
# The way you pass arguments here is fragile. If it's not correct, it will bomb with "undefined method 'before'...".
|
26
|
-
(ActionController::Base.require_login
|
46
|
+
(ActionController::Base.require_login(RackWarden::App.require_login || {})) if RackWarden::App.require_login != false
|
27
47
|
end
|
28
48
|
|
29
49
|
end
|
@@ -2,10 +2,10 @@ module RackWarden
|
|
2
2
|
module Frameworks
|
3
3
|
module Sinatra
|
4
4
|
|
5
|
-
extend
|
5
|
+
extend Frameworks
|
6
6
|
|
7
7
|
def selector
|
8
|
-
|
8
|
+
App.logger.debug "RW Sinatra.selector parent_app.ancestors #{parent_app.ancestors}"
|
9
9
|
parent_app.ancestors.find{|x| x.to_s=='Sinatra::Base'}
|
10
10
|
end
|
11
11
|
|
@@ -13,17 +13,21 @@ module RackWarden
|
|
13
13
|
[File.join(Dir.pwd, "views/rack_warden"), File.join(Dir.pwd,"views")]
|
14
14
|
end
|
15
15
|
|
16
|
+
module ClassMethods
|
17
|
+
def require_login(*args)
|
18
|
+
App.logger.debug "RW class.require_login self #{self}, args #{args}"
|
19
|
+
before(*args) do
|
20
|
+
require_login
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
16
25
|
def setup_framework
|
17
|
-
|
26
|
+
App.logger.debug "RW setup_framework for sinatra app #{parent_app}"
|
18
27
|
parent_app.helpers(RackWarden::UniversalHelpers)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
before(accept_conditions){require_login unless reject_conditions}
|
23
|
-
end
|
24
|
-
|
25
|
-
# Add require_login to before filter of sinatra app.
|
26
|
-
parent_app.require_login(rack_warden_app_class.require_login) if rack_warden_app_class.require_login != false
|
28
|
+
App.logger.info "RW registering class methods with #{parent_app}"
|
29
|
+
parent_app.register ClassMethods
|
30
|
+
parent_app.require_login(RackWarden::App.require_login) if RackWarden::App.require_login != false
|
27
31
|
end
|
28
32
|
|
29
33
|
end # Sinatra
|
data/lib/rack_warden/helpers.rb
CHANGED
@@ -1,89 +1,257 @@
|
|
1
1
|
module RackWarden
|
2
2
|
|
3
|
+
|
4
|
+
module AppClassMethods
|
5
|
+
|
6
|
+
def self.registered(app)
|
7
|
+
app.initialize_app_class
|
8
|
+
end
|
9
|
+
|
10
|
+
# Main RackWarden::App class setup.
|
11
|
+
def initialize_app_class
|
12
|
+
|
13
|
+
initialize_logging
|
14
|
+
logger.warn "RW initializing RackWarden::App in process #{$0}"
|
15
|
+
logger.warn "RW running in #{environment} environment"
|
16
|
+
initialize_config_files
|
17
|
+
initialize_logging
|
18
|
+
|
19
|
+
use Rack::Cookies
|
20
|
+
Namespace::NamespacedMethods.prefixed :require_login
|
21
|
+
Sinatra::Namespace::NamespacedMethods.prefixed(:require_login) if Sinatra.const_defined?(:Namespace) && Sinatra::Namespace.const_defined?(:NamespacedMethods)
|
22
|
+
|
23
|
+
register RackWarden::Namespace
|
24
|
+
register RackWarden::RespondWith
|
25
|
+
|
26
|
+
# Setup flash if not already
|
27
|
+
# TODO: put code to look for existing session management in rack middlewares (how?). See todo.txt for more.
|
28
|
+
use Rack::Flash, :accessorize=>[:rw_error, :rw_success, :rw_test]
|
29
|
+
|
30
|
+
helpers RackWarden::WardenConfig
|
31
|
+
helpers RackWarden::Routes
|
32
|
+
|
33
|
+
helpers RackWardenHelpers
|
34
|
+
helpers UniversalHelpers
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def setup_framework(app, *args)
|
39
|
+
opts = args.last.is_a?(Hash) ? args.pop : {}
|
40
|
+
# Get framework module.
|
41
|
+
framework_module = Frameworks.select_framework(app)
|
42
|
+
#logger.info "RW selected framework module #{framework_module}"
|
43
|
+
|
44
|
+
# Prepend views from framework_module if framework_module exists.
|
45
|
+
# TODO: should this line be elsewhere?
|
46
|
+
settings.overlay_settings(:views=>framework_module.views_path) if framework_module && ![settings.views, opts[:views]].flatten.include?(false)
|
47
|
+
|
48
|
+
# Overlay settings with opts.
|
49
|
+
settings.overlay_settings opts
|
50
|
+
|
51
|
+
# Setup framework if framework_module exists.
|
52
|
+
framework_module.setup_framework if framework_module
|
53
|
+
end
|
54
|
+
|
55
|
+
# Load config from file, if any exist.
|
56
|
+
def initialize_config_files(more_config={})
|
57
|
+
Hash.new.tap do |hash|
|
58
|
+
config_files.each {|c| hash.merge!(YAML.load_file(File.join(Dir.pwd, c))) rescue nil}
|
59
|
+
hash.merge! more_config
|
60
|
+
overlay_settings hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Apply new settings on top of existing settings, prepending new views to old views.
|
65
|
+
def overlay_settings(new_settings)
|
66
|
+
new_views = new_settings.extract(:views).values
|
67
|
+
logger.debug "RW overlay_settings new_views #{new_views.inspect}"
|
68
|
+
set :views, [new_views, views].flatten.compact.uniq
|
69
|
+
set new_settings
|
70
|
+
end
|
71
|
+
|
72
|
+
# Initialize logging.
|
73
|
+
def initialize_logging(reset=reset_logger)
|
74
|
+
# We take existing log file from settings, enable sync (disables buffering), then put it back in settings.
|
75
|
+
_log_file = !logging && File.new('/dev/null', 'a') || !reset && settings.log_file || File.new(settings.log_path, 'a+')
|
76
|
+
_log_file.sync = true
|
77
|
+
set :log_file, _log_file
|
78
|
+
set :logger, Logger.new(_log_file, 'daily') unless settings.logger && !reset
|
79
|
+
logger.level = eval "Logger::#{log_level}"
|
80
|
+
|
81
|
+
# Setup Rack::CommonLogger
|
82
|
+
if use_common_logger
|
83
|
+
mw = @middleware.find {|m| Array(m)[0] == Rack::CommonLogger}
|
84
|
+
#@middleware.delete_if {|m| Array(m)[0] == Rack::CommonLogger}
|
85
|
+
mw ? mw[1]=[_log_file] : use(Rack::CommonLogger, _log_file)
|
86
|
+
end
|
87
|
+
|
88
|
+
#if logger.level < 2
|
89
|
+
#DataMapper::Logger.new(_log_file) #$stdout) #App.log_path)
|
90
|
+
DataMapper.logger.instance_variable_set :@log, _log_file
|
91
|
+
DataMapper.logger.instance_variable_set :@level, DataMapper::Logger::Levels[log_level.to_s.downcase.to_sym]
|
92
|
+
# logger.info "RW DataMapper using log_file #{_log_file.inspect}"
|
93
|
+
#end
|
94
|
+
|
95
|
+
logger.debug "RW initialized logging (level #{logger.level}) #{_log_file.inspect}"
|
96
|
+
rescue
|
97
|
+
puts "There was an error setting up logging: #{$!}"
|
98
|
+
end
|
99
|
+
|
100
|
+
# Creates uri-friendly codes/keys/hashes from raw unfriendly strings (like BCrypt hashes).
|
101
|
+
def uri_encode(string)
|
102
|
+
URI.encode(Base64.encode64(string))
|
103
|
+
end
|
104
|
+
|
105
|
+
def uri_decode(string)
|
106
|
+
Base64.decode64(URI.decode(string))
|
107
|
+
end
|
108
|
+
|
109
|
+
# Generic template rendering. Does not have automatic access to 'controller' environment.
|
110
|
+
# Pass 'object' to be the context of rendered template.
|
111
|
+
# See this for more info on using templates here http://stackoverflow.com/questions/5446283/how-to-use-sinatras-haml-helper-inside-a-model.
|
112
|
+
def render_template(template_name, locals_hash={}, object=self )
|
113
|
+
tmpl = settings.views.collect {|v| Tilt.new(File.join(v, template_name)) rescue nil}.compact[0]
|
114
|
+
if tmpl
|
115
|
+
tmpl.render(object, locals_hash)
|
116
|
+
else
|
117
|
+
App.logger.info "RW self.render_template found no templates to render"
|
118
|
+
nil
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
end # AppClassMethods
|
123
|
+
|
124
|
+
|
3
125
|
module UniversalHelpers
|
4
|
-
protected
|
126
|
+
#protected ... might need this for rails, but not for sinatra.
|
5
127
|
|
6
128
|
def require_login
|
7
|
-
|
8
|
-
|
129
|
+
App.logger.debug "RW running #{self}.require_login with rack_warden: #{rack_warden}, and warden: #{warden}"
|
130
|
+
#App.logger.debug "RW instance #{self}.require_login ancestors #{self.class.ancestors.inspect}"
|
131
|
+
#logged_in? || warden.authenticate!
|
132
|
+
warden.authenticated? || warden.authenticate!
|
9
133
|
end
|
10
134
|
|
11
135
|
def warden
|
12
136
|
request.env['warden']
|
13
137
|
end
|
138
|
+
|
139
|
+
def warden_options
|
140
|
+
request.env['warden.options']
|
141
|
+
end
|
14
142
|
|
15
143
|
def current_user
|
16
|
-
warden.user
|
144
|
+
#warden.authenticated? && warden.user
|
145
|
+
logged_in? && warden.user
|
17
146
|
end
|
18
147
|
|
19
148
|
def logged_in?
|
20
|
-
|
149
|
+
App.logger.debug "RW logged_in? #{warden.authenticated?}"
|
150
|
+
warden.authenticated? || warden.authenticate(:remember_me)
|
21
151
|
end
|
22
152
|
|
23
|
-
def authorized?(
|
24
|
-
|
153
|
+
def authorized?(options=request)
|
154
|
+
App.logger.debug "RW authorized? user '#{current_user}'"
|
155
|
+
current_user && current_user.authorized?(options) || request.script_name[/login|new|create|logout/]
|
156
|
+
end
|
157
|
+
|
158
|
+
def require_authorization(authenticate_on_fail=false, options=request)
|
159
|
+
App.logger.debug "RW require_authorization"
|
160
|
+
logged_in? || warden.authenticate!
|
161
|
+
unless authorized?(options)
|
25
162
|
if authenticate_on_fail
|
26
|
-
flash
|
27
|
-
redirect "/
|
163
|
+
flash[:rw_error] = ("Please login to continiue")
|
164
|
+
redirect url_for("/login")
|
28
165
|
else
|
29
|
-
flash
|
166
|
+
flash[:rw_error] = ("You are not authorized to do that")
|
30
167
|
redirect back
|
31
168
|
end
|
32
|
-
end
|
169
|
+
end
|
33
170
|
end
|
34
171
|
|
35
172
|
# Returns the current rack_warden app instance stored in env.
|
36
173
|
def rack_warden
|
37
|
-
|
38
|
-
request.env['rack_warden_instance']
|
174
|
+
App.logger.debug "RW helper method 'rack_warden' request.env['rack_warden_instance'] #{request.env['rack_warden_instance']}"
|
175
|
+
request.env['rack_warden_instance'] #.tap {|rw| rw.request = request} #request}
|
176
|
+
end
|
177
|
+
|
178
|
+
def account_widget
|
179
|
+
rack_warden.erb :'rw_account_widget.html', :layout=>false
|
180
|
+
end
|
181
|
+
|
182
|
+
def flash_widget
|
183
|
+
App.logger.debug "RW flash_widget self.flash #{self.flash}"
|
184
|
+
App.logger.debug "RW flash_widget rack.flash #{env['x-rack.flash']}"
|
185
|
+
App.logger.debug "RW flash_widget.rack_warden.flash #{rack_warden.request.env['x-rack.flash']}"
|
186
|
+
rack_warden.erb :'rw_flash_widget.html', :layout=>false
|
39
187
|
end
|
40
188
|
|
41
189
|
end # UniversalHelpers
|
42
190
|
|
191
|
+
|
192
|
+
|
193
|
+
|
43
194
|
# Also bring these into your main app helpers.
|
44
195
|
module RackWardenHelpers
|
196
|
+
|
197
|
+
# Access main logger from app instance.
|
198
|
+
def logger
|
199
|
+
settings.logger
|
200
|
+
end
|
201
|
+
|
45
202
|
# WBR - override. This passes block to be rendered to first template that matches.
|
46
203
|
def find_template(views, name, engine, &block)
|
47
|
-
#
|
48
|
-
# puts "THE NAME: #{name}"
|
49
|
-
# puts "THE ENGINE: #{engine}"
|
50
|
-
# puts "THE BLOCK: #{block}"
|
204
|
+
logger.debug "RW find_template name: #{name}, engine: #{engine}, block: #{block}, views: #{views}"
|
51
205
|
Array(views).each { |v| super(v, name, engine, &block) }
|
52
206
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
207
|
+
|
208
|
+
# Because accessing app instance thru env seems to loose flash access.
|
209
|
+
def flash
|
210
|
+
request.env['x-rack.flash']
|
211
|
+
end
|
212
|
+
|
57
213
|
def valid_user_input?
|
58
214
|
params['user'] && params['user']['email'] && params['user']['password']
|
59
215
|
end
|
216
|
+
|
217
|
+
def rw_prefix(_route='')
|
218
|
+
settings.rw_prefix.to_s + _route.to_s
|
219
|
+
end
|
220
|
+
|
221
|
+
def url_for(_url, _full_uri=false)
|
222
|
+
url(rw_prefix(_url), _full_uri)
|
223
|
+
end
|
224
|
+
|
225
|
+
|
60
226
|
|
61
227
|
def verify_recaptcha(skip_redirect=false, ip=request.ip, response=params['g-recaptcha-response'])
|
62
228
|
secret = settings.recaptcha[:secret]
|
63
229
|
_recaptcha = ActiveSupport::JSON.decode(open("https://www.google.com/recaptcha/api/siteverify?secret=#{secret}&response=#{response}&remoteip=#{ip}").read)
|
64
|
-
|
230
|
+
logger.warn "RW recaptcha #{_recaptcha.inspect}"
|
65
231
|
unless _recaptcha['success']
|
66
|
-
flash
|
232
|
+
flash.rw_error = "Please confirm you are human"
|
67
233
|
redirect back unless skip_redirect
|
68
234
|
Halt "You appear to be a robot."
|
69
235
|
end
|
70
236
|
end
|
71
237
|
|
72
238
|
def default_page
|
73
|
-
nested_erb :'rw_index.html', :'rw_layout_admin.html', settings.layout
|
239
|
+
nested_erb :'rw_index.html', :'rw_layout_admin.html', settings.layout
|
74
240
|
end
|
75
241
|
|
76
242
|
def nested_erb(*list)
|
77
|
-
|
78
|
-
counter =0
|
79
|
-
list.inject(template) do |tmplt, lay|
|
80
|
-
#puts "RW LAYOUTS lay: #{lay}, rslt: #{tmplt}"
|
243
|
+
list.inject do |tmplt, lay|
|
81
244
|
erb tmplt, :layout=>lay
|
82
245
|
end
|
83
246
|
end
|
84
247
|
|
85
248
|
def return_to(fallback=settings.default_route)
|
86
|
-
redirect session[:return_to] ||
|
249
|
+
redirect session[:return_to] || url_for(fallback)
|
250
|
+
end
|
251
|
+
|
252
|
+
def redirect_error(message="Error")
|
253
|
+
flash.rw_error = message
|
254
|
+
redirect url_for("/error")
|
87
255
|
end
|
88
256
|
|
89
257
|
def account_bar
|