-
Notifications
You must be signed in to change notification settings - Fork 5
/
ci_authentication_model.php
235 lines (210 loc) · 5.44 KB
/
ci_authentication_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ci_authentication_model
*
* All queries for ci_authentication package.
*
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @author Mike Funk
* @link http://mikefunk.com
* @email mike@mikefunk.com
*
* @file ci_authentication_model.php
* @version 1.1.3
* @date 02/17/2012
*
* Copyright (c) 2012
*/
// --------------------------------------------------------------------------
/**
* ci_authentication_model class.
*
* @extends CI_Model
*/
class ci_authentication_model extends CI_Model
{
// --------------------------------------------------------------------------
/**
* __construct function.
*
* load common resources.
*
* @access public
* @return void
*/
public function __construct()
{
parent::__construct();
$this->load->database();
$this->config->load('authentication');
log_message('debug', 'CI Authentication: model loaded.');
}
// --------------------------------------------------------------------------
/**
* password_check function.
*
* checks whether the password for a given user matches the passed password
* after being encrypted.
*
* @access public
* @param string $username
* @param string $password
* @param bool $encrypted (default: false)
* @return bool
*/
public function password_check($username, $password, $encrypted = false)
{
// check for blanks
if ($username == '' || $password == '') { return false; }
// check for existing email
$q = $this->get_user_by_username($username);
if ($q->num_rows() == 0)
{
return false;
}
// if it exists, *then* check for matching password
else
{
// set up values
$r = $q->row();
$salt = substr($r->password, 0, config_item('salt_length'));
$this->load->helper('encrypt_helper');
if (!$encrypted) { $password = encrypt_this($password, $salt); }
// check password and return match
$this->db->where(config_item('username_field'), $username);
$this->db->where(config_item('password_field'), $password);
$q = $this->db->get(config_item('users_table'));
if ($q->num_rows() == 0)
{
return false;
}
else
{
return true;
}
}
}
// --------------------------------------------------------------------------
/**
* username_check function.
*
* checks whether a user by this username exists.
*
* @access public
* @param mixed $username
* @return bool
*/
public function username_check($username)
{
$q = $this->get_user_by_username($username);
if ($q->num_rows() == 0)
{
return false;
}
else
{
return true;
}
}
// --------------------------------------------------------------------------
/**
* confirm_string_check function.
*
* checks to make sure this username does not have a confirm string
*
* @access public
* @param mixed $username
* @return bool
*/
public function confirm_string_check($username)
{
$this->db->where(config_item('username_field'), $username);
$this->db->where(config_item('confirm_string_field'), '');
$q = $this->db->get(config_item('users_table'));
if ($q->num_rows() > 0)
{
return true;
}
else
{
return false;
}
}
// --------------------------------------------------------------------------
/**
* get_user_by_username function.
*
* returns the query for a user based on a passed username.
*
* @access public
* @param mixed $username
* @param mixed $join (default: true) whether to join in the role.
* @return object
*/
public function get_user_by_username($username, $join = true)
{
$ut = config_item('users_table');
$rt = config_item('roles_table');
// join in roles
if ($join)
{
$this->db->select(
$rt . '.*,' .
$ut . '.id,' .
$ut . '.' . config_item('username_field') . ',' .
$ut . '.' . config_item('password_field') . ',' .
$ut . '.' . config_item('confirm_string_field') . ','
);
$this->db->join($rt, $rt . '.id = ' . $ut . '.' . config_item('role_id_field'), 'left');
}
$this->db->where(config_item('username_field'), $username);
return $this->db->get($ut);
}
// --------------------------------------------------------------------------
/**
* get_user_by_confirm_string function.
*
* returns the query for a user with the passed confirm string
*
* @access public
* @param mixed $confirm_string
* @return void
*/
public function get_user_by_confirm_string($confirm_string)
{
$this->db->where(config_item('confirm_string_field'), $confirm_string);
return $this->db->get(config_item('users_table'));
}
// --------------------------------------------------------------------------
/**
* edit_user function.
*
* edits a user passed via an array.
*
* @access public
* @param array $post
* @return bool
*/
public function edit_user($post)
{
$this->db->where('id', $post['id']);
return $this->db->update(config_item('users_table'), $post);
}
// --------------------------------------------------------------------------
/**
* add_user function.
*
* adds a user passed via an array.
*
* @access public
* @param array $post
* @return bool
*/
public function add_user($post)
{
return $this->db->insert(config_item('users_table'), $post);
}
// --------------------------------------------------------------------------
}
/* End of file ci_authentication_model.php */
/* Location: ./ci_authentication/models/ci_authentication_model.php */