authorization and authentication

Hi all,

I am new to Yii,I have 3 users like artists,listeners,fans in my website.

Hence I want to display different pages to different users when they login based on their type

How can i provide authorization for them.

please help me in writing the code for these authentications

please try to provide complete code for (Model,View and Controller)

This may help in my project.

-regards

Shivakumar

Hey , you made many post in very short time and asked basic questions.

seem you not tried to search things that how they work.

…

the posts you make is already on Yii forum [ Solved ], please try to search by appropriate keyword.

We are here to help but first try to refer yii guide.

Joining the above recommendation… .

This will be a good read for you to start with: http://www.yiiframework.com/doc/guide/1.1/en/topics.auth

yaa!

you are right my dear friends

but I am very much new to this field,and was straight away put into the project

no time to refer more things.

Thats why i came for your help.

thanks for ur feedback

I will try to google more with good key words

Will come back to u guys if there is any urgent requirement

Thank you once again for ur valuable feedback

–regards

shivakumar

@shivakumar , this can help you … refer it.

step : 1

first you have to override id using getId() function , I post here working code of components folder , UserIdentity file ,


class UserIdentity extends CUserIdentity

{

   private $_id;

   public function authenticate()

   {

       $record=Employee::model()->findByAttributes(array('E_EMAIL'=>$this->username));  // here I use Email as user name which comes from database

       if($record===null)

               {

                       $this->_id='user Null';

         			   $this->errorCode=self::ERROR_USERNAME_INVALID;

               }

       else if($record->E_PASSWORD!==$this->password)            // here I compare db password with passwod field

               {        $this->_id=$this->username;

                       $this->errorCode=self::ERROR_PASSWORD_INVALID;

               }

	 else if($record['E_STATUS']!=='Active')                //  here I check status as Active in db

               {        

			   		$err = "You have been Inactive by Admin.";

			        $this->errorCode = $err;

               }

	

       else

       {  

          $this->_id=$record['E_NAME'];

	   $this->setState('title', $record['E_NAME']);

           $this->errorCode=self::ERROR_NONE;


       }

       return !$this->errorCode;

   }


   public function getId()

   {

       return $this->_id;

   }

}




step : 2

here is code for models folder LoginForm file ,


public function authenticate($attribute,$params)

	{

		if(!$this->hasErrors())

		{

			$this->_identity=new UserIdentity($this->username,$this->password);

			if(!$this->_identity->authenticate())

				$this->addError('password','Incorrect username or password.');

		}

	}

step : 3

code for controller file ,


public function actionLogin()

	{

		$model=new LoginForm;

		$empModel = new Employee;   //  for validating different user to redirect different pages


		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		if(isset($_POST['LoginForm']))

		{

				

			$model->attributes=$_POST['LoginForm'];

			

			if($model->validate() && $model->login())

			{

				$empData = $empModel->getEmpLoginData($_POST['LoginForm']['username']);   // get employee data from database comparing username ( in my case email )

				$eid = $empData[0]['E_ID'];          

				if($empData[0]['E_ROLE']=='Admin'){        //  checking user is Admin or not

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

				}else{

					$this->redirect(array("user_page_here"));   //  redirect normal user page

				}

			}

				

		}

		

		$this->render('login',array('model'=>$model));

	}