Show Section Of The Page/view Only To Logged In Users

Hi,

I want to show the ‘Area 2’ only to the logged in users(please refer the attached image). How can I do this? Please help on this.

Thank you

Hi,

If you are using Area 2 in a div element. you can use as follows




<?php

if(!Yii::app()->user->isGuest){

?>

<div id="area2">

Your area2 div...

</div>

<?php

}

?>



Hi,

Thank you very much for your reply. It should be working on authenticated user. I will try that. I have another small question. How can use this with right module. For example only the users in ‘role 1’ should see ‘Area 2’. Users in role ‘role 2’ cannot see this ‘Area 2’ etc… If that is the scenario how can we achieve this?

Hi,

You can directly check with 2 conditions - one is NOT A isGuest the other is user role 2 should not access… it can be written as




if(Yii::app()->user->role !=2 && !Yii::app()->user->isGuest ){


Your area 2...

}



Cheers!

Don’t hesitate to press + If you like this post

Hi,

Thanks again for your reply. But if i am correct right module uses controller to give permission.




public function filters() 

{ 

   return array( 

      'rights', 

   ); 

}



And when i used Yii::app()->user->role in the view, it gave following error.




CException


Property "RWebUser.role" is not defined. 



If you could help on this it would very grateful of you.

Thanks

We usually do it like this:




if(Yii::app()->user->checkAccess('role1'))

{

    // do something that is permitted for 'role1'

}



The access control can be performed both 1) in the controller’s filter and 2) in the controller’s action (including the view script) using CWebUser::checkAccess. When the filter doesn’t permit the action, the whole action is canceled. But even when the user has passed the filter, you still have a chance to limit the output result depending on the user’s right.

http://www.yiiframework.com/doc/api/1.1/CWebUser#checkAccess-detail

http://www.yiiframework.com/doc/guide/1.1/en/topics.auth

It is working. Thanks for both Arockia and Softark