PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/sfDoctrineGuardPlugin/lib/validator/sfGuardValidatorUser.class.php

https://bitbucket.org/Kudlaty/360kdw
PHP | 69 lines | 43 code | 9 blank | 17 comment | 7 complexity | 53af4ec674e47854c1de2f79ed2af69b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. *
  11. * @package symfony
  12. * @subpackage plugin
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. * @version SVN: $Id: sfGuardValidatorUser.class.php 25546 2009-12-17 23:27:55Z Jonathan.Wage $
  15. */
  16. class sfGuardValidatorUser extends sfValidatorBase
  17. {
  18. public function configure($options = array(), $messages = array())
  19. {
  20. $this->addOption('username_field', 'username');
  21. $this->addOption('password_field', 'password');
  22. $this->addOption('throw_global_error', false);
  23. $this->setMessage('invalid', 'The username and/or password is invalid.');
  24. }
  25. protected function doClean($values)
  26. {
  27. $username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : '';
  28. $password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';
  29. $allowEmail = sfConfig::get('app_sf_guard_plugin_allow_login_with_email', true);
  30. $method = $allowEmail ? 'retrieveByUsernameOrEmailAddress' : 'retrieveByUsername';
  31. // don't allow to sign in with an empty username
  32. if ($username)
  33. {
  34. if ($callable = sfConfig::get('app_sf_guard_plugin_retrieve_by_username_callable'))
  35. {
  36. $user = call_user_func_array($callable, array($username));
  37. } else {
  38. $user = $this->getTable()->retrieveByUsername($username);
  39. }
  40. // user exists?
  41. if($user)
  42. {
  43. // password is ok?
  44. if ($user->getIsActive() && $user->checkPassword($password))
  45. {
  46. return array_merge($values, array('user' => $user));
  47. }
  48. }
  49. }
  50. if ($this->getOption('throw_global_error'))
  51. {
  52. throw new sfValidatorError($this, 'invalid');
  53. }
  54. throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));
  55. }
  56. protected function getTable()
  57. {
  58. return Doctrine::getTable('sfGuardUser');
  59. }
  60. }