Yii Framework Forum: User Authentification - Yii Framework Forum

Jump to content

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

User Authentification where to create the code? Rate Topic: *---- 1 Votes

#1 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 16 June 2013 - 04:37 PM

Hi =)

I'm getting started with yii, and have read Larry's tutorial. I know my question must be silly, but I'm a bit confused. I've found this tutorial fur user creation http://www.yiiframew.../en/topics.auth and I'm surprised how easy it seams to be to create a user auth system. But I don't know where to create this code...

Before I've read this tutorial, I had created a "user" table with login, password , name etc field, and wanted to bind them with the login feature automatically inserted in yii, but it didn't seam to be the right approach...

Can anyone guide me a little bit through this problem?

Thanks a lot!
0

#2 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 18 June 2013 - 02:12 AM

I found this tutorial http://www.yiiframew.../prototype.auth and tried to adapt to what I already have.
I have a user class with ('id' 'login' 'password' ... fields)

So I've tried with this code


<?php
class UserIdentity extends CUserIdentity
{
    private $_id;
 
    public function authenticate()
    {
        $username=strtolower($this->username);
        $user=User::model()->find('LOWER(login)=?',array($username));
        if($user===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if(!$user->validatePassword($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->username=$user->username;
            $this->errorCode=self::ERROR_NONE;
        }
        return $this->errorCode==self::ERROR_NONE;
    }
 
    public function getId()
    {
        return $this->_id;
    }
}



But it doesn't work with the user (login='test' password='test) that I have created in phpmyadmin...

Does anyone have a clue?

Thanks a lot!
0

#3 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 18 June 2013 - 04:44 AM

View Postgelly, on 18 June 2013 - 02:12 AM, said:

I found this tutorial http://www.yiiframew.../prototype.auth and tried to adapt to what I already have.
I have a user class with ('id' 'login' 'password' ... fields)

So I've tried with this code


<?php
class UserIdentity extends CUserIdentity
{
    private $_id;
 
    public function authenticate()
    {
        $username=strtolower($this->username);
        $user=User::model()->find('LOWER(login)=?',array($username));
        if($user===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if(!$user->validatePassword($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$user->id;
            $this->username=$user->username;
            $this->errorCode=self::ERROR_NONE;
        }
        return $this->errorCode==self::ERROR_NONE;
    }
 
    public function getId()
    {
        return $this->_id;
    }
}



But it doesn't work with the user (login='test' password='test) that I have created in phpmyadmin...

Does anyone have a clue?

Thanks a lot!



try to create a model for your user table, then controller and views, and then create a user . then try log in
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#4 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 18 June 2013 - 05:09 AM

Thanks for your reply,

I've created a Model, and then a CRUD for the user. Then I've created a user in phpmyadmin... but it doesn't work .

It says that I have an invalid username or password. It must be because in my table I use a 'login' field and not 'username'. I've tried to do the modifications (see my previous message) but something must be wrong :/
0

#5 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 18 June 2013 - 05:24 AM

View Postgelly, on 18 June 2013 - 05:09 AM, said:

Thanks for your reply,

I've created a Model, and then a CRUD for the user. Then I've created a user in phpmyadmin... but it doesn't work .

It says that I have an invalid username or password. It must be because in my table I use a 'login' field and not 'username'. I've tried to do the modifications (see my previous message) but something must be wrong :/


Hi,

Please post your User table schema (or the attributes)
Also post the validatePassword method
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#6 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 18 June 2013 - 07:04 AM

OK here is my table


CREATE TABLE IF NOT EXISTS `user` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `login` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `firstName` varchar(20) NOT NULL,
  `lastName` varchar(40) NOT NULL,
  `email` varchar(60) NOT NULL,
  `adress` varchar(100) DEFAULT NULL,
  `zip` int(11) DEFAULT NULL,
  `city` varchar(60) DEFAULT NULL,
  `departmentId` tinyint(3) unsigned NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (departmentId) REFERENCES Department(id)',
  `subscriptionDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `birthday` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`lastName`,`firstName`),
  KEY `departmentId` (`departmentId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;



And my validatepassword which is is models/user


	    public function validatePassword($password)
    {
        return crypt($password,$this->password)===$this->password;
    }


0

#7 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 18 June 2013 - 09:43 AM

View Postgelly, on 18 June 2013 - 07:04 AM, said:

OK here is my table


CREATE TABLE IF NOT EXISTS `user` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `login` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `firstName` varchar(20) NOT NULL,
  `lastName` varchar(40) NOT NULL,
  `email` varchar(60) NOT NULL,
  `adress` varchar(100) DEFAULT NULL,
  `zip` int(11) DEFAULT NULL,
  `city` varchar(60) DEFAULT NULL,
  `departmentId` tinyint(3) unsigned NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (departmentId) REFERENCES Department(id)',
  `subscriptionDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `birthday` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`lastName`,`firstName`),
  KEY `departmentId` (`departmentId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;



And my validatepassword which is is models/user


	    public function validatePassword($password)
    {
        return crypt($password,$this->password)===$this->password;
    }




Are you sure that the validatePassword works correctly?

check it by this code, at the moment you are login to the system
        public function validatePassword($password)
    {
        var_dump(crypt($password,$this->password)===$this->password);
        die();
    }

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#8 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 18 June 2013 - 11:07 AM

"Boolean false" appears...

(thanks again for your help)
0

#9 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 18 June 2013 - 03:24 PM

View Postgelly, on 18 June 2013 - 11:07 AM, said:

"Boolean false" appears...

(thanks again for your help)


So, you have to solve it.
This is it the problem.

Check how the password stored in database for example as md5($password)
in this way you have to check like that if (md5($password)==$this->password) {...success...}
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#10 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 19 June 2013 - 02:03 AM

It means that validatePassword is called right?

Maybe the problem comes from the fact that password are not crypted in the database? (which is another problem to solve, but at the account creation)
0

#11 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 19 June 2013 - 04:14 AM

View Postgelly, on 19 June 2013 - 02:03 AM, said:

It means that calidatePassword is called right?

Maybe the problem comes from the fact that password are not crypted in the database? (which is another problem to solve, but at the account creation)


Hi gelly again,

I mean your code may not compares the passwords in the right way

I don't check the crypt php function by myself but you can check if the crypted password
is the same with password after of encryption.

So you could change the compare by simpler algorithm as I mentioned (at least temporarly)

Also check the method that encrypts the passwords before stores in the database.

So, the problem is the comparison
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#12 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 19 June 2013 - 09:01 AM

Yes, I think that the problem comes from the fact that the site doesn't encrypt the password at the creation of the user.

I check it and I'll come back,

Thanks for your help
0

#13 User is offline   gelly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 13-June 13

Posted 20 June 2013 - 08:28 AM

Hi again,

I'm still blocked, I can't figure out where I have to encrypt the password.

How it is used:

The user goest to "site/index.php/user/create" enter the fields and the clicks on "create" , it leads him to "site/index.php/user/8" where we can see the user infos.

But I can't find which file to modify :/

It's confusing...
0

#14 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 20 June 2013 - 08:41 AM

View Postgelly, on 20 June 2013 - 08:28 AM, said:

Hi again,

I'm still blocked, I can't figure out where I have to encrypt the password.

How it is used:

The user goest to "site/index.php/user/create" enter the fields and the clicks on "create" , it leads him to "site/index.php/user/8" where we can see the user infos.

But I can't find which file to modify :/

It's confusing...



Hi again gelly!

So, check this

on your model override these methods

public $pass_stored;
 
protected function afterFind(){
	 $this->pass_stored = $this->password;
      parent::afterFind();	 
}

public function beforeSave() {
        if (parent::beforeSave()) {
	    if ($this->pass_stored!=$this->password) $this->password = md5($this->password);            
            return true;
        } else {
            return false;
        }
}


create a new account and try to log-in replacing first the validatePassword

public function validatePassword($password)
{
  return md5($password)===$this->password;
}

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#15 User is offline   RSfTDL 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 89
  • Joined: 22-June 12

Posted 20 June 2013 - 08:43 AM

Hi gelly!

Show us your code (User/loginform model/controller/UserIdentity.php). It makes the things easy to help you.
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