Skip to content

Instantly share code, notes, and snippets.

@ykurnia
Created April 30, 2020 11:09
Show Gist options
  • Save ykurnia/ddb7f41b0cd846ef091dbb77821d1eef to your computer and use it in GitHub Desktop.
Save ykurnia/ddb7f41b0cd846ef091dbb77821d1eef to your computer and use it in GitHub Desktop.
Modified UserIdentity.php from Yii2 template, to use session instead of token, and reduce connection to database
<?php
namespace app\models;
class UserIdentity extends \yii\base\BaseObject implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public $idOrg; // company ID
public $idBranch; // branch ID
public $idProfile; // profile ID
public $profileType; // profile type
public $idBidang;
public $idSection;
public $activeUser = null;
private static $users = []; // change, using database and session
/* create array for user identity data
// input : either User database, or froom this database
// --- if $self is true, $obj is UserIdentity, else $obj is database model of User class
*/
public static function getUserIdentityArray($obj,$self = false)
{
if ($obj != null) {
if ($self) {
$user = ['id' => $obj->id, 'username' => $obj->username, 'idOrg' => $obj->idOrg, 'idProfile' => $obj->idProfile, 'profileType' => $obj->profileType, 'password' => $obj->password, 'authKey' => '', 'accessToken' => '', 'idBranch' => $obj->idBranch, 'idBidang' => $obj->idBidang, 'idSection' => $obj->idSection];
}
else {
//$user = ['id' => $obj->id, 'username' => $obj->user_name, 'idOrg' => $obj->id_company, 'idProfile' => $obj->id_profile, 'profileType' => $obj->profileType, 'password' => $obj->pwd, 'authKey' => '', 'accessToken' => '', 'idBranch' => $obj->id_branch, 'idBidang' => $obj->id_bidang, 'idSection' => $obj->id_section];
$user = ['id' => $obj->id, 'username' => $obj->user_name, 'idOrg' => $obj->id_company, 'idProfile' => $obj->id_profile, 'profileType' => $obj->profileType, 'password' => $obj->pwd, 'authKey' => '', 'accessToken' => '', 'idBranch' => $obj->id_branch];
}
return $user;
}
return null;
}
/**
* find identity using ID
*/
public static function findIdentity($id)
{
if ($id != '')
{
/*
$data = User::findOne($id);
if ($data) {
$user = ['id' => $data->id, 'username' => $data->user_name, 'idOrg' => $data->id_company, 'password' => $data->pwd, 'authKey' => '', 'accessToken' => ''];
return new static($user);
}
*/
$obj = \Yii::$app->session->get(\Yii::$app->params['userSession']);
if ($obj) return new static (self::getUserIdentityArray($obj, true));
}
return null;
// return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* find identity using TOKEN
*/
public static function findIdentityByAccessToken($token, $type = null)
{
/*
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
*/
// need to change using database
return null;
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
/*
foreach (self::$users as $user) {
if (strcasecmp($user['username'], $username) === 0) {
return new static($user);
}
}
*/
// change using database
// check session first
$obj = \Yii::$app->session->get(\Yii::$app->params['userSession']);
if ($obj) {
return new static (self::getUserIdentityArray($obj,true));
}
// not in session, get database
$data = User::find()->where(['user_name'=>$username, 'status'=>'active'])->one();
if ($data)
{
return new static (self::getUserIdentityArray($data,false));
}
return null;
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getIdOrg()
{
return $this->idOrg;
}
/**
* @inheritdoc
*/
public function getIdProfile()
{
return $this->idProfile;
}
/**
* @inheritdoc
*/
public function getIdBidang()
{
return $this->idBidang;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === md5($password);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment