Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Improving the code of UserBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Oct 1, 2015
1 parent 3134c41 commit 910b985
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions src/Model/Behavior/UserBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,30 +215,30 @@ protected function _emailVerification(EntityInterface &$entity, $options) {
* @return Entity
*/
protected function _beforeRegister(Entity $entity, $options = []) {
extract(Hash::merge($this->_config['register'], $options));
$options = Hash::merge($this->_config['register'], $options);

if ($this->_config['useUuid'] === true) {
$primaryKey = $this->_table->primaryKey();
$entity->{$primaryKey} = Text::uuid();
}

if ($userActive === true) {
if ($options['userActive'] === true) {
$entity->{$this->_field('active')} = true;
}

$this->_emailVerification($entity, $options);

if (!isset($entity->{$this->_field('role')})) {
$entity->{$this->_field('role')} = $defaultRole;
$entity->{$this->_field('role')} = $options['defaultRole'];
}

if ($generatePassword === true) {
$password = $this->generatePassword((int) $generatePassword);
if ($options['generatePassword'] === true) {
$password = $this->generatePassword();
$entity->{$this->_field('password')} = $password;
$entity->clear_password = $password;
}

if ($hashPassword === true) {
if ($options['hashPassword'] === true) {
$entity->{$this->_field('password')} = $this->hashPassword($entity->{$this->_field('password')});
}

Expand All @@ -254,7 +254,7 @@ protected function _beforeRegister(Entity $entity, $options = []) {
*/
public function findEmailVerified(Query $query, array $options) {
$query->where([
$this->alias() . '.email_verified' => true,
$this->alias() . '.' . $this->_field('emailVerified') => true,
]);
return $query;
}
Expand All @@ -268,7 +268,7 @@ public function findEmailVerified(Query $query, array $options) {
*/
public function findActive(Query $query, array $options) {
$query->where([
$this->alias() . '.active' => true,
$this->alias() . '.' . $this->_field('active') => true,
]);
return $query;
}
Expand Down Expand Up @@ -461,8 +461,20 @@ public function resetPassword(Entity $user) {
* @return string
*/
public function generatePassword($length = 8, $options = []) {
$options = $this->_passwordDictionary($options);
$password = '';

srand((double) microtime() * 1000000);
for ($i = 0; $i < $length; $i++) {
$password .=
$options['cons'][mt_rand(0, count($options['cons']) - 1)] .
$options['vowels'][mt_rand(0, count($options['vowels']) - 1)];
}

return substr($password, 0, $length);
}

public function _passwordDictionary(array $options = []) {
$defaults = [
'vowels' => [
'a', 'e', 'i', 'o', 'u'
Expand All @@ -473,25 +485,13 @@ public function generatePassword($length = 8, $options = []) {
'dr', 'ch', 'ph', 'wr', 'st', 'sp', 'sw', 'pr', 'sl', 'cl'
]
];

if (isset($options['cons'])) {
unset($defaults['cons']);
}

if (isset($options['vowels'])) {
unset($defaults['vowels']);
}

$options = Hash::merge($defaults, $options);
$password = '';

for ($i = 0; $i < $length; $i++) {
$password .=
$options['cons'][mt_rand(0, count($options['cons']) - 1)] .
$options['vowels'][mt_rand(0, count($options['vowels']) - 1)];
}

return substr($password, 0, $length);
return Hash::merge($defaults, $options);
}

/**
Expand Down Expand Up @@ -545,6 +545,18 @@ public function removeExpiredRegistrations($conditions = []) {
* @return boolean
*/
public function changePassword(Entity $entity) {
$this->_changePasswordValidation();
if ($entity->errors()) {
return false;
}
$entity->password = $this->hashPassword($entity->password);
if ($this->_table->save($entity, ['validate' => false])) {
return true;
}
return false;
}

protected function _changePasswordValidation() {
$validator = $this->_table->validator('default');
$validator->provider('userTable', $this->_table);
$validator->add('old_password', 'notBlank', [
Expand All @@ -557,14 +569,6 @@ public function changePassword(Entity $entity) {
'message' => __d('user_tools', 'Wrong password, please try again.')
]);
$this->_table->validator('default', $validator);
if ($entity->errors()) {
return false;
}
$entity->password = $this->hashPassword($entity->password);
if ($this->_table->save($entity, ['validate' => false])) {
return true;
}
return false;
}

/**
Expand All @@ -573,6 +577,7 @@ public function changePassword(Entity $entity) {
* @param mixed $value
* @param string $field
* @param mixed $context
* @return boolean
*/
public function validateOldPassword($value, $field, $context) {
$result = $this->_table->find('all', [
Expand Down Expand Up @@ -616,13 +621,17 @@ public function initPasswordReset($value, $options = []) {
$this->_table->alias() . '.' . $this->_field('username')
]
];

$options = Hash::merge($defaults, $this->_config['initPasswordReset'], $options);
$result = $this->_getUser($value, $options);

if (empty($result)) {
throw new RecordNotFoundException(__d('user_tools', 'User not found.'));
}

$result->{$this->_field('passwordToken')} = $this->generateToken($options['tokenLength']);
$result->{$this->_field('passwordTokenExpires')} = $this->expirationTime($options['expires']);

$this->_table->save($result, ['checkRules' => false]);
return $this->sendPasswordResetToken($result, [
'to' => $result->{$this->_field('email')}
Expand Down Expand Up @@ -678,7 +687,7 @@ protected function _getUser($value, $options = []) {
}

/**
* Sends a new password to an user by email
* Sends a new password to an user by email.
*
* Note that this is *not* a recommended way to reset an user password. A much
* more secure approach is to have the user manually enter a new password and
Expand Down

0 comments on commit 910b985

Please sign in to comment.