/
admin.rb
350 lines (308 loc) · 8.38 KB
/
admin.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
module Roam
class AdminApp < App
register Mustache::Sinatra
set :mustache, {
:views => 'views/admin/',
:templates => 'templates/admin/'
}
configure do
use Rack::Session::Cookie, :secret => 'H1. Th1s 1s @ dirty s3cr3t j0y!'
use Rack::Flash
end
not_found do
mustache :missing
end
error do
mustache :error
end
before do
@flash = flash
@site = Site.default
login_required
Time.zone = site.timezone
Chronic.time_class = Time.zone
end
helpers do
def login_required
return true if request.path_info.match(/^\/session/) || current_user
redirect '/admin/session'
end
def current_user
@current_user ||= User.by_id(session[:user]) if session[:user]
end
def site
@site ||= Site.default
end
end
get '/?' do
redirect "/admin/posts"
end
get '/templates' do
mustache :templates
end
get '/settings' do
mustache :settings
end
put '/settings' do
site.doc.update(params[:site])
if site.update
flash.now[:notice] = "Settings saved."
mustache :settings
else
flash.now[:warning] = "Error saving settings."
mustache :settings
end
end
post '/settings' do
begin
name = params[:setting][:name].slugize if params[:setting]
value = params[:setting][:value] if params[:setting]
raise(StandardError, "name cannot be empty") if name.empty?
site.settings.update({name => value})
if site.insert
flash.now[:notice] = "Setting added."
mustache :settings
else
flash.now[:warning] = "Error saving settings."
mustache :settings
end
rescue StandardError => e
flash.now[:warning] = e.message
mustache :settings
end
end
delete '/settings/:id' do
site.settings.delete(params[:id])
if site.update
flash[:notice] = "Setting removed."
redirect '/admin/settings'
else
flash[:warning] = "Error deleting setting."
redirect '/admin/settings'
end
end
get '/session' do
if current_user
flash[:notice] = "You are already logged in."
redirect '/admin'
else
mustache :login
end
end
post '/session' do
if user = User.authenticate(params[:login], params[:password])
session[:user] = user.id.to_s
user.record_login
flash[:notice] = "Login Successful."
redirect '/admin'
else
flash.now[:warning] = "Login Failed, please try again."
mustache :login
end
end
delete '/session' do
session.clear if current_user
redirect '/admin'
end
get '/posts' do
@nodate = Post.nodate.to_a
@future = Post.future.to_a
@active = Post.active.to_a
mustache :posts
end
get '/posts/new' do
@post = Post.new
mustache :post
end
post '/posts' do
@post = Post.new(params[:post])
if @post.insert
flash[:notice] = "Post created."
redirect "/admin/posts/#{@post.slug}"
else
flash.now[:warning] = "Error creating post."
mustache :post
end
end
get '/posts/:id' do
not_found unless @post = Post.by_slug(params[:id])
mustache :post
end
put '/posts/:id' do
not_found unless @post = Post.by_slug(params[:id])
@post.doc.update(params[:post])
if @post.update
flash[:notice] = "Post updated."
redirect "/admin/posts/#{@post.slug}"
else
flash.now[:warning] = "Error updating post."
mustache :post
end
end
delete '/posts/:id' do
not_found unless @post = Post.by_slug(params[:id])
if @post.destroy
flash[:notice] = "Post deleted."
redirect '/admin/posts'
else
flash[:warning] = "Error deleting post."
redirect '/admin/posts'
end
end
get '/pages' do
@_pages = Page.sort_updated.to_a
mustache :pages
end
post '/pages' do
@page = Page.new(params[:page])
if @page.insert
flash[:notice] = "Page created."
redirect "/admin/pages/#{@page.slug}"
else
flash.now[:warning] = "Error creating page."
mustache :page
end
end
get '/pages/new' do
@page = Page.new
mustache :page
end
get '/pages/:id' do
not_found unless @page = Page.by_slug(params[:id])
mustache :page
end
put '/pages/:id' do
not_found unless @page = Page.by_slug(params[:id])
@page.doc.update(params[:page])
if @page.update
flash[:notice] = "Page updated."
redirect "/admin/pages/#{@page.slug}"
else
flash.now[:warning] = "Error updating page."
mustache :page
end
end
delete '/pages/:id' do
not_found unless @page = Page.by_slug(params[:id])
if @page.destroy
flash[:notice] = "Page deleted."
redirect '/admin/pages'
else
flash[:warning] = "Error deleting page."
redirect '/admin/pages'
end
end
get '/designs' do
@_designs = Design.sort_updated.to_a
mustache :designs
end
post '/designs' do
@design = Design.new(params[:design])
if @design.insert
flash[:notice] = "Design created."
redirect "/admin/designs/#{@design.id}"
else
flash.now[:warning] = "Error creating design."
mustache :design
end
end
get '/designs/new' do
if params[:copy]
unless @design = Design.duplicate(params[:copy])
flash[:warning] = "Unable to copy design"
redirect "/admin/designs"
end
else
@design = Design.new
end
mustache :design
end
get '/designs/:id' do
not_found unless @design = Design.by_id(params[:id])
mustache :design
end
post '/designs/:id' do
not_found unless design = Design.by_id(params[:id])
if design.activate
flash[:notice] = "Design marked active."
redirect "/admin/designs"
else
flash[:warning] = "Error activating design."
redirect "/admin/designs"
end
end
put '/designs/:id' do
not_found unless @design = Design.by_id(params[:id])
@design.doc.update(params[:design])
if @design.update
flash[:notice] = "Design updated."
redirect "/admin/designs/#{@design.id}"
else
flash.now[:warning] = "Error updating design."
mustache :design
end
end
delete '/designs/:id' do
not_found unless design = Design.by_id(params[:id])
if design.destroy
flash[:notice] = "Design removed."
redirect "/admin/designs"
else
flash[:warning] = "Error removing design."
redirect "/admin/designs"
end
end
get '/users' do
@_users = User.sort_logins.to_a
mustache :users
end
post '/users' do
@user = User.new(params[:user])
if @user.insert
flash[:notice] = "User created."
redirect "/admin/users/#{@user.id}"
else
flash.now[:warning] = "Error creating user."
mustache :user
end
end
get '/users/new' do
@user = User.new
mustache :user
end
get '/users/:id' do
not_found unless @user = User.by_id(params[:id])
mustache :user
end
put '/users/:id' do
not_found unless @user = User.by_id(params[:id])
@user.password = params[:password]
@user.password_confirmation = params[:password_confirmation]
@user.doc.update(params[:user])
if @user.update
flash[:notice] = "User updated."
redirect "/admin/users/#{@user.id}"
else
flash.now[:warning] = "Error updating user."
mustache :user
end
end
delete '/users/:id' do
begin
not_found unless @user = User.by_id(params[:id])
raise(StandardError, "Cannot delete yourself") if @user == current_user
raise(StandardError, "Cannot delete only user") if User.count < 2
if @user.destroy
flash["notice"] = "User removed."
redirect "/admin/users"
else
flash.now[:warning] = "Error deleting user."
mustache :user
end
rescue StandardError => e
flash.now[:warning] = e.message
mustache :user
end
end
end
end