Yii Framework Forum: Show Section Of The Page/view Only To Logged In Users - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Show Section Of The Page/view Only To Logged In Users Rate Topic: -----

#1 User is offline   Nuwan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 29-July 13
  • Location:Panadura, Sri Lanka.

Posted 25 November 2013 - 01:06 AM

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

Attached File(s)


Thanks & Best Regards,
Nuwan
www.nuwanblog.com
0

#2 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 25 November 2013 - 03:51 AM

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
}
?>


Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
1

#3 User is offline   Nuwan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 29-July 13
  • Location:Panadura, Sri Lanka.

Posted 25 November 2013 - 04:28 AM

View PostArockia Johnson, on 25 November 2013 - 03:51 AM, said:

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?
Thanks & Best Regards,
Nuwan
www.nuwanblog.com
0

#4 User is offline   Arockia Johnson 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 25-June 13

Posted 25 November 2013 - 04:57 AM

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
Thanks & Regards,
S.A. Johnson
www.arojohnson.tk
0

#5 User is offline   Nuwan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 29-July 13
  • Location:Panadura, Sri Lanka.

Posted 25 November 2013 - 06:48 AM

View PostArockia Johnson, on 25 November 2013 - 04:57 AM, said:

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
Thanks & Best Regards,
Nuwan
www.nuwanblog.com
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 25 November 2013 - 07:17 AM

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.yiiframew...ckAccess-detail
http://www.yiiframew.../en/topics.auth
1

#7 User is offline   Nuwan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 29-July 13
  • Location:Panadura, Sri Lanka.

Posted 25 November 2013 - 11:33 PM

View Postsoftark, on 25 November 2013 - 07:17 AM, said:

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.yiiframew...ckAccess-detail
http://www.yiiframew.../en/topics.auth


It is working. Thanks for both Arockia and Softark
Thanks & Best Regards,
Nuwan
www.nuwanblog.com
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users