Skip to content

Commit

Permalink
Fixes #3607 - Adding create_user() and update_user() methods to ORM's…
Browse files Browse the repository at this point in the history
… Auth_User_Model.
  • Loading branch information
kiall committed Jan 15, 2011
1 parent 635b328 commit 3e874f7
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions classes/model/auth/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,76 @@ public function unique_key($value)
return Valid::email($value) ? 'email' : 'username';
}

/**
* Password validation for plain passwords.
*
* @param array $values
* @return Validation
*/
public static function get_password_validation($values)
{
return Validation::factory($values)
->rule('password', 'min_length', array(':value', 8))
->rule('password_confirm', 'matches', array(':validation', ':field', 'password'));
}

/**
* Create a new user
*
* Example usage:
* ~~~
* $user = ORM::factory('user')->create_user($_POST, array(
* 'username',
* 'password',
* 'email',
* );
* ~~~
*
* @param array $values
* @param array $expected
* @throws ORM_Validation_Exception
*/
public function create_user($values, $expected)
{
// Validation for passwords
$extra_validation = Model_User::get_password_validation($values)
->rule('password', 'not_empty');

return $this->values($values, $expected)->create($extra_validation);
}

/**
* Update an existing user
*
* [!!] We make the assumption that if a user does not supply a password, that they do not wish to update their password.
*
* Example usage:
* ~~~
* $user = ORM::factory('user')
* ->where('username', '=', 'kiall')
* ->find()
* ->update_user($_POST, array(
* 'username',
* 'password',
* 'email',
* );
* ~~~
*
* @param array $values
* @param array $expected
* @throws ORM_Validation_Exception
*/
public function update_user($values, $expected = NULL)
{
if (empty($values['password']))
{
unset($values['password'], $values['password_confirm']);
}

// Validation for passwords
$extra_validation = Model_User::get_password_validation($values);

return $this->values($values, $expected)->update($extra_validation);
}

} // End Auth User Model

0 comments on commit 3e874f7

Please sign in to comment.