Question
Direct users to page based on groupID
I have a login system which returns a message user
authenticated or login failed and the users groupID which defines
his/her access level. I want to find the best way to handle what
happens now.
The users have three access levels (1,2,3) and can have more than one.
How do I send users to a page based on their access level? What is the most secure way of doing this? I am using cfcs so maybe I can create a method that handles this? Is it better to have the relocation done with cfifs on the actionform page (my index.cfm acts as a form and actionform page)? Thanks for any advice.
index.cfm
<cfparam name="form.username" default="">
<cfparam name="form.groupID" default="0">
<!--- Check for form submission --->
<cfif structKeyExists(form,"checkAuth")>
<!--- The user pressed the authenticate button --->
<cfinvoke
component="security"
method="authenticate"
returnVariable="authenticated"
cfcUsername="#form.username#"
cfcPassword="#form.password#">
<cfif len(variables.authenticated)>
<!--- now get their groupID --->
<cfinvoke component="security"
method="authorize"
cfcUsername="#form.username#"
returnVariable="grpID" />
<cfset groupID="#grpID#">
</cfif></cfif>
<cfif isDefined("variables.authenticated")>
<cfif variables.authenticated NEQ 0>
<b>Username and Password Authenticated Successfully!
<!--- output groupID in this case it only outputs one even though there are more for some users--->
<cfoutput>#variables.grpID#</cfoutput></b>
<cfinvoke
component="security"
method="authorize"
returnVariable="authorize"
cfcUsername="#form.username#">
<cfelse>
<b>Username and/or Password was incorrect!</b>
</cfif>
</cfif>
<form name="checkAuth" method="post" action="index1.cfm">
<br> <b>Email:</b>
<input name="username" type="Text" class="ftforminputsmall" tabindex="1" maxlength="50">
<b>Password:</b>
<input name="password" type="password" class="ftforminputsmall" maxlength="50" tabindex="2">
<input tabindex="3" type="Submit" name="checkAuth" class="ftforminputsmall">
security.cfc
<cfcomponent>
<cffunction access="public" name="authenticate" output="0">
<!--- security authentication function --->
<!--- username and password required --->
<cfargument name="cfcUsername" type="string" required="1" />
<cfargument name="cfcPassword" type="string" required="1" />
<!--- query the SecurityDB for the passed username and password --->
<cfquery name="checkAuthentication" datasource="SecurityDB" username="root" password="riveravon">
SELECT username, userID, totallogins, lastIP, lastbrowser, lastlogin
FROM Security
WHERE username = '#arguments.cfcUsername#'
AND password = '#arguments.cfcPassword#'
</cfquery>
<!--- return the appropriate result --->
<cfif checkAuthentication.recordCount>
<!--- check the users security groups so we can see what groupID and their access level--->
<cfquery name="getUserGroups" datasource="SecurityDB" username="root" password="riveravon">
SELECT security_groups.groupID, groups.groupID
FROM groups, security_groups
WHERE groups.groupID = security_groups.groupID
AND security_groups.username = '#arguments.cfcUsername#'
</cfquery>
<cfreturn checkAuthentication.username />
<cfelse>
<cfreturn 0 />
</cfif>
</cffunction>
<cffunction access="public" name="authorize" output="0">
<!--- security function finding what groupID and therefore access level--->
<!--- username from login form used to check group IDs --->
<cfargument name="cfcUsername" type="string" required="1" />
<!--- query the SecurityDB and get all group id for the passed username --->
<cfquery name="getUserGroups" datasource="SecurityDB" username="root" password="riveravon">
SELECT groupID
FROM Security_Groups
WHERE username = '#arguments.cfcUsername#'
</cfquery>
<!--- return the appropriate groupID(s) --->
<cfif getUserGroups.recordCount>
<cfreturn getUserGroups.groupID />
<cfelse>
<cfreturn 0 />
</cfif>
</cffunction>
</cfcomponent>
The users have three access levels (1,2,3) and can have more than one.
How do I send users to a page based on their access level? What is the most secure way of doing this? I am using cfcs so maybe I can create a method that handles this? Is it better to have the relocation done with cfifs on the actionform page (my index.cfm acts as a form and actionform page)? Thanks for any advice.
index.cfm
<cfparam name="form.username" default="">
<cfparam name="form.groupID" default="0">
<!--- Check for form submission --->
<cfif structKeyExists(form,"checkAuth")>
<!--- The user pressed the authenticate button --->
<cfinvoke
component="security"
method="authenticate"
returnVariable="authenticated"
cfcUsername="#form.username#"
cfcPassword="#form.password#">
<cfif len(variables.authenticated)>
<!--- now get their groupID --->
<cfinvoke component="security"
method="authorize"
cfcUsername="#form.username#"
returnVariable="grpID" />
<cfset groupID="#grpID#">
</cfif></cfif>
<cfif isDefined("variables.authenticated")>
<cfif variables.authenticated NEQ 0>
<b>Username and Password Authenticated Successfully!
<!--- output groupID in this case it only outputs one even though there are more for some users--->
<cfoutput>#variables.grpID#</cfoutput></b>
<cfinvoke
component="security"
method="authorize"
returnVariable="authorize"
cfcUsername="#form.username#">
<cfelse>
<b>Username and/or Password was incorrect!</b>
</cfif>
</cfif>
<form name="checkAuth" method="post" action="index1.cfm">
<br> <b>Email:</b>
<input name="username" type="Text" class="ftforminputsmall" tabindex="1" maxlength="50">
<b>Password:</b>
<input name="password" type="password" class="ftforminputsmall" maxlength="50" tabindex="2">
<input tabindex="3" type="Submit" name="checkAuth" class="ftforminputsmall">
security.cfc
<cfcomponent>
<cffunction access="public" name="authenticate" output="0">
<!--- security authentication function --->
<!--- username and password required --->
<cfargument name="cfcUsername" type="string" required="1" />
<cfargument name="cfcPassword" type="string" required="1" />
<!--- query the SecurityDB for the passed username and password --->
<cfquery name="checkAuthentication" datasource="SecurityDB" username="root" password="riveravon">
SELECT username, userID, totallogins, lastIP, lastbrowser, lastlogin
FROM Security
WHERE username = '#arguments.cfcUsername#'
AND password = '#arguments.cfcPassword#'
</cfquery>
<!--- return the appropriate result --->
<cfif checkAuthentication.recordCount>
<!--- check the users security groups so we can see what groupID and their access level--->
<cfquery name="getUserGroups" datasource="SecurityDB" username="root" password="riveravon">
SELECT security_groups.groupID, groups.groupID
FROM groups, security_groups
WHERE groups.groupID = security_groups.groupID
AND security_groups.username = '#arguments.cfcUsername#'
</cfquery>
<cfreturn checkAuthentication.username />
<cfelse>
<cfreturn 0 />
</cfif>
</cffunction>
<cffunction access="public" name="authorize" output="0">
<!--- security function finding what groupID and therefore access level--->
<!--- username from login form used to check group IDs --->
<cfargument name="cfcUsername" type="string" required="1" />
<!--- query the SecurityDB and get all group id for the passed username --->
<cfquery name="getUserGroups" datasource="SecurityDB" username="root" password="riveravon">
SELECT groupID
FROM Security_Groups
WHERE username = '#arguments.cfcUsername#'
</cfquery>
<!--- return the appropriate groupID(s) --->
<cfif getUserGroups.recordCount>
<cfreturn getUserGroups.groupID />
<cfelse>
<cfreturn 0 />
</cfif>
</cffunction>
</cfcomponent>