device_tracker 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +4 -0
- data/Gemfile +4 -0
- data/Rakefile +10 -0
- data/Readme.md +32 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/device_tracker.gemspec +63 -0
- data/exe/device_tracker +91 -0
- data/lib/device_tracker/app.rb +22 -0
- data/lib/device_tracker/config-schema.json +111 -0
- data/lib/device_tracker/config.ru +9 -0
- data/lib/device_tracker/controllers/application_controller.rb +226 -0
- data/lib/device_tracker/controllers/devices_controller.rb +315 -0
- data/lib/device_tracker/controllers/heartbeat_controller.rb +50 -0
- data/lib/device_tracker/controllers/os_controller.rb +38 -0
- data/lib/device_tracker/controllers/transactions_controller.rb +18 -0
- data/lib/device_tracker/controllers/users_controller.rb +131 -0
- data/lib/device_tracker/db/migrate/20150521071815_create_users.rb +14 -0
- data/lib/device_tracker/db/migrate/20150521082155_create_devices.rb +19 -0
- data/lib/device_tracker/db/migrate/20150521120335_create_operating_systems.rb +9 -0
- data/lib/device_tracker/db/migrate/20150527162242_create_transactions.rb +12 -0
- data/lib/device_tracker/db/migrate/20151027073050_create_heartbeat.rb +11 -0
- data/lib/device_tracker/db/migrate/20151028132946_add_user_verification.rb +8 -0
- data/lib/device_tracker/db/migrate/20151028141328_remove_is_active_from_users.rb +6 -0
- data/lib/device_tracker/db/migrate/20151029085629_add_password_reset_code_to_users.rb +8 -0
- data/lib/device_tracker/db/migrate/20151030130341_add_missing_column_to_devices.rb +8 -0
- data/lib/device_tracker/db/migrate/20151102141601_add_serial_number_to_devices.rb +8 -0
- data/lib/device_tracker/db/schema.rb +61 -0
- data/lib/device_tracker/db/seeds.rb +21 -0
- data/lib/device_tracker/dependencies.rb +16 -0
- data/lib/device_tracker/device_tracker.rb +18 -0
- data/lib/device_tracker/helpers/application_helper.rb +116 -0
- data/lib/device_tracker/helpers/user_helper.rb +24 -0
- data/lib/device_tracker/models/device.rb +42 -0
- data/lib/device_tracker/models/heartbeat.rb +7 -0
- data/lib/device_tracker/models/operating_system.rb +7 -0
- data/lib/device_tracker/models/transaction.rb +62 -0
- data/lib/device_tracker/models/user.rb +28 -0
- data/lib/device_tracker/public/css/bootstrap-sortable.css +100 -0
- data/lib/device_tracker/public/css/bootstrap.min.css +5 -0
- data/lib/device_tracker/public/css/custom.css +88 -0
- data/lib/device_tracker/public/favicon/android-chrome-144x144.png +0 -0
- data/lib/device_tracker/public/favicon/android-chrome-192x192.png +0 -0
- data/lib/device_tracker/public/favicon/android-chrome-36x36.png +0 -0
- data/lib/device_tracker/public/favicon/android-chrome-48x48.png +0 -0
- data/lib/device_tracker/public/favicon/android-chrome-72x72.png +0 -0
- data/lib/device_tracker/public/favicon/android-chrome-96x96.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-114x114.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-120x120.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-144x144.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-152x152.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-180x180.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-57x57.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-60x60.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-72x72.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-76x76.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon-precomposed.png +0 -0
- data/lib/device_tracker/public/favicon/apple-touch-icon.png +0 -0
- data/lib/device_tracker/public/favicon/browserconfig.xml +12 -0
- data/lib/device_tracker/public/favicon/favicon-16x16.png +0 -0
- data/lib/device_tracker/public/favicon/favicon-32x32.png +0 -0
- data/lib/device_tracker/public/favicon/favicon-96x96.png +0 -0
- data/lib/device_tracker/public/favicon/favicon.ico +0 -0
- data/lib/device_tracker/public/favicon/manifest.json +41 -0
- data/lib/device_tracker/public/favicon/mstile-144x144.png +0 -0
- data/lib/device_tracker/public/favicon/mstile-150x150.png +0 -0
- data/lib/device_tracker/public/favicon/mstile-310x150.png +0 -0
- data/lib/device_tracker/public/favicon/mstile-310x310.png +0 -0
- data/lib/device_tracker/public/favicon/mstile-70x70.png +0 -0
- data/lib/device_tracker/public/favicon/safari-pinned-tab.svg +21 -0
- data/lib/device_tracker/public/favicon.png +0 -0
- data/lib/device_tracker/public/fonts/glyphicons-halflings-regular.eot +0 -0
- data/lib/device_tracker/public/fonts/glyphicons-halflings-regular.svg +288 -0
- data/lib/device_tracker/public/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/lib/device_tracker/public/fonts/glyphicons-halflings-regular.woff +0 -0
- data/lib/device_tracker/public/fonts/glyphicons-halflings-regular.woff2 +0 -0
- data/lib/device_tracker/public/js/bootstrap-sortable.js +211 -0
- data/lib/device_tracker/public/js/bootstrap.min.js +7 -0
- data/lib/device_tracker/public/js/jquery-2.1.4.min.js +4 -0
- data/lib/device_tracker/version.rb +4 -0
- data/lib/device_tracker/views/404.erb +25 -0
- data/lib/device_tracker/views/_alert.erb +5 -0
- data/lib/device_tracker/views/_device_form.erb +52 -0
- data/lib/device_tracker/views/_device_list.erb +47 -0
- data/lib/device_tracker/views/_footer.erb +3 -0
- data/lib/device_tracker/views/_header.erb +97 -0
- data/lib/device_tracker/views/_heartbeat_list.erb +25 -0
- data/lib/device_tracker/views/_user_form.erb +30 -0
- data/lib/device_tracker/views/devices/edit.erb +11 -0
- data/lib/device_tracker/views/devices/index.erb +12 -0
- data/lib/device_tracker/views/devices/new.erb +10 -0
- data/lib/device_tracker/views/devices/show.erb +283 -0
- data/lib/device_tracker/views/devices/users.erb +14 -0
- data/lib/device_tracker/views/emails/new_password.erb +17 -0
- data/lib/device_tracker/views/emails/password_reset.erb +18 -0
- data/lib/device_tracker/views/emails/registration.erb +16 -0
- data/lib/device_tracker/views/emails/reminder.erb +15 -0
- data/lib/device_tracker/views/emails/verification.erb +18 -0
- data/lib/device_tracker/views/forgot_password.erb +6 -0
- data/lib/device_tracker/views/index.erb +38 -0
- data/lib/device_tracker/views/layout.erb +8 -0
- data/lib/device_tracker/views/login.erb +14 -0
- data/lib/device_tracker/views/operating_system/operating_systems.json.jbuilder +9 -0
- data/lib/device_tracker/views/os/manage.erb +38 -0
- data/lib/device_tracker/views/transactions/_transactions_list.erb +18 -0
- data/lib/device_tracker/views/transactions/index.erb +3 -0
- data/lib/device_tracker/views/users/edit.erb +9 -0
- data/lib/device_tracker/views/users/manage.erb +31 -0
- data/lib/device_tracker/views/users/new.erb +7 -0
- metadata +505 -0
@@ -0,0 +1,315 @@
|
|
1
|
+
require_relative 'application_controller'
|
2
|
+
|
3
|
+
module DeviceTracker
|
4
|
+
# :nodoc:
|
5
|
+
class DevicesController < ApplicationController
|
6
|
+
before { protected! }
|
7
|
+
|
8
|
+
get '/new' do
|
9
|
+
admin_check
|
10
|
+
|
11
|
+
@device = Device.new
|
12
|
+
@operating_systems = OperatingSystem.select(:name, :id)
|
13
|
+
.distinct.order(:name)
|
14
|
+
erb :"devices/new"
|
15
|
+
end
|
16
|
+
|
17
|
+
post '/create' do
|
18
|
+
admin_check
|
19
|
+
|
20
|
+
attributes = {
|
21
|
+
unid: params[:device][:unid],
|
22
|
+
manufacturer: params[:device][:manufacturer],
|
23
|
+
device: params[:device][:device],
|
24
|
+
description: params[:device][:description],
|
25
|
+
imei: params[:device][:imei],
|
26
|
+
serial_number: params[:device][:serial_number],
|
27
|
+
operating_system_id: params[:device][:operating_system_id]
|
28
|
+
}
|
29
|
+
|
30
|
+
attributes[:sim_card] = true if params[:device][:sim_card]
|
31
|
+
|
32
|
+
attributes[:debug_device] = true if params[:device][:debug_device]
|
33
|
+
|
34
|
+
attributes.each do |key, value|
|
35
|
+
flash[key] = value
|
36
|
+
end
|
37
|
+
|
38
|
+
@device = Device.new(attributes)
|
39
|
+
|
40
|
+
if @device.valid?
|
41
|
+
@device.save
|
42
|
+
create_flash 'success', ["Created new device #{@device.full_name}"]
|
43
|
+
redirect "/devices/#{@device.id}"
|
44
|
+
else
|
45
|
+
create_flash 'warning', @device.errors.full_messages
|
46
|
+
redirect back
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
get '/' do
|
51
|
+
@title = 'All Devices'
|
52
|
+
|
53
|
+
@devices = Device.all
|
54
|
+
@users = User.all
|
55
|
+
|
56
|
+
@show_search = true
|
57
|
+
|
58
|
+
if params[:device_name] && !params[:device_name].empty?
|
59
|
+
@show_search = false
|
60
|
+
@devices = @devices.where(
|
61
|
+
'device like ? or manufacturer like ?',
|
62
|
+
"%#{params[:device_name]}%",
|
63
|
+
"%#{params[:device_name]}%"
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
@devices = @devices.sort { |a, b| a.full_name <=> b.full_name }
|
68
|
+
|
69
|
+
erb :"devices/index"
|
70
|
+
end
|
71
|
+
|
72
|
+
get '/checked-out' do
|
73
|
+
@devices = Device.where(available: false).order(checked_out_since: :desc)
|
74
|
+
@users = User.all
|
75
|
+
|
76
|
+
if params[:device_name] && !params[:device_name].empty?
|
77
|
+
@devices = @devices.where(
|
78
|
+
'description like ?',
|
79
|
+
"%#{params[:device_name]}%"
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
@title = 'Checked out Devices'
|
84
|
+
|
85
|
+
erb :"devices/index"
|
86
|
+
end
|
87
|
+
|
88
|
+
get '/:device_id' do |device_id|
|
89
|
+
fail Sinatra::NotFound unless Device.exists?(device_id)
|
90
|
+
|
91
|
+
@users = User.where(is_verified: true).order(email: :desc)
|
92
|
+
|
93
|
+
@device = Device.find(device_id)
|
94
|
+
|
95
|
+
if @device.missing?
|
96
|
+
@last_checkout_transaction = Transaction.where(
|
97
|
+
transaction_type: 'CHECKOUT',
|
98
|
+
device_id:
|
99
|
+
@device.id
|
100
|
+
).order(id: :desc).first
|
101
|
+
end
|
102
|
+
|
103
|
+
@transactions = @device.transactions.order(created_at: :desc).take(10)
|
104
|
+
|
105
|
+
@current_user = logged_in_user
|
106
|
+
|
107
|
+
erb :"devices/show"
|
108
|
+
end
|
109
|
+
|
110
|
+
put '/:device_id' do |device_id|
|
111
|
+
admin_check
|
112
|
+
|
113
|
+
fail Sinatra::NotFound unless Device.exists?(device_id)
|
114
|
+
|
115
|
+
params[:device].each do |key, value|
|
116
|
+
flash[key.to_sym] = value
|
117
|
+
end
|
118
|
+
|
119
|
+
@device = Device.find(device_id)
|
120
|
+
@device.update(params[:device])
|
121
|
+
|
122
|
+
if @device.save
|
123
|
+
|
124
|
+
create_flash 'success', ['The device has been successfully updated.']
|
125
|
+
redirect "/devices/#{device_id}"
|
126
|
+
else
|
127
|
+
create_flash 'warning', @device.errors.full_messages
|
128
|
+
redirect back
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# devices/users/1
|
133
|
+
get '/users/:user_id' do |user_id|
|
134
|
+
user = logged_in_user
|
135
|
+
|
136
|
+
if user[:id] != user_id.to_i
|
137
|
+
create_flash 'warning', ["You don't have permission to view this page."]
|
138
|
+
redirect back
|
139
|
+
end
|
140
|
+
|
141
|
+
fail Sinatra::NotFound unless User.exists?(user_id)
|
142
|
+
|
143
|
+
# Get the user
|
144
|
+
@user = User.find(user_id)
|
145
|
+
|
146
|
+
@devices = @user.devices
|
147
|
+
|
148
|
+
erb :"devices/users", devices: @devices
|
149
|
+
end
|
150
|
+
|
151
|
+
put '/:device_id/return' do |device_id|
|
152
|
+
unid = device_id.upcase
|
153
|
+
|
154
|
+
if Device.exists?(unid: unid)
|
155
|
+
@device = Device.find_by_unid(unid)
|
156
|
+
|
157
|
+
user = @device.user
|
158
|
+
|
159
|
+
current_user = logged_in_user
|
160
|
+
if current_user[:id] == @device.user.id
|
161
|
+
report_transaction(
|
162
|
+
"#{user.email} returned #{@device.device}",
|
163
|
+
Transaction.return, @device
|
164
|
+
)
|
165
|
+
else
|
166
|
+
report_transaction(
|
167
|
+
"#{current_user[:email]} returned #{@device.device} " \
|
168
|
+
"on behalf of #{@device.user.email}",
|
169
|
+
Transaction.return,
|
170
|
+
@device
|
171
|
+
)
|
172
|
+
end
|
173
|
+
|
174
|
+
@device.user = nil
|
175
|
+
@device.available = true
|
176
|
+
@device.checked_out_since = nil
|
177
|
+
|
178
|
+
@device.save
|
179
|
+
|
180
|
+
create_flash 'success',
|
181
|
+
['Device has been successfully returned, thank you.']
|
182
|
+
redirect back
|
183
|
+
else
|
184
|
+
create_flash 'warning', ["Device not found with id #{device_id}."]
|
185
|
+
redirect back
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
put '/:device_id/checkout' do |device_id|
|
190
|
+
unid = device_id.upcase
|
191
|
+
|
192
|
+
if Device.exists?(unid: unid)
|
193
|
+
|
194
|
+
@user = User.find(session[:user][:id])
|
195
|
+
|
196
|
+
device = Device.find_by_unid(unid)
|
197
|
+
device.user = @user
|
198
|
+
|
199
|
+
device.available = false
|
200
|
+
device.checked_out_since = Time.now.utc.iso8601
|
201
|
+
|
202
|
+
if device.save
|
203
|
+
report_transaction(
|
204
|
+
"#{@user.email} checked out #{device.device}.",
|
205
|
+
Transaction.checkout,
|
206
|
+
device
|
207
|
+
)
|
208
|
+
|
209
|
+
create_flash 'success',
|
210
|
+
['Device successfully checked out.']
|
211
|
+
redirect back
|
212
|
+
|
213
|
+
else
|
214
|
+
create_flash 'warning', device.errors.full_messages
|
215
|
+
redirect back
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
# /devices/<%= @device.id %>/assign
|
222
|
+
put '/:device_id/assign' do |device_id|
|
223
|
+
admin_check
|
224
|
+
|
225
|
+
if Device.exists?(device_id)
|
226
|
+
|
227
|
+
user_id = params[:user_id]
|
228
|
+
|
229
|
+
if user_id.nil?
|
230
|
+
create_flash 'warning',
|
231
|
+
['You must select a user to assign the device to']
|
232
|
+
redirect back
|
233
|
+
end
|
234
|
+
|
235
|
+
@current_user = User.find(session[:user][:id])
|
236
|
+
unless User.exists?(user_id)
|
237
|
+
create_flash 'danger', ["User not found with id #{user_id}"]
|
238
|
+
end
|
239
|
+
|
240
|
+
@assigned_user = User.find(user_id)
|
241
|
+
|
242
|
+
device = Device.find(device_id)
|
243
|
+
device.user = @assigned_user
|
244
|
+
|
245
|
+
device.available = false
|
246
|
+
device.checked_out_since = Time.now.utc.iso8601
|
247
|
+
|
248
|
+
if device.save
|
249
|
+
report_transaction(
|
250
|
+
"#{@current_user.email} checked out #{device.device} on " \
|
251
|
+
"behalf of #{@assigned_user.email}",
|
252
|
+
Transaction.checkout,
|
253
|
+
device
|
254
|
+
)
|
255
|
+
end
|
256
|
+
|
257
|
+
create_flash 'success',
|
258
|
+
["Device has been assigned to #{@assigned_user.name}."]
|
259
|
+
redirect back
|
260
|
+
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
post '/:device_id/send_reminder' do |device_id|
|
265
|
+
admin_check
|
266
|
+
|
267
|
+
if Device.exists?(device_id)
|
268
|
+
|
269
|
+
@device = Device.find(device_id)
|
270
|
+
@device_url = request.base_url + '/devices/' + device_id
|
271
|
+
|
272
|
+
template = File.read(settings.email_path + '/reminder.erb')
|
273
|
+
body = ERB.new(template).result(binding)
|
274
|
+
|
275
|
+
send_email(
|
276
|
+
to: @device.user.email,
|
277
|
+
from: 'no-reply@device-tracker',
|
278
|
+
subject: 'Reminder | Device Tracker',
|
279
|
+
body: body
|
280
|
+
)
|
281
|
+
|
282
|
+
# Send email
|
283
|
+
create_flash 'success',
|
284
|
+
["A reminder email has been sent to #{@device.user.name}."]
|
285
|
+
redirect "/devices/#{device_id}"
|
286
|
+
|
287
|
+
else
|
288
|
+
create_flash 'warning', ['Sorry, That device doesn\'t exists.']
|
289
|
+
redirect "/devices/#{device_id}"
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
get '/:device_id/edit' do |device_id|
|
294
|
+
admin_check
|
295
|
+
|
296
|
+
fail Sinatra::NotFound unless Device.exists?(device_id)
|
297
|
+
|
298
|
+
@device = Device.find(device_id)
|
299
|
+
@operating_systems = OperatingSystem.select(:name, :id)
|
300
|
+
.distinct.order(:name)
|
301
|
+
|
302
|
+
erb :"devices/edit"
|
303
|
+
end
|
304
|
+
|
305
|
+
delete '/:device_id' do |device_id|
|
306
|
+
admin_check
|
307
|
+
|
308
|
+
fail Sinatra::NotFound unless Device.exists?(device_id)
|
309
|
+
|
310
|
+
@device = Device.find(device_id).destroy
|
311
|
+
create_flash 'success', ["Deleted device #{@device.unid}"]
|
312
|
+
redirect '/devices'
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative 'application_controller'
|
2
|
+
|
3
|
+
module DeviceTracker
|
4
|
+
# :nodoc:
|
5
|
+
class HeartbeatController < ApplicationController
|
6
|
+
post '/' do
|
7
|
+
begin
|
8
|
+
heartbeat = JSON.parse(request.body.read)
|
9
|
+
rescue JSON::ParserError
|
10
|
+
halt 412, {
|
11
|
+
error: true,
|
12
|
+
message: 'Error parsing payload, check and try again.'
|
13
|
+
}.to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
# {"heartbeat": {"device_id": "BBCFM003",
|
17
|
+
# "longitude": 53.470096, "latitude": -2.281457}}
|
18
|
+
if valid_heartbeat?(heartbeat)
|
19
|
+
|
20
|
+
heartbeat = heartbeat['heartbeat']
|
21
|
+
|
22
|
+
unid = heartbeat['device_id'].upcase
|
23
|
+
|
24
|
+
if Device.exists?(unid: unid)
|
25
|
+
device = Device.find_by_unid(unid)
|
26
|
+
|
27
|
+
Heartbeat.create(longitude: heartbeat['longitude'],
|
28
|
+
latitude: heartbeat['latitude'],
|
29
|
+
device_id: device.id)
|
30
|
+
|
31
|
+
halt 201, {
|
32
|
+
error: false,
|
33
|
+
message: "Heartbeat for device: #{unid} received."
|
34
|
+
}.to_json
|
35
|
+
else
|
36
|
+
halt 404, {
|
37
|
+
error: true,
|
38
|
+
message: "The device with id: #{unid} doesn't exist."
|
39
|
+
}.to_json
|
40
|
+
end
|
41
|
+
|
42
|
+
else
|
43
|
+
halt 412, {
|
44
|
+
error: true,
|
45
|
+
message: 'Manformed heartbeat received, so will not be processed.'
|
46
|
+
}.to_json
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require_relative 'application_controller'
|
2
|
+
module DeviceTracker
|
3
|
+
# :nodoc:
|
4
|
+
class OSController < ApplicationController
|
5
|
+
before do
|
6
|
+
admin_check
|
7
|
+
end
|
8
|
+
|
9
|
+
get '/manage' do
|
10
|
+
@os = OperatingSystem.all.order(:name)
|
11
|
+
erb :"os/manage"
|
12
|
+
end
|
13
|
+
|
14
|
+
post '/create' do
|
15
|
+
@os = OperatingSystem.create(params[:os])
|
16
|
+
|
17
|
+
if @os.valid?
|
18
|
+
@os.save
|
19
|
+
create_flash 'success', ["Successfully added #{@os.name}"]
|
20
|
+
redirect back
|
21
|
+
else
|
22
|
+
create_flash 'warning', @os.errors.full_messages
|
23
|
+
redirect back
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
delete '/:os_id/delete' do |os_id|
|
28
|
+
if OperatingSystem.exists?(os_id)
|
29
|
+
OperatingSystem.destroy(os_id)
|
30
|
+
create_flash 'success', ['Operating system deleted.']
|
31
|
+
redirect back
|
32
|
+
else
|
33
|
+
create_flash 'warning', ['Operating system not found.']
|
34
|
+
redirect back
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require_relative 'application_controller'
|
2
|
+
|
3
|
+
module DeviceTracker
|
4
|
+
# :nodoc:
|
5
|
+
class TransactionsController < ApplicationController
|
6
|
+
get '/' do
|
7
|
+
@transactions = Transaction.limit(100).order(created_at: :desc).all
|
8
|
+
|
9
|
+
erb :"transactions/index"
|
10
|
+
end
|
11
|
+
|
12
|
+
get '/:device_id' do |device_id|
|
13
|
+
@transactions = Transaction.where(device_id: device_id)
|
14
|
+
.limit(100).order(created_at: :desc).all
|
15
|
+
erb :"transactions/index"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require_relative 'application_controller'
|
2
|
+
|
3
|
+
module DeviceTracker
|
4
|
+
# :nodoc:
|
5
|
+
class UsersController < ApplicationController
|
6
|
+
helpers UserHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
path = request.path_info.split('/')[1]
|
10
|
+
protected! unless %w(new create).include? path
|
11
|
+
admin_check if %w(manage).include? path
|
12
|
+
end
|
13
|
+
|
14
|
+
get '/manage' do
|
15
|
+
@users = User.all
|
16
|
+
erb :"users/manage"
|
17
|
+
end
|
18
|
+
|
19
|
+
get '/new' do
|
20
|
+
erb :"users/new"
|
21
|
+
end
|
22
|
+
|
23
|
+
get '/:user_id/edit' do |user_id|
|
24
|
+
unless allowed_to_edit_page?(logged_in_user, user_id)
|
25
|
+
flash_and_go_back 'warning',
|
26
|
+
["You don't have permission to view this page."]
|
27
|
+
end
|
28
|
+
|
29
|
+
@current_user = logged_in_user
|
30
|
+
|
31
|
+
if User.exists?(user_id)
|
32
|
+
@user = User.find(user_id)
|
33
|
+
@is_admin = @current_user[:is_admin]
|
34
|
+
erb :"users/edit"
|
35
|
+
else
|
36
|
+
flash_and_go_back 'warning', ["Sorry, but that user doesn't exist"]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
put '/:user_id' do |user_id|
|
41
|
+
unless User.exists?(user_id)
|
42
|
+
flash_and_go_back 'warning',
|
43
|
+
['I\'m sorry but that user does not exist!']
|
44
|
+
end
|
45
|
+
|
46
|
+
user = User.find(user_id)
|
47
|
+
users_previous_verification = user.is_verified
|
48
|
+
|
49
|
+
original_params = params[:user].clone
|
50
|
+
user.assign_attributes(user_params)
|
51
|
+
|
52
|
+
if admin?
|
53
|
+
user.is_admin = (!params[:user][:is_admin].nil? &&
|
54
|
+
params[:user][:is_admin] == 'on')
|
55
|
+
user.is_verified = (!params[:user][:is_verified].nil? &&
|
56
|
+
params[:user][:is_verified] == 'on')
|
57
|
+
end
|
58
|
+
|
59
|
+
unless original_params[:password].empty?
|
60
|
+
user.password = original_params[:password]
|
61
|
+
user.password_confirmation = original_params[:password_confirmation]
|
62
|
+
end
|
63
|
+
|
64
|
+
if user.save
|
65
|
+
if user.is_verified && users_previous_verification == false
|
66
|
+
verification_email(user)
|
67
|
+
end
|
68
|
+
flash_and_go_back 'success', ['The account was successfully updated.']
|
69
|
+
else
|
70
|
+
flash_and_go_back 'warning', user.errors.full_messages
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
post '/create' do
|
75
|
+
user = User.new(params[:user])
|
76
|
+
|
77
|
+
user.flashable_attrs.each do |key|
|
78
|
+
flash[key] = user.send(:"#{key}")
|
79
|
+
end
|
80
|
+
|
81
|
+
if user.valid?
|
82
|
+
user.is_verified = true if admin?
|
83
|
+
|
84
|
+
user.save
|
85
|
+
|
86
|
+
if admin?
|
87
|
+
create_flash 'info', ["#{user.name}'s account has been created"]
|
88
|
+
redirect '/users/manage'
|
89
|
+
else
|
90
|
+
report_transaction(
|
91
|
+
"#{user.email} registered a new account.",
|
92
|
+
Transaction.registration
|
93
|
+
)
|
94
|
+
admin_email_registration(request, user)
|
95
|
+
create_flash 'info',
|
96
|
+
['Success, check your email and activate your account.']
|
97
|
+
redirect '/'
|
98
|
+
end
|
99
|
+
|
100
|
+
else
|
101
|
+
flash_and_go_back 'warning', [user.errors.full_messages.first]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
delete '/:user_id/delete' do |user_id|
|
106
|
+
if User.exists?(user_id)
|
107
|
+
user = User.find(user_id)
|
108
|
+
|
109
|
+
email = user.email
|
110
|
+
|
111
|
+
if user.devices.count > 0
|
112
|
+
Device.where(user_id: user.id)
|
113
|
+
.update_all(available: true, checked_out_since: nil, user_id: nil)
|
114
|
+
end
|
115
|
+
|
116
|
+
if user.destroy
|
117
|
+
report_transaction(
|
118
|
+
"#{email} was deleted by #{logged_in_user[:email]}.",
|
119
|
+
Transaction.deletion
|
120
|
+
)
|
121
|
+
end
|
122
|
+
|
123
|
+
flash_and_go_back 'success',
|
124
|
+
['The user account has been successfully removed.']
|
125
|
+
else
|
126
|
+
flash_and_go_back 'warning',
|
127
|
+
['Unable to remove the user account, try again.']
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
class CreateUsers < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :users do |t|
|
5
|
+
t.string :username, index: true
|
6
|
+
t.string :name
|
7
|
+
t.string :password_digest
|
8
|
+
t.string :email
|
9
|
+
t.boolean :is_active, default: false
|
10
|
+
t.boolean :is_admin, default: false
|
11
|
+
t.timestamps null: false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
class CreateDevices < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :devices do |t|
|
5
|
+
t.string :unid, index: true
|
6
|
+
t.string :manufacturer
|
7
|
+
t.string :device
|
8
|
+
t.text :description
|
9
|
+
t.datetime :checked_out_since
|
10
|
+
t.boolean :available, default: true
|
11
|
+
t.string :imei, default: nil
|
12
|
+
t.belongs_to :operating_system
|
13
|
+
t.belongs_to :user
|
14
|
+
t.boolean :sim_card, default: false
|
15
|
+
t.boolean :debug_device, default: false
|
16
|
+
t.timestamps null: false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
class CreateTransactions < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :transactions do |t|
|
5
|
+
t.string :transaction_type, index: true
|
6
|
+
t.belongs_to :user
|
7
|
+
t.belongs_to :device
|
8
|
+
t.text :description
|
9
|
+
t.datetime :created_at
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# :nodoc:
|
2
|
+
class CreateHeartbeat < ActiveRecord::Migration
|
3
|
+
def change
|
4
|
+
create_table :heartbeats do |t|
|
5
|
+
t.decimal :longitude, precision: 10, scale: 6
|
6
|
+
t.decimal :latitude, precision: 10, scale: 6
|
7
|
+
t.belongs_to :device
|
8
|
+
t.timestamps null: false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|