Hallo !!
I'd like to create a login widget for module Yii-User. I made under module/user/components file called UserLoginWidget.php with code:
<?php
class UserLoginWidget extends CWidget
{
public function init()
{
}
public function run()
{
Yii::app()->getModule('user');
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
//$this->redirect(Yii::app()->controller->module->returnUrl);
}
}
// display the login form
$this->render('userLogin',array('model'=>$model));
}
}
I also made view file userLogin.php under module/user/components/view with code:
<div class="span-8">
<div class="form box">
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'username'); ?>
<?php echo CHtml::activeTextField($model,'username', array('size'=>35)) ?>
</div>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'password'); ?>
<?php echo CHtml::activePasswordField($model,'password', array('size'=>35)) ?>
</div>
<div class="row">
<p class="hint">
<?php echo CHtml::link(UserModule::t("Register"),Yii::app()->getModule('user')->registrationUrl); ?> | <?php echo CHtml::link(UserModule::t("Lost Password?"),Yii::app()->getModule('user')->recoveryUrl); ?>
</p>
</div>
<div class="row rememberMe">
<?php echo CHtml::activeCheckBox($model,'rememberMe'); ?>
<?php echo CHtml::activeLabelEx($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton(UserModule::t("Login")); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
</div>
<?php
$form = new CForm(array(
'elements'=>array(
'username'=>array(
'type'=>'text',
'maxlength'=>32,
),
'password'=>array(
'type'=>'password',
'maxlength'=>32,
),
'rememberMe'=>array(
'type'=>'checkbox',
)
),
'buttons'=>array(
'login'=>array(
'type'=>'submit',
'label'=>'Login',
),
),
), $model);
?>
I've just simply copied login code form Yii-User module. I'd like to use this widget on my home page site/index. I placed this code in site/index.php view :
<?php $this->widget('user.components.UserLoginWidget'); ?>
Till this moments everything is correct. Login form is correctly displayed, but login funtionality is not working. I keep getting this error:
Fatal error: Call to a member function encrypting() on a non-object in C:\xampp\htdocs\milosnicyzwierzat.pl\protected\modules\user\components\UserIdentity.php on line 35
My UserIdentityForm looks like this:
<?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
{
private $_id;
const ERROR_EMAIL_INVALID=3;
const ERROR_STATUS_NOTACTIV=4;
const ERROR_STATUS_BAN=5;
/**
* 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.
*/
public function authenticate()
{
if (strpos($this->username,"@")) {
$user=User::model()->notsafe()->findByAttributes(array('email'=>$this->username));
} else {
$user=User::model()->notsafe()->findByAttributes(array('username'=>$this->username));
}
if($user===null)
if (strpos($this->username,"@")) {
$this->errorCode=self::ERROR_EMAIL_INVALID;
} else {
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else if($user->status==0&&Yii::app()->controller->module->loginNotActiv==false)
$this->errorCode=self::ERROR_STATUS_NOTACTIV;
else if($user->status==-1)
$this->errorCode=self::ERROR_STATUS_BAN;
else {
$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
/**
* @return integer the ID of the user record
*/
public function getId()
{
return $this->_id;
}
}
I belive this is the problem: Yii::app()->controller->module->encrypting, but so far I couldn't find correct answer. Can anyone help me out?
Regards
LukBB
I'd like to create a login widget for module Yii-User. I made under module/user/components file called UserLoginWidget.php with code:
<?php
class UserLoginWidget extends CWidget
{
public function init()
{
}
public function run()
{
Yii::app()->getModule('user');
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
//$this->redirect(Yii::app()->controller->module->returnUrl);
}
}
// display the login form
$this->render('userLogin',array('model'=>$model));
}
}
I also made view file userLogin.php under module/user/components/view with code:
<div class="span-8">
<div class="form box">
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'username'); ?>
<?php echo CHtml::activeTextField($model,'username', array('size'=>35)) ?>
</div>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'password'); ?>
<?php echo CHtml::activePasswordField($model,'password', array('size'=>35)) ?>
</div>
<div class="row">
<p class="hint">
<?php echo CHtml::link(UserModule::t("Register"),Yii::app()->getModule('user')->registrationUrl); ?> | <?php echo CHtml::link(UserModule::t("Lost Password?"),Yii::app()->getModule('user')->recoveryUrl); ?>
</p>
</div>
<div class="row rememberMe">
<?php echo CHtml::activeCheckBox($model,'rememberMe'); ?>
<?php echo CHtml::activeLabelEx($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton(UserModule::t("Login")); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
</div>
<?php
$form = new CForm(array(
'elements'=>array(
'username'=>array(
'type'=>'text',
'maxlength'=>32,
),
'password'=>array(
'type'=>'password',
'maxlength'=>32,
),
'rememberMe'=>array(
'type'=>'checkbox',
)
),
'buttons'=>array(
'login'=>array(
'type'=>'submit',
'label'=>'Login',
),
),
), $model);
?>
I've just simply copied login code form Yii-User module. I'd like to use this widget on my home page site/index. I placed this code in site/index.php view :
<?php $this->widget('user.components.UserLoginWidget'); ?>
Till this moments everything is correct. Login form is correctly displayed, but login funtionality is not working. I keep getting this error:
Fatal error: Call to a member function encrypting() on a non-object in C:\xampp\htdocs\milosnicyzwierzat.pl\protected\modules\user\components\UserIdentity.php on line 35
My UserIdentityForm looks like this:
<?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
{
private $_id;
const ERROR_EMAIL_INVALID=3;
const ERROR_STATUS_NOTACTIV=4;
const ERROR_STATUS_BAN=5;
/**
* 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.
*/
public function authenticate()
{
if (strpos($this->username,"@")) {
$user=User::model()->notsafe()->findByAttributes(array('email'=>$this->username));
} else {
$user=User::model()->notsafe()->findByAttributes(array('username'=>$this->username));
}
if($user===null)
if (strpos($this->username,"@")) {
$this->errorCode=self::ERROR_EMAIL_INVALID;
} else {
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else if($user->status==0&&Yii::app()->controller->module->loginNotActiv==false)
$this->errorCode=self::ERROR_STATUS_NOTACTIV;
else if($user->status==-1)
$this->errorCode=self::ERROR_STATUS_BAN;
else {
$this->_id=$user->id;
$this->username=$user->username;
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
/**
* @return integer the ID of the user record
*/
public function getId()
{
return $this->_id;
}
}
I belive this is the problem: Yii::app()->controller->module->encrypting, but so far I couldn't find correct answer. Can anyone help me out?
Regards
LukBB