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
|
# File 'lib/sinatra-authentication.rb', line 6
def self.registered(app)
app.set :sinatra_authentication_view_path, File.expand_path('../views/', __FILE__)
unless defined?(options.template_engine)
app.set :template_engine, :slim
end
app.get '/users/?' do
login_required
redirect "/" unless current_user.admin?
@users = User.all
if @users != []
send options.template_engine, get_view_as_string("index.#{options.template_engine}"), :layout => use_layout?
else
redirect '/signup'
end
end
app.get '/users/:id/?' do
login_required
@user = User.get(:id => params[:id])
send options.template_engine, get_view_as_string("show.#{options.template_engine}"), :layout => use_layout?
end
app.get '/logged_in' do
if session[:user]
"true"
else
"false"
end
end
app.get '/login/?' do
if session[:user]
redirect '/'
else
send options.template_engine, get_view_as_string("login.#{options.template_engine}"), :layout => use_layout?
end
end
app.post '/login/?' do
if user = User.authenticate(params[:name], params[:password])
session[:user] = user.id
if Rack.const_defined?('Flash')
flash[:notice] = "Login successful."
end
if session[:return_to]
redirect_url = session[:return_to]
session[:return_to] = false
redirect redirect_url
else
redirect '/'
end
else
if Rack.const_defined?('Flash')
flash[:error] = "The email or password you entered is incorrect."
end
redirect '/login'
end
end
app.get '/logout/?' do
session[:user] = nil
if Rack.const_defined?('Flash')
flash[:notice] = "Logout successful."
end
return_to = ( session[:return_to] ? session[:return_to] : '/' )
redirect return_to
end
app.get '/signup/?' do
if session[:user]
redirect '/'
else
send options.template_engine, get_view_as_string("signup.#{options.template_engine}"), :layout => use_layout?
end
end
app.post '/signup/?' do
@user = User.set(params[:user])
if @user.valid && @user.id
session[:user] = @user.id
if Rack.const_defined?('Flash')
flash[:notice] = "Account created."
end
redirect '/'
else
if Rack.const_defined?('Flash')
flash[:error] = "There were some problems creating your account: #{@user.errors}."
end
redirect '/signup?' + hash_to_query_string(params['user'])
end
end
app.get '/users/:id/edit/?' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
@user = User.get(:id => params[:id])
send options.template_engine, get_view_as_string("edit.#{options.template_engine}"), :layout => use_layout?
end
app.post '/users/:id/edit/?' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
user = User.get(:id => params[:id])
user_attributes = params[:user]
if params[:user][:password] == ""
user_attributes.delete("password")
user_attributes.delete("password_confirmation")
end
if user.update(user_attributes)
if Rack.const_defined?('Flash')
flash[:notice] = 'Account updated.'
end
redirect '/'
else
if Rack.const_defined?('Flash')
flash[:error] = "Whoops, looks like there were some problems with your updates: #{user.errors}."
end
redirect "/users/#{user.id}/edit?" + hash_to_query_string(user_attributes)
end
end
app.get '/users/:id/delete/?' do
login_required
redirect "/users" unless current_user.admin? || current_user.id.to_s == params[:id]
if User.delete(params[:id])
if Rack.const_defined?('Flash')
flash[:notice] = "User deleted."
end
else
if Rack.const_defined?('Flash')
flash[:error] = "Deletion failed."
end
end
redirect '/'
end
if Sinatra.const_defined?('FacebookObject')
app.get '/connect/?' do
if fb[:user]
if current_user.class != GuestUser
user = current_user
else
user = User.get(:fb_uid => fb[:user])
end
if user
if !user.fb_uid || user.fb_uid != fb[:user]
user.update :fb_uid => fb[:user]
end
session[:user] = user.id
else
user = User.set!(:fb_uid => fb[:user])
session[:user] = user.id
end
end
redirect '/'
end
app.get '/receiver' do
%[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script>
</body>
</html>]
end
end
end
|