Authenticate Admin

Hello Guys, i want to set a particular user to admin when the user logs in. I have a table user which has a field type. For members, type=0 and for Admin, type=1. How can i set the user as admin if type=1?

My authenticate() method is as follows:




public function authenticate()

	{

		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));

		$password=User::model()->find('LOWER(password)=?',array(strtolower($this->password)));

		if($user===null)

			$this->errorCode=self::ERROR_USERNAME_INVALID;

		else if($password===null)		

			$this->errorCode=self::ERROR_PASSWORD_INVALID;

		else

		{

			


			$this->_id=$user->user_id;

			$this->errorCode=self::ERROR_NONE;

		}

		return $this->errorCode==self::ERROR_NONE;

	}


	public function getId()

        {

                return $this->_id;

        } // End of getId



have a look here

http://www.yiiframework.com/wiki/80/add-information-to-yii-app-user-by-extending-cwebuser-better-version/

Hi , if you want to redirect on different page using same login , then you can use this trick …

[size="3"]step : 1 =>[/size]

put this code in your controller like ,

public function actionLogin()

{ …

$empData = $empModel->getEmpLoginData($_POST[‘LoginForm’][‘username’]); // to get login user data .

// now you can check admin role from db ( I have two roles as Admin and Employee )

if($empData[‘E_ROLE’]==‘Admin’)

{

Yii::app()->session[‘adminUser’] = “admin”; // start admin session

$this->redirect(array("admin/index")); // redirect to admin page

}else{

$this->redirect(array("employee/home&id=$eid")); // redirect to normal user page

}

[size="3"]step : 2 =>[/size]

no you can define access rules in any module like ,

public function accessRules()

{


	if(Yii::app()->session['adminUser'] == "admin"){


		$arr = array('create','update','index','view','admin','delete');  //  create rule array for admin


	}else{


		$arr = array('');   //  deny all pages for normal user


	}


	//unset(Yii::app()->session['adminUser']);


	return array(


		array('allow',  


			'actions'=>array(''),


			'users'=>array('*'),


		),


		array('allow', 


			'actions'=>$arr,       //  return array of access pages  


			'users'=>array('@'),


		),


		array('allow', 


			'actions'=>array(''),


			'users'=>array('admin'),


		),


		array('deny',  // deny all users


			'users'=>array('*'),


		),


	);


}

[size="3"]step : 3 =>[/size]

now you can unset session in site controller like ,

public function actionLogout()

{


	if(isset(Yii::app()->session['adminUser']))


	{


		unset(Yii::app()->session['adminUser']);   // unset the admin session


	}


	Yii::app()->user->logout();


	$this->redirect(Yii::app()->homeUrl);


}

[size="4"][u][b]

Thanks ,

this works 100%[/b][/u][/size]