2012-01-20 66 views
0

我正在尝试在cakephp 2.0中创建更改密码表单。我发现了EuroMark为1.3创建的行为,现在我很难将这段代码转换为2.0。我知道它与Auth Component有关,因为在2.0中这个组件发生了重大变化。Cakephp 2.0更改密码

public function validateCurrentPwd(Model $Model, $data) { 
if (is_array($data)) { 
    $pwd = array_shift($data); 
} else { 
    $pwd = $data; 
} 

$uid = null; 
if ($Model->id) { 
    $uid = $Model->id; 
} elseif (!empty($Model->data[$Model->alias]['id'])) { 
    $uid = $Model->data[$Model->alias]['id']; 
} else { 
    return false; 
} 

if (class_exists('AuthExtComponent')) { 
    $this->Auth = new AuthExtComponent(); 
} elseif (class_exists($this->settings[$Model->alias]['auth'].'Component')) { 
    $auth = $this->settings[$Model->alias]['auth'].'Component'; 
    $this->Auth = new $auth(); 
} else { 
    return true; 
} 
return $this->Auth->verifyUser($uid, $pwd); 
} 

我在读取$ this-> Auth = new $ auth();的行时出错。 错误如下:

Argument 1 passed to Component::__construct() must be an instance of ComponentCollection, none given, called in C:\UniServer\www\new_company_test\app\Model\Behavior\change_password.php on line 117 and defined [CORE\Cake\Controller\Component.php, line 77] 

Undefined variable: collection [CORE\Cake\Controller\Component.php, line 78] 

它也引发此

Call to undefined method AuthComponent::verifyUser() in C:\UniServer\www\new_company_test\app\Model\Behavior\change_password.php on line 121 

我不知道是否有其他任何需要在脚本中予以解决,我猜测没有其他地方使用Auth。

关于我需要做什么才能使其发挥作用的任何建议?任何帮助表示赞赏。 谢谢

+0

我目前的工作是对“当前密码”功能进行干净的2.0重写(必须是1.3的相当黑客)。将在我完成后立即发布。 – mark

回答

1

你确实发现还有一个2.0分支,不是吗? :) 它应该包含相同的行为: https://github.com/dereuromark/tools/tree/2.0

无论哪种方式,你需要一个组件集合传递到它:

$this->Auth = new AuthExtComponent(new ComponentCollection()); 

你应该在你的自定义组件AuthExt延伸验证创建的方法verifyUser组件的“当前密码”的工作,像这样:

/** 
* Quickfix 
* TODO: improve - maybe use Authenticate 
* @return bool $success 
*/ 
public function verifyUser($id, $pwd) { 
    $options = array(
     'conditions' => array('id'=>$id, 'password'=>$this->password($pwd)), 
    ); 
    return $this->getModel()->find('first', $options); 

    $this->constructAuthenticate(); 
    $this->request->data['User']['password'] = $pwd; 
    return $this->identify($this->request, $this->response); 
} 

/** 
* returns the current User model 
* @return object $User 
*/ 
public function getModel() { 
    return ClassRegistry::init(CLASS_USER); 
} 

也许还可以使用现有的identify方法结合无线在行为中直接伪造一个请求对象? 我正在考虑使用

$this->authenticate = array('Form'=>array('fields'=>array('username' => 'id'))); 

随时到餐桌的行为,并提交pull请求。

“当前密码”是目前尚未完全解决的唯一问题。

+0

谢谢euromark这有帮助。我仍然从上面获取verifyUser()的错误。当我查看AuthComponent时,我没有看到这个函数。是否需要在某处添加?我明白了它应该做什么,但不知道这个函数在哪里执行它。 –

+0

你是对的。它缺少(我添加它为我的自定义AuthComponent)。我不确定如何在不破坏太多MVC的情况下继续进行 - 因为Auth是一个通常不应用于行为上下文的组件。所以,如果你想出一个更好的主意,让我知道。 – mark

+0

差不多有我的想法。我收到错误: –