使用结构将变量添加到登录信息中?

时间:2012-01-27 10:04:36

标签: login coldfusion passwords

我正在尝试使用结构来安排三组登录名和密码。理想情况下,我希望form.password和form.username通过userThree检查结构userOne的变量,而不是只检查密码和用户名的一个实例。

为了实现这个目标,有人能指出我需要知道/看到的方向吗?

提前谢谢。

<cfset userOne = structNew()>
<cfset userOne.name = "cat">
<cfset userOne.password = "meow">
<cfset userOne.group = "1">

<cfset userTwo = structNew()>
<cfset userTwo.name = "bird">
<cfset userTwo.password = "chirp">
<cfset userTwo.group = "2">

<cfset userThree = structNew()>
<cfset userThree.name = "mouse">
<cfset userThree.password = "squeak">
<cfset userThree.group = "3">

 <cfset this_password = "squeak">
   <cfif form.password neq "squeak" or form.username neq "mouse">
    <cflocation url="login.cfm?status=le">
         <cfelse>
       <cflock scope="session" timeout="10">
         <cfset session.valid_user = "yes">
       </cflock>
    <cflocation url="index.cfm" addtoken="no">
 </cfif>

3 个答案:

答案 0 :(得分:0)

<cfset userOne = structNew()>
<cfset userOne.name = "cat">
<cfset userOne.password = "meow">
<cfset userOne.group = "1">

<cfset userTwo = structNew()>
<cfset userTwo.name = "bird">
<cfset userTwo.password = "chirp">
<cfset userTwo.group = "2">

<cfset userThree = structNew()>
<cfset userThree.name = "mouse">
<cfset userThree.password = "squeak">
<cfset userThree.group = "3">

 <cfset this_password = "squeak">
   <cfif NOT (authUser(userOne, form.username, form.password)
        OR authUser(userTwo, form.username, form.password)
        OR authUser(userThree, form.username, form.password))>
    <cflocation url="login.cfm?status=le">
         <cfelse>
       <cflock scope="session" timeout="10">
         <cfset session.valid_user = "yes">
       </cflock>
    <cflocation url="index.cfm" addtoken="no">
 </cfif>


<cffunction name="authUser" access="public" output="false" returntype="boolean">
    <cfargument name="user"        type="struct" required="true">
    <cfargument name="username" type="string" required="true">
    <cfargument name="password" type="string" required="true">
    <cfreturn (arguments.user.name EQ arguments.username) AND (arguments.user.password EQ arguments.password)>
</cffunction>

这会有效,但有点乱。我希望这只是你问题的一个例子,而不是它是如何运作的。

答案 1 :(得分:0)

如果您可以将3个用户结构嵌套在按名称键入的用户结构中,则可以执行structKeyExists()查找匹配的用户,然后只需检查密码即可。堆叠比在多个结构上循环更快,一个接一个。

例如

<cfscript>
users = {
    cat = { name: "cat", password: "meow", group: 1 },
    bird = { name: "bird", password: "chirp", group: 2 },
    mouse = { name: "mouse", password: "squeak", group: 3 }
};

if (
    structKeyExists( form, 'username' )
    && structKeyExists( form, 'password' )
    && len( form.username & form.password ) > 0
    && structKeyExists( users, form.username )
    && structKeyExists( users[form.username], 'password' )
    && users[form.username].password == form.password
) {
    // sign them in
}
else {
    // throw error "invalid username or password"
}
</cfscript>

答案 2 :(得分:0)

以下是处理此问题的另一种方法:

<cfscript>
users = [{
    username = "cat",
    password = "meow"
},{
    username = "bird",
    password = "chirp"
},{
    username = "mouse",
    password = "squeak"
}];

for (user in users) {
    if (user.username == form.username && user.password == form.password) {
        // handle success log in
        // redirect
    }
}

// handle invalid log in
// redirect
</cfscript>