Help me, I want to make my menu visible for user wich have priveledge Operator and my table like this
|ID|USERNAME|PASSWORD|PRIVELEDGE|SUPERUSER|
|1 |Admin |test |Operator | 1 |
|2 |Test |test |Testing | 0 |
User.php
<?php
class User extends CActiveRecord
{
//digunakan untuk memproses data setelah di validasi
protected function afterValidate(){
parent::afterValidate();
//lakukan enskripsi pada password yang di input
$this->PASSWORD=$this->encrypt($this->PASSWORD);
}
//membuat function untuk mengenkripsi data
public function encrypt($value){
return md5($value);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'TBL_USER';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('USERNAME, PASSWORD, PRIVELEDGE', 'required'),
array('SUPERUSER', 'numerical'),
array('USERNAME, PASSWORD', 'length', 'max'=>32),
array('PRIVELEDGE', 'length', 'max'=>13),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('ID, USERNAME, PASSWORD, PRIVELEDGE, SUPERUSER', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'ID' => 'ID',
'USERNAME' => 'Username',
'PASSWORD' => 'Password',
'PRIVELEDGE' => 'Priveledge',
'SUPERUSER' => 'Superuser',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('ID',$this->ID);
$criteria->compare('USERNAME',$this->USERNAME,true);
$criteria->compare('PASSWORD',$this->PASSWORD,true);
$criteria->compare('PRIVELEDGE',$this->PRIVELEDGE,true);
$criteria->compare('SUPERUSER',$this->SUPERUSER);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return User the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
}
I used gii to make CRUD and it’s work, so I decide to make my menu visible for Operator priveledge only so I make new method for Yii::app()->user-> called isRole(‘RoleName’). so I make new component called WebUser.
WebUser.php:
<?php
// this file must be stored in:
// protected/components/WebUser.php
class WebUser extends CWebUser {
// Store model to not repeat query.
private $_model;
// This is a function that checks the field 'role'
// in the User model to be equal to 1, that means it's admin
// access it by Yii::app()->user->isAdmin()
function isRole($RoleParam){
$user = $this->loadUser(Yii::app()->user->name);
if($user->PRIVELEDGE === $RoleParam){
return true;
}else{
return false;
}
}
// Load user model.
protected function loadUser($name=null)
{
if($this->_model===null)
{
if($name!==null)
$this->_model=User::model()->findByAttributes(array('USERNAME'=>$name));
}
return $this->_model;
}
}
?>
but when I tested, there’s an error in line if($user->PRIVELEDGE === $RoleParam) like this :
Trying to get property of non-object
and this is the menu main.php in layout view:
<?php $this->widget('bootstrap.widgets.TbNavbar',array(
'items'=>array(
array(
'class'=>'bootstrap.widgets.TbMenu',
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index'),'items'=>array(
'...',
array('label'=>'Level 1','url'=>'#'),
array('label'=>'Level One','url'=>'#', 'visible'=>(Yii::app()->user->isRole('Operator')), 'itemOptions'=>array('class'=>'dropdown-submenu'),
'items'=>array(
'...',
array('label'=>'Level One','url'=>'#',),
),
))),
//array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
//array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Manage User', 'url'=>array('/user/index'), 'visible'=>(Yii::app()->user->name=='super user')),
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)
),
),
),
)); ?>