public function testEncryptDecrypt() { $this->assertEquals(Cii::decrypt(Cii::encrypt(1)), 1); // Integer $this->assertEquals(Cii::decrypt(Cii::encrypt("1")), "1"); // String integer $this->assertEquals(Cii::decrypt(Cii::encrypt(3.14)), 3.14); // Float $this->assertEquals(Cii::decrypt(Cii::encrypt("3.14")), "3.14"); // String float $this->assertEquals(Cii::decrypt(Cii::encrypt("string")), "string"); // String // Test a variety of hashes of various sizes generated by Cii::generateSafeHash() $hash1 = Cii::generateSafeHash(4); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash1)), $hash1); $hash2 = Cii::generateSafeHash(16); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash2)), $hash2); $hash3 = Cii::generateSafeHash(32); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash3)), $hash3); $hash4 = Cii::generateSafeHash(64); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash4)), $hash4); $hash5 = Cii::generateSafeHash(128); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash5)), $hash5); $hash6 = Cii::generateSafeHash(256); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash6)), $hash6); $hash7 = Cii::generateSafeHash(512); $this->assertEquals(Cii::decrypt(Cii::encrypt($hash7)), $hash7); }
/** * Initiates the password reset process on behalf of the user * Generates a unique hash and an expiration time that the hash is valid up until (defaults to 15 minutes) * This key will internally expire (but not be expunged) after that time */ public function initPasswordResetProcess() { if (!$this->validate()) { return false; } $hash = Cii::generateSafeHash(); $expires = strtotime("+15 minutes"); $meta = UserMetadata::model()->findByAttributes(array('user_id' => $this->_user->id, 'key' => 'passwordResetCode')); if ($meta === NULL) { $meta = new UserMetadata(); } $meta->user_id = $this->_user->id; $meta->key = 'passwordResetCode'; $meta->value = $hash; $meta->save(); $meta = UserMetadata::model()->findByAttributes(array('user_id' => $this->_user->id, 'key' => 'passwordResetExpires')); if ($meta === NULL) { $meta = new UserMetadata(); } $meta->user_id = $this->_user->id; $meta->key = 'passwordResetExpires'; $meta->value = $expires; $meta->save(); $emailSettings = new EmailSettings(); $emailSettings->send($this->_user, Yii::t('ciims.email', 'Your Password Reset Information'), 'webroot.themes.' . Cii::getConfig('theme', 'default') . '.views.email.forgot', array('user' => $this->_user, 'hash' => $hash), true, true); // Set success flash Yii::app()->user->setFlash('success', Yii::t('ciims.controllers.Site', 'An email has been sent to {{email}} with further instructions on how to reset your password', array('{{email}}' => $this->email))); return true; }
/** * Sends an invite to a new user * @return boolean */ public function invite() { if (!$this->validate()) { return false; } $user = new Users(); $user->attributes = array('email' => $this->email, 'firstName' => null, 'lastName' => null, 'displayName' => null, 'password' => null, 'user_role' => 5, 'status' => Users::PENDING_INVITATION); // Create a new user, but bypass validation if ($user->save(false)) { $meta = new UserMetadata(); $meta->attributes = array('user_id' => $user->id, 'key' => 'invitationKey', 'value' => Cii::generateSafeHash()); // If the key was savedm send the email out if ($meta->save()) { $emailSettings = new EmailSettings(); $emailSettings->send($user, Yii::t('ciims.models.InvitationForm', "You've Been Invited..."), 'webroot.themes.' . Cii::getConfig('theme', 'default') . '.views.email.invite', array('user' => $user, 'hash' => $meta->value), true, true); return true; } $user->delete(); } return false; }
/** * Generates a new change key * @return boolean */ public function setNewEmailChangeKey() { $metadata = UserMetadata::model()->findByAttributes(array('user_id' => $this->_user->id, 'key' => 'newEmailAddressChangeKey')); if ($metadata == NULL) { $metadata = new UserMetadata(); $metadata->attributes = array('user_id' => $this->_user->id, 'key' => 'newEmailAddressChangeKey'); } // Generate a new key $metadata->value = Cii::generateSafeHash(); // Save the record if ($metadata->save()) { return $metadata->value; } throw new CHttpException(500, Yii::t('ciims.ProfileForm', 'Unable to save change key')); }
/** * Creates a new user, and sends the appropriate messaging out * @return boolean */ public function save($sendEmail = true) { if (!$this->validate()) { return false; } $this->_user = new Users(); // Set the model attributes $this->_user->attributes = array('email' => $this->email, 'password' => $this->password, 'username' => $this->username, 'user_role' => 1, 'status' => $sendEmail ? Users::PENDING_INVITATION : Users::ACTIVE); // If we saved the user model, return true if ($this->_user->save()) { // This class my be extended by other modules, in which case we don't need to send an activation form if we don't want need it to. if ($sendEmail) { $meta = new UserMetadata(); $meta->attributes = array('user_id' => $this->_user->id, 'key' => 'activationKey', 'value' => Cii::generateSafeHash()); $meta->save(); // Send the registration email $emailSettings = new EmailSettings(); $emailSettings->send($this->_user, Yii::t('ciims.email', 'Activate Your Account'), 'base.themes.' . Cii::getConfig('theme', 'default') . '.views.email.register', array('user' => $this->_user, 'hash' => $meta->value), true, true); } return true; } return false; }