2010-10-01 33 views
0

我正在嘗試構建自己的用戶認證系統(僅僅因爲那裏的問題太複雜而且很大)。PHP Symfony - 表單處理問題

雖然我無法掌握Symfony表單處理。 我在看sfDoctrineGuardPlugin,但在我的生活中,無法弄清楚,輸入的密碼在保存到db之前轉換爲SHA1哈希值。

我在哪裏可以閱讀關於教義可能在兩者之間做什麼的表單處理和自動生成的東西?我一直在看「Symfony的一個溫柔的介紹」,但它並沒有真正的幫助。

我發現,它發生在updateObject()方法的某處。

if ($request->isMethod('post')) 
    { 
     $this->form->bind($request->getParameter($this->form->getName())); 
     if ($this->form->isValid()) 
     { 
     var_dump($this->form->getObject()->password); 
     $this->form->updateObject(); 
     var_dump($this->form->getObject()->password); 
     } 
    } 
// Prints: 
// null 
// string '989d88b585ce29839687f2938303e828e191ecef' (length=40) 

但是我很難找到該方法的實現,以及它究竟調用/做了什麼。

任何人都可以點亮一下嗎?我只想了解Symfony在後臺做什麼。我認爲有太多的魔術正在進行,有時文檔缺乏。

回答

1

http://trac.symfony-project.org/browser/plugins/sfDoctrineGuardPlugin/branches/1.3/lib/model/doctrine/PluginsfGuardUser.class.php#L33

public function setPassword($password) 
    { 
    if (!$password && 0 == strlen($password)) 
    { 
     return; 
    } 

    if (!$salt = $this->getSalt()) 
    { 
     $salt = md5(rand(100000, 999999).$this->getUsername()); 
     $this->setSalt($salt); 
    } 
    $modified = $this->getModified(); 
    if ((!$algorithm = $this->getAlgorithm()) || (isset($modified['algorithm']) && $modified['algorithm'] == $this->getTable()->getDefaultValueOf('algorithm'))) 
    { 
     $algorithm = sfConfig::get('app_sf_guard_plugin_algorithm_callable', 'sha1'); 
    } 
    $algorithmAsStr = is_array($algorithm) ? $algorithm[0].'::'.$algorithm[1] : $algorithm; 
    if (!is_callable($algorithm)) 
    { 
     throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithmAsStr)); 
    } 
    $this->setAlgorithm($algorithmAsStr); 

    parent::_set('password', call_user_func_array($algorithm, array($salt.$password))); 
    } 
+0

男人,怎麼是我這輩子要找到它? ;)所以當您調用save()時,Doctrine實際上爲每個字段調用setValue?我在哪裏可以看到代碼中? (只是想在automagic背後)。謝謝您的幫助。 – 2010-10-01 11:36:53

+1

從來沒有挖掘到教條的代碼,以便能夠回答這個問題,但我已經足夠深入到這個插件的代碼來知道你原來的問題的答案。 – Maerlyn 2010-10-01 12:34:19

+0

謝謝。我想這樣就可以用它,而不是問問題;) – 2010-10-01 15:39:51