Yii Framework Forum: Login Memlihi Level - Yii Framework Forum

Jump to content

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

Login Memlihi Level Rate Topic: -----

#1 User is offline   kacung 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-June 13

Posted 29 July 2013 - 04:01 AM

kak mau tanya tetang login misalnya gini
form pada login
terlmapir.,
bagaimana supaya user ketika dia login dan menginput level misalnya 1:
di file userindentity.php ketika usernya memlih level 1 maka username dan password di cek di tabel1 blabla
ketika level maka username dan password di cek di tabel2 blabla.

sya bingun ini contoh skripsinya pada UserIdentity.php kurang lebih sama kasusnya:
UserIdentity.php

Quote

<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
  private $_id;
	private $_level;
	
	public function authenticate()
	{
		switch($this->_level){ // cek level saat login
			case 0: // kalau yang login mahasiswa
				$mhs = Mahasiswa::model()->findByAttributes(array('nim' => $this->username));
				if($mhs === null){
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if($mhs->password !== $this->password){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						$this->_id = $mhs->id_mahasiswa;
						if(null === $mhs->last_login){
							$last_login = time();
						}else{
							$last_login = strtotime($mhs->last_login);
						}
						$this->setState('last_login', $last_login); // set session untuk waktu login terakhir
						$this->setState('login_as', 'mhs'); // set session untuk login sebagai
						$this->setState('nama', $mhs->nama);
						$this->errorCode = self::ERROR_NONE;
					}				
				}
				break;
			case 1: // kalau yang login mentor
				$mentor = Mentor::model()->findByAttributes(array('nim' => $this->username));
				if($mentor === null){
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if($mentor->password !== $this->password){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						$this->_id = $mentor->id_mentor;
						if(null === $mentor->last_login){
							$last_login = time();
						}else{
							$last_login = strtotime($mentor->last_login);
						}
						$this->setState('last_login', $last_login);
						$this->setState('login_as', 'mentor');
						$this->setState('nama', $mentor->nama);
						$this->errorCode = self::ERROR_NONE;
					}
				}
				break;
			case 2: // kalau yang login dosen pembimbing
				$dosen = Pembimbing::model()->findByAttributes(array('nama' => $this->username));
				if($dosen === null){
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if($dosen->password !== $this->password){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						$this->_id = $dosen->id_pembimbing;
						if(null === $dosen->last_login){
							$last_login = time();
						}else{
							$last_login = strtotime($dosen->last_login);
						}
						$this->setState('last_login', $last_login);
						$this->setState('login_as', 'dosen');
						$this->setState('nama', $dosen->nama);
						$this->errorCode = self::ERROR_NONE;
					}
				}
				break;
			case 3: //kalau yang login admin
				$user = User::model()->findByAttributes(array('nama' => $this->username));
				if($user === null){
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if($user->password !== $this->password){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						$this->_id = $user->id_user;
						if(null === $user->last_login){
							$last_login = time();
						}else{
							$last_login = strtotime($user->last_login);
						}
						$this->setState('last_login', $last_login);
						$this->setState('login_as', 'admin');
						$this->setState('nama', $user->nama);
						$this->errorCode = self::ERROR_NONE;
					}
				}
				break;
			case 4: //kalau yang login koordinator
				$koordinator = Koordinator::model()->findByAttributes(array('nama' => $this->username));
				if($koordinator === null){
					$this->errorCode = self::ERROR_USERNAME_INVALID;
				}else{
					if($koordinator->password !== $this->password){
						$this->errorCode = self::ERROR_PASSWORD_INVALID;
					}else{
						$this->_id = $koordinator->id_koordinator;
						if(null === $koordinator->last_login){
							$last_login = time();
						}else{
							$last_login = strtotime($koordinator->last_login);
						}
						$this->setState('last_login', $last_login);
						$this->setState('login_as', 'koordinator');
						$this->setState('nama', $koordinator->nama);
						$this->errorCode = self::ERROR_NONE;
					}
				}
				break;
			default:
				break;
		}
		return !$this->errorCode;
	}
	
	public function getId(){
		return $this->_id;
	}
	
	// ini untuk mengeset level login saat sebelum authenticate
	public function setLevel($level){
		$this->_level = $level;
	}
}




file model yang punya saya LoginForm.php

Quote

<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $username;
	public $password;
	public $rememberMe;
        public $level;
        // public $usertype;
	private $_identity;

       /* public function _construct($arg='Fornt'){
            $this->usertype = $arg;
        }*/

        /**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// username and password are required
			array('username, password, level', 'required'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean'),
			// password needs to be authenticated
			array('password', 'authenticate'),
                   // array('status', 'authenticate'),
                    
                        
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'rememberMe'=>'Remember me next time',
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())
		{
			$this->_identity=new UserIdentity($this->username,$this->password,$this->level);
                       // $this->_identity->userType = $this->usertype;
			if(!$this->_identity->authenticate())
				$this->addError('password','Incorrect username or password.');
		}
                
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if($this->_identity===null)
		{
			$this->_identity=new UserIdentity($this->username,$this->password,  $this->level);
                     
			$this->_identity->authenticate();
		}
                
               /* if($this->_identity->authenticate());
switch($identity->errorCode)
{
    case UserIdentity::ERROR_NONE:
        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
        Yii::app()->user->login($identity,$duration);
        break;
    case UserIdentity::ERROR_USERNAME_INVALID:
        $this->addError('username','Username is incorrect.');
        break;
    default: // UserIdentity::ERROR_PASSWORD_INVALID
        $this->addError('password','Password is incorrect.');
        break;
}*/
		if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
		{
			$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user ->login($this->_identity,$duration);
			return true;
		}
		else
			return false;
	}
}



ini file form login nya
login.php

Quote

<?php
/* @var $this SiteController */
/* @var $model LoginForm */
/* @var $form CActiveForm  */

$this->pageTitle=Yii::app()->name . ' - Login';
$this->breadcrumbs=array(
	'Login',
);
?>

<h1>Login</h1>

<p>Please fill out the following form with your login credentials:</p>

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'login-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<div class="row">
		<?php echo $form->labelEx($model,'username'); ?>
		<?php echo $form->textField($model,'username'); ?>
		<?php echo $form->error($model,'username'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'password'); ?>
		<?php echo $form->passwordField($model,'password'); ?>
		<?php echo $form->error($model,'password'); ?>
		<p class="hint">
			Hint: You may login with <kbd>demo</kbd>/<kbd>demo</kbd> or <kbd>admin</kbd>/<kbd>admin</kbd>.
		</p>
	</div>
        <div class="row">
            <?php echo $form->labelEx($model,'level'); ?>
		<?php echo $form->textField($model,'level'); ?>
		<?php echo $form->error($model,'level'); ?>
            <?php
            
            /*echo $form->labelEx($model,'status');
              echo $form->dropDownList(
                        $model,'status',array('1'=>'Advertise','2'=>'Publisher','3'=>'Administrator'),
                        //CHtml::listData($model,'status',array('1','Advertise','2','Publisher','3','Administrator')),
                        array('empty'=>''));
                echo $form->error($model,'status');*/
             ?>
           
        </div>

	<div class="row rememberMe">
		<?php echo $form->checkBox($model,'rememberMe'); ?>
		<?php echo $form->label($model,'rememberMe'); ?>
		<?php echo $form->error($model,'rememberMe'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton('Login'); ?>
	</div>

<?php $this->endWidget(); ?>
</div><!-- form -->



nah pertanyanya bagaimana ya suapaya bs di varibel level tersebut apakha berisi
1,2,3 dan jika 1 maka di cek di tabel1 blabla,... jika 2 maka dicek ditabel2 blabla..,

mohon bantunya kak terimkasih.,

Attached File(s)


0

#2 User is offline   daudtm 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-April 13
  • Location:Malang

Posted 30 July 2013 - 08:08 PM

View Postkacung, on 29 July 2013 - 04:01 AM, said:

bagaimana supaya user ketika dia login dan menginput level misalnya 1:
di file userindentity.php ketika usernya memlih level 1 maka username dan password di cek di tabel1 blabla
ketika level maka username dan password di cek di tabel2 blabla.

nah pertanyanya bagaimana ya suapaya bs di varibel level tersebut apakha berisi
1,2,3 dan jika 1 maka di cek di tabel1 blabla,... jika 2 maka dicek ditabel2 blabla..,

mohon bantunya kak terimkasih.,


yg dibingungin di mana? contoh codenya udah bener. cukup seperti itu mestinya sudah jalan,
apa ada pesan error?
kalo saya saranin, biar lebih user friendly, mending level jangan dijadikan input textbox, tapi listbox
contoh:
<?php 
...[snip]...
  echo $form->dropDownList($model, 'level', array('mahasiswa', 'mentor', 'admin', 'koordinator', dst..) );
...[snip]..


atau lebih ideal lagi, seharusnya user cukup disimpan di 1 tabel saja, table user, tapi nanti profil dan rolenya bisa diset jadi mahasiswa,mentor, admin, dll.
0

#3 User is offline   toor 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-August 13

Posted 26 August 2013 - 08:00 PM

betul, mending di 1 tabel saja, di accessRule nya di pilah2 masing2 role
user juga tidak perlu input role saat loginnya
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