![]() |
|
Snippets |
|
Here is a little hack to use http auth when credentials or auth is insufficient:
public function executeSecure() { if (!$this->getUser()->hasAttribute("secure_referer")) $this->getUser()->setAttribute("secure_referer", $this->getRequest()->getReferer()); if (!isset($_SERVER['PHP_AUTH_USER'])) { header('WWW-Authenticate: Basic realm="Member Area"'); header('HTTP/1.0 401 Unauthorized'); return sfView::NONE; } else { if ($this->getUser()->tryLogin($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW'])) { return $this->redirect($this->getUser()->getAttribute("secure_referer")); } else { header('WWW-Authenticate: Basic realm="Member Area"'); header('HTTP/1.0 401 Unauthorized'); return sfView::NONE; } } }
No template is needed, as everytime you access it will redirect to the referer. Then change in app/yourapp/config/settings.yml the secure_module and secure_action to match this module.
You will need a myUser::tryLogin function that returns a boolean saying "auth is ok" or "bad auth"
And then you're done :p
[from my Wiki Post ab out that]
Comments on this snippet
One should never forget the DRY principle. Beside you should take advantage of the symfony methods. Here is a better version of that snippet:
Yeah thanks, you can erase the echo line too, it was only for debugging purposes :p
My browser goes into a redirect loop when using this. When I type in the URL for a secure page directly and login through HTTP_AUTH, because $_SERVER['PHP_AUTH_USER']) is set, it redirects me to the referring page. The referring page is secure, so symfony routes the request through the secure module again, and because $_SERVER['PHP_AUTH_USER']) is still set, it redirects me to the referring page again, and so on. Any suggestions?
In my version of symfony this line doesn't work:
Instead I have to do this:
I don't think that $this->sendHttpHeaders() has ever been possible in any version. ;-) Just one more mistake of mine. Thanks Scott.
(unfortunately i can't seem to be able to edit my own comments... :-( )
This could be comfortable, but I have a big doubt: What about logout? I think it cannot be safely implemented cause it's not fully server guaranteed. If the browser keeps in its cache PHP_AUTH_USER and PHP_AUTH_PW, even after logout someone can still navigate back with the browser having it automagically feed cached AUTH parameters with correct data!
please read: [..] with the browser having it automagically feed AUTH parameters with correct cache data!