Created
July 29, 2013 11:59
-
-
Save nirleka/6103849 to your computer and use it in GitHub Desktop.
Example of UserIdentity and WebUser in Yii
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserIdentity extends CUserIdentity { | |
private $_username; // username | |
private $_role; // nama user | |
private $_id; // id user | |
private $_statusPenyuluh; // Status penyuluh | |
public function authenticate() { | |
$user = User::model()->findByAttributes(array('username'=>$this->username)); | |
if ($user === NULL) { | |
$this->errorCode = self::ERROR_USERNAME_INVALID; | |
return false; | |
} else { | |
if ($user->status == User::STATUS_ACTIVE){ | |
if (User::model()->getHashEngine()->CheckPassword($this->password . $user->password2, $user->password1)){ | |
$this->saveData($user); | |
$this->errorCode = self::ERROR_NONE; | |
return true; | |
} else{ | |
$this->errorCode = self::ERROR_PASSWORD_INVALID; | |
return false; | |
} | |
} else { | |
$this->errorCode = self::ERROR_UNKNOWN_IDENTITY; | |
return false; | |
} | |
} | |
} | |
public function saveData($user){ | |
$this->_username = $user->username; | |
$this->_role = $user->role; | |
$this->_id = $user->id; | |
$this->_statusPenyuluh = $user->checkPenyuluh(); | |
} | |
public function getUsername() { | |
return $this->_username; | |
} | |
public function getRole() { | |
return $this->_role; | |
} | |
public function getId() { | |
return $this->_id; | |
} | |
public function getPenyuluh() { | |
return $this->_statusPenyuluh; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WebUser extends CWebUser { | |
private $_model; | |
/** | |
* Overide login process | |
*/ | |
public function login($identity, $duration = 0) { | |
parent::login($identity, $duration); | |
$this->setUsername($identity->username); | |
$this->setRole($identity->role); | |
$this->setId($identity->id); | |
$this->setStatusPenyuluh($identity->penyuluh); | |
} | |
/* | |
* SETTER | |
*/ | |
public function setUsername($username) { | |
$this->setState('__username', $username); | |
} | |
public function setRole($role) { | |
$this->setState('__role', $role); | |
} | |
public function setId($id) { | |
$this->setState('__id', $id); | |
} | |
public function setStatuspenyuluh($penyuluh) { | |
$this->setState('__penyuluh', $penyuluh); | |
} | |
/* | |
* GETTER | |
*/ | |
public function getUsername() { | |
return $this->getState('__username'); | |
} | |
public function getRole() { | |
return $this->getState('__role'); | |
} | |
public function getId() { | |
return $this->getState('__id'); | |
} | |
public function getStatusPenyuluh() { | |
return $this->getState('__penyuluh'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment