removing menu

Here’s my problem, by default, this is what my homepage menu looks like (works)

when I click the login, and logged in a ‘User’, it becomes like this (works)

when log out, it comes back to the first screen shot.

Now, when I click the ‘Companies’ link, here’s what one will see,

then click a company, the visitor get’s redirected to a ‘Staff Login Page’,

after the staff logged in, my menu now looks like this,

how will I remove the ‘My Companies’ link at the menu ‘if and only if’ the logged in user is a staff ?

here’s my code at the main.php layout file




	<div id="header">

		<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>

	</div><!-- header -->


	<div id="mainmenu">

		<?php $this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				array('label'=>'Home', 'url'=>array('/site/index')),

				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about'),'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Contact', 'url'=>array('/site/contact'),'visible'=>Yii::app()->user->isGuest),

				array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index'),'visible'=>!Yii::app()->user->isGuest),

				array('label'=>'Companies', 'url'=>array('/companies/index'),'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)

			),

		)); ?>

	</div><!-- mainmenu -->



if am gonna do it like this ,




array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index'),'visible'=>!Yii::app()->user->isGuest),'visible'=>!Yii::app()->staffUser->isGuest,



am getting an error




Object configuration must be an array containing a "class" element.



I had my user components set to this




	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

            'loginUrl'=>array('companies/login'),

		),

        'staffUser'=>array(

            'allowAutoLogin'=>true,

            'stateKeyPrefix'=>'staff_',

        ),



what should I do ?

none of these trial and error codes worked so far




(!Yii::app()->user->isGuest && !Yii::app()->staffUser->isGuest)?'visible':'false')


'visible'=>(!Yii::app()->user->isGuest && !Yii::app()->staffUser->isGuest)


'visible'=>array(!Yii::app()->user->isGuest, !Yii::app()->staffUser->isGuest)


'visible'=>array(array("!Yii::app()->user->isGuest"=>true),array("!Yii::app()->staffUser->isGuest"=>true)


'visible'=>(!Yii::app()->staffUser->isGuest && !Yii::app()->user->isGuest)



Those rules mean that they will be hidden if the current user is logged in and a staff member.??

So it would be hidden if the user is a guest or a staff member.


visible = ( (!Yii::app()->user->isGuest) && (!Yii->app()->user->isStaff) )

Are you overriding CWebUser?

If you are, you can just add a new function:


	public function getIsStaff()

	{

		return $this->getState('_staff_whatever') !== null;

	}



the ‘my companies’ should be hidden by default and if the current logged-in is a ‘staff’

am not trying to override, but is this configuration wrong ?




	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

            'loginUrl'=>array('companies/login'),

		),

        'staffUser'=>array(

            'class'=>'CWebUser',

            'allowAutoLogin'=>true,

          //  'loginUrl'=>array('companies/login'),

            'stateKeyPrefix'=>'staff_',

        ),



when I do




    <?php 

    var_dump(Yii::app()->staffUser->id); //not being recognized

    var_dump(Yii::app()->user->id);

     ?>



at the view file, the staffUser->id always returns null and then the user->id returns the id regardless whether the loggedin person is a ‘user’ from my members table or a ‘staff’ from my staff table, why is that ?

I tried to override the CWebUser




<?php


class StaffUser extends CWebUser

{

    private $_model;

    

    

    

    public function getLoginName()

    {

        $user = $this->loadUser(Yii::app()->staffUser->id);

        return $user->LoginName;

    }

    

    public function getIsStaff()

    {

        return $this->getState('staff') !== null;

    }

    

    //load staff model

    protected function loadUser($id = null)

    {

        if($this->_model === null)

        {

            if($id !== null)

            {

                $this->_model = Wsmembersstaff::model()->findByPk($id);

            }

            return $this->_model;

        }

    }

}




?>



with the model




class Wsmembersstaff extends CActiveRecord

{

	

	const YES = 1;

	const NO = 0;


	/**

	 * Returns the static model of the specified AR class.

	 * @return Wsmembersstaff the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

	



then I added this at the config file




	// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>true,

            'loginUrl'=>array('companies/login'),

		),

        'staffUser'=>array(

            'class'=>'StaffUser',

            'allowAutoLogin'=>true,

            //'loginUrl'=>array('companies/login'),

            'stateKeyPrefix'=>'staff_',

        ),



when I var_dumped it at the main layout,




        var_dump(Yii::app()->user->id);

        var_dump(Yii::app()->staffUser->id);



still the staffUser->id returns null while the user->id is is outputing either the

‘user’ id or the ‘staffUser’ id

if i`m in right you have two login ways one is usually user and the second one is staff user, and bouth of them using same CWebuseridenty, you need to explain litle bit more you geting two users??? ,

class CWebUser extends CApplicationComponent implements IWebUser,


   'allowAutoLogin'=>true,

wich setup a kookie , and colect your users just setup $this->allowAutoLogin to false and you will not have problems ,

EDIT: and clear your browser cookies

you’re right, I have two login ways,

  1. site/login

  2. companies/login

but based from the codes I pasted above, I already created the required things in order to set this




	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>false,

            'loginUrl'=>array('companies/login'),

		),

        'staffUser'=>array(

            'class'=>'StaffUser',

            'allowAutoLogin'=>false,

            //'loginUrl'=>array('companies/login'),

            'stateKeyPrefix'=>'staff_',

        ),



but still it didn’t helped,or am I still missing something?

one more question did you using same tables ? and same ids for companies

I am using different tables

‘user’ is from members table

‘company’ is from company table

‘staff’ is from staff table

company table has memberID

staff table has companyID

that’s how I linked them them

problem is when you log in your logins using same interfaces, ok you dont need than

‘visible’=>array(array("!Yii::app()->user->isGuest"=>true),array("!Yii::app()->staffUser->isGuest"=>true)

and you have to setup menu like this,





 <?php $this->widget('zii.widgets.CMenu',array(

                        'items'=>array(

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

                                array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index')),

}

                        

                        ),

                )); ?>




I dont really understand why you have two different users, is the ‘roles’ approach not enough?

Or even the one explained by jacmoe

I did your suggestion and added it




		<?php $this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				array('label'=>'Home', 'url'=>array('/site/index')),

				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about'),'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Contact', 'url'=>array('/site/contact'),'visible'=>Yii::app()->user->isGuest),

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

                array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index')),

                }   

               

				array('label'=>'Companies', 'url'=>array('/companies/index'),'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),

				array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest),

			),

		)); ?>



am getting a syntax error unexpected ‘T_IF’ expecting ‘)’

it didn’t get solved even if i did




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

                array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index')),

                }   



am not sure if it’s right to place an if statement inside an array? , or am really just missing some basic

php closing parenthesis ?

are you referring to the RBAC ? , i didn’t implement it ( i don’t know how to, for now )

i was given a man user table , and a table where staffs should be placed , that’s why, now am struggling

This was my suggestion - i have used this successfully in some applications where admin users and frontend users where completely unrelated but still an admin should be able to login to backend and frontend independently.

i`ve building same websites on difrent servers and i test on one and i see other site

do you still have the other fragments of your example ?

sassori why dont you use only one table users wich have column usertype, and fill them with user or admin,

and then call logins based on database criteria condition

From your example i don’t really understand what you try to do. I configured 2 different users for 2 different parts of my application (backend + frontend). Your example looks like you want to mix 2 users somehow in the same section of your application. Otherwhise i can’t see why you would have such problems.

hmmn ok, I’ll take note of that.

yep, am trying to do that, because my staff table doesn’t have the same columns as the member’s table

and they are not allowed to create a company, that’s why I want the ‘mycompanies’ link to disappear

‘if and only if’ at the default home page mode and the staff is logged in

Still don’t really understand. But if it’s about conditions, you can simply combine guest states:




// condition that requires both types of user to be logged in:

(!Yii::app()->user->isGuest && !Yii::app()->staffUser->isGuest) 


// condition that requires "normal" user to be logged in and staff user to be not logged in:

(!Yii::app()->user->isGuest && Yii::app()->staffUser->isGuest)

With these condidtions you can control visibility of all kinds of things in your view.