root/trunk/website/forums/cfcs/user.cfc @ 5

Revision 5, 19.4 kB (checked in by DanWilson, 17 years ago)

Initial Commit Of ModelGlue? Website (upgrade to blogcfc 511)

Line 
1<!---
2        Name         : user.cfc
3        Author       : Raymond Camden
4        Created      : January 25, 2005
5        Last Updated : November 6, 2006
6        History      : Switched to UUID (rkc 1/25/05)
7                                   Added subscribe (rkc 7/24/05)
8                                   Added unsubscribe, modified code for adduser/saveuser (rkc 7/29/05)
9                                   Fixed bugs relating to last changes (rkc 8/3/05)
10                                   New init, use of prefix (rkc 8/27/05)
11                                   subscribe method didn't restrict to one user (rkc 10/6/05)
12                                   require confirmation support, dynamic title (rkc 7/12/06)
13                                   password encryption option, signatures (rkc 11/3/06)
14                                   if no confirmtation, set confirmation to 1 (rkc 11/6/06)
15        Purpose          :
16--->
17<cfcomponent displayName="User" hint="Handles all user/security issues for the application.galleon.">
18
19        <cfset variables.dsn = "">
20        <cfset variables.dbtype = "">
21        <cfset variables.tableprefix = "">
22        <cfset variables.requireconfirmation = 0>
23        <cfset variables.title = "">
24        <cfset variables.encryptpasswords = false>
25       
26
27        <cffunction name="init" access="public" returnType="user" output="false"
28                                hint="Returns an instance of the CFC initialized with the correct DSN.">
29                <cfreturn this>
30               
31        </cffunction>
32
33        <cffunction name="addUser" access="public" returnType="void" output="false"
34                                hint="Attempts to create a new user.">
35                <cfargument name="username" type="string" required="true">
36                <cfargument name="password" type="string" required="true">
37                <cfargument name="emailaddress" type="string" required="true">
38                <cfargument name="groups" type="string" required="false">
39                <cfargument name="confirmed" type="boolean" required="false" default="false">
40                <cfargument name="signature" type="string" required="false">
41               
42                <cfset var checkuser = "">
43                <cfset var insuser = "">
44                <cfset var newid = createUUID()>
45               
46                <cflock name="user.cfc" type="exclusive" timeout="30">
47                        <cfquery name="checkuser" datasource="#variables.dsn#">
48                                select  id
49                                from    #variables.tableprefix#users
50                                where   username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
51                        </cfquery>
52                       
53                        <cfif checkuser.recordCount>
54                                <cfset variables.utils.throw("User CFC","User already exists")>
55                        <cfelse>
56                                <!--- If system requires confirmation, set it to 0. --->
57                                <cfif variables.requireconfirmation and not arguments.confirmed>
58                                        <cfmail to="#arguments.emailaddress#" from="#application.galleon.settings.fromAddress#" subject="#variables.title# Confirmation Required">
59To complete your registration at #variables.title#, please click on the link below.
60
61#application.galleon.settings.rooturl#confirm.cfm?u=#newid#
62                                        </cfmail>
63                                </cfif>
64                               
65                                <!--- hash password --->
66                                <cfif variables.encryptpasswords>
67                                        <cfset arguments.password = hash(arguments.password)>
68                                </cfif>
69                                <cfif not variables.requireconfirmation>
70                                        <cfset arguments.confirmed = 1>
71                                </cfif>
72                               
73                                <cfquery name="insuser" datasource="#variables.dsn#">
74                                insert into #variables.tableprefix#users(id,username,password,emailaddress,datecreated,confirmed,signature)
75                                values(<cfqueryparam value="#newid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">,
76                                <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">,
77                                <cfqueryparam value="#arguments.password#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">,
78                                <cfqueryparam value="#arguments.emailaddress#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
79                                <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">,
80                            <cfqueryparam value="#arguments.confirmed#" cfsqltype="CF_SQL_BIT">,
81                                <cfif structKeyExists(arguments, "signature")>
82                            <cfqueryparam value="#left(htmlEditFormat(arguments.signature),1000)#" cfsqltype="cf_sql_varchar">
83                            <cfelse>
84                            ''
85                            </cfif>
86                                )
87                                </cfquery>
88                                <cfif isDefined("arguments.groups") and len(arguments.groups)>
89                                        <cfset assignGroups(arguments.username,arguments.groups)>
90                                </cfif>
91                        </cfif>
92                       
93                </cflock>
94        </cffunction>
95
96        <cffunction name="assignGroups" access="private" returnType="void" output="false"
97                                hint="Assigns a user to groups.">
98                <cfargument name="username" type="string" required="true">
99                <cfargument name="groups" type="string" required="true">
100                <cfset var uid = getUserId(arguments.username)>
101                <cfset var gid = "">
102                <cfset var group = "">
103                               
104                <cfloop index="group" list="#arguments.groups#">
105                        <cfset gid = getGroupID(group)>
106                        <cfquery datasource="#variables.dsn#">
107                                insert into #variables.tableprefix#users_groups(useridfk,groupidfk)
108                                values(<cfqueryparam value="#uid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">,<cfqueryparam value="#gid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">)
109                        </cfquery>
110                </cfloop>
111               
112        </cffunction>
113               
114        <cffunction name="authenticate" access="public" returnType="boolean" output="false"
115                                hint="Returns true or false if the user authenticates.">
116                <cfargument name="username" type="string" required="true">
117                <cfargument name="password" type="string" required="true">
118                <cfset var qAuth = "">
119               
120                <cfif variables.encryptpasswords>
121                        <cfset arguments.password = hash(arguments.password)>
122                </cfif>
123                               
124                <cfquery name="qAuth" datasource="#variables.dsn#">
125                        select  id
126                        from    #variables.tableprefix#users
127                        where   username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
128                        and             password = <cfqueryparam value="#arguments.password#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
129                        and             confirmed = 1
130                </cfquery>
131               
132                <cfreturn qAuth.recordCount gt 0>
133                       
134        </cffunction>
135
136        <cffunction name="confirm" access="public" returnType="boolean" output="false"
137                                hint="Confirms a user.">
138                <cfargument name="id" type="uuid" required="true">
139                <cfset var q = "">
140               
141                <cfquery name="q" datasource="#variables.dsn#">
142                select  id
143                from    #variables.tableprefix#users
144                where   id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_varchar" maxlength="35">
145                </cfquery>
146               
147                <cfif q.recordCount is 1>
148                        <cfquery datasource="#variables.dsn#">
149                        update  #variables.tableprefix#users
150                        set             confirmed = 1
151                        where   id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_varchar" maxlength="35">
152                        </cfquery>
153                </cfif>
154               
155                <cfreturn q.recordCount is 1>
156               
157        </cffunction>
158       
159        <cffunction name="deleteUser" access="public" returnType="void" output="false"
160                                hint="Deletes a user.">
161                <cfargument name="username" type="string" required="true">
162                <cfset var uid = getUserId(arguments.username)>
163
164                <cflock name="user.cfc" type="exclusive" timeout="30">
165
166                <cfquery datasource="#variables.dsn#">
167                        delete  from #variables.tableprefix#users_groups
168                        where   useridfk = <cfqueryparam value="#uid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
169                </cfquery>
170                       
171                <cfquery datasource="#variables.dsn#">
172                        delete  from #variables.tableprefix#users
173                        where   id = <cfqueryparam value="#uid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
174                </cfquery>
175
176                <cfquery datasource="#variables.dsn#">
177                        delete  from #variables.tableprefix#subscriptions
178                        where   useridfk = <cfqueryparam value="#uid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
179                </cfquery>
180
181                </cflock>
182                                               
183        </cffunction>
184       
185        <cffunction name="getGroupID" access="public" returnType="uuid" output="false"
186                                hint="Returns a group id.">
187                <cfargument name="group" type="string" required="true">
188                <cfset var qGetGroup = "">
189               
190                <cfquery name="qGetGroup" datasource="#variables.dsn#">
191                        select  id
192                        from    #variables.tableprefix#groups
193                        where
194                        <cfif variables.dbtype is not "mysql">
195                        [group]
196                        <cfelse>
197                        #variables.tableprefix#groups.group
198                        </cfif>
199                         = <cfqueryparam value="#arguments.group#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
200                </cfquery>
201               
202                <cfif qGetGroup.recordCount>
203                        <cfreturn qGetGroup.id>
204                <cfelse>
205                        <cfset variables.utils.throw("UserCFC","Invalid Group [#arguments.group#]")>
206                </cfif>
207                               
208        </cffunction>
209
210        <cffunction name="getGroups" access="public" returnType="query" output="false"
211                                hint="Returns a query of all the known groups.">
212                <cfset var qGetGroups = "">
213
214                <cfquery name="qGetGroups" datasource="#variables.dsn#">
215                        select  id,
216                        <cfif variables.dbtype is not "mysql">
217                        [group]
218                        <cfelse>
219                        #variables.tableprefix#groups.group
220                        </cfif>
221                        from    #variables.tableprefix#groups
222                </cfquery>
223               
224                <cfreturn qGetGroups>
225               
226        </cffunction>
227       
228        <cffunction name="getGroupsForUser" access="public" returnType="string" output="false"
229                                hint="Returns a list of groups for a user.">
230                <cfargument name="username" type="string" required="true">
231                <cfset var qGetGroups = "">
232               
233                <cfquery name="qGetGroups" datasource="#variables.dsn#">
234                        <cfif variables.dbtype is not "mysql">
235                                select  #variables.tableprefix#groups.[group]
236                        <cfelse>
237                                select #variables.tableprefix#groups.group
238                        </cfif>
239                        from    #variables.tableprefix#users, #variables.tableprefix#groups, #variables.tableprefix#users_groups
240                        where   #variables.tableprefix#users_groups.useridfk = #variables.tableprefix#users.id
241                        and             #variables.tableprefix#users_groups.groupidfk = #variables.tableprefix#groups.id
242                        and             #variables.tableprefix#users.username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
243                </cfquery>
244               
245                <cfreturn valueList(qGetGroups.group)>
246                       
247        </cffunction>
248
249
250        <cffunction name="getSubscriptions" access="public" returnType="query" output="false"
251                                hint="Gets subscriptions for a user.">
252                <cfargument name="username" type="string" required="true">
253                <cfset var uid = getUserId(arguments.username)>
254                <cfset var q = "">
255               
256                <cfquery name="q" datasource="#variables.dsn#">
257                        select  id, threadidfk, forumidfk, conferenceidfk
258                        from    #variables.tableprefix#subscriptions
259                        where   useridfk = <cfqueryparam value="#uid#" cfsqltype="cf_sql_varchar" maxlength="35">
260                </cfquery>
261               
262                <cfreturn q>
263        </cffunction>
264       
265        <cffunction name="getUser" access="public" returnType="struct" output="false"
266                                hint="Returns a user.">
267                <cfargument name="username" type="string" required="true">
268                <cfset var qGetUser = "">
269                <cfset var user = structNew()>
270               
271                <!---
272                <cfquery name="qGetUser" datasource="#variables.dsn#">         
273                select #variables.tableprefix#users.id, #variables.tableprefix#users.username, #variables.tableprefix#users.password,
274                #variables.tableprefix#users.emailaddress, #variables.tableprefix#users.datecreated, count(#variables.tableprefix#messages.id) as postcount,
275                #variables.tableprefix#users.confirmed, #variables.tableprefix#users.signature
276                from #variables.tableprefix#users
277                left join  #variables.tableprefix#messages
278                on  #variables.tableprefix#users.id = #variables.tableprefix#messages.useridfk
279                where username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
280                group by #variables.tableprefix#users.id, #variables.tableprefix#users.username, #variables.tableprefix#users.password, #variables.tableprefix#users.emailaddress, #variables.tableprefix#users.datecreated, #variables.tableprefix#users.confirmed
281                </cfquery>
282                Decided to switch to 2 queries since I can't get the signature w/o group by and it's going to be a longvarchar in access
283                --->
284               
285                <cfquery name="qGetUser" datasource="#variables.dsn#">         
286                select #variables.tableprefix#users.id, #variables.tableprefix#users.username, #variables.tableprefix#users.password,
287                #variables.tableprefix#users.emailaddress, #variables.tableprefix#users.datecreated,
288                #variables.tableprefix#users.confirmed, #variables.tableprefix#users.signature
289                from #variables.tableprefix#users
290                where #variables.tableprefix#users.username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
291                </cfquery>
292
293                <cfquery name="qGetPostCount" datasource="#variables.dsn#">
294                select  count(id) as postcount
295                from    #variables.tableprefix#messages
296                where   useridfk = <cfqueryparam cfsqltype="cf_sql_varchar" value="#qGetUser.id#" maxlength="35">
297                </cfquery>
298                               
299                <cfset user = variables.utils.queryToStruct(qGetUser)>
300                <cfif qGetPostCount.postcount neq "">
301                        <cfset user.postcount = qGetPostCount.postcount>
302                <cfelse>
303                        <cfset user.postcount = 0>
304                </cfif>
305                <cfset user.groups = getGroupsForUser(arguments.username)>
306               
307                <cfreturn user>
308                       
309        </cffunction>
310
311        <cffunction name="getUserID" access="public" returnType="uuid" output="false"
312                                hint="Returns a user id.">
313                <cfargument name="username" type="string" required="true">
314                <cfset var qGetUser = "">
315               
316                <cfquery name="qGetUser" datasource="#variables.dsn#">
317                        select  id
318                        from    #variables.tableprefix#users
319                        where   username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
320                </cfquery>
321               
322                <cfif qGetUser.recordCount>
323                        <cfreturn qGetUser.id>
324                <cfelse>
325                        <cfset variables.utils.throw("UserCFC","Invalid Username")>
326                </cfif>
327                               
328        </cffunction>
329
330        <cffunction name="getUsernameFromID" access="public" returnType="string" output="false"
331                                hint="Returns a username from a user id.">
332                <cfargument name="userid" type="string" required="true">
333                <cfset var qGetUser = "">
334               
335                <cfquery name="qGetUser" datasource="#variables.dsn#">
336                        select  username
337                        from    #variables.tableprefix#users
338                        where   id = <cfqueryparam value="#arguments.userid#" cfsqltype="cf_sql_varchar" maxlength="35">
339                </cfquery>
340               
341                <cfreturn qGetUser.username>
342        </cffunction>
343       
344        <cffunction name="getUsers" access="public" returnType="query" output="false"
345                                hint="Returns all the users.">
346                <cfset var qGetUsers = "">
347               
348                <cfquery name="qGetUsers" datasource="#variables.dsn#">
349                select #variables.tableprefix#users.id, #variables.tableprefix#users.username, #variables.tableprefix#users.password, #variables.tableprefix#users.emailaddress, #variables.tableprefix#users.datecreated, count(#variables.tableprefix#messages.id) as postcount, #variables.tableprefix#users.confirmed
350                from #variables.tableprefix#users
351                left join  #variables.tableprefix#messages
352                on  #variables.tableprefix#users.id = #variables.tableprefix#messages.useridfk
353                group by #variables.tableprefix#users.id, #variables.tableprefix#users.username, #variables.tableprefix#users.password, #variables.tableprefix#users.emailaddress, #variables.tableprefix#users.datecreated, #variables.tableprefix#users.confirmed
354                order by #variables.tableprefix#users.username
355                </cfquery>
356               
357                <cfreturn qGetUsers>
358                       
359        </cffunction>
360
361        <cffunction name="removeGroups" access="private" returnType="void" output="false"
362                                hint="Removes all groups from a user.">
363                <cfargument name="username" type="string" required="true">
364               
365                <cfset var uid = getUserId(arguments.username)>
366                               
367                <cfquery datasource="#variables.dsn#">
368                        delete from #variables.tableprefix#users_groups
369                        where useridfk = <cfqueryparam value="#uid#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
370                </cfquery>
371               
372        </cffunction>
373       
374        <cffunction name="saveUser" access="public" returnType="void" output="false"
375                                hint="Attempts to save a user.">
376                <cfargument name="username" type="string" required="true">
377                <cfargument name="password" type="string" required="true">
378                <cfargument name="emailaddress" type="string" required="true">
379                <cfargument name="datecreated" type="date" required="true">
380                <cfargument name="groups" type="string" required="false">
381                <cfargument name="confirmed" type="boolean" required="false" default="false">
382                <cfargument name="signature" type="string" required="false" >
383               
384                <cfset var uid = getUserId(arguments.username)>
385
386                <cfquery datasource="#variables.dsn#">
387                        update  #variables.tableprefix#users
388                        set             emailaddress = <cfqueryparam value="#arguments.emailaddress#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">,
389                                        password = <cfqueryparam value="#arguments.password#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">,
390                                        datecreated = <cfqueryparam value="#arguments.datecreated#" cfsqltype="CF_SQL_TIMESTAMP">,
391                                        confirmed = <cfqueryparam value="#arguments.confirmed#" cfsqltype="CF_SQL_BIT">
392                                        <cfif structKeyExists(arguments, "signature")>
393                                        ,
394                                        signature = <cfqueryparam value="#left(htmleditFormat(arguments.signature),1000)#" cfsqltype="cf_sql_varchar">
395                                        </cfif>
396                        where   username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
397                </cfquery>
398                       
399                <!--- remove groups --->
400                <cfset removeGroups(arguments.username)>
401               
402                <!--- assign groups --->
403                <cfset assignGroups(arguments.username,arguments.groups)>
404               
405        </cffunction>
406
407        <cffunction name="subscribe" access="public" returnType="void" output="false"
408                                hint="Subscribes a user to Galleon.">
409                <cfargument name="username" type="string" required="true">
410                <cfargument name="mode" type="string" required="true">
411                <cfargument name="id" type="uuid" required="true">
412                <cfset var uid = getUserId(arguments.username)>
413                <cfset var check = "">
414               
415                <cfif not listFindNoCase("conference,forum,thread", arguments.mode)>
416                        <cfset variables.utils.throw("UserCFC","Invalid Mode")>
417                </cfif>
418               
419                <cfquery name="check" datasource="#variables.dsn#">
420                select  useridfk
421                from    #variables.tableprefix#subscriptions
422                where   
423                                <cfif arguments.mode is "conference">
424                                conferenceidfk =
425                                <cfelseif arguments.mode is "forum">
426                                forumidfk =
427                                <cfelseif arguments.mode is "thread">
428                                threadidfk =
429                                </cfif>
430                                <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_varchar" maxlength="35">
431                and             useridfk = <cfqueryparam value="#uid#" cfsqltype="cf_sql_varchar" maxlength="35">                               
432                </cfquery>
433               
434                <cfif check.recordCount is 0>
435                        <cfquery datasource="#variables.dsn#">
436                        insert into #variables.tableprefix#subscriptions(id,useridfk,
437                                <cfif arguments.mode is "conference">
438                                conferenceidfk
439                                <cfelseif arguments.mode is "forum">
440                                forumidfk
441                                <cfelseif arguments.mode is "thread">
442                                threadidfk
443                                </cfif>)
444                        values(<cfqueryparam value="#createUUID()#" cfsqltype="cf_sql_varchar" maxlength="35">,
445                        <cfqueryparam value="#uid#" cfsqltype="cf_sql_varchar" maxlength="35">,
446                        <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_varchar" maxlength="35">)
447                        </cfquery>
448                </cfif>
449                               
450        </cffunction>           
451
452        <cffunction name="unsubscribe" access="public" returnType="void" output="false"
453                                hint="Unsubscribes a user from Galleon data.">
454                <cfargument name="username" type="string" required="true">
455                <cfargument name="id" type="uuid" required="true">
456                <cfset var uid = getUserId(arguments.username)>
457                               
458                <cfquery datasource="#variables.dsn#">
459                delete  from    #variables.tableprefix#subscriptions
460                where   id = <cfqueryparam value="#arguments.id#" cfsqltype="cf_sql_varchar" maxlength="35">
461                and             useridfk = <cfqueryparam value="#uid#" cfsqltype="cf_sql_varchar" maxlength="35">                               
462                </cfquery>             
463                               
464        </cffunction>           
465
466        <cffunction name="setSettings" access="public" output="No" returntype="void">
467                <cfargument name="settings" required="true" hint="Setting">
468
469                <cfset var cfg = arguments.settings.getSettings() />
470                <cfset variables.dsn = cfg.dsn>
471                <cfset variables.dbtype = cfg.dbtype>
472                <cfset variables.tableprefix = cfg.tableprefix>
473                <cfset variables.requireconfirmation = cfg.requireconfirmation>
474                <cfset variables.title = cfg.title>
475                <cfset variables.fromAddress = cfg.fromAddress>
476                <cfset variables.rooturl = cfg.rooturl>
477               
478        </cffunction>
479       
480        <cffunction name="setUtils" access="public" output="No" returntype="void">
481                <cfargument name="utils" required="true" hint="utils">
482                <cfset variables.utils = arguments.utils />
483        </cffunction>
484
485</cfcomponent>
Note: See TracBrowser for help on using the browser.