Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class User extends DataMapper {
- public $table = "users";
- public function __construct()
- {
- parent::__construct();
- $this->load->config('auth');
- $this->load->helper('string');
- $this->_default_rounds = $this->config->item('default_rounds');
- $this->_random_rounds = $this->config->item('random_rounds');
- $this->_min_rounds = $this->config->item('min_rounds');
- $this->_max_rounds = $this->config->item('max_rounds');
- $this->_password_recovery_key_expire = $this->config->item('password_recovery_key_expire');
- if ($this->_random_rounds)
- {
- $rand = rand($this->_min_rounds,$this->_max_rounds);
- $rounds = array('rounds' => $rand);
- }
- else
- {
- $rounds = array('rounds' => $this->_default_rounds);
- }
- // Load Bcrypt
- $this->load->library('bcrypt', $rounds);
- }
- /**
- * email_exists
- *
- * Checks if given email exists
- *
- * @param string $email
- * @return bool
- */
- function email_exists($email)
- {
- $this->where('email', $email)->get();
- if ($this->exists())
- {
- return TRUE;
- }
- }
- /**
- * check_password
- *
- * This function takes a password and validates it
- * against an entry in the users table.
- *
- * @param string $password The password the user provides
- * @param string $hashed_password The hashed password stored in the database
- * @return bool
- */
- function check_password($password, $hashed_password)
- {
- $CI =& get_instance();
- return $CI->bcrypt->verify($password, $hashed_password);
- }
- /**
- * change_password
- *
- * @param int $user_id
- * @param string $password
- * @return bool
- */
- function change_password($user_id, $password)
- {
- $this->where('id', $user_id)->get();
- $this->password = self::_encrypt($password);
- if ($this->save())
- {
- return TRUE;
- }
- }
- /**
- * is_valid_activation_key
- *
- * @param string $key
- * @return bool
- */
- function is_valid_activation_key($key)
- {
- $this->get_by_activation_key($key);
- if ($this->exists())
- {
- return TRUE;
- }
- }
- /**
- * set_activation_key
- *
- * @param int $user_id
- * @return bool
- */
- function set_activation_key($user_id)
- {
- $this->where('id', $user_id);
- $this->activation_key = self::_generate_unique_str();
- if ($this->save())
- {
- return TRUE;
- }
- }
- /**
- * is_activated
- *
- * @param int $user_id
- * @return bool
- */
- function is_activated($user_id)
- {
- $this->where('id', $user_id)->get();
- if ($this->activated)
- {
- return TRUE;
- }
- }
- /**
- * activate
- *
- * @param string $key
- * @return bool
- */
- function activate($key)
- {
- if (self::is_valid_activation_key($key))
- {
- $this->where('activation_key', $key)->get();
- $this->activated = TRUE;
- $this->activation_key = NULL; // Remove activation key
- if ($this->save())
- {
- return TRUE;
- }
- }
- }
- /**
- * is_valid_password_recovery_key
- *
- * @param int $user_id
- * @param string $key
- * @return bool
- */
- function is_valid_password_recovery_key($user_id, $key)
- {
- $this->where('id', $user_id);
- $this->where('recover_password_key', $key);
- $this->where('recover_password_key_requested >', time() - $this->_password_recovery_key_expire);
- $this->get();
- if ($this->exists())
- {
- return TRUE;
- }
- }
- /**
- * set_password_recovery_key
- *
- * If the current recovery key isn't old enough
- * only update when this function was last
- * requested to prevent sabotage
- *
- * @param string $email
- */
- function set_password_recovery_key($email)
- {
- $this->where('email', $email)->get();
- if ($this->recover_password_key_requested < time() - $this->_password_recovery_key_expire){
- $this->recover_password_key = self::_generate_unique_str();
- $this->recover_password_key_requested = time();
- }else{
- //$this->recover_password_key = $this->recover_password_key;
- $this->recover_password_key_requested = time();
- }
- if ($this->save())
- {
- return TRUE;
- }
- }
- /**
- * reset_password
- *
- * @param int $user_id
- * @param string $key
- * @param string $password
- * @return bool
- */
- function reset_password($user_id, $key, $password)
- {
- if (self::is_valid_password_recovery_key($user_id, $key))
- {
- $this->password = self::_encrypt($password);
- $this->recover_password_key = NULL;
- $this->recover_password_key_requested = NULL;
- if ($this->save())
- {
- return TRUE;
- }
- }
- }
- /**
- * login
- *
- * @param string $username
- * @param string $password
- * @return bool
- */
- function login($username, $password)
- {
- $CI =& get_instance();
- $this->where('username', $username)->get();
- if ($this->exists() && self::check_password($password, $this->password))
- {
- $this->last_login = time();
- $this->save();
- return TRUE;
- }
- }
- /**
- * register
- *
- * @param mixed $data
- * @param bool $require_activation
- * @return bool
- */
- function register($data, $require_activation)
- {
- $CI =& get_instance();
- $this->username = $data['username'];
- $this->password = self::_encrypt($data['password']);
- $this->email = $data['email'];
- $this->first_name = $data['first_name'];
- $this->last_name = $data['last_name'];
- $this->activated = $require_activation === TRUE ? FALSE : TRUE;
- $this->activation_key = $require_activation === TRUE ? self::_generate_unique_str() : NULL;
- $this->ip_address = $this->_ip_address;
- $this->created_on = time();
- if ($this->save())
- {
- return TRUE;
- }
- }
- /**
- * _generate_unique_key
- *
- * Generates a unique string (32 chars in lenght)
- *
- * @return string
- */
- function _generate_unique_str()
- {
- return random_string('unique');
- }
- /**
- * _encrypt
- *
- * @param string $password
- */
- function _encrypt($password)
- {
- $CI =& get_instance();
- return $CI->bcrypt->hash($password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement