Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][sfDoctrineGuard]Logowanie a nieaktywne konto., Jak dodać error o nieaktywnym koncie.
cyklotrial
post 8.10.2010, 17:16:17
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 1
Dołączył: 5.06.2009

Ostrzeżenie: (0%)
-----


Witam!

Przy użyciu pluginu sfDoctrineGuard utworzyłem sobie system użytkowników (rejestracja, logowanie itp).
Niby wszystko działa jak należy ale jedna rzecz cały czas nie daje mi spokoju... Dokładniej rzecz ujmując "nie podobają" mi się komunikaty o błędach podczas logowania.
Dodałem sobie do /lib/form/doctrine plik sfGuardFormSignin.class.php, tam ustawiłem sobie polskie komunikaty o błędach i wszystko byłoby OK gdyby nie brak komunikatu o tym że konto jest nieaktywne.

Podejrzałem sobie co zawiera metoda sfGuardValidatorUser znajdująca się w pliku sfGuardValidatorUser.class.php i ku mojemu zdziwieniu, sprawdzanie czy konto jest aktywne czy też nie, wykonywane jest wraz ze sprawdzaniem poprawności hasła. Skutkuje to tym, że pomimo iż dane podane przez użytkownika podczas logowania są poprawne a jego konto jest nieaktywne, zwracany jest ogólny błąd o niepoprawności loginu/hasła.

W jaki sposób należałoby poprawnie zwrócić błąd o nieaktywnym koncie?
Domyślam się że należałoby nadpisać metodę doClean z pliku sfGuardValidatorUser.class.php, oraz że ingerencja w pliki pluginu to niezbyt dobry pomysł... Gdzie więc należałoby umieścić plik ze zmodyfikowaną metodą doClean
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
destroyerr
post 8.10.2010, 17:53:33
Post #2





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Zazwyczaj staram się odtworzyć strukturę frameworka czy pluginu w swoim projekcie. W związku z czym umieściłbym to w lib/validator/GuardValidatorUser.class.php.
Go to the top of the page
+Quote Post
cyklotrial
post 8.10.2010, 19:01:14
Post #3





Grupa: Zarejestrowani
Postów: 11
Pomógł: 1
Dołączył: 5.06.2009

Ostrzeżenie: (0%)
-----


destroyerr - Dziękuje - ta ścieżka faktycznie jest logiczna.

Ok no to jeszcze jedno pytanko mam do tego tematu smile.gif
W jaki sposób poprawnie zwrócić taki error/wyjątek?

zrobiłem coś takiego:
nadpisałem metode configure:
  1. public function configure($options = array(), $messages = array())
  2. {
  3. parent::configure();
  4. $this->setMessage('inactive', 'To konto jest nieaktywne.');
  5. }


no i w odpowiednim miejscu dodałem sprawdzanie czy konto jest aktywne:
  1. if($user->getIsActive())
  2. {
  3. array_merge($values, array('user' => $user));
  4. }
  5. else
  6. {
  7. throw new sfValidatorError($this, 'inactive');
  8. }



Ale otrzymałem taki oto błąd:
Kod
sfGuardValidatorUser does not support the following error code: 'inactive'.


Co oznacza ten błąd - WIEM, ale niestety moja wiedza na temat SYMFONY jest niestety nadal zbyt mała, abym wiedział jak sprawić, by moj "kod błędu" został przyjęty.

Wszelkie pomysły/sugestie tu bardzo mile widziane.
Go to the top of the page
+Quote Post
destroyerr
post 9.10.2010, 07:20:00
Post #4





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

Ostrzeżenie: (0%)
-----


http://www.symfony-project.org/api/1_4/sfV...thod_addmessage
Go to the top of the page
+Quote Post
cyklotrial
post 9.10.2010, 13:37:24
Post #5





Grupa: Zarejestrowani
Postów: 11
Pomógł: 1
Dołączył: 5.06.2009

Ostrzeżenie: (0%)
-----


destroyerr raz jeszcze dziękuje za pomoc.
Nie wiem jakim cudem ta metoda umknęła mojej uwadze - dwa razy czytałem opis sfValidatorBase withstupidsmiley.gif sciana.gif


Niestety, nadal coś nie działa tak jak powinno sadsmiley02.gif
Ciągle otrzymuje tylko komunikat o nieprawidłowym loginie lub haśle.
Kod metody doClean wygląda tak:
  1. protected function doClean($values)
  2. {
  3. $username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : '';
  4. $password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';
  5.  
  6. $allowEmail = sfConfig::get('app_sf_guard_plugin_allow_login_with_email', true);
  7. $method = $allowEmail ? 'retrieveByUsernameOrEmailAddress' : 'retrieveByUsername';
  8.  
  9. // don't allow to sign in with an empty username
  10. if ($username)
  11. {
  12. if ($callable = sfConfig::get('app_sf_guard_plugin_retrieve_by_username_callable'))
  13. {
  14. $user = call_user_func_array($callable, array($username));
  15. } else {
  16. $user = $this->getTable()->retrieveByUsername($username);
  17. }
  18. // user exists?
  19. if($user)
  20. {
  21. // password is ok?
  22. if ($user->checkPassword($password))
  23. {
  24. //account is active?
  25. if($user->getIsActive())
  26. {
  27. return array_merge($values, array('user' => $user));
  28. }
  29. else
  30. {
  31. throw new sfValidatorError($this, 'inactive');
  32. }
  33. }
  34. }
  35. }
  36.  
  37. if ($this->getOption('throw_global_error'))
  38. {
  39. throw new sfValidatorError($this, 'invalid');
  40. }
  41.  
  42. throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));
  43. }


Domyślam się jedynie, że linia 42 wysyła ogólny error z kodem "invalid"... Jak sprawić żeby błąd o nieaktywnym koncie został wyświetlony?
Go to the top of the page
+Quote Post
destroyerr
post 9.10.2010, 19:25:49
Post #6





Grupa: Zarejestrowani
Postów: 879
Pomógł: 189
Dołączył: 14.06.2006
Skąd: Bytom

Ostrzeżenie: (0%)
-----


Najprawdopodobniej wyjątek z Twoim błędem nie jest w ogóle rzucany. Po kodzie wszystko wygląda ok.
Go to the top of the page
+Quote Post
cyklotrial
post 10.10.2010, 14:40:31
Post #7





Grupa: Zarejestrowani
Postów: 11
Pomógł: 1
Dołączył: 5.06.2009

Ostrzeżenie: (0%)
-----


Chyba sobie jakoś poradziłem z tym problemem. Zastanawiam się tylko czy zrobiłem to we właściwy sposób.

W /lib/form/doctrine utworzyłem plik: sfGuardFormSignin.class.php
  1. class sfGuardFormSignin extends BasesfGuardFormSignin
  2. {
  3. public function configure()
  4. {
  5. parent::configure();
  6. $this->validatorSchema->setPostValidator(new sfGuardValidatorUserCustom(
  7. array('throw_global_error' => true),
  8. 'invalid' => "The email doesn't exist or the password is invalid",
  9. 'inactive' => "This account is inactive")
  10. ));
  11. }
  12. }

W /lib/validator utworzyłem plik: sfGuardValidatorUserCustom.class.php
  1. class sfGuardValidatorUserCustom extends sfValidatorSchema
  2. {
  3. public function __construct($options = array(), $messages = array())
  4. {
  5. $this->addOption('username_field', 'username');
  6. $this->addOption('password_field', 'password');
  7. $this->addOption('throw_global_error', false);
  8.  
  9. $this->addMessage('inactive', 'This account is inactive.');
  10.  
  11. parent::__construct(null, $options, $messages);
  12. }
  13.  
  14.  
  15.  
  16. protected function doClean($values)
  17. {
  18. $username = isset($values[$this->getOption('username_field')]) ? $values[$this->getOption('username_field')] : '';
  19. $password = isset($values[$this->getOption('password_field')]) ? $values[$this->getOption('password_field')] : '';
  20.  
  21.  
  22. // don't allow to sign in with an empty username
  23. if ($username)
  24. {
  25.  
  26. $query = Doctrine_Query::create()
  27. ->from('sfGuardUser')
  28. ->where('username = ?', $username);
  29.  
  30. // Get the user
  31. $user = $query->fetchOne();
  32.  
  33. // user exists?
  34. if($user)
  35. {
  36. // password is ok?
  37. if ($user->checkPassword($password))
  38. {
  39. //account is active?
  40. if($user->getIsActive())
  41. {
  42. return array_merge($values, array('user' => $user));
  43. }
  44. else
  45. {
  46. throw new sfValidatorError($this, 'inactive');
  47. throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'inactive')));
  48. }
  49. }
  50. else
  51. {
  52. throw new sfValidatorError($this, 'invalid');
  53. throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));
  54. }
  55. }
  56. else
  57. {
  58. throw new sfValidatorError($this, 'invalid');
  59. throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, 'invalid')));
  60. }
  61. }
  62. return $values;
  63. }
  64. }


Nie licząc faktu ze takie rozwiązanie działa - Czy jest ono poprawne i ma sens?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 04:36