Skip to main content
Inspiring
May 7, 2006
Question

Direct users to page based on groupID

  • May 7, 2006
  • 4 replies
  • 1480 views
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>
This topic has been closed for replies.

4 replies

Inspiring
May 8, 2006
Quote the 1.
Also, do a cfdump of grpID for a user who is in more than one group. What does it look like?
Inspiring
May 8, 2006
Hi Dan the cfdump gives me:
AUTHENTICATED aking
GROUPID 1

I commented out the cfinvoke as it was duplicated above. As you can see only one groupID is being shown.

<cfparam name="form.username" default="">
<cfparam name="form.password" default="">
<!--- 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="groupID" />
<cfset groupID="#groupID#">
</cfif></cfif>

<cfif isDefined("variables.authenticated")>
<cfif variables.authenticated NEQ 0>
<b>Username and Password Authenticated Successfully!
<cfdump var="#variables#">

<!---
<cfif ListFind(Variables.groupID, "1")>
<cflocation url="member_welcome.cfm" addtoken="No">
<cfelse ListFind(Variables.groupID, "2") >
<cflocation url="admin_welcome.cfm" addtoken="No">
<cfelseif ListFind(Variables.groupID, "3") >
<cflocation url="premium_welcome.cfm" addtoken="No">
</cfif>--->
<!---<cfinvoke
component="security"
method="authorize"
returnVariable="authorize"
cfcUsername="#form.username#">--->
<cfelse>
<b>Username and/or Password was incorrect!</b>
</cfif>
</cfif>
Inspiring
May 8, 2006
quote:

Originally posted by: Hydrowizard
Hi Dan the cfdump gives me:
AUTHENTICATED aking
GROUPID 1

<!---<cfinvoke
component="security"
method="authorize"
returnVariable="authorize"
cfcUsername="#form.username#">--->
<cfelse>
<b>Username and/or Password was incorrect!</b>


In your authorize function, change this:
<cfreturn getUserGroups.groupID />
to this
<cfreturn ValueList(getUserGroups.groupID) />

Inspiring
May 7, 2006
I have had a look at the online livedocs, I get this error Invalid CFML construct found on line 81 at column 9.
ColdFusion was looking at the following text: ListFind.

The syntax should be like this:
ListFind(list, value [, delimiters ])
What is my list name?
Thanks



<cfif isDefined("variables.authenticated")>
<cfif variables.authenticated NEQ 0>
<b>Username and Password Authenticated Successfully!
<cfoutput>#variables.grpID#</cfoutput>

<cfif ListFind(Variables.grpID) EQ 1 >
<cflocation url="member_welcome.cfm" addtoken="No">
<cfelse ListFind(Variables.grpID) EQ 2 >
<cflocation url="admin_welcome.cfm" addtoken="No">
<cfelseif ListFind(Variables.grpID) EQ 3 >
<cflocation url="premium_welcome.cfm" addtoken="No">
</cfif>
<cfinvoke
component="security"
method="authorize"
returnVariable="authorize"
cfcUsername="#form.username#">
<cfelse>
<b>Username and/or Password was incorrect!</b>
</cfif>
</cfif>
Inspiring
May 8, 2006
quote:

Originally posted by: Hydrowizard
I have had a look at the online livedocs, I get this error Invalid CFML construct found on line 81 at column 9.
ColdFusion was looking at the following text: ListFind.

The syntax should be like this:
ListFind(list, value [, delimiters ])
What is my list name?
Thanks

<cfif isDefined("variables.authenticated")>
<cfif variables.authenticated NEQ 0>
<b>Username and Password Authenticated Successfully!
<cfoutput>#variables.grpID#</cfoutput>

<cfif ListFind(Variables.grpID) EQ 1 >



With any luck, your list is Variables.grpID. That being the case, where are you supposed to put your value?
This is what you read:
ListFind(list, value [, delimiters ])
This is what you wrote:
<cfif ListFind(Variables.grpID) EQ 1 >



Inspiring
May 8, 2006
Right I´m still getting an error Invalid CFML construct found looking at the following text:
ListFind with this <cfif ListFind(Variables.grpID,1)>
The list is: variables.grpID
The value is: 1
What are the delimiters?
Thanks

<cfif ListFind(Variables.grpID,1)>
<cflocation url="member_welcome.cfm" addtoken="No">
<cfelse ListFind(Variables.grpID,2) >
<cflocation url="admin_welcome.cfm" addtoken="No">
<cfelseif ListFind(Variables.grpID, 3) >
<cflocation url="premium_welcome.cfm" addtoken="No">
</cfif>
Inspiring
May 7, 2006
So the code would be something like this?:

<cfif groupID=1><cflocation url = "member_welcome.cfm" addToken = "Yes" or "No">
<cfelse groupID=2><cflocation url = "admin_welcome.cfm" addToken = "Yes" or "No">
</cfif>
Inspiring
May 7, 2006
Before you start to code, you have to know what you want to do about people who have more than one access level. If you can describe that in plain English, you might be able to figure out the code yourself. If not, post the rule and we can tell you how to code it.

By the way, coding inside a cfc is a bit different than coding outside one, but the similarities outwheigh the differences.
Inspiring
May 7, 2006
Thanks for the reply Dan. well in plain english it would be as set out below.

if groupID= 1 send to member_welcome.cfm
if groupID= 2 send to admin_welcome.cfm
if groupID= 3 send to premium_welcome.cfm
if groupID= 3 and 1 send to premium_welcome.cfm
if groupID= 3 and 2 send to admin_welcome.cfm
if groupID= 2 and 1 send to admin_welcome.cfm
if groupID= 2 and 3 send to admin_welcome.cfm
if groupID= 1 and 2 send to admin_welcome.cfm

[Levels: users (groupID 1)
administrators (groupID 2)
premium users (groupID 3)]

What is the best way to do this setting the groupID in a session and putting cflocation in the existing authroize cfc? Should I set a cfcookie to store the information? Thanks a lot
Inspiring
May 7, 2006
quote:

Originally posted by: Hydrowizard
Thanks for the reply Dan. well in plain english it would be as set out below.

if groupID= 1 send to member_welcome.cfm
if groupID= 2 send to admin_welcome.cfm
if groupID= 3 send to premium_welcome.cfm
if groupID= 3 and 1 send to premium_welcome.cfm
if groupID= 3 and 2 send to admin_welcome.cfm
if groupID= 2 and 1 send to admin_welcome.cfm
if groupID= 2 and 3 send to admin_welcome.cfm
if groupID= 1 and 2 send to admin_welcome.cfm

[Levels: users (groupID 1)
administrators (groupID 2)
premium users (groupID 3)]

What is the best way to do this setting the groupID in a session and putting cflocation in the existing authroize cfc? Should I set a cfcookie to store the information? Thanks a lot


var GoTo = "member_welcome.cfm";
if (ListFind(session.GroupId, "2")
GoTo = "admin_welcome.cfm;
else if (ListFind(session.GroupId, "3")
GoTo = "premium_welcome.cfm;