This is a filter which allows specific controller actions or controllers as a whole to be protected via HTTP basic auth.
Note: Basic auth features no encryption or obfuscation beyond a base64 transport encoding. Usage of SSL is advised in order to ensure the confidentiality of login credentials.
Requirements ¶
- Yii 1.0 or above
Usage ¶
Copy the HttpAuthFilter.php
file to your protected\components
folder.
Modify the controller you wish to protect in a way that the filters()
method starts of like this:
public function filters()
{
return array(
array(
'HttpAuthFilter',
)
...
);
}
Make sure the filter is the first in the list and does not cover actions that should be reachable by unauthenticated users (which were just cruel).
The following properties are available to modify the filter's behaviour:
realm
: Specifies a descriptive text. Defaults to whatYii::app()->name
returns. If you modify this, please make sure all characters fit into iso-8859-1.authModel
: The model to authenticate against. This defaults toLoginModel
which is just right for freshly created applications. If you modify this, ensure the auth model features alogin()
method.usernameAttribute
: The auth model's attribute carrying the username. This should only worry you if your auth model is very different from the generated one (e.g. the attribute is "user" instead of "username")passwordAttribute
: The same asusernameAttribute
. But for the password, instead.
Resources ¶
- GitHub: yii-httpauth
Thank you!
You have saved my time! Thank you a lot :)
P.S. It would by very useful if you add an "enabled" property. So one could have an ability to manage this filter from a some kind of "settings" action.
RE: Thank you!
Always glad to help :)
I'm not sure I fully understand the purpose of an
enabled
property. Can you describe a more elaborated use-case?RE: Thank you!
For example I want to provide for my clients the ability to choose the auth method they want. If I use your filter like on this page it will be hardcoded, but with
"enabled"=>false
I could skip it
RE[2]: Thank you!
Hm, interesting idea. I'll consider it.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.