-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.rb
184 lines (158 loc) · 5.22 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
require 'sinatra'
require 'models/user'
require 'models/service_space'
require 'models/event'
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:domain => CONFIG['app']['cookie_domain'],
:secret => 'averymanteroldfatherbesseyhamilton',
:old_secret => 'averymanteroldfatherbesseyhamilton',
:expire_after => 30*24*60*60
Time.zone = "America/Chicago"
# this gives the user messages
def flash(type, header, message)
session["notice"] ||= []
session["notice"] << {
:type => type,
:header => header,
:message => message
}
end
SS_ID = ServiceSpace.where(:id => CONFIG['app']['service_space_id']).first.id
before do
# site defaults
@inline_body_script_content = ''
@affiliation = 'Nebraska Innovation Campus'
@affiliation_link = 'https://innovate.unl.edu/'
CONFIG['app']['title'] = 'Innovation Studio Manager'
@breadcrumbs = [
{
:href => 'https://www.unl.edu/',
:text => 'Nebraska',
:title => 'University of Nebraska–Lincoln Home'
},
{
:href => 'https://innovationstudio.unl.edu/',
:text => 'Innovation Studio'
},
{
:href => '/',
:text => CONFIG['app']['title']
}
]
if SS_ID == 8
@inline_body_script_content = ''
@affiliation = 'College of Engineering'
@affiliation_link = 'https://engineering.unl.edu/'
CONFIG['app']['title'] = 'Engineering Garage'
@breadcrumbs = [
{
:href => 'https://www.unl.edu/',
:text => 'Nebraska',
:title => 'University of Nebraska–Lincoln Home'
},
{
:href => 'https://engineering.unl.edu/',
:text => 'College of Engineering'
},
{
:href => '/',
:text => CONFIG['app']['title']
}
]
end
session[:init] = true
# check if the user is currently logged in
if session.has_key?(:user_id)
@user = (User.includes(:permissions).find(session[:user_id]) rescue nil)
else
@user = nil;
end
end
def append_script_tag(src)
@inline_body_script_content << "<script type=\"text/javascript\" src=\"#{src}\"></script>\n"
end
def append_script_declaration(content)
@inline_body_script_content << "<script>#{content}</script>\n"
end
def has_permission?(permission)
[email protected]? && @user.has_permission?(permission)
end
def require_login(redirect_after_login=nil)
if @user.nil?
flash(:alert, 'You Must Login', 'That page requires you to be logged in. If you don\'t have an account, please sign up for <a href="/new_members/">New Member Orientation</a>.')
if redirect_after_login.nil?
redirect '/login/'
else
redirect "/login/?next_page=#{redirect_after_login}"
end
elsif SS_ID == 8 && [email protected]_super_user? && [email protected]_admin?
require_renewal(redirect_after_login)
end
end
def require_renewal(redirect_from=nil)
renewal_needed = false
if @user.get_user_agreement_expiration_date.nil?
renewal_needed = true
elsif @user.get_user_agreement_expiration_date <= Date.today
renewal_needed = true
end
if renewal_needed
# Avoid a redirect loop when coming from the user_agreement view
if !redirect_from.eql? "engineering_garage/user_agreement"
flash(:alert, 'User Agreement Expired', 'To continue to use the Engineering Garage, please renew your User Agreement.')
redirect '/engineering_garage/user_agreement'
end
end
end
end
def require_active(redirect_to=nil)
if SS_ID == 1
flash(:alert, 'You Must Be An Active User', 'That page requires you to be an active user. To activate your account please visit Innovation Studio.')
elsif SS_ID == 8
flash(:alert, 'You Must Be An Active User', 'That page requires you to be an active user. To activate your account please visit the Engineering Garage.')
end
if redirect_to.nil?
redirect '/'
else
redirect redirect_to
end
end
end
def drupal_link_lookup(key)
# Leading and Trailing Slash IMPORTANT
nodes = {
sop_training_doc: '/equipment/sop-training-documents/',
}
return nodes[key] if nodes.key?(key)
end
not_found do
@breadcrumbs << {:text => 'Not Found'}
erb 'That page was not found.', :layout => :fixed
end
error do
@breadcrumbs << {:text => 'Error'}
flash(:danger, 'Sorry! There was an error.', "We apologize. A really bad error occurred and it didn't work. We're fixing this as we speak.")
erb 'In the meantime, you can <a href="/">go back to the homepage</a>.', :layout => :fixed
end
get '/' do
@breadcrumbs << {:text => 'Home'}
redirect '/login/'
end
get '/images/user/:user_id/?' do
user = User.find_by(:id => params[:user_id], :service_space_id => SS_ID)
if user.nil? || user.imagedata.nil?
raise Sinatra::NotFound
end
return user.imagedata
end
get '/images/:event_id/?' do
event = Event.find_by(:id => params[:event_id])
if event.nil? || event.imagedata.nil?
raise Sinatra::NotFound
end
return event.imagedata
end
Dir.glob("#{ROOT}/routes/*.rb") { |file| require file }