/
KeylessEntry.cfc
263 lines (195 loc) · 10.6 KB
/
KeylessEntry.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
<cfcomponent output="false">
<cffunction name="init">
<cfset this.version = "0.9.3">
<cfreturn this>
</cffunction>
<cffunction name="loginRequired">
<cfif Not structKeyExists(session,"currentUser")>
<cfset flashInsert(error="You do not have permissions to do that!")>
<cfset redirectTo(controller="sessions", action="new")>
</cfif>
</cffunction>
<cffunction name="loginProhibited">
<cfif structKeyExists(session,"currentUser")>
<cfset flashInsert(error="You are logged in, you cant do that!")>
<cfset redirectTo(route="home")>
</cfif>
</cffunction>
<cffunction name="isLoggedIn">
<cfif structKeyExists(session, "currentUser")>
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>
<cffunction name="isAdmin">
<cfif structKeyExists(session, "currentUser") and NOT session.currentUser.isAdmin>
<cfset flashInsert(error="You do not have permissions to do that!")>
<cfset redirectTo(route="home")>
</cfif>
</cffunction>
<!---
TODO Might look to add a function for checking if the user is the current user
--->
<!---
GENERATION METHODS
--->
<cffunction name="generateAuthCode">
<cfset var loc = {}>
<!--- Check the environment for "design" or "development" --->
<cfset $checkDevEnv()>
<!--- Setup the information for the user --->
<cfset loc.message = "">
<!--- Create the model --->
<cfset loc.message = loc.message & $generateModelFile('user') & "<br/>">
<!--- Create the controllers --->
<cfset loc.message = loc.message & $generateControllerFile('user') & "<br/>">
<cfset loc.message = loc.message & $generateControllerFile('session') & "<br/>">
<cfset loc.message = loc.message & $generateControllerFile('activation') & "<br/>">
<!--- Create the view folders--->
<cfset loc.message = loc.message & $generateViewFolders('user') & "<br/>">
<!--- Create the view files --->
<cfset loc.message = loc.message & $generateViewFile('index','user') & "<br/>">
<cfset loc.message = loc.message & $generateViewFile('new','user') & "<br/>">
<cfset loc.message = loc.message & $generateViewFile('edit','user') & "<br/>">
<cfset loc.message = loc.message & $generateViewFile('show','user') & "<br/>">
<cfset loc.message = loc.message & $generateViewFolders('session') & "<br/>">
<cfset loc.message = loc.message & $generateViewFile('new','session') & "<br/>">
<cfreturn loc.message>
</cffunction>
<cffunction name="$generateModelFile" access="public" returnType="string" output="false">
<cfargument name="name" type="string" required="true" hint="Name of the object">
<cfset var loc = {}>
<!--- Check that the file has not been already created --->
<cfif $checkIfExists(arguments.name, "Model")>
<cfset loc.message = "File 'models/#capitalize(arguments.name)#.cfc' already exists so skipped.">
<cfelse>
<cfset $moveFilesToFolder(arguments.name, "Model")>
<cfset loc.message = "File 'models/#capitalize(arguments.name)#.cfc' created.">
</cfif>
<cfreturn loc.message>
</cffunction>
<cffunction name="$generateViewFile" access="public" returnType="string" output="false">
<cfargument name="name" type="string" required="true" hint="Name of the file">
<cfargument name="modelName" type="string" required="true" hint="Name of the object">
<cfset var loc = {}>
<!--- Check that the file has not been already created --->
<cfif $checkIfExists(arguments.name, "ViewFile", arguments.modelName)>
<cfset loc.message = "File 'views/#lCase(pluralize(arguments.modelName))#/#arguments.name#.cfm' already exists so skipped.">
<cfelse>
<cfset $moveFilesToFolder(arguments.name, "ViewFile", arguments.modelName)>
<cfset loc.message = "File 'views/#lCase(pluralize(arguments.modelName))#/#arguments.name#.cfm' created.">
</cfif>
<cfreturn loc.message>
</cffunction>
<cffunction name="$generateViewFolders" access="public" returnType="string" output="false">
<cfargument name="name" type="string" required="true">
<cfset var loc = {}>
<!--- Check that the folder to store the views has not been already created --->
<cfif $checkIfExists(arguments.name, "View")>
<cfset loc.message = "Folder 'views/#capitalize(pluralize(arguments.name))#/' already exists so skipped.">
<cfelse>
<cfset $moveFilesToFolder(arguments.name, "View")>
<cfset loc.message = "Folder 'views/#capitalize(pluralize(arguments.name))#/' created.">
</cfif>
<cfreturn loc.message>
</cffunction>
<cffunction name="$generateControllerFile" access="public" returnType="string" output="false">
<cfargument name="name" type="string" required="true" hint="Name of the object">
<cfset var loc = {}>
<!--- Check that the file has not been already created --->
<cfif $checkIfExists(arguments.name, "Controller")>
<cfset loc.message = "File 'controllers/#capitalize(pluralize(arguments.name))#.cfc' already exists so skipped.">
<cfelse>
<cfset $moveFilesToFolder(arguments.name, "Controller")>
<cfset loc.message = "File 'controllers/#capitalize(pluralize(arguments.name))#.cfc' created.">
</cfif>
<cfreturn loc.message>
</cffunction>
<cffunction name="$checkIfExists" access="public" returntype="boolean" hint="Checks if the desired object is already created" output="false">
<cfargument name="name" type="string" required="true" hint="Name of the file to search for">
<cfargument name="type" type="string" required="true" hint="Type of file to look for (Model, View, Controller)">
<cfargument name="modelName" type="string" required="false">
<cfset var loc = {}>
<!--- Expand the target folder --->
<cfswitch expression="#arguments.type#">
<cfcase value="Model">
<cfset loc.targetFolderPath = expandPath("models/")>
</cfcase>
<cfcase value="View">
<!---
TODO may need to set something up here to check which folder we are working on, users, or sessions
--->
<cfset loc.targetFolderPath = expandPath("views/" & LCase(pluralize(arguments.name)))>
</cfcase>
<cfcase value="ViewFile">
<cfset loc.targetFolderPath = expandPath("views/" & LCase(pluralize(arguments.modelName)))>
</cfcase>
<cfcase value="Controller">
<cfset loc.targetFolderPath = expandPath("controllers/")>
</cfcase>
</cfswitch>
<!--- Find the names of all the files in the targeted folder --->
<cfdirectory name="loc.files" action="list" directory="#loc.targetFolderPath#" type="file">
<!--- Check if the desired file is already in the targeted folder --->
<cfif FindNoCase(arguments.name, ValueList(loc.files.name)) GT 0 OR DirectoryExists(loc.targetFolderPath) AND arguments.type IS "View">
<cfset loc.wasFound = true>
<cfelse>
<cfset loc.wasFound = false>
</cfif>
<cfreturn loc.wasFound>
</cffunction>
<cffunction name="$moveFilesToFolder" access="public" returntype="void" output="false">
<cfargument name="name" type="string" required="true" hint="Name to set the file when moved">
<cfargument name="type" type="string" required="true" hint="Type of file to move for (Model, View, Controller)">
<cfargument name="modelName" type="string" required="false">
<cfset var loc = {}>
<!--- Expand the destination folder and read the file move over --->
<cfswitch expression="#arguments.type#">
<cfcase value="Model">
<!--- Expand the from and destination folders depding on the typ --->
<cfset loc.fromFolderPath = expandPath("plugins/keylessentry/templates/models")>
<cfset loc.destinationFolderPath = expandPath("models")>
<!--- Read the template file --->
<cffile action="read" file="#loc.fromFolderPath#/User.cfm" variable="loc.file">
<!--- Write the file in the corresponding folder --->
<cffile action="write" file="#loc.destinationFolderPath#/#capitalize(arguments.name)#.cfc" output="#loc.file#" mode="644">
</cfcase>
<cfcase value="View">
<!--- Expand the from and destination folders --->
<cfset loc.fromFolderPath = expandPath("plugins/keylessentry/templates/views/" & LCase(pluralize(arguments.name)))>
<cfset loc.destinationFolderPath = expandPath("views/" & LCase(pluralize(arguments.name)))>
<!--- Create the directory to store the views in --->
<cfif NOT DirectoryExists(loc.destinationFolderPath)>
<cfdirectory action="create" directory="#loc.destinationFolderPath#">
</cfif>
</cfcase>
<cfcase value="viewFile">
<!--- Expand the from and destination folders --->
<cfset loc.fromFolderPath = expandPath("plugins/keylessentry/templates/views/" & LCase(pluralize(arguments.modelName)))>
<cfset loc.destinationFolderPath = expandPath("views/" & LCase(pluralize(arguments.modelName)))>
<cffile action="read" file="#loc.fromFolderPath#/#lCase(arguments.name)#.cfm" variable="loc.file">
<cffile action="write" file="#loc.destinationFolderPath#/#lCase(arguments.name)#.cfm" output="#loc.file#" mode="644">
</cfcase>
<cfcase value="Controller">
<!--- Expand the from and destination folders --->
<cfset loc.fromFolderPath = expandPath("plugins/keylessentry/templates/controllers")>
<cfset loc.destinationFolderPath = expandPath("controllers")>
<!--- Read the template file --->
<cffile action="read" file="#loc.fromFolderPath#/#capitalize(pluralize(arguments.name))#.cfm" variable="loc.file">
<!--- Write the file in the corresponding folder --->
<cffile action="write" file="#loc.destinationFolderPath#/#capitalize(pluralize(arguments.name))#.cfc" output="#loc.file#" mode="644">
</cfcase>
<cfdefaultcase>
<!--- Display a nice Wheels error? --->
</cfdefaultcase>
</cfswitch>
</cffunction>
<cffunction name="$checkDevEnv" access="public" returnType="void" hint="Checks that the enviroment is either 'design' or 'development'" output="false">
<!--- Only run this if the environment is not "design" or "development" --->
<cfif get("environment") IS NOT "design" AND get("environment") IS NOT "development">
<!--- Throw an useful Wheels error --->
<cfset $throw(type="Wheels.Plugins.KeylessEntry.InvalidEnviroment", message="The method of this plugin that you are calling is not meant to run on production mode.", extendedInfo="If you want to use the plugin to generate your authentication code, please change your environment to 'design' or 'development'.")>
</cfif>
</cffunction>
</cfcomponent>