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 Auth_mod extends CI_Model
- {
- /**
- * all database tables
- *
- * @var array
- */
- private $tables = array('users' => 'users', 'cookies' => 'cookies', 'forgotten_passwords' => 'forgotten_passwords');
- /**
- * construct function
- */
- public function __construct()
- {
- parent::__construct();
- $this->load->database();
- }
- /**
- * create a new user
- *
- * @param string $name
- * @param string $email
- * @param string $password
- */
- public function create_user($name, $email, $password)
- {
- $this->db->insert($this->tables['users'], array('name' => $name, 'email' => $email, 'password' => $password));
- }
- /**
- * login the user
- *
- * @param string $email
- * @param string $password
- *
- * @return object|FALSE
- */
- public function login($email, $password)
- {
- $query = $this->db->select('id, name, email')->from($this->tables['users'])->where(array('email' => $email, 'password' => $password, 'active' => TRUE))->get();
- if ($query->num_rows() === 1)
- {
- return $query->row();
- }
- return FALSE;
- }
- /**
- * auto login the user for "remember me"
- *
- * @param integer $id
- */
- public function auto_login($cookie_value)
- {
- $query = $this->db->select('id, name, email')
- ->from($this->tables['users'])
- ->join($this->tables['cookies'], $this->tables['cookies'] . '.user_id = ' . $this->tables['users'] . '.id AND ' . $this->tables['cookies'] . '.value = ' . $cookie_value)
- ->where('active', TRUE)
- ->get();
- if ($query->num_rows() === 1)
- {
- $query->row();
- }
- return FALSE;
- }
- /**
- * update user last login
- *
- * @param integer $id
- */
- public function update_last_login($id)
- {
- $this->db->where('id', $id)->update('last_login', CURRENT_TIMESTAMP);
- }
- /**
- * check if email is avaiable
- *
- * @param string $email
- *
- * @return boolean
- */
- public function check_email($email)
- {
- if ($this->db->where('email', $email)->count_all_results($this->tables['users']) === 1) return TRUE;
- return FALSE;
- }
- /**
- * reset user password
- *
- * @param string $email
- */
- public function update_password($user_id, $password)
- {
- $this->db->where('email', $email)->update('password', $password);
- }
- /**
- * insert cookie for "remember me"
- *
- * @param integer $user_id
- */
- public function create_cookie($user_id, $value)
- {
- $this->db->insert($this->tables['cookies'], array('user_id' => $user_id, 'value' => $value));
- }
- /**
- * delete cookie
- *
- * @param integer $user_id
- */
- public function delete_cookie($user_id)
- {
- $this->db->where('user_id', $user_id)->delete($this->tables['cookies']);
- }
- /**
- * create forgotten password value
- *
- * @param string $user_email
- * @param string $value
- */
- public function create_forgotten_password($user_email, $value)
- {
- $this->db->insert($this->tables['forgotten_passwords'], array('user_email' => $user_email, 'value' => $value));
- }
- /**
- * check forgotten password value
- *
- * @param string $user_email
- */
- public function check_forgotten_password($value)
- {
- $query_row = $this->db->select('user_email')->from($this->tables['forgotten_passwords'])->where('value', $value)->get()->row();
- if ($query->num_rows() === 1)
- {
- $this->db->where('value', $value)->delete($this->tables['forgotten_passwords']);
- return $query_row->user_email;
- }
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement