Remove hash helper and use existing string helper instead

This commit is contained in:
Eike Foken
2011-09-21 01:08:43 +02:00
parent 11ee2107f6
commit 4439fa9dfe
11 changed files with 113 additions and 140 deletions

View File

@@ -56,7 +56,6 @@ class User extends CI_Model {
parent::__construct();
$this->load->config('auth', true);
$this->load->helper('cookie');
$this->load->helper('date');
$this->storeSalt = $this->config->item('store_salt', 'auth');
$this->saltLength = $this->config->item('salt_length', 'auth');
@@ -120,6 +119,32 @@ class User extends CI_Model {
return substr(sha1(uniqid(rand(), true)), 0, $this->saltLength);
}
/**
* Checks entered usernames.
*
* @param string $username
* @return boolean
*/
private function checkUsername($username = '') {
if (empty($username)) {
return false;
}
return $this->db->where('username', $username)->count_all_results('users') > 0;
}
/**
* Checks entered emails.
*
* @param string $email
* @return boolean
*/
private function checkEmail($email = '') {
if (empty($email)) {
return false;
}
return $this->db->where('email', $email)->count_all_results('users') > 0;
}
/**
* Changes the password of the given user.
*
@@ -130,7 +155,7 @@ class User extends CI_Model {
*/
public function changePassword($username, $old, $new) {
$query = $this->db->select('password, salt')
->where('username', $username)->limit(1)->get('users');
->where('username', $username)->limit(1)->get('users');
$result = $query->row();
@@ -149,32 +174,6 @@ class User extends CI_Model {
return false;
}
/**
* Checks entered usernames.
*
* @param string $username
* @return boolean
*/
public function checkUsername($username = '') {
if (empty($username)) {
return false;
}
return $this->db->where('username', $username)->count_all_results('users') > 0;
}
/**
* Checks entered emails.
*
* @param string $email
* @return boolean
*/
public function checkEmail($email = '') {
if (empty($email)) {
return false;
}
return $this->db->where('email', $email)->count_all_results('users') > 0;
}
/**
* Inserts a forgotten password key.
*
@@ -258,11 +257,11 @@ class User extends CI_Model {
* @param string $email
* @param array $additionalData
* @param string $groupName
* @return boolean
* @return mixed Returns the ID of the new user, or FALSE if the
* registration was unsuccessful.
*/
public function register($username, $password, $email, $additionalData = array(), $groupName = '') {
if ($this->checkUsername($username)) {
$this->access->setError('account_creation_duplicate_username');
return false;
}
@@ -280,7 +279,6 @@ class User extends CI_Model {
// users table
$data = array(
'id' => random_hash(16),
'username' => $username,
'password' => $password,
'email' => $email,
@@ -288,6 +286,10 @@ class User extends CI_Model {
'last_login' => now(),
);
do { // generate unique hash
$data['id'] = random_string('sha1', 16);
} while ($this->db->where('id', $data['id'])->count_all_results('users') > 0);
if ($this->storeSalt) {
$data['salt'] = $salt;
}
@@ -374,21 +376,6 @@ class User extends CI_Model {
return $this->get()->result_array();
}
/**
* Returns the number of users.
*
* @param mixed $group
* @return integer The number of users
*/
public function count($group = false) {
if (is_string($group)) {
$this->db->where('groups.name', $group);
} else if (is_array($group)) {
$this->db->where_in('groups.name', $group);
}
return $this->db->from('users')->count_all_results();
}
/**
* Gets a user by ID.
*
@@ -465,6 +452,21 @@ class User extends CI_Model {
return $query->row_array();
}
/**
* Returns the number of users.
*
* @param mixed $group
* @return integer The number of users
*/
public function count($group = false) {
if (is_string($group)) {
$this->db->where('groups.name', $group);
} else if (is_array($group)) {
$this->db->where_in('groups.name', $group);
}
return $this->db->from('users')->count_all_results();
}
/**
* Updates a user.
*
@@ -506,17 +508,6 @@ class User extends CI_Model {
return $this->db->affected_rows() > 0;
}
/**
* Deletes a specified user.
*
* @param string $id
* @return boolean Returns TRUE if the deletion was successful.
*/
public function delete($id) {
$this->db->delete('users', array('id' => $id));
return $this->db->affected_rows() > 0;
}
/**
* Updates a users last login time.
*
@@ -528,6 +519,17 @@ class User extends CI_Model {
return $this->db->affected_rows() == 1;
}
/**
* Deletes a specified user.
*
* @param string $id
* @return boolean Returns TRUE if the deletion was successful.
*/
public function delete($id) {
$this->db->delete('users', array('id' => $id));
return $this->db->affected_rows() > 0;
}
/**
* Logs a remembed user in.
*