Yii Framework Forum: Login Priveledge - Yii Framework Forum

Jump to content

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

Login Priveledge Rate Topic: -----

#1 User is offline   wawan_blitar 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 16-September 14

Posted 08 January 2015 - 07:43 AM

mohon bantuannya dong jadi aku punya kasus nih

gimana sih buat menu visible untuk yang berhak saja jadi didalam model User scriptnya seperti ini :

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

dan di main layoutnya yaitu aku buat seperti ini
<?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'))/**(Yii::app()->user->name=='admin'|Yii::app()->user->name=='demo')*/, '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)
            ),
        ),
    ),
)); ?>


terlihat jelas bahwa awalnya mencoba untuk hak akses berdasarkan nama user dan aku ingin merubahnya menjadi berdasarkan role yaitu yang mempunyai role 'Operator' yang berhak melihat menu itu jadi aku buat method tambahan yaitu isRole untuk cek apakah user yang login mempunyai role 'Operator' jadi aku buat component WebUser seperti dibawah ini :
<?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;
	}
}
?>


tetapi yang ada keluar error yaitu :

Trying to get property of non-object dan errornya di script ini
14        if($user->PRIVELEDGE === $RoleParam){
15             return true;
16         }else{
17             return false;
18         }

ini solusinya gimana ya?
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