Cflogin

Discussion in 'Programming' started by forumposters, Apr 18, 2007.

  1. #1
    I'd like to use cflogin not to restrict access not only to an entire folder or page, but to also show edit links on pages that the end user also sees. These links would be hidden unless an admin or moderator has logged in.
    Can anyone please point me in the right direction?
     
    forumposters, Apr 18, 2007 IP
  2. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you log them in, use cfloginuser to set their role, and then on the page you wish to show the edit links, do something like this:

    <cfif IsUserInRole("Admin") >
    {{edit link}}
    <cfelse >
    {{show normal/public content}}
    </cfif>
     
    IsRoss(), Apr 18, 2007 IP
  3. forumposters

    forumposters Peon

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For some reason, I'm not able to use the function IsUserInRole with any success. It returns NO, even though I'm able to output the following two variables which show me that the cflogin worked.
    <cfset contactID = listFirst(getAuthUser())>
    <cfset contactName = listRest(getAuthUser())>

    Here's the page where my login form is submitted too:


    <cflogin>

    <!--- If the user hasn't gotten the login form yet, display it --->
    <cfif not (isDefined("FORM.userLogin") and isDefined("FORM.userPassword"))>
    <cfinclude template="UserLoginForm.cfm">
    <cfabort>

    <cfelse>

    <cfquery name="getUser" datasource="#APPLICATION.dataSource#">
    SELECT ContactID, FirstName, UserRoleName
    FROM Contacts LEFT OUTER JOIN UserRoles
    ON Contacts.UserRoleID = UserRoles.UserRoleID
    WHERE UserLogin = '#FORM.UserLogin#'
    AND UserPassword = '#FORM.UserPassword#'
    </cfquery>

    <cfif getUser.recordCount eq 1>
    <cfloginuser
    name="#getUser.ContactID#,#getUser.FirstName#"
    password="#FORM.userPassword#"
    roles="#getUser.userRoleName#">

    <cfelse>
    <cfinclude template="UserLoginForm.cfm">
    <cfabort>
    </cfif>


    </cfif>

    </cflogin>
     
    forumposters, Apr 18, 2007 IP
  4. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You need to wrap your CFLOGINUSER code with CFLOGIN.

    That is.
    <CFLOGIN>

    <cfloginuser
    name="#getUser.ContactID#,#getUser.FirstName#"
    password="#FORM.userPassword#"
    roles="#getUser.userRoleName#">

    </CFLOGIN>
     
    IsRoss(), Apr 19, 2007 IP
  5. forumposters

    forumposters Peon

    Messages:
    270
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Isn't it wrapped already? Please look at my code more closely. Sorry that it is formatted a little sloppy. Here it is formatted better:

    
    <cflogin>
    
     <cfif not (isDefined("FORM.userLogin") and isDefined("FORM.userPassword"))>
       <cfinclude template="UserLoginForm.cfm">
       <cfabort>
     <cfelse>
       <cfquery name="getUser" datasource="#APPLICATION.dataSource#">
         SELECT ContactID, FirstName, UserRoleName
         FROM Contacts LEFT OUTER JOIN UserRoles
         ON Contacts.UserRoleID = UserRoles.UserRoleID
         WHERE UserLogin = '#FORM.UserLogin#'
         AND UserPassword = '#FORM.UserPassword#'
       </cfquery>
       <cfif getUser.recordCount eq 1>
         <cfloginuser
         name="#getUser.ContactID#,#getUser.FirstName#"
         password="#FORM.userPassword#"
         roles="#getUser.userRoleName#">
       <cfelse>
         <cfinclude template="UserLoginForm.cfm">
         <cfabort>
       </cfif>
      </cfif>
    
    </cflogin>
    Code (markup):
     
    forumposters, Apr 19, 2007 IP
  6. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So it is...sorry, I missed that...

    Not that I think it should make a difference, but perhaps just having the cfloginuser tag within the cflogin containter?

    Also note that cflogin will only execute the body of the command if there is **no logged in user**. If, during your testing, you logged in, and then re-ran this code without logging out, no changes to the login data would be made.
     
    IsRoss(), Apr 19, 2007 IP
  7. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #7
    IsRoss is correct, CFLOGIN only executes if no one is logged in. If someone is, then that block of code does not get executed.

    What IsRoss was saying is that in your cfloginuser tag, state what roles the user is in. At this juncture consider these suggestions:

    1) Trim the roles, they are case-sensitive and the spaces matter
    2) Output the role that is being assigned to the user and then test for that exact value.
     
    datropics, Apr 20, 2007 IP