I’m writing an application that’s essentially a web services client. It has two initial logins. One for the application itself and another for users.
The application login doesn’t require prompts (will get credentials from config) and must happen successfully before anything else can happen.
Basically I need to call this app login service very early on. What is the best place for this? Extend the Application class? Put in botstrap script? etc.
Basically you need two authentications right? one is whole application authentication and the other is user login.
For the whole application authentication you can set a session separately. That can be done by extending CBehaviour class do the stuffs as follows in the components folder the class and file name must be same.
class RequireLogin extends CBehavior{
public function attach($owner) {
$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest')); // handleBeginRequest is the function where you can define the sessions.
}
public function handleBeginRequest($event) {
//Set your own whole application authentication session var here and check
if(empty(Yii::app()->session['whole_app_auth'])){
//Read your config and set the sessions
Yii::app()->session['whole_app_auth'] = 'value';
//Redirect script for further user authentication web service page... and stuffs...
}
}
}
Don’t forget to add the following code into your config file
'behaviors' => array(
'onBeginRequest' => array(
'class' => 'application.components.RequireLogin' // points your class file and stuffs
),
),
For user login
check the yii’s default isGuest property and you may do the checkpoint in beforeAction of the controller.php file. example
if (Yii::app()->user->isGuest){
//The user need to login
}else
return true;