-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·336 lines (196 loc) · 5.66 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
require 'bundler'
Bundler.require
# load the db and models
require './models'
# load external Helpers file
require './helpers'
#DataMapper::Model.raise_on_save_failure = true
class LowPowerRadio < Sinatra::Base
Site = SiteOptions.first()
Site_title = Site.name
register Sinatra::Flash
# Set the helpers to the helper Module
helpers Helpers
use Rack::Session::Cookie, secret: 'fD0fXJ71YrH5gK6g0O74E57KRc1Iz3AJ'
use Warden::Manager do | config |
# Tell Warden how to save our User info into a session.
# Sessions can only take strings, not Ruby code, we'll store
# the User's `id`
config.serialize_into_session{ | user | user.id }
# Now tell Warden how to take what we've stored in the session
# and get a User from that information.
config.serialize_from_session{ | id | User.get( id ) }
config.scope_defaults :default,
# "strategies" is an array of named methods with which to
# attempt authentication. We have to define this later.
strategies: [:password],
# The action is a route to send the user to when
# warden.authenticate! returns a false answer. We'll show
# this route below.
action: 'auth/unauthenticated'
# When a user tries to log in and cannot, this specifies the
# app to send the user to.
config.failure_app = self
end
Warden::Manager.before_failure do | env, opts |
env['REQUEST_METHOD'] = "POST"
end
# Define the Warden Strategies
Warden::Strategies.add( :password ) do
def valid?
params['user'] && params['user']['username'] && params['user']['password']
end
def authenticate!
user = User.first( username: params['user']['username'] )
@username = user.username
if user.nil?
fail!( "That login is incorrect." )
elsif user.authenticate( params['user']['password'] )
success!(user, "Hello #{@username}.")
else
fail!( "Could not log in." )
end
end
end
# Routes
## Main Page Routes
get '/' do
@title = "LPFM Radio Framework"
@shows = Show.all
@djs = Dj.all
@posts = Post.all
erb :index
end
get '/shows/:id/?' do
@show = Show.get(params[:id])
erb :'shows/show'
end
### Admin Routes
get '/admin/' do
@title = "Admin"
env['warden'].authenticate!
erb :'admin/admin'
end
get '/admin/users' do
@title = "Admin"
@users = User.all(:order => [ :id.desc ] )
erb :'admin/users/users'
end
#### Admin User Routes
##### Edit User Page
get '/admin/user/edit/:id' do
@id = params[:id]
@title = "Admin"
@user = User.get(@id)
erb :'admin/users/single_user'
end
##### Edit User Object
post '/admin/user/edit/:id' do
@user = User.get(params[:id])
@user.update( :first_name => params[:first_name], :last_name => params[:last_name])
success?
redirect "/admin/user/edit/#{@user.id}"
end
##### Create New User
post '/admin/user/new?' do
@user = User.create( username: params[:username])
@user.password = params[:password]
@user.email = params[:email]
@user.first_name = params[:first_name]
@user.last_name = params[:last_name]
@user.save
@id = @user.id
redirect "/admin/user/edit/#{@id}"
end
#### Admin Show Routes
##### Show all shows
get '/admin/shows/?' do
@sub_title = "All Shows"
@shows = Show.all
erb :'/admin/shows/shows'
end
##### Create New Show
get '/admin/shows/new/?' do
erb :'admin/shows/new_show'
end
post '/admin/shows/new/?' do
@show = Show.create(
title: params['show']['title'],
description: params['show']['description'],
blurb: params['show']['blurb'],
dj: Dj.get(params['dj']['id'])
)
show_save?
end
##### Edit Show
get '/admin/shows/edit/:id/?' do
@show = Show.get( params[:id] )
erb :'admin/shows/edit_show'
end
post '/admin/shows/edit/:id/?' do
@show = Show.get(params[:id])
@show.update(
title: params['show']['title'],
description: params['show']['description'],
blurb: params['show']['blurb'],
dj: Dj.get(params['dj']['id'])
)
end
#### Admin Blog Post Routes
get '/admin/posts/?' do
@posts = Post.all
erb :'admin/posts/posts'
end
get '/admin/posts/new/?' do
erb :'admin/posts/new_post'
end
post '/admin/posts/new/?' do
@post = Post.create(
title: params['post']['title'],
content: params['post']['content'],
user: User.get(params['user']['id']),
created_at: Time.now
)
post_save?
end
get '/admin/posts/edit/:id/?' do
@post = Post.get(params[:id])
erb :'admin/posts/edit_post'
end
post '/admin/posts/edit/:id/?' do
@post = Post.get(params[:id])
@post.update(
title: params['post']['title'],
content: params['post']['content'],
user: User.get(params['user']['id']),
updated_at: Time.now
)
post_save?
end
## Authentication Routes
get '/auth/login' do
redirect '/'
end
post '/auth/login' do
env['warden'].authenticate!
flash[:success] = env['warden'].message
if session[:return_to].nil?
redirect '/'
else
redirect session[:return_to]
end
end
post '/auth/unauthenticated' do
session[:return_to] = env['warden.options'][:attempted_path]
puts env['warden.options'][:attempted_path]
flash[:error] = env['warden'].message || "You must log in"
redirect '/auth/login'
end
get '/auth/logout' do
env['warden'].raw_session.inspect
env['warden'].logout
flash[:success] = 'Successfully logged out'
redirect '/'
end
# Controllers
end