-
Notifications
You must be signed in to change notification settings - Fork 0
/
app[Conflict].rb
196 lines (167 loc) · 4.86 KB
/
app[Conflict].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
require 'sequel'
require 'sinatra'
require 'omniauth'
require 'google/omniauth'
require 'google/api_client/client_secrets'
#DATABASE
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://db/wps_rate_teachers.db')
require './models/entry.rb'
require './models/user.rb'
#GOOGLE AUTH
CLIENT_SECRETS = Google::APIClient::ClientSecrets.load
def client
c = (Thread.current[:client] ||=
Google::APIClient.new(:application_name => 'Ruby Google+ sample',
:application_version => '1.0.0'))
# It's really important to clear these out,
# since we reuse client objects across requests
# for caching and performance reasons.
c.authorization.clear_credentials!
return c
end
client1 = Google::APIClient.new
oauth2 = client1.discovered_api('oauth2')
def api_client; settings.api_client; end
#LOGOUT CODE (CLEAR SESSIONS)
get '/logout' do
session['credentials'] = nil
session.clear
redirect '/'
end
get '/' do
redirect '/main'
end
get '/login' do
if session['credentials']
redirect '/main'
else
erb :login
end
end
# Support both GET and POST for callbacks. (AGAIN, MORE GOOGLE AUTH STUFF...)
%w(get post).each do |method|
send(method, "/auth/:provider/callback") do
Thread.current[:client] = env['omniauth.auth']['extra']['client']
# Keep track of the tokens. Use a real database in production.
session['uid'] = env['omniauth.auth']['uid']
session['credentials'] = env['omniauth.auth']['credentials']
redirect '/main'
end
end
get '/auth/failure' do
redirect '/'
end
use Rack::Session::Cookie
use OmniAuth::Builder do
provider OmniAuth::Strategies::Google,
CLIENT_SECRETS.client_id,
CLIENT_SECRETS.client_secret,
:scope => [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me'
],
:skip_info => false
end
get '/main' do
if session['credentials']
# Build an authorization object from the client secrets.
authorization = CLIENT_SECRETS.to_authorization
authorization.update_token!(
:access_token => session['credentials']['access_token'],
:refresh_token => session['credentials']['refresh_token']
)
@token = session['credentials']['access_token']
result = client1.execute(
:api_method => oauth2.userinfo.get,
:authorization => authorization
)
puts result.status
if result.status == 401
# The access token expired, fetch a new one and retry once.
redirect '/logout'
end
puts session['credentials']
session['email'] = result.data.email
user = User.first(:email => session['email'])
logged_in = true
wepo_only = false
if !user
if result.data.hd.include? "westport.k12.ct.us"
new_user = User.new
new_user.name = result.data.name
new_user.email = result.data.email
new_user.save
logged_in = true
wepo_only = false
first_time = true
else
session['credentials'] = nil
session.clear
logged_in = false
wepo_only = true
end
end
else
logged_in = false
end
entries = Entry.all
@teacher_entries = Entry.all
f = File.new('./staff_directory.json', 'rb')
data = JSON.parse(f.read)
f.close
puts data.inspect
names = []
data.each do |person|
names << person['name']
end
erb :index, :locals => {
:result => result,
:data => data["Employees"],
:logged_in => logged_in,
:entries => Entry.all,
:wepo_only => wepo_only,
:first_time => first_time,
:names => names
}
end
post '/post/create' do
if session['credentials']
user = User.first(:email => session['email'])
entry = Entry.new
entry.rating = params[:rating].to_i
entry.body = params[:body]
entry.teacher_id = params[:teacherID].to_i
entry.user = user
entry.created_at = Time.now
entry.save
stars = ""
entry.rating.times do
stars << '<div class="reviewRating"></div>'
end
d = DateTime.parse (entry.created_at.to_s)
"<li class='list-group-item entryList'>
<span style='float: left; color: #d50000; display: inline-block' align='left'>#{entry.user.name}</span>
<span style='float: right; display: inline-block; color: greenyellow'>
#{d.strftime('%A, %B %d, %Y')} -
#{d.strftime('%-I:%M:%S %p')}
</span><br /><br />
<B>Rating:</B><br />
#{stars}
<br />
<b>Review:</b><br /> #{entry.body}
</li>"
else
redirect '/'
end
end
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
def confirmation_post(url)
"<form method='post' action='' class='inline'>" +
"<input type='submit' value='delete'>" +
"</form>"
end
end