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: 10
  • 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

#2 User is offline   mrkoeh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 31-October 12
  • Location:Indonesia

Posted 12 February 2015 - 01:57 AM

Kalo ane biasanya ngrubah di UserIdentity, disana kita bikin role berdasar user yang login.
Kemudian di menu kita bisa akali dengan menampilkan data berdasar session dari tiap user yang login

kurang lebih seperti ini :
useridentity.php
	public function authenticate()
	{
		
		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
		if($user===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if(!$user->validatePassword($this->password))
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else
		{
				$otoritas_id = $this->username = $user->otoritas_id;
				
				Yii::app()->session['otoritas_id']=$otoritas_id;

				switch ($user->otoritas_id) {
						case 1: $otoritas = '1'; break;
						case 2: $otoritas = '2'; break;
						case 3: $otoritas = '3'; break;
						case 4: $otoritas = '4'; break;
						case 5: $otoritas = '5'; break;
						case 6: $otoritas = '6'; break;
						case 99: $otoritas = '99'; break;
						default:$otoritas = '';  break;
				}
				$this->setState('otoritas', $otoritas);
				$this->setState('otoritas_id', $user->otoritas_id);
				$this->errorCode=self::ERROR_NONE;
		}
		return !$this->errorCode;
	}


Kemudian di menu :

$otoritas_id = Yii::app()->session['otoritas_id'];
if($otoritas_id=="1" || $otoritas_id=="99"){ // Direktur dan Administrator

.... // tampilkan menu

}else{

....// tampilkan menu

}


kurang lebih begitu,semoga bisa memberikan jalan keluar CMIIW
0

#3 User is offline   bayu444 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-January 17

Posted 18 January 2017 - 10:33 AM

View Postmrkoeh, on 12 February 2015 - 01:57 AM, said:

Kalo ane biasanya ngrubah di UserIdentity, disana kita bikin role berdasar user yang login.
Kemudian di menu kita bisa akali dengan menampilkan data berdasar session dari tiap user yang login

kurang lebih seperti ini :
useridentity.php
	public function authenticate()
	{
		
		$user=User::model()->find('LOWER(username)=?',array(strtolower($this->username)));
		if($user===null)
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		else if(!$user->validatePassword($this->password))
			$this->errorCode=self::ERROR_PASSWORD_INVALID;
		else
		{
				$otoritas_id = $this->username = $user->otoritas_id;
				
				Yii::app()->session['otoritas_id']=$otoritas_id;

				switch ($user->otoritas_id) {
						case 1: $otoritas = '1'; break;
						case 2: $otoritas = '2'; break;
						case 3: $otoritas = '3'; break;
						case 4: $otoritas = '4'; break;
						case 5: $otoritas = '5'; break;
						case 6: $otoritas = '6'; break;
						case 99: $otoritas = '99'; break;
						default:$otoritas = '';  break;
				}
				$this->setState('otoritas', $otoritas);
				$this->setState('otoritas_id', $user->otoritas_id);
				$this->errorCode=self::ERROR_NONE;
		}
		return !$this->errorCode;
	}


Kemudian di menu :

$otoritas_id = Yii::app()->session['otoritas_id'];
if($otoritas_id=="1" || $otoritas_id=="99"){ // Direktur dan Administrator

.... // tampilkan menu

}else{

....// tampilkan menu

}


kurang lebih begitu,semoga bisa memberikan jalan keluar CMIIW

Contoh script nya "... // tampilkan menu" itu gimana ya gan? terimakasih. maaf newbie.

trus scirip yang bawah itu diletakkan di menu, mksdnya menu yg mana 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