Skip to main content
Known Participant
February 11, 2009
Question

UserRoles and CF Script Problem

Hi,

I am trying to create a way for only "administrators" to download an application. I'm not getting any errors but I'm not getting the result I expected. I want it to see that if you're logged in as "admin" you get a link that says "download". If not, you get some other text. This is what I have:



<!-- COLDFUSION login_action.cfm --->

<cfif Len(trim(Username)) IS 0>
<font face="Arial, Helvetica, sans-serif" size="-1" color="#990000" >
Error: Username is required.
<cfabort></cfif>

<cfif Len(trim(userpassword)) is 0>
<font face="Arial, Helvetica, sans-serif" size="-1" color="#990000">Error: Password is required.
<cfabort>
</cfif>


<cfquery datasource="myDS" name="qryLogin">
SELECT username, userpassword, userrole
FROM userinfo
WHERE username = '#form.username#' and userpassword= '#form.userpassword#'
</cfquery>

<cfif qryLogin.recordCount gt 0>
<font face="Arial, Helvetica, sans-serif" size="-1">Login successful. Welcome <cfoutput>
#username#. </cfoutput>

<cfset SESSION.isLoggedIn ="yes">
<cflogin>
<cfif IsDefined ( "cflogin" ) >
<cfif cflogin.username eq "#qryLogin.username#" and "#qryLogin.userrole#" eq "0">
<cfset roles="administrator">
<cfelse>
<cfset roles="user">
</cfif>


<cfloginuser name = "#cflogin.username#"
password="#cflogin.userpassword#"
roles="#roles#" >
</cfif></cflogin>

<cfscript>
if (IsUserInRole("0"))
WriteOutput("you're admin!<br>");
if (IsUserInRole("1"))
WriteOutput("foo");

</cfscript>

<cfelse>

<font face="Arial, Helvetica, sans-serif" size="-1" color="#990000">That record does not exist. Please try again.
<cfinclude template="login_form.cfm">
<cfabort></cfif>

<cfif not isDefined ("SESSION.isLoggedIn")>
Please log in.<cfabort></cfif>



If anyone can give me some illumination on why it's not seeing the userroles, I would be very appreciative. Also, there are nine user roles and one one can download the application. Would it make more sense in the CFScript to say

If 0, you're admin and get download link
if else, you get plain text.

?

Thanks for your help!

    This topic is closed to new replies. Start a new post to keep the conversation going.

    5 replies

    Inspiring
    February 12, 2009
    is it the number 0 or the string "0"?
    Translate
    Known Participant
    February 11, 2009
    It's not working. It just spits out the same user role and name no matter what I do. Is the "eq 0" okay?
    Translate
    Inspiring
    February 11, 2009
    I generally troubleshoot if/else logic like this.

    <cfif something>
    true
    <cfelse>
    false
    <cfdump something>
    </cfif>
    Translate
    Known Participant
    February 11, 2009
    Yes, that is what it is supposed to mean.
    Translate
    Inspiring
    February 11, 2009
    Does this mean the person is an administrator?
    "#qryLogin.userrole#" eq "0"
    Translate