/
Application.cfc
271 lines (244 loc) · 11.3 KB
/
Application.cfc
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
<!--- Application Controller --->
<!--- Thomas Dye, August 2016 --->
<cfcomponent>
<cfset this.name='AdvisorPortal'>
<cfset this.datasource='AdvisorPortal'>
<cfset this.applicationTimeout=CreateTimeSpan(10, 0, 0, 0)>
<cfset this.sessionManagement=true>
<cfset this.sessionTimeout=CreateTimeSpan(0, 0, 30, 0)>
<cfset this.loginStorage='session'>
<cfset this.invokeImplicitAccessor=true>
<cfset this.sessionCookie.httpOnly=true>
<cfset this.sessionCookie.timeout='10'>
<cfset this.sessionCookie.disableupdate=true>
<cfset this.directory = getDirectoryFromPath( getCurrentTemplatePath() )>
<cfset this.mappings['/cfcMapping'] = "#this.directory#cfc/" />
<cfset this.mappings['cfc'] = "#this.directory#cfc/" />
<!--- Intercept all page requests by website users --->
<cffunction name="onRequest">
<cfargument name="templatename">
<!--- Display the login form if users aren't logged in. --->
<cflogin>
<cfset messageBean=createObject('cfc.messageBean').init()>
<cfset StructClear(session)>
<!--- Define form action for "Log in" button. --->
<cfif isDefined("form.loginButton")>
<!--- Perform simple validation on form fields --->
<cfif !len(trim(form.emailAddress))>
<cfset messageBean.addError('An email address is required.', 'emailAddress')>
<cfelseif !IsValid("email", trim(form.emailAddress))>
<cfset messageBean.addError('The email address is not valid.', 'emailAddress')>
</cfif>
<cfif !len(trim(form.password))>
<cfset messageBean.addError('A password is required.', 'password')>
</cfif>
<!--- Stop here if errors were detected --->
<cfif messageBean.hasErrors()>
<cfinclude template="login.cfm">
<cfreturn>
</cfif>
<!--- Find the account, if exists --->
<cftry>
<!--- Try to contact the database --->
<cfquery name="qLoginGetAccount">
SELECT id, active, first_name, last_name, email, password, salt
FROM ACCOUNTS
WHERE email = <cfqueryparam value="#trim(form.emailAddress)#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfcatch type="database">
<!--- If the connection timed out silently, try again --->
<cftry>
<cfquery name="qLoginGetAccount">
SELECT id, active, first_name, last_name, email, password, salt
FROM ACCOUNTS
WHERE email = <cfqueryparam value="#trim(form.emailAddress)#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfcatch type="database">
<!--- Generate a friendly error message and give up --->
<cfset messageBean.addError('The server is not responding. Please try again later.', 'email')>
<cfinclude template="login.cfm">
<cfreturn>
</cfcatch>
</cftry>
</cfcatch>
</cftry>
<!--- Check password. If does not match, fail. Otherwise, evaluate student or faculty account --->
<cfif !qLoginGetAccount.RecordCount || qLoginGetAccount.password NEQ Hash(form.password & qLoginGetAccount.salt, "SHA-512")>
<!--- The user login credentials were incorrect, so stop here --->
<cfset messageBean.addError('User or password were incorrect; please try again', 'email')>
<cfinclude template="login.cfm">
<cfreturn>
<cfelseif !qLoginGetAccount.active>
<!--- The user account is deactivated, so stop here --->
<cfset messageBean.addError('The account could not be loaded; please contact the administrator.', 'email')>
<cfinclude template="login.cfm">
<cfreturn>
<cfelse>
<cfquery name="qLoginGetStudent">
SELECT accounts_id, student_id
FROM STUDENTS
WHERE accounts_id = <cfqueryparam value="#qLoginGetAccount.id#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif qLoginGetStudent.RecordCount>
<!--- Found student record, so log in --->
<cfloginuser name="#qLoginGetAccount.id#" password="#form.password#" roles="student">
<cfset session.loginId="#qLoginGetAccount.id#">
<cfset session.loginName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfset session.accountId="#qLoginGetStudent.accounts_id#">
<cfset session.studentId="#qLoginGetStudent.student_id#">
<cfset session.studentName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfelse>
<cfquery name="qLoginGetFaculty">
SELECT accounts_id, editor, administrator
FROM FACULTY
WHERE accounts_id = <cfqueryparam value="#qLoginGetAccount.id#" cfsqltype="cf_sql_integer">
</cfquery>
<cfif qLoginGetFaculty.RecordCount && qLoginGetFaculty.administrator>
<!--- Log in as a faculty administrator --->
<cfloginuser name="#qLoginGetAccount.id#" password="#form.password#" roles="administrator,editor,advisor">
<cfset session.loginId="#qLoginGetAccount.id#">
<cfset session.loginName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfelseif qLoginGetFaculty.RecordCount && qLoginGetFaculty.editor>
<!--- Log in as a faculty editor --->
<cfloginuser name="#qLoginGetAccount.id#" password="#form.password#" roles="editor,advisor">
<cfset session.loginId="#qLoginGetAccount.id#">
<cfset session.loginName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfelseif qLoginGetFaculty.RecordCount>
<!--- Log in as a faculty advisor --->
<cfloginuser name="#qLoginGetAccount.id#" password="#form.password#" roles="advisor">
<cfset session.loginId="#qLoginGetAccount.id#">
<cfset session.loginName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfelse>
<!--- An account record exists, but a faculty record does not, so stop here --->
<cfset messageBean.addError('The account could not be loaded; please contact the administrator.', 'accounts_id')>
<cfinclude template="login.cfm">
<cfreturn>
</cfif>
</cfif>
</cfif>
<!--- Define form action for "Create an account" button. --->
<cfelseif isDefined("form.createButton")>
<!--- Perform simple validation on form fields --->
<cfif !len(trim(form.firstName))>
<cfset messageBean.addError('A first name is required.', 'firstName')>
</cfif>
<cfif !len(trim(form.lastName))>
<cfset messageBean.addError('A last name is required.', 'lastName')>
</cfif>
<cfif !len(trim(form.studentId))>
<cfset messageBean.addError('A student ID number is required.', 'studentId')>
<cfelseif !IsValid("integer", trim(form.studentId))>
<cfset messageBean.addError('Enter the student ID as a number with no spaces', 'studentId')>
</cfif>
<cfif !len(trim(form.emailAddress))>
<cfset messageBean.addError('An email address is required.', 'emailAddress')>
<cfelseif !IsValid("email", trim(form.emailAddress))>
<cfset messageBean.addError('The email address is not valid.', 'emailAddress')>
</cfif>
<cfif !len(trim(form.password))>
<cfset messageBean.addError('The password cannot be blank.', 'password')>
<cfelseif trim(form.password) NEQ trim(form.password2)>
<cfset messageBean.addError('The passwords do not match.', 'password')>
</cfif>
<!--- Stop here if errors were detected --->
<cfif messageBean.hasErrors()>
<cfinclude template="login.cfm">
<cfreturn>
</cfif>
<!--- Perform uniqueness validation on form fields --->
<cftry>
<!--- Try to contact the database --->
<cfquery name="qLoginCheckStudentId">
SELECT accounts_id, student_id
FROM STUDENTS
WHERE student_id = <cfqueryparam value="#trim(form.studentId)#" cfsqltype="cf_sql_integer">
</cfquery>
<cfcatch type="database">
<!--- If the connection timed out silently, try again --->
<cftry>
<cfquery name="qLoginCheckStudentId">
SELECT accounts_id, student_id
FROM STUDENTS
WHERE student_id = <cfqueryparam value="#trim(form.studentId)#" cfsqltype="cf_sql_integer">
</cfquery>
<cfcatch type="database">
<!--- Generate a friendly error message and give up --->
<cfset messageBean.addError('The server is not responding. Please try again later.', 'studentId')>
<cfinclude template="login.cfm">
<cfreturn>
</cfcatch>
</cftry>
</cfcatch>
</cftry>
<cfquery name="qLoginCheckEmail">
SELECT email
FROM ACCOUNTS
WHERE email = <cfqueryparam value="#trim(form.emailAddress)#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfif qLoginCheckStudentId.RecordCount>
<cfset messageBean.addError('This student ID is already in use.', 'studentId')>
<cfelseif qLoginCheckEmail.RecordCount>
<cfset messageBean.addError('This email address is already in use.', 'emailAddress')>
</cfif>
<!--- Stop here if errors were detected --->
<cfif messageBean.hasErrors()>
<cfinclude template="login.cfm">
<cfreturn>
</cfif>
<!--- Looks good, so create account --->
<cfif len(trim(form.password))>
<cfset salt=Hash(GenerateSecretKey("AES"), "SHA-512")>
<cfset password=Hash(trim(form.password) & salt, "SHA-512")>
</cfif>
<cfset emailAddress=canonicalize(trim(form.emailAddress), true, true)>
<cfset firstName=canonicalize(trim(form.firstName), true, true)>
<cfset lastName=canonicalize(trim(form.lastName), true, true)>
<cfquery>
INSERT INTO ACCOUNTS (
active, email, first_name, last_name, password, salt)
VALUES (
1,
<cfqueryparam value="#emailAddress#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#firstName#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#lastName#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#password#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#salt#" cfsqltype="cf_sql_varchar">)
</cfquery>
<cfquery name="qLoginGetAccount">
SELECT id, first_name, last_name, email, password, salt
FROM ACCOUNTS
WHERE email = <cfqueryparam value="#trim(form.emailAddress)#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfquery>
INSERT INTO STUDENTS (
accounts_id, student_id)
VALUES (
<cfqueryparam value="#qLoginGetAccount.id#" cfsqltype="cf_sql_integer">,
<cfqueryparam value="#trim(form.studentId)#" cfsqltype="cf_sql_integer">
)
</cfquery>
<cfquery name="qLoginGetStudent">
SELECT accounts_id, student_id
FROM STUDENTS
WHERE accounts_id = <cfqueryparam value="#qLoginGetAccount.id#" cfsqltype="cf_sql_integer">
</cfquery>
<!--- Log in the user in if the account was created successfully --->
<cfif qLoginGetStudent.RecordCount>
<cfloginuser name="#qLoginGetAccount.id#" password="#form.password#" roles="student">
<cfset session.loginId="#qLoginGetAccount.id#">
<cfset session.loginName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfset session.accountId="#qLoginGetStudent.accounts_id#">
<cfset session.studentId="#qLoginGetStudent.student_id#">
<cfset session.studentName="#qLoginGetAccount.first_name# #qLoginGetAccount.last_name#">
<cfelse>
<cfset messageBean.addError('Unable to create account.', 'emailAddress')>
</cfif>
<!--- Display default landing page. --->
<cfelse>
<cfinclude template="login.cfm">
<cfreturn>
</cfif>
</cflogin>
<cfinclude template="#arguments.templatename#">
</cffunction>
</cfcomponent>