Login +

How To ? / Login a user

Suppose the Model of the table user generated:

Controller:

public function action()
{
    // Get sls XML
    $xml = $this->getXML();
    
    // Object
    $this->_generic->useModel("User");
    $user = new Main_User();
    $errors = array();
    
    // If form reloading
    if ($this->_http->getParam("reload_register") === 'true')
    {
        // User POST params
        $userInfos = $this->_http->getParam("user");
        
        // Search user
        $result = array_shift($user->searchModels("user", array(), array(0 => array("column" => "user_email", "value" => userInfos["user_email"], "mode" => "equal"), 1 => array("column" => "user_password", "value" => sha1($userInfos["user_password"]), "mode" => "equal"))));
        
        if (!empty($result))
        {
            // Ok login
        }
        else
        {
            $xml->addFullTag("error", "Unknown credentials.", true);
        }
    }
    
    // Save sls XML updated
    $this->saveXML($xml);
}

View:

<fieldset class="login">
    <legend>Login</legend>
    <xsl:if test="//View/error != ''">
        <div class="error">
            <xsl:value-of select="//View/error" />
        </div>
    </xsl:if>
    <form method="post" action="">
        <input type="hidden" name="reload_user" value="true" />
        <div class="row">
            <label for="user_email">
                Email address
            </label>
            <input type="text" name="user[user_email]" id="user_email" />
        </div>
        <div class="row">
            <label for="user_password">
                Passord
            </label>
            <input type="password" name="user[user_password]" id="user_password" />
        </div>
        <div class="row">
            <input type="submit" id="login_submit" value="Connection" />
        </div>
    </form>
</fieldset>