Authenticate agains database - not table

Hey there,

i want to authenticate the UserIdentity against a database, not a table. How can I achieve this?

I've just changed the authenticate() - Method:



try {





	$conection = new CDbConnection("mysql:host=web", $this->username, $this->password);


	$conection->active = true;





	$this->errorCode = self::ERROR_NONE;





	// Store password in UserIdentity


	$this->setState("password", $this->password);





	// Store password in session


	//Yii::app()->session->add("password", $this->password);





} catch (Exception $ex) {





	$this->errorCode = self::ERROR_AUTHENTICATION_FAILED;


	$this->errorMessage = $ex->getMessage();





}


So far, that works pretty fine. My problem now is, that the user's login data is needed to authenticate against the database all the time. So I changed the application config as follows:



'db'=>array(


	'class' => 'CDbConnection',


	'connectionString' => 'mysql:host=web;dbname=information_schema',


	'username'=> Yii::app()->user->getName(),


	'password'=> Yii::app()->user->password,


	'charset' => 'utf8'


)


But, of course, if the user calls the login form, this information is (not yet) available (username, password) and the application throws an exception.

So here's the question:

How can I configure the app to only connect to the database, if the user is logged in?

Thanks for your suggestions,

Rene

Do not set username and password in app config. Set them in authenticate() after the user is authenticated (or simply reuse the connection in authenticate()).

Here's the code for authenticate():



$db=Yii::app()->db;  // not activated yet


$db->password=$this->password;


$db->username=$this->username;


try {


   $db->active=true;


   $this->errorCode=self::ERROR_NONE;


   $this->setState('password', $this->password);


} catch (Exception $ex) {





   $this->errorCode = self::ERROR_AUTHENTICATION_FAILED;


   $this->errorMessage = $ex->getMessage();





}


Ok, but when I executed the first line



$db=Yii::app()->db;  // not activated yet


it said, that the connection could not be established, of course.

So i set the autoConnect property in main.php to false.

Seemed to work out, but the connection would no only be established in the LoginForm. So i set the autoConnect property in the main.php to

'autoConnect' => Yii::app()->user->isGuest ? false : true

But that don't seem to work, because the username and password for the connection is only set in the login form.

FYI:

If the connection could be established with given username and password, the connection should be available on every page…

Could you give me some further help?

One way that I could think of is to insert the following code in index.php:



$app=Yii::createWebApplication($configFile);


if(!$app->user->isGuest)


{


    $app->db->username=...


    $app->db->password=...


    $app->db->autoConnect=true;


}


$app->run();


It's not elegant, but should work under this strange scenario.