Yii Framework Forum: [ASK] UserIdentity - Yii Framework Forum

Jump to content

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

[ASK] UserIdentity Bantuin saya kak :( Rate Topic: -----

#1 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 25 April 2012 - 01:05 AM

pas kita login kan nyimpen variable session untuk username,password dan ID kan, trs saya mau tambahin untuk LEVEL user yg login, tappi errorrr , :( kira2 knp yaa,

ini codenya.


UserIdentity.php

class UserIdentity extends CUserIdentity
{
    private $_id;
    private $level;      
    
    public function authenticate()
    {
        $record=Emp::model()->findByAttributes(array('email'=>$this->username));
        if($record===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($record->password!==$this->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$record->id_emp;
            $this->username = $record->fname;
            $this->level = $record->id_lev;
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
 
    public function getId()
    {
        return $this->_id;
    }
    
    public function getLevel()
    {
        return $this->level;
    }
}



view
 <h2><?php echo CHtml::encode(Yii::app()->user->name); ?></h2>
 <h2><?php echo CHtml::encode(Yii::app()->user->id); ?></h2>

//ini agagal untuk manggil user level nyaaa.
                    <h2><?php echo CHtml::encode(Yii::app()->user->level);?></h2>


ERRORnya : "Property "CWebUser.level" is not defined.", kira2 ada yang bisa bantu gak yah. dari pagi usaha sndiri tp gak kelar2 :( pemula bgt saya om.
0

#2 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 25 April 2012 - 03:00 AM

sementara saya buat kaya gini sih, gak tau ini bener apa engga :(

soalnya saya set session[id_lev] di site controller pas lg actionLogin, soalnya kalo mau set session di Useridentity gagal terus alias saya ga tau caranya :(

user identity
class UserIdentity extends CUserIdentity
{
    private $_id;
          
    
    public function authenticate()
    {
        $record=Emp::model()->findByAttributes(array('email'=>$this->username));
        if($record===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($record->password!==$this->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$record->id_emp;
            $this->username = $record->fname.' '.$record->lname;
           
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
 
    public function getId()
    {
        return $this->_id;
    }
 
}


siteController
public function actionLogin()
	{
		$model=new LoginForm;
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
                        {
//saya tambahin ini, jadi saya set session level/id_lev ini di sitecontroller
                            $id = yii::app()->user->id;
                            $load = $this->loadModel($id);

                            $session = yii::app()->session;
                            
                            $session["id_lev"]=$load->id_lev;
                            $this->redirect(Yii::app()->user->returnUrl);
                        }
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	//pake fungsi ini buat ambil dari db
        public function loadModel($id)
	{
		$model=Emp::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}


view
  <h2>id_emp <?php echo CHtml::encode(Yii::app()->user->id); ?></h2>
  <h2>ID LEV <?php echo CHtml::encode(Yii::app()->session["id_lev"]);?></h2>


session dengan nama id_lev berhasil tapi gak tau ini paling bener apa engga, kalo ada yang punya coding berbeda mohon di share ya siapa tau lebih baik :)
0

#3 User is offline   teguh11 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 13-November 11

Posted 25 April 2012 - 10:20 PM

di userIdentity aja set sessionnya.
class UserIdentity extends CUserIdentity
{
    private $_id;
    private $level;      
    
    public function authenticate()
    {
        $record=Emp::model()->findByAttributes(array('email'=>$this->username));
        if($record===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($record->password!==$this->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$record->id_emp;
            $this->username = $record->fname;
            Yii::app()->user->setState('level',$record->id_level); <-- coba diganti jadi gini nanti kalo mau panggil tinggal $user->level ato Yii::app()->user->getState("level")
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
 
    public function getId()
    {
        return $this->_id;
    }
    
    public function getLevel()
    {
        return $this->level;
    }
}


belajar itu mudah..
0

#4 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 26 April 2012 - 02:50 AM

View Postteguh11, on 25 April 2012 - 10:20 PM, said:

di userIdentity aja set sessionnya.
class UserIdentity extends CUserIdentity
{
    private $_id;
    private $level;      
    
    public function authenticate()
    {
        $record=Emp::model()->findByAttributes(array('email'=>$this->username));
        if($record===null)
            $this->errorCode=self::ERROR_USERNAME_INVALID;
        else if($record->password!==$this->password)
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        else
        {
            $this->_id=$record->id_emp;
            $this->username = $record->fname;
            Yii::app()->user->setState('level',$record->id_level); <-- coba diganti jadi gini nanti kalo mau panggil tinggal $user->level ato Yii::app()->user->getState("level")
            $this->errorCode=self::ERROR_NONE;
        }
        return !$this->errorCode;
    }
 
    public function getId()
    {
        return $this->_id;
    }
    
    public function getLevel()
    {
        return $this->level;
    }
}





hii teguh :)

thanks before ya

SOLVED dan dapet cara baru. :)
0

#5 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 26 April 2012 - 03:14 AM

View Postteguh11, on 25 April 2012 - 10:20 PM, said:


class UserIdentity extends CUserIdentity
{
        private $level;      
  
    public function getLevel()
    {
        return $this->level;
    }
}




function getLevel ini buat apa ya guh? soalnya gue delete gak ada pengaruhnya, tetep bisa jalan buat panggil sessionnya. penasaran jdnya :D
0

#6 User is offline   teguh11 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 13-November 11

Posted 01 May 2012 - 02:02 AM

View Postkfahmi, on 26 April 2012 - 03:14 AM, said:

function getLevel ini buat apa ya guh? soalnya gue delete gak ada pengaruhnya, tetep bisa jalan buat panggil sessionnya. penasaran jdnya :D


itu sih emang diapus aja..
soalnya ga kepake..
cuma kemaren lupa aja diapus..
LOL.. ;D

oiya itu yang userIdentity qo cuma username doang yang di cek?
$record=Emp::model()->findByAttributes(array('email'=>$this->username));

kalo misalnya usernamenya ada dua gimana?
belajar itu mudah..
0

#7 User is offline   mhdrasad 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 199
  • Joined: 20-April 11
  • Location:Bumi Indonesia

Posted 01 May 2012 - 08:29 PM

View Postteguh11, on 01 May 2012 - 02:02 AM, said:

oiya itu yang userIdentity qo cuma username doang yang di cek?
$record=Emp::model()->findByAttributes(array('email'=>$this->username));

kalo misalnya usernamenya ada dua gimana?


ikut nimbrung , :D
itu di yang di cek emailnya kali gan...
emailnya gak boleh sama (sok tau saya aja ini mah :P)
Wait For True Concept or Finish it With Coding Concept .
#chadnox
0

#8 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 01 May 2012 - 09:35 PM

View Postputrilangit, on 01 May 2012 - 08:29 PM, said:

ikut nimbrung , :D
itu di yang di cek emailnya kali gan...
emailnya gak boleh sama (sok tau saya aja ini mah :P)


iyak usernamenya diisi sama Email bro :D, iya sekarang jd gue kasih 2 array email dan password bro :D thanks udah diingetin. tapi pada halaman registrasi juga gue pake validasi kalo di DB udah terdaftar emailnya, dia ga bisa pake email itu lg :)
0

#9 User is offline   mrhands 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 03-February 12

Posted 09 May 2012 - 09:31 AM

Kalo ane pake ini nih..

<?php
class EWebUser extends CWebUser{
	const ADMIN=1;//konstanta ini untuk level
	const MEMBER=0;
	public function getLevel(){
		$user=User::model()->findByPk($this->id);
		if($user)
			return $user->LEVEL;
	}
	public function isAdmin(){
		if($this->getLevel()==EWebUser::ADMIN){
			return true;
		}else{
			return false;
		}
	}
}
?>


Jangan lupa diset di main confignya

// application components
	'components'=>array(
		'user'=>array(
			'class'=>'application.components.EWebUser',
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
		),



Nah untuk manggilnya tinggal
Yii::app()->user->getLevel();
YIi::app()->user->isAdmin();

CMIIW...
:) :)
0

#10 User is offline   teguh11 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 129
  • Joined: 13-November 11

Posted 09 May 2012 - 09:49 PM

wah mantap..
dari dulu ane pengen pake yang extends dari EWebUser. tapi ga ngerti caranya..
hahaha...

<?php
class EWebUser extends CWebUser{
        const ADMIN=1;//konstanta ini untuk level
        const MEMBER=0;
        public function getLevel(){
                $user=User::model()->findByPk($this->id);
                if($user)
                        return $user->LEVEL;
        }
        public function isAdmin(){
                if($this->getLevel()==EWebUser::ADMIN){
                        return true;
                }else{
                        return false;
                }
        }
}
?>

yang ini disave dimana ya??
belajar itu mudah..
0

#11 User is offline   kfahmi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 19-March 12

Posted 09 May 2012 - 10:04 PM

View Postmrhands, on 09 May 2012 - 09:31 AM, said:

Kalo ane pake ini nih..

<?php
class EWebUser extends CWebUser{
	const ADMIN=1;//konstanta ini untuk level
	const MEMBER=0;
	public function getLevel(){
		$user=User::model()->findByPk($this->id);
		if($user)
			return $user->LEVEL;
	}
	public function isAdmin(){
		if($this->getLevel()==EWebUser::ADMIN){
			return true;
		}else{
			return false;
		}
	}
}
?>




Jangan lupa diset di main confignya

// application components
	'components'=>array(
		'user'=>array(
			'class'=>'application.components.EWebUser',
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
		),



Nah untuk manggilnya tinggal
Yii::app()->user->getLevel();
YIi::app()->user->isAdmin();

CMIIW...
:) :)

kereen,, sering2 maen diforum ini jadi nambah ilmu :D
0

#12 User is offline   mrhands 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 03-February 12

Posted 09 May 2012 - 11:31 PM

View Postteguh11, on 09 May 2012 - 09:49 PM, said:

yang ini disave dimana ya??

Di components gan.. sama url yang di config
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