Krisztian Ferenczi
unread,Oct 15, 2009, 6:25:30 AM10/15/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to symfo...@googlegroups.com
Van egy securityUser osztályom. Ez végzi a felhasználó kezelést. Kellett csinálnom egy formot, ahol a felhasználó meg tudja változtatni a jelszavát. Ehhez a következő mezők vannak:
- current_password
- new_password
- password_repeat
A "problémát" az okozta, hogy vhogy jó lenne ellenőrizni, hogy az isValid() fv meghívásánál az aktuális jelszó stimmel-e. Mivel nem csak egy mezei md5 hashként van tárolva, ez az ellenőrzés megkívánja a $securityUserObj->checkPassword($password) fv meghívását.
Kódban most ezt a következő képen oldottam meg:
action-ben:
$this->password_form = new ChangePasswordForm();
// Itt külön átadom neki a $securityUserObj objektumot.
$this->password_form->setUser($this->getUser());
[...]
if($this->password_form->isValid())
{
[módosítások végrehajtása]
}
A form maga:
class ChangePasswordForm extends BaseFormDoctrine
{
protected $user = null;
public function setUser(sfUser $user)
{
$this->user = $user;
}
public function setup()
{
$this->setWidgets(array(
'current_password' => new sfWidgetFormInputPassword(),
'new_password' => new sfWidgetFormInputPassword(),
'password_repeat' => new sfWidgetFormInputPassword(),
));
$this->setValidators(array(
'current_password' => new sfValidatorCallback(array('callback'=>array($this, 'checkPassword')), array(
'invalid' => 'Az aktuális jelszó nem egyezik meg!')),
'new_password' => new sfValidatorString(array('min_length' => 4, 'max_length' => 20, 'required' => true), array(
'required' => 'A jelszó megadása kötelező!',
'min_length' => 'Minimum 4 karakteres jelszót várunk!',
'max_length' => 'Maximum 20 karakteres lehet a jelszó!')),
'password_repeat' => new sfValidatorPass(),
));
// Itt ellenőrizzük, hogy a kétszer megadott jelszó egyezik-e.
$this->validatorSchema->setPostValidator(
new sfValidatorSchemaCompare('new_password', sfValidatorSchemaCompare::EQUAL, 'password_repeat', array(), array('invalid'=>'A két jelszó nem egyezik!'))
);
$this->widgetSchema->setNameFormat('password[%s]');
$this->widgetSchema->setFormFormatterName('list');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
parent::setup();
}
public function getModelName()
{
return 'User';
}
public function checkPassword($validator, $value, $args)
{
if(!$this->user->checkPassword($value))
{
$error = new sfValidatorError($validator,
'invalid');
// throw an error schema so the error appears at the field "password"
throw new sfValidatorErrorSchema($validator, array($error));
}
}
}
Amint látható, egy sfValidatorCallback() ellenőrzéssel nézem meg, hogy a jelszó egyezik-e. Ez így működik, a kérdés annyi, hogy lát-e vki egyszerűbb megoldást, amire én nem jöttem rá? Van-e másik Validator, amivel ugyanezt elérem, csak egyszerűbben esetleg, csak nekem elkerülte a figyelmemet?
Chris