Yii Framework Forum: Unable To Login With Database User - Yii Framework Forum

Jump to content

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

Unable To Login With Database User Rate Topic: ***-- 1 Votes

#1 User is offline   sanjay1024 

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

Posted 13 October 2012 - 02:21 PM

Hi,

I am still learning YII.

I want to login in my application with database user. but i am getting Incorrect username or password while login.
password is stored in encrypted format in database. I am using the below code.

can someone please help me.

class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */

	 private $_id;

	public function authenticate()
	{
		$user=User::model()->findbyAttributes(array('username'=>$this->username));

		if($user===null)
		{
		  $this->errorCode=self::ERROR_USERNAME_INVALID;
		}
		else
		{
			if($user->password!==$user->encrypt($this->password))
			{
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
			}
			
		}
		return !$this->errorCode;
	}

	public function getId()
	{
		return $this->_id;
	}
}



protected function afterValidate()
	{
		parent::afterValidate();
		$this->password = $this->encrypt($this->password);
	}
		public function encrypt($value)
		{
			return md5($value);
		}


Thanks
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 13 October 2012 - 02:54 PM

Dear Sanjay

Kindly check the following is helpful.

 public function authenticate()
        {
                $user=User::model()->findbyAttributes(array('username'=>$this->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->errorCode=self::ERROR_NONE;

                return !$this->errorCode;
        }


0

#3 User is offline   Stefano Mtangoo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 248
  • Joined: 02-January 12
  • Location:Dar es Salaam, Tanzania

Posted 13 October 2012 - 03:05 PM

if($user->password!==$user->encrypt($this->password))
{
   $this->errorCode=self::ERROR_PASSWORD_INVALID;
}

Are you sure you are storing plain text passwords? If no thn change it to

if($user->password!==$user->$this->password)
{
   $this->errorCode=self::ERROR_PASSWORD_INVALID;
}

And God said, Let there be science...and it was so!
Training Manager Written in Yii here
0

#4 User is offline   sanjay1024 

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

Posted 13 October 2012 - 03:18 PM

Hi seenivasan,

Thanks for your quick response.

I used your code, for wrong password it is giving error incorrect username or password but for correct password it is showing nothing and remains on the login page only.


Hi Stefano Mtangoo,

Thanks for your quick response.

I am storing the password in md5 format.

When i changed the line suggested by you it is giving below error
Recoverable error
Object of class UserIdentity could not be converted to string 

0

#5 User is offline   sanjay1024 

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

Posted 14 October 2012 - 12:42 AM

OK Thank for your support,

Now I am able to login with database user.

here is my code below.

class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */

	 private $_id;  
	 
	 public function authenticate() 
        { 
                $user=User::model()->findbyAttributes(array('username'=>$this->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->errorCode=self::ERROR_NONE; 
 
                return !$this->errorCode; 
        }

}


Model User.php
protected function afterValidate()
	{
		parent::afterValidate();
		$this->password = $this->encrypt($this->password);
	} 
		public function encrypt($value)
		{
			return md5($value);
		} 



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 he will see only his data.

can it be possible to create seperate login table & login page for customer eg:- (tbl_cust) and another table eg:- (tbl_staff)

customer's user ids will be stored in tbl_cust & Staffs user ids will be saved in tbl_staff table.

Kindly suggest.

Thanks
0

#6 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 14 October 2012 - 02:05 AM

Hi sanjay

that is possible you can do that, but there is another approach that i would suggest you can have only one table for all your users and then you assign them groups based on groups/permission you can give them permission to access certain areas of your website/app.
0

#7 User is offline   sanjay1024 

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

Posted 14 October 2012 - 03:09 AM

Hi alirz23,

We don't want to mess the table with both the username (Staff & cutomer).

We want it to be seperate.

Thanks
0

#8 User is offline   sanjay1024 

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

Posted 14 October 2012 - 10:07 PM

Hi alirz23,

On searching the forum i came to know that for my below requirement i have to do the following.

1) create 2 table tbl_cust & tbl_staff

tbl_cust will contain the customer data and customer user login
tbl_staff will contain the members/admin login to manage the data.

2)create a front end and back end directories (http://www.yiiframew...ionend-behavior)

front end for customer view & back end for members/admin to manage

3) create 2 UserIdentity.php file one for front end and other for back end.

front end UserIdentity.php file will check for users in tbl_cust if found it will login and redirect to customer profile page and if not found it will give error. it wont be checking tbl_staff for user's.

back end UserIdentity.php file will check for users in tbl_staff if found it will login and redirect to members page and if not found it will give error. it wont be checking tbl_cust for user's.


please correct me i am wrong, so that i can start working on it.

Thanks in advance
0

#9 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 17 October 2012 - 02:15 AM

you can go ahead there is nothing wrong with that approach
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