/** * Performs environmental set-up similar to that in {@link ApplicationConfigBehavior} */ public static function setUpAppEnvironment($full = false) { // uses a specific key/iv for unit testing foreach (array('iv', 'key') as $ext) { $file = Yii::app()->basePath . "/config/encryption.{$ext}"; $testFile = Yii::app()->basePath . "/tests/data/encryption/encryption.{$ext}"; self::${$ext} = $file; if (file_exists($file)) { rename($file, "{$file}.bak"); copy($testFile, $file); } } EncryptedFieldsBehavior::setup(self::$key, self::$iv); if ($full) { self::setUpAppEnvironment2(); } }
/** * Override that uses a specific key/iv for unit testing */ public static function setUpBeforeClass() { parent::setUpBeforeClass(); EncryptedFieldsBehavior::setupUnsafe(); }
public static function setupUnsafe() { self::$encrypt = false; }
/** * Sets the encryption flag such that it accurately reflects the status of * data going into the database. * @param type $event */ public function beforeSave($event) { $encryptFlag = $this->encryptedFlagAttr; if ((bool) $encryptFlag) { $this->getOwner()->{$encryptFlag} = self::$encrypt; } parent::beforeSave($event); }
/** * Instantiates the encryption utility object so that components depending * on {@link EncryptedFieldsBehavior} can also be instantiated. */ public function cryptInit() { if (!$this->_cryptInit) { $key = $this->owner->basePath . '/config/encryption.key'; $iv = $this->owner->basePath . '/config/encryption.iv'; if (extension_loaded('openssl') && extension_loaded('mcrypt') && file_exists($key) && file_exists($iv)) { EncryptedFieldsBehavior::setup($key, $iv); } else { // Use unsafe method with encryption EncryptedFieldsBehavior::setupUnsafe(); } } }
/** * Tests transparent transition between encrypted and unencrypted */ public function testUnsafe() { $cred = $this->credentials('testUser'); $expectedAttributes = $cred->auth->attributes; EncryptedFieldsBehavior::setupUnsafe(); $cred->save(); $unencryptedJSON = Yii::app()->db->createCommand()->select('auth')->from('x2_credentials')->where('id=:id', array(':id' => $cred->id))->queryScalar(); $attributes = CJSON::decode($unencryptedJSON); $this->assertEquals($expectedAttributes, $attributes); }