Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Difficulty Retrieving Encrypted Attributes From Db Into Model

$
0
0
Hi,

I have a table where I am storing driver license details. In the database, the field is encrypted using MySQL AES_ENCRYPT. However, I am having a difficulty in getting the un-encrypted values using models.
The approach I have tried follows:

class User extends CActiveRecord
{
public static function model($className=__CLASS__) {...}
public function tableName() {...}
public function rules() {...}
public function relations() {...}
public function attributeLabels() {...}
public function search() {...}

   protected function afterFind()
    {
        list($this->birth_year, $this->birth_month, $this->birth_day) = explode('-', $this->birthday);

        $this->origPassword = $this->password;
        $this->origDriversLicence = $this->user_drivers_license;
        $this->origDriversLicenseState = $this->user_drivers_license_state;
    }
    
    public function beforeSave()
    {
        if (!parent::beforeSave()) return false;

        $this->password = $this->password == $this->origPassword ?
            $this->password : generate_new_hash($this->password);

        $this->user_drivers_license = $this->user_drivers_license == $this->origDriversLicence ?
            $this->user_drivers_license : new CDbExpression("AES_ENCRYPT('$this->user_drivers_license','" . self::getEncryptionKey() . "')");

        $this->user_drivers_license_state = $this->user_drivers_license_state == $this->origDriversLicenseState ?
            $this->user_drivers_license_state : new CDbExpression("AES_ENCRYPT('$this->user_drivers_license_state','" . self::getEncryptionKey() . "')");

        return true;
    }

    public function defaultScope()
    {
        return array(
            'select'=>array(
                '*',
                "AES_DECRYPT('$this->user_drivers_license','" . self::getEncryptionKey() . "')" => 'user_drivers_license',
                "AES_DECRYPT('$this->user_drivers_license_state','" . self::getEncryptionKey() . "')" => 'user_drivers_license_state'
            ),
        );
    }
}


For some reason, the AES_DECRYPT part doesnt seem to get appended to the query and I end up with the encrypted version of the license. Any pointers in this direction would be greatly appreciated.

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>