rahul_dev
(Raoul Dev)
December 13, 2010, 9:20am
1
How can i customize my authentication so that it takes the username from my user table instead of using demo/demo or admin/admin? I’ve read some tutorials but i’m getting all confused coz the UserIdentity is not similar to mine.
<?php
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}
iivano71
(Igor Zg1987)
December 13, 2010, 9:23am
2
How can i customize my authentication so that it takes the username from my user table instead of using demo/demo or admin/admin? I’ve read some tutorials but i’m getting all confused coz the UserIdentity is not similar to mine.
<?php
class UserIdentity extends CUserIdentity
{
public function authenticate()
{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
}
Read the definitive guide
Guide
rahul_dev
(Raoul Dev)
December 13, 2010, 9:30am
3
i did. it tells me to create a private variable $_id. then i have to create the function getId(). But the codes that they use is different from mine.
$record=User::model()->findByAttributes(array(‘username’=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
This part is different. Mine uses isset.
doodleme
(Kakibox)
December 13, 2010, 9:41am
4
I am using this module : http://www.yiiframework.com/extension/yii-user
& modify it accordingly.
doodleme
(Kakibox)
December 13, 2010, 9:44am
5
iivano71
(Igor Zg1987)
December 13, 2010, 9:54am
6
i did. it tells me to create a private variable $_id. then i have to create the function getId(). But the codes that they use is different from mine.
$record=User::model()->findByAttributes(array(‘username’=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
This part is different. Mine uses isset.
its problem because you not returning error code;
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode=self::ERROR_NONE;
}
rahul_dev
(Raoul Dev)
December 13, 2010, 10:03am
7
its problem because you not returning error code;
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState('title', $record->title);
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode;
}
<?php
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->setState(‘title’, $record->title);
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
it should be like above right?
But then i get this error:
PHP Error
Description
include(User.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory
Source File
C:\wamp\www\yii\framework\YiiBase.php(395)
iivano71
(Igor Zg1987)
December 13, 2010, 10:03am
8
did you create model login , extend CActive form ??
and then call CuserIdenttiy in that form model
mdomba
(Maurizio Domba Cerin)
December 13, 2010, 10:04am
9
With "mine" you mean the code generated by yiic… That code is very simple and allows to login with "demo/demo" or "admin/admin"… so that code needs to be changed with the code in the definitive guide so that it works with the database…
mdomba
(Maurizio Domba Cerin)
December 13, 2010, 10:06am
10
@Rahul Dev
when posting code… use the <> icon in the editor… or insert your code between [ code] and [ /code] … without spaces… so that your code is readable…