I m new to yii .
i have created an application which has admin panel
I have used Webuser for authentication
My directory Structure is
controller/
-> frontend
---->SiteController.php
->backend
----->SiteController.php
…same for views…
See UserIdentity.php
class UserIdentity extends CUserIdentity {
const ERROR_EMAIL_INVALID=3;
public $id;
public function authenticate() {
$record = User::model()->findByAttributes(array('username' => $this->username));
if ($record === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} elseif ($record->password !== md5($this->password) and $record->password !== $this->password) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} else {
$this->username = $record->username;
$this->id = $record->id;
$this->setStates($record);
$this->errorCode = self::ERROR_NONE;
}
return!$this->errorCode;
}
public function getId() {
return $this->id;
}
private function setStates($user) {
$this->setState('rank', $user->group_id);
$this->setState('email', $user->email);
}
}
and WebUser.php
<?php
class WebUser extends CWebUser {
public $email;
public $rank = 1;
public function init() {
parent::init();
/*
* Sets the user email and rank
* The reason I use this method is so that I can access the user states as attributes (you can do that anyways as of Yii 1.0.3 though)
* and so that the user rank defaults to 1 (meaning not logged on)
* See the group model for information on the ranks
*/
$this->email = $this->getState('email');
$rank = $this->getState('rank');
if ($rank != null)
$this->rank = $rank;
}
/**
* Compares the current user to $rank
*
* Should be used in view to decide if e.g. an admin-only link should be rendered
* Example:
* <?php if (Yii::app()->user->hasAuth(Group::ADMIN, 'min')){ ?>
* <p>Something only an admin or higher ranking user should see</p>
* <?php } ?>
*
* This is a very simple yet fairly flexible authorization technic.
* Note I have also extended the AccessControlFilter to be simpler and yet also
* reasonably flexible
*
* @param integer the rank to campare the current user to
* @param string the camparison type. Can be 'min', 'max', or 'equal'
*/
public function hasAuth($rank = 2, $comparison = 'min') {
$mapConditions = array(
'min' => ($this->rank >= $rank),
'max' => ($this->rank <= $rank),
'equal' => ($this->rank == $rank),
);
return $mapConditions[$comparison];
}
}
?>
AND model/User.php
public function authenticatePass($attribute,$params) {
if (!$this->hasErrors()) { // we only want to authenticate when no input errors
$identity = new UserIdentity($this->username,$this->password);
$identity->authenticate();
switch ($identity->errorCode) {
case UserIdentity::ERROR_NONE:
$duration = $this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($identity, $duration);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username','Nom d\'utilisateur est incorrect.');
break;
case UserIdentity::ERROR_EMAIL_INVALID:
$this->addError('username','Vous devez valider votre adresse e-mail avant de vous connecter.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Mote de passe est incorrect.');
break;
}
}
}
I have used widgets UserLogin.php
class UserLogin extends Portlet {
public $title = '';
protected function renderContent() {
$form = new LoginForm;
if (isset($_POST['LoginForm'])) {
$form->attributes = $_POST['LoginForm'];
if ($form->validate()) {
$userConnected = User::model()->find('username = :un', array('un' => $form->username));
if ($userConnected && ($userConnected->group_id == Group::ADMIN || $userConnected->group_id == Group::SITE_ADMIN)){
$this->controller->redirect(array('home/index'));
}elseif( $userConnected->group_id == Group::AGENCE){
$this->controller->redirect(array('site/index'));
}
}//if
}
$this->render('userLogin', array('form' => $form));
}
}
Here everything is correct…i mean username and password are correct
when i check in homeController/actionIndex Yii::app()->user->id is empty and it display the Login form
i have two files for frontend and backend
for
front.php
‘components’=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
),
back.php
‘components’=>array(
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'admin'=>'site/index',
'admin/<controller:\w+>/<id:\d+>'=>'<controller>/view',
'admin/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
),
main config file
main.php
‘import’=>array(
'application.models.*',
'application.components.*',
'application.components.widgets.*',
'application.controllers.AdminController',
'application.extensions.*',
#user module starts
),
‘components’=>array(
'user' => array(
'class' => 'WebUser',
// enable cookie-based authentication
// 'allowAutoLogin' => true,
'loginUrl' => array('user/login'),
),
I m not getting where is the problem exists…
plz i need help urgent