-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.rb
261 lines (220 loc) · 6.13 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
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
39
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
require 'sinatra'
require 'rack-flash'
require 'haml'
require 'v8'
require 'json'
require 'coffee-script'
require 'carQuote'
require 'lifeQuote'
require 'carPremiumCalculator'
require 'lifePremiumCalculator'
require 'emailValidator'
require 'rack-google-analytics'
require 'logger'
require "warden"
require 'data_mapper'
require "dm_noisy_failures"
require 'bcrypt'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/db.sqlite")
DataMapper::Model.raise_on_save_failure = true
require './models/policies'
require './models/users'
DataMapper.finalize
DataMapper.auto_upgrade!
use Rack::GoogleAnalytics, :tracker => 'UA-53462613-1'
configure :production do
require 'newrelic_rpm'
end
class App < Sinatra::Base
$logger = Logger.new(STDERR)
emailValidator = EmailValidator.new()
#configure do
# Dir.mkdir('log') unless File.exists?('log')
# enable :logging
# log_file = File.new('log/access.log', 'a+')
# log_file.sync = true
# use Rack::CommonLogger, log_file
#end
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'secret_stuff'
use Rack::Flash, :sweep => true
use Warden::Manager do |config|
config.serialize_into_session{|user| user.username }
config.serialize_from_session{|username| User.get(username) }
config.scope_defaults :default,
strategies: [:password],
action: '/auth'
config.failure_app = self
end
Warden::Manager.before_failure do |env,opts|
env['REQUEST_METHOD'] = 'POST'
end
Warden::Strategies.add(:password) do
def valid?
$logger.info "WARDEN: Validating ..."
params['username'] && params['password']
end
def authenticate!
$logger.info params
user = User.get(params['username'])
$logger.info user.inspect
if user.nil?
fail!("The username you entered does not exist.")
elsif user.authenticate(params['password'])
success!(user)
else
fail!("Incorrect Password")
end
end
end
get '/' do
session["quote"] ||= nil
haml :index
end
get '/seed' do
username = "[email protected]"
createUser(username, "admin", "Mr Admin", :admin)
carQuote = getCarQuote({"age" => "23", "email" => username, "make" => "bmw", "year" => "2009", "gender" => "male", "state" => "nsw"})
createPolicy(username, carQuote)
redirect '/usersfree'
end
get "/bad" do
content_type :json
{ :login_successful => "false" }.to_json
end
get "/good" do
content_type :json
{ :login_successful => "true", :name => "Hans"}.to_json
end
get '/login' do
session["quote"] ||= nil
haml :login
end
post '/login' do
$logger.info params
warden_handler.authenticate!
if warden_handler.authenticated?
$logger.info session.inspect
redirect session[:return_to], flash[:success] = "You have logged in successfully"
else
redirect '/login', flash[:danger] = warden_handler.message
end
end
post '/auth' do
session[:return_to] = env['warden.options'][:attempted_path]
puts env['warden.options'][:attempted_path]
redirect '/login', flash[:danger] = warden_handler.message
end
get '/logout' do
warden_handler.raw_session.inspect
warden_handler.logout
session.clear
flash[:success] = "Successfully logged out"
redirect '/'
end
get '/life' do
haml :life
end
get '/car' do
haml :car
end
get '/payment' do
@quote = session["quote"]
haml :payment
end
get '/users' do
warden_handler.authenticate!
@users = User.all
haml :users
end
get '/usersfree' do
@users = User.all
haml :users
end
get '/policies' do
@policies = Policy.all
haml :policies
end
post '/pay' do
@quote = session["quote"]
$logger.info @quote.inspect
$logger.info @quote.type.inspect
if @quote.premium.to_f > 50
createUser(params["username"], params["password"], params["cardholdername"], :user)
createPolicy(params["username"], @quote)
haml :done
else
haml :not_done
end
end
post '/quote' do
type = params["typeOfInsurance"]
if type == "life"
@quote = getLifeQuote(params)
else
@quote = getCarQuote(params)
end
session["quote"] = @quote
haml :quote
end
post '/checkemail' do
email = params["email"]
content_type :json
{:valid => emailValidator.isEmailValid?(email)}.to_json
end
get '/checkemail' do
email = params["email"]
content_type :json
{:valid => emailValidator.isEmailValid?(email)}.to_json
end
get '/javascripts/application.js' do
coffee :application
end
not_found do
haml :not_found
end
def warden_handler
env['warden']
end
def current_user
warden_handler.user
end
def check_authentication
redirect '/login' unless warden_handler.authenticated?
end
def getLifeQuote(params)
age = params["age"]
email = params["email"]
occupationCategory = params["occupation"]
gender = params["gender"]
state = params["state"]
LifeQuote.new(age, email, state, occupationCategory, gender)
end
def getCarQuote(params)
age = params["age"]
email = params["email"]
make = params["make"]
year = params["year"]
gender = params["gender"]
state = params["state"]
CarQuote.new(age, email, state, make, gender, year)
end
def createPolicy(username, quote)
user = User.get(username)
$logger.info "BEFORE: CREATING POLICY: " + Policy.all.count.to_s
$logger.info quote.inspect
$logger.info quote.type.to_s
policy = Policy.new(:user => user, :type=>quote.type, :quote=>quote, :created_at => Time.now, :updated_at => Time.now)
policy.save
$logger.info "AFTER: SIZE OF TABLE: " + Policy.all.count.to_s
end
def createUser(username, password, name, role)
if User.get(username).nil?
$logger.info "BEFORE: CREATING USER"
User.create(:role=>role, :username=>username, :password=>password, :name => name)
$logger.info "AFTER: SIZE OF TABLE: " + User.all.count.to_s
end
end
end