仅当strlen()>时才设置SetPassword 0 - Symfony 1.4

时间:2012-05-11 13:00:37

标签: php security symfony1 symfony-1.4

我有简单的分类:

User:
  id | login | password | desc

并与此形成。

如何输入 - 如果输入密码为空(strlen == 0),那么在模型中无关紧要。

现在我在函数save中有User.class.php:

$this->setPassword(sha512($this->getPassword));

1 个答案:

答案 0 :(得分:1)

您必须确保在数据库级别上没有“NOT NULL”,因此ORM通常会忽略空值。

通过自定义函数更改给定密码有两种可能性,因此我将为您提供一些简单的示例。

1)在您的模型文件中(我猜你有学说或推进?!):

 /**
 * via object's event handler
 */
 preSave(){
    if(strlen($this->getPassword()) > 0){
        $this->setPassword(sha512($this->getPassword()));
    }
 }

2)或甚至作为表单验证器:

/**
 * custom validator
 */
class myValidatorOldPassword extends sfValidatorBase{

    /**
     * Clean and validate user input
     *
     * @param mixed $value Value of form input
     * @return mixed Value 
     */
     protected function doClean($value)
     {
        // trim is not needed
        $clean = (string) $value;

        // password is ok?
        if (strlen($clean) > 0)
        {
            return sha512($clean);
        }

        // Throw error - if you want
        // throw new sfValidatorError($this, 'invalid', array('value' => $value)); 

        // or return empty value
        return $clean;
    }

}

当然这段代码可能会有所改进,因为它只是暗示你的。