| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class sfGuardValidatorUser extends sfValidatorBase |
|---|
| 19 |
{ |
|---|
| 20 |
public function configure($options = array(), $messages = array()) |
|---|
| 21 |
{ |
|---|
| 22 |
$this->addOption('username_field', 'username'); |
|---|
| 23 |
$this->addOption('password_field', 'password'); |
|---|
| 24 |
$this->addOption('throw_global_error', false); |
|---|
| 25 |
|
|---|
| 26 |
$this->setMessage('invalid', 'The username and/or password is invalid.'); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
protected function doClean($values) |
|---|
| 30 |
{ |
|---|
| 31 |
$username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : ''; |
|---|
| 32 |
$password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : ''; |
|---|
| 33 |
|
|---|
| 34 |
$allowEmail = sfConfig::get('app_sf_guard_plugin_allow_login_with_email', true); |
|---|
| 35 |
$method = $allowEmail ? 'retrieveByUsernameOrEmailAddress' : 'retrieveByUsername'; |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
if ($username) |
|---|
| 39 |
{ |
|---|
| 40 |
if ($callable = sfConfig::get('app_sf_guard_plugin_retrieve_by_username_callable')) |
|---|
| 41 |
{ |
|---|
| 42 |
$user = call_user_func_array($callable, array($username)); |
|---|
| 43 |
} else { |
|---|
| 44 |
$user = $this->getTable()->$method($username); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
if($user) |
|---|
| 48 |
{ |
|---|
| 49 |
|
|---|
| 50 |
if ($user->getIsActive() && $user->checkPassword($password)) |
|---|
| 51 |
{ |
|---|
| 52 |
return array_merge($values, array('user' => $user)); |
|---|
| 53 |
} |
|---|
| 54 |
} |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
if ($this->getOption('throw_global_error')) |
|---|
| 58 |
{ |
|---|
| 59 |
throw new sfValidatorError($this, 'invalid'); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid'))); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
protected function getTable() |
|---|
| 66 |
{ |
|---|
| 67 |
return Doctrine_Core::getTable('sfGuardUser'); |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|