Database:two accounts for authentication [MERGED duplicate post]

<?php

/**

  • UserIdentity represents the data needed to identity a user.

  • It contains the authentication method that checks if the provided

  • data can identity the user.

*/

class UserIdentity extends CUserIdentity

{

 // Need to store the user's ID:


private &#036;_id;


public function authenticate()


{


	&#036;username=strtolower(&#036;this-&gt;username);


	&#036;user=accountEmployee::model()-&gt;find('LOWER(username)=?',array(&#036;username));


	if (&#036;user==null)// No user found&#33;


	{


		&#036;this-&gt;errorCode=self::ERROR_USERNAME_INVALID;


	}


	 elseif ((&#036;this-&gt;password)&#33;==&#036;user-&gt;password)// Invalid password&#33;


	 {


		&#036;this-&gt;errorCode=self::ERROR_PASSWORD_INVALID;


	}


	elseif(&#036;user=accountStudent::model()-&gt;find('LOWER(username)=?',array(&#036;username))


	{


		else if (&#036;user==null)


		&#036;this-&gt;errorCode=self::ERROR_USERNAME_INVALID;


	}


	elseif ((&#036;this-&gt;password)&#33;==&#036;user-&gt;password)// Invalid password&#33;


	{


		&#036;this-&gt;errorCode=self::ERROR_PASSWORD_INVALID;


	}


	 else 


	 {


	    &#036;this-&gt;_id = &#036;user-&gt;id;


	    &#036;this-&gt;username=&#036;user-&gt;username;


		&#036;this-&gt;errorCode=self::ERROR_NONE;


	}


	return &#33;&#036;this-&gt;errorCode;


}





public function getId()


{


 return &#036;this-&gt;_id;


}

}

the above code is my example for the authentication. For a week i have been trying to figure out how to do this but unfortunately i cant debug it. I need your help guys with this problem. Advance thx.

i have a two tables for my site which are for the employee and the student accounts. i just want to know how will i connect the two tables so that yii useridentity.php can read the database on my phpmyadmin. :)

In phpmyadmin you should create the two tables in the same database and only one user of the database.

This user (created on phpmyadmin) will be used by Yii for connect to both the tables. You should specify username and password of the database in your config/main.php

Now your application can read the database.

The user/employ distiction is a your convecion related to your application, has nothing congerning the user that the application uses for conect to database.

When posting code use the [code ]…[/code ] directives, it’s easier to read your code…

Your code looks a big mess of if elsif… and it’s not clear what you want to do…

If I understood you… you would like the user to enter a username and password and you would check it in the emplyee and if not found then in the student model?

But what if the same username exists in both tables?

yep, but how?

First thing first…

what if the same username exists in both tables?

username has a different format from the two table

(ex.) for student firstletter of the firstname and the first letter of the middle initial then the full lastname.

  Kristine M. Reyes (kmreyes)





  for employee firstname, first letter of the middle initial and the firstletter of the lastname

OK, If you are sure there is no same username in both tables you can use something like this




	public function authenticate()

	{

		$username = strtolower($this->username);

		$userType='Emplyee';

		$user = accountEmployee::model()->find('LOWER(username)=:username',array('username' => $username));

		if($user == null)

		{

			$userType='Student';

			$user = accountStudent::model()->find('LOWER(username)=:username',array('username' => $username));

		}


		if($user == null)

			$this->errorCode = self::ERROR_USERNAME_INVALID;

		elseif(($this->password) !== $user->password)

			$this->errorCode = self::ERROR_PASSWORD_INVALID;

		else

		{

			$this->_id = $user->id;

			$this->username = $user->username;

			$this->errorCode = self::ERROR_NONE;

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

		}

		return!$this->errorCode;

	}



This way to get the usertype of the logged user you can use


Yii::app()->user->userType

Yehey its working now super thx sir. But i have one problem for the user authentication if i log in as an employee it doesn’t want me to manage some pages.

"Error 403

You are not authorized to perform this action."

Docs say that:

So don’t do access checks (or important things) depending on userType.

Hi Maurizio Domba,

i wanted exactly the same thing.

I am developing a application which will have customer login to view their details and admin/members login to manage/update the application/user’s data.

I am having one table which consists of user’s data and the same data when login with customer login id is redirected to a page where he will see only his data in readonly.

customer’s user ids are stored in tbl_master & Staffs user ids are stored in tbl_user table.

tbl_master has following field where Mobile is the username

Name

Mobile <=====this will be the username

Password <==== this will be the password, Notice the P in caps

tbl_user has following field

username <=====this will be the username

password <==== this will be the password

I entered your code it is able to login staff user but for customer login it is giving CException error.

Find below the code.




private $_id;  


	public function authenticate() 

        { 

                $username = strtolower($this->username); 

                $userType='staff'; 

                $user = User::model()->find('LOWER(username)=:username',array('username' => $username)); 

                if($user == null) 

                { 

                        $userType='customer'; 

                        $user = Master::model()->find('LOWER(Mobile)=:username',array('username' => $username)); 

                } 

 

                if($user == null) 

                        $this->errorCode = self::ERROR_USERNAME_INVALID; 

                else if($user->password!==$user->encrypt($this->password))

                        $this->errorCode = self::ERROR_PASSWORD_INVALID; 

                else 

                { 

                        $this->_id = $user->id; 

                        $this->username = $user->username; 

                        $this->errorCode = self::ERROR_NONE; 

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

                } 

                return!$this->errorCode; 

        }



User Model used for Staff login




protected function afterValidate()

	{

		parent::afterValidate();

		$this->password = $this->encrypt($this->password);

	} 

		public function encrypt($value)

		{

			return md5($value);

		} 



Master Model used for customer login




protected function afterValidate()

	{

		parent::afterValidate();

		$this->Password = $this->encrypt($this->Password);

	} 


		public function encrypt($value)

		{

			return md5($value);

		} 



Thanks in advance