1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed.');
2
3 4 5
6 class User_Model extends CI_Model {
7 8 9
10 public function __construct() {
11 parent::__construct();
12 }
13
14 15 16 17 18 19
20 public function get_settings($user_id) {
21 $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array();
22 $user['settings'] = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row_array();
23 unset($user['settings']['id_users']);
24 return $user;
25 }
26
27 28 29 30 31 32
33 public function save_settings($user) {
34 $user_settings = $user['settings'];
35 $user_settings['id_users'] = $user['id'];
36 unset($user['settings']);
37
38
39 if (isset($user_settings['password'])) {
40 $this->load->helper('general');
41 $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user['id']))->row()->salt;
42 $user_settings['password'] = hash_password($salt, $user_settings['password']);
43 }
44
45 if (!$this->db->update('ea_users', $user, array('id' => $user['id']))) {
46 return FALSE;
47 }
48
49 if (!$this->db->update('ea_user_settings', $user_settings, array('id_users' => $user['id']))) {
50 return FALSE;
51 }
52
53 return TRUE;
54 }
55
56 57 58 59 60 61
62 public function get_salt($username) {
63 $user = $this->db->get_where('ea_user_settings', array('username' => $username))->row_array();
64 return ($user) ? $user['salt'] : '';
65 }
66
67 68 69 70 71 72 73 74
75 public function check_login($username, $password) {
76 $this->load->helper('general');
77 $salt = $this->user_model->get_salt($username);
78 $password = hash_password($salt, $password);
79
80 $user_data = $this->db
81 ->select('ea_users.id AS user_id, ea_users.email AS user_email, '
82 . 'ea_roles.slug AS role_slug, ea_user_settings.username')
83 ->from('ea_users')
84 ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
85 ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id')
86 ->where('ea_user_settings.username', $username)
87 ->where('ea_user_settings.password', $password)
88 ->get()->row_array();
89
90 return ($user_data) ? $user_data : NULL;
91 }
92
93 94 95 96 97 98
99 public function get_user_display_name($user_id) {
100 if (!is_numeric($user_id))
101 throw new Exception ('Invalid argument given ($user_id = "' . $user_id . '").');
102 $user = $this->db->get_where('ea_users', array('id' => $user_id))->row_array();
103 return $user['first_name'] . ' ' . $user['last_name'];
104 }
105
106 107 108 109 110 111 112 113
114 public function regenerate_password($username, $email) {
115 $this->load->helper('general');
116
117 $result = $this->db
118 ->select('ea_users.id')
119 ->from('ea_users')
120 ->join('ea_user_settings', 'ea_user_settings.id_users = ea_users.id', 'inner')
121 ->where('ea_users.email', $email)
122 ->where('ea_user_settings.username', $username)
123 ->get();
124
125 if ($result->num_rows() == 0) return FALSE;
126
127 $user_id = $result->row()->id;
128
129
130 $new_password = generate_random_string();
131 $salt = $this->db->get_where('ea_user_settings', array('id_users' => $user_id))->row()->salt;
132 $hash_password = hash_password($salt, $new_password);
133 $this->db->update('ea_user_settings', array('password' => $hash_password), array('id_users' => $user_id));
134
135 return $new_password;
136 }
137 }
138
139
140