Yii Framework Forum: Database:two accounts for authentication [MERGED duplicate post] - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Database:two accounts for authentication [MERGED duplicate post] Rate Topic: -----

#1 User is offline   musikako 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 20-October 10

Posted 26 October 2010 - 07:46 AM

<?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 $_id;
public function authenticate()
{
$username=strtolower($this->username);
$user=accountEmployee::model()->find('LOWER(username)=?',array($username));
if ($user==null)// No user found!
{
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
elseif (($this->password)!==$user->password)// Invalid password!
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
elseif($user=accountStudent::model()->find('LOWER(username)=?',array($username))
{
else if ($user==null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
elseif (($this->password)!==$user->password)// Invalid password!
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else
{
$this->_id = $user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}

public function getId()
{
return $this->_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. :)
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 26 October 2010 - 07:55 AM

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.
0

#3 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 26 October 2010 - 08:08 AM

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?
Find more about me.... btw. Do you know your WAN IP?
0

#4 User is offline   musikako 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 20-October 10

Posted 28 October 2010 - 07:58 AM

View Postmdomba, on 26 October 2010 - 08:08 AM, said:

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?
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 October 2010 - 08:14 AM

First thing first...

what if the same username exists in both tables?
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   musikako 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 20-October 10

Posted 03 November 2010 - 10:25 PM

View Postmdomba, on 28 October 2010 - 08:14 AM, said:

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
0

#7 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 04 November 2010 - 03:19 AM

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

Find more about me.... btw. Do you know your WAN IP?
0

#8 User is offline   musikako 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 20-October 10

Posted 07 November 2010 - 10:26 PM

View Postmdomba, on 04 November 2010 - 03:19 AM, said:

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."
0

#9 User is offline   galymzhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 46
  • Joined: 27-October 10
  • Location:Алматы

Posted 08 November 2010 - 01:22 AM

Docs say that:

Quote

Note, when cookie-based authentication is enabled, all these persistent data will be stored in cookie. Therefore, do not store password or other sensitive data in the persistent storage. Instead, you should store them directly in session on the server side if needed.

So don't do access checks (or important things) depending on userType.
2b || !2b that's the question
0

#10 User is offline   sanjay1024 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 06-October 12

Posted 14 October 2012 - 02:54 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users