/blogCFC6/org/camden/blog/user.cfc

http://github.com/boyzoid/BlogCFC5 · ColdFusion CFScript · 408 lines · 332 code · 76 blank · 0 comment · 4 complexity · a8d57a39c686f45662c4d20ecd011571 MD5 · raw file

  1. <cfcomponent>
  2. <cffunction name="addSubscriber" access="remote" returnType="string" output="false"
  3. hint="Adds a subscriber to the blog.">
  4. <cfargument name="email" type="string" required="true">
  5. <cfset var token = createUUID()>
  6. <cfset var getMe = "">
  7. <!--- First, lets see if this guy is already subscribed. --->
  8. <cfquery name="getMe" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  9. select email
  10. from tblblogsubscribers
  11. where email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="50">
  12. and blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  13. </cfquery>
  14. <cfif getMe.recordCount is 0>
  15. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  16. insert into tblblogsubscribers(email,
  17. token,
  18. blog,
  19. verified)
  20. values(<cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="50">,
  21. <cfqueryparam value="#token#" cfsqltype="cf_sql_varchar" maxlength="35">,
  22. <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">,
  23. 0
  24. )
  25. </cfquery>
  26. <cfreturn token>
  27. <cfelse>
  28. <cfreturn "">
  29. </cfif>
  30. </cffunction>
  31. <cffunction name="addUser" access="public" returnType="void" output="false">
  32. <cfargument name="username" type="string" required="true">
  33. <cfargument name="name" type="string" required="true">
  34. <cfargument name="password" type="string" required="true">
  35. <cfset var q = "">
  36. <cflock name="blogcfc.adduser" type="exclusive" timeout="60">
  37. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  38. select username
  39. from tblusers
  40. where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  41. and blog = <cfqueryparam cfsqltype="cf_sql_varchar" value="#instance.name#" maxlength="50">
  42. </cfquery>
  43. <cfif q.recordCount>
  44. <cfset variables.utils.throw("#arguments.name# already exists as a user.")>
  45. </cfif>
  46. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  47. insert into tblusers(username, name, password, blog)
  48. values(
  49. <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">,
  50. <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.name#" maxlength="50">,
  51. <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#" maxlength="50">,
  52. <cfqueryparam cfsqltype="cf_sql_varchar" value="#instance.name#" maxlength="50">
  53. )
  54. </cfquery>
  55. </cflock>
  56. </cffunction>
  57. <cffunction name="authenticate" access="public" returnType="boolean" output="false">
  58. <cfargument name="username" type="string" required="true">
  59. <cfargument name="password" type="string" required="true">
  60. <cfset var q = "">
  61. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  62. select username
  63. from tblusers
  64. where username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  65. and password = <cfqueryparam value="#arguments.password#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  66. and blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  67. </cfquery>
  68. <cfreturn q.recordCount is 1>
  69. </cffunction>
  70. <cffunction name="confirmSubscription" access="public" returnType="void" output="false"
  71. hint="Confirms a user's subscription to the blog.">
  72. <cfargument name="token" type="uuid" required="false">
  73. <cfargument name="email" type="string" required="false">
  74. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  75. update tblblogsubscribers
  76. set verified = 1
  77. <cfif structKeyExists(arguments, "token")>
  78. where token = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="35" value="#arguments.token#">
  79. <cfelseif structKeyExists(arguments, "email")>
  80. where email = <cfqueryparam cfsqltype="cf_sql_varchar" maxlength="255" value="#arguments.email#">
  81. <cfelse>
  82. <cfthrow message="Invalid call to confirmSubscription. Must pass token or email.">
  83. </cfif>
  84. </cfquery>
  85. </cffunction>
  86. <cffunction name="deleteUser" access="public" returnType="void" output="false" hint="Deletes a user.">
  87. <cfargument name="username" type="string" required="true">
  88. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  89. delete from tblusers
  90. where blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  91. and username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  92. </cfquery>
  93. </cffunction>
  94. <cffunction name="getBlogRoles" access="public" returnType="query" output="false">
  95. <cfset var q = "">
  96. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  97. select id, role, description
  98. from tblblogroles
  99. </cfquery>
  100. <cfreturn q>
  101. </cffunction>
  102. <cffunction name="getNameForUser" access="public" returnType="string" output="false">
  103. <cfargument name="username" type="string" required="true">
  104. <cfset var q = "">
  105. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  106. select name
  107. from tblusers
  108. where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  109. </cfquery>
  110. <cfreturn q.name>
  111. </cffunction>
  112. <cffunction name="getSubscribers" access="public" returnType="query" output="false"
  113. hint="Returns all people subscribed to the blog.">
  114. <cfargument name="verifiedonly" type="boolean" required="false" default="false">
  115. <cfset var getPeople = "">
  116. <cfquery name="getPeople" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  117. select email, token, verified
  118. from tblblogsubscribers
  119. where blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  120. <cfif arguments.verifiedonly>
  121. and verified = 1
  122. </cfif>
  123. order by email asc
  124. </cfquery>
  125. <cfreturn getPeople>
  126. </cffunction>
  127. <cffunction name="getUser" access="public" returnType="struct" output="false" hint="Returns a user for a blog.">
  128. <cfargument name="username" type="string" required="true">
  129. <cfset var q = "">
  130. <cfset var s = structNew()>
  131. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  132. select username, password, name
  133. from tblusers
  134. where blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  135. and username = <cfqueryparam value="#arguments.username#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  136. </cfquery>
  137. <cfif q.recordCount>
  138. <cfset s.username = q.username>
  139. <cfset s.password = q.password>
  140. <cfset s.name = q.name>
  141. <cfreturn s>
  142. <cfelse>
  143. <cfthrow message="Unknown user #arguments.username# for blog.">
  144. </cfif>
  145. </cffunction>
  146. <cffunction name="getUserByName" access="public" returnType="string" output="false"
  147. hint="Get username based on encoded name.">
  148. <cfargument name="name" type="string" required="true">
  149. <cfset var q = "">
  150. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  151. select username
  152. from tblusers
  153. where name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#replace(arguments.name,"_"," ","all")#" maxlength="50">
  154. </cfquery>
  155. <cfreturn q.username>
  156. </cffunction>
  157. <cffunction name="getUserBlogRoles" access="public" returnType="string" output="false">
  158. <cfargument name="username" type="string" required="true">
  159. <cfset var q = "">
  160. <!--- MSACCESS fix provided by Andy Florino --->
  161. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  162. <cfif instance.blogDBType is "MSACCESS">
  163. select tblblogroles.id
  164. from tblblogroles, tbluserroles, tblusers
  165. where (tblblogroles.id = tbluserroles.roleidfk and tbluserroles.username = tblusers.username)
  166. and tblusers.username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  167. and tblusers.blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  168. <cfelse>
  169. select tblblogroles.id
  170. from tblblogroles
  171. left join tbluserroles on tbluserroles.roleidfk = tblblogroles.id
  172. left join tblusers on tbluserroles.username = tblusers.username
  173. where tblusers.username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  174. and tblusers.blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  175. </cfif>
  176. </cfquery>
  177. <cfreturn valueList(q.id)>
  178. </cffunction>
  179. <cffunction name="getUsers" access="public" returnType="query" output="false" hint="Returns users for a blog.">
  180. <cfset var q = "">
  181. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  182. select username, password, name
  183. from tblusers
  184. where blog = <cfqueryparam value="#instance.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
  185. </cfquery>
  186. <cfreturn q>
  187. </cffunction>
  188. <cffunction name="isBlogAuthorized" access="public" returnType="boolean" output="false" hint="Simple wrapper to check session roles and see if you are cool to do stuff. Admin role can do all.">
  189. <cfargument name="role" type="string" required="true">
  190. <!--- Roles are IDs, but to make code simpler, we allow you to specify a string, so do a cached lookup conversion. --->
  191. <cfset var q = "">
  192. <!--- cache admin once --->
  193. <cfif not structKeyExists(variables.roles, 'admin')>
  194. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  195. select id
  196. from tblblogroles
  197. where role = <cfqueryparam cfsqltype="cf_sql_varchar" value="admin" maxlength="50">
  198. </cfquery>
  199. <cfset variables.roles['admin'] = q.id>
  200. </cfif>
  201. <cfif not structKeyExists(variables.roles, arguments.role)>
  202. <cfquery name="q" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  203. select id
  204. from tblblogroles
  205. where role = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.role#" maxlength="50">
  206. </cfquery>
  207. <cfset variables.roles[arguments.role] = q.id>
  208. </cfif>
  209. <cfreturn (listFindNoCase(session.roles, variables.roles[arguments.role]) or listFindNoCase(session.roles, variables.roles['admin']))>
  210. </cffunction>
  211. <cffunction name="removeSubscriber" access="remote" returnType="boolean" output="false"
  212. hint="Removes a subscriber user.">
  213. <cfargument name="email" type="string" required="true">
  214. <cfargument name="token" type="uuid" required="false">
  215. <cfset var getMe = "">
  216. <cfif not isUserInRole("admin") and not structKeyExists(arguments,"token")>
  217. <cfset variables.utils.throw("Unauthorized removal.")>
  218. </cfif>
  219. <!--- First, lets see if this guy is already subscribed. --->
  220. <cfquery name="getMe" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  221. select email
  222. from tblblogsubscribers
  223. where email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="50">
  224. <cfif structKeyExists(arguments, "token")>
  225. and token = <cfqueryparam value="#arguments.token#" cfsqltype="cf_sql_varchar" maxlength="35">
  226. </cfif>
  227. </cfquery>
  228. <cfif getMe.recordCount is 1>
  229. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  230. delete from tblblogsubscribers
  231. where email = <cfqueryparam value="#arguments.email#" cfsqltype="cf_sql_varchar" maxlength="50">
  232. <cfif structKeyExists(arguments, "token")>
  233. and token = <cfqueryparam value="#arguments.token#" cfsqltype="cf_sql_varchar" maxlength="35">
  234. </cfif>
  235. and blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  236. </cfquery>
  237. <cfreturn true>
  238. <cfelse>
  239. <cfreturn false>
  240. </cfif>
  241. </cffunction>
  242. <cffunction name="removeUnverifiedSubscribers" access="remote" returnType="void" output="false" roles="admin"
  243. hint="Removes all subscribers who are not verified.">
  244. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  245. delete from tblblogsubscribers
  246. where blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  247. and verified = 0
  248. </cfquery>
  249. </cffunction>
  250. <cffunction name="saveUser" access="public" returnType="void" output="false">
  251. <cfargument name="username" type="string" required="true">
  252. <cfargument name="name" type="string" required="true">
  253. <cfargument name="password" type="string" required="true">
  254. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  255. update tblusers
  256. set name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.name#" maxlength="50">,
  257. password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#" maxlength="50">
  258. where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  259. and blog = <cfqueryparam cfsqltype="cf_sql_varchar" value="#instance.name#" maxlength="50">
  260. </cfquery>
  261. </cffunction>
  262. <cffunction name="setUserBlogRoles" access="public" returnType="void" output="false" roles="admin" hint="Sets a user's blog roles">
  263. <cfargument name="username" type="string" required="true">
  264. <cfargument name="roles" type="string" required="true">
  265. <cfset var r = "">
  266. <!--- first, nuke old roles --->
  267. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  268. delete from tbluserroles
  269. where username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">
  270. and blog = <cfqueryparam cfsqltype="cf_sql_varchar" value="#instance.name#" maxlength="50">
  271. </cfquery>
  272. <cfloop index="r" list="#arguments.roles#">
  273. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  274. insert into tbluserroles(username, roleidfk, blog)
  275. values(
  276. <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#" maxlength="50">,
  277. <cfqueryparam cfsqltype="cf_sql_varchar" value="#r#" maxlength="35">,
  278. <cfqueryparam cfsqltype="cf_sql_varchar" value="#instance.name#" maxlength="50">
  279. )
  280. </cfquery>
  281. </cfloop>
  282. </cffunction>
  283. <cffunction name="unsubscribeThread" access="public" returnType="boolean" output="false"
  284. hint="Removes a user from a thread.">
  285. <cfargument name="commentID" type="UUID" required="true">
  286. <cfargument name="email" type="string" required="true">
  287. <cfset var verifySubscribe = "">
  288. <!--- First ensure that the commentID equals the email --->
  289. <cfquery name="verifySubscribe" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  290. select entryidfk
  291. from tblblogcomments
  292. where id = <cfqueryparam value="#arguments.commentID#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">
  293. and email = <cfqueryparam value="#arguments.email#" cfsqltype="CF_SQL_VARCHAR" maxlength="100">
  294. </cfquery>
  295. <!--- If we have a result, then set subscribe=0 for this user for ALL comments in the thread --->
  296. <cfif verifySubscribe.recordCount>
  297. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  298. update tblblogcomments
  299. set subscribe = 0
  300. where entryidfk = <cfqueryparam value="#verifySubscribe.entryidfk#"
  301. cfsqltype="CF_SQL_VARCHAR" maxlength="35">
  302. and email = <cfqueryparam value="#arguments.email#" cfsqltype="CF_SQL_VARCHAR" maxlength="100">
  303. </cfquery>
  304. <cfreturn true>
  305. </cfif>
  306. <cfreturn false>
  307. </cffunction>
  308. <cffunction name="updatePassword" access="public" returnType="boolean" output="false"
  309. hint="Updates the current user's password.">
  310. <cfargument name="oldpassword" type="string" required="true">
  311. <cfargument name="newpassword" type="string" required="true">
  312. <cfset var checkit = "">
  313. <cfquery name="checkit" datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  314. select password
  315. from tblusers
  316. where username = <cfqueryparam value="#getAuthUser()#" cfsqltype="cf_sql_varchar" maxlength="50">
  317. and password = <cfqueryparam value="#arguments.oldpassword#" cfsqltype="cf_sql_varchar" maxlength="50">
  318. and blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  319. </cfquery>
  320. <cfif checkit.recordCount is 0>
  321. <cfreturn false>
  322. <cfelse>
  323. <cfquery datasource="#instance.dsn#" username="#instance.username#" password="#instance.password#">
  324. update tblusers
  325. set password = <cfqueryparam value="#arguments.newpassword#" cfsqltype="cf_sql_varchar" maxlength="50">
  326. where username = <cfqueryparam value="#getAuthUser()#" cfsqltype="cf_sql_varchar" maxlength="50">
  327. and blog = <cfqueryparam value="#instance.name#" cfsqltype="cf_sql_varchar" maxlength="50">
  328. </cfquery>
  329. <cfreturn true>
  330. </cfif>
  331. </cffunction>
  332. </cfcomponent>