-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
auth.php
360 lines (320 loc) · 16 KB
/
auth.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends Controller {
function __construct()
{
parent::__construct();
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->database();
$this->load->helper('url');
}
//redirect if needed, otherwise display the user list
function index()
{
if (!$this->ion_auth->logged_in()) {
//redirect them to the login page
redirect('auth/login', 'refresh');
}
elseif (!$this->ion_auth->is_admin()) {
//redirect them to the home page because they must be an administrator to view this
redirect($this->config->item('base_url'), 'refresh');
}
else {
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
//list the users
$this->data['users'] = $this->ion_auth->get_users_array();
$this->load->view('auth/index', $this->data);
}
}
//log the user in
function login()
{
$this->data['title'] = "Login";
//validate form input
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == true) { //check to see if the user is logging in
//check for "remember me"
if ($this->input->post('remember') == 1) {
$remember = true;
}
else {
$remember = false;
}
if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember)) { //if the login is successful
//redirect them back to the home page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect($this->config->item('base_url'), 'refresh');
}
else { //if the login was un-successful
//redirect them back to the login page
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
}
}
else { //the user is not logging in so display the login page
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
);
$this->load->view('auth/login', $this->data);
}
}
//log the user out
function logout()
{
$this->data['title'] = "Logout";
//log the user out
$logout = $this->ion_auth->logout();
//redirect them back to the page they came from
redirect('auth', 'refresh');
}
//change password
function change_password()
{
$this->form_validation->set_rules('old', 'Old password', 'required');
$this->form_validation->set_rules('new', 'New Password', 'required|min_length['.$this->config->item('min_password_length', 'ion_auth').']|max_length['.$this->config->item('max_password_length', 'ion_auth').']|matches[new_confirm]');
$this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required');
if (!$this->ion_auth->logged_in()) {
redirect('auth/login', 'refresh');
}
$user = $this->ion_auth->get_user($this->session->userdata('user_id'));
if ($this->form_validation->run() == false) { //display the form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['old_password'] = array('name' => 'old',
'id' => 'old',
'type' => 'password',
);
$this->data['new_password'] = array('name' => 'new',
'id' => 'new',
'type' => 'password',
);
$this->data['new_password_confirm'] = array('name' => 'new_confirm',
'id' => 'new_confirm',
'type' => 'password',
);
$this->data['user_id'] = array('name' => 'user_id',
'id' => 'user_id',
'type' => 'hidden',
'value' => $user->id,
);
//render
$this->load->view('auth/change_password', $this->data);
}
else {
$identity = $this->session->userdata($this->config->item('identity', 'ion_auth'));
$change = $this->ion_auth->change_password($identity, $this->input->post('old'), $this->input->post('new'));
if ($change) { //if the password was successfully changed
$this->session->set_flashdata('message', $this->ion_auth->messages());
$this->logout();
}
else {
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/change_password', 'refresh');
}
}
}
//forgot password
function forgot_password()
{
$this->form_validation->set_rules('email', 'Email Address', 'required');
if ($this->form_validation->run() == false) {
//setup the input
$this->data['email'] = array('name' => 'email',
'id' => 'email',
);
//set any errors and display the form
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->load->view('auth/forgot_password', $this->data);
}
else {
//run the forgotten password method to email an activation code to the user
$forgotten = $this->ion_auth->forgotten_password($this->input->post('email'));
if ($forgotten) { //if there were no errors
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth/login", 'refresh'); //we should display a confirmation page here instead of the login page
}
else {
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("auth/forgot_password", 'refresh');
}
}
}
//reset password - final step for forgotten password
public function reset_password($code)
{
$reset = $this->ion_auth->forgotten_password_complete($code);
if ($reset) { //if the reset worked then send them to the login page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth/login", 'refresh');
}
else { //if the reset didnt work then send them back to the forgot password page
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("auth/forgot_password", 'refresh');
}
}
//activate the user
function activate($id, $code=false)
{
$activation = $this->ion_auth->activate($id, $code);
if ($activation) {
//redirect them to the auth page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth", 'refresh');
}
else {
//redirect them to the forgot password page
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect("auth/forgot_password", 'refresh');
}
}
//deactivate the user
function deactivate($id = NULL)
{
// no funny business, force to integer
$id = (int) $id;
$this->load->library('form_validation');
$this->form_validation->set_rules('confirm', 'confirmation', 'required');
$this->form_validation->set_rules('id', 'user ID', 'required|is_natural');
if ( $this->form_validation->run() == FALSE )
{
// insert csrf check
$this->data['csrf'] = $this->_get_csrf_nonce();
$this->data['user'] = $this->ion_auth->get_user($id);
$this->load->view('auth/deactivate_user', $this->data);
}
else
{
// do we really want to deactivate?
if ( $this->input->post('confirm') == 'yes' )
{
// do we have a valid request?
if ( $this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id') )
{
show_404();
}
// do we have the right userlevel?
if ( $this->ion_auth->logged_in() && $this->ion_auth->is_admin() )
{
$this->ion_auth->deactivate($id);
}
}
//redirect them back to the auth page
redirect('auth','refresh');
}
}
//create a new user
function create_user()
{
$this->data['title'] = "Create User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) {
redirect('auth', 'refresh');
}
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|min_length['.$this->config->item('min_password_length', 'ion_auth').']|max_length['.$this->config->item('max_password_length', 'ion_auth').']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
if ($this->form_validation->run() == true) {
$username = strtolower($this->input->post('first_name')).' '.strtolower($this->input->post('last_name'));
$email = $this->input->post('email');
$password = $this->input->post('password');
$additional_data = array('first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') .'-'. $this->input->post('phone2') .'-'. $this->input->post('phone3'),
);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($username,$password,$email,$additional_data)) { //check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', "User Created");
redirect("auth", 'refresh');
}
else { //display the create user form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? ($this->ion_auth->errors()) ? $this->ion_auth->errors() : validation_errors()) : $this->session->flashdata('message');
$this->data['first_name'] = array('name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array('name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['company'] = array('name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company'),
);
$this->data['phone1'] = array('name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1'),
);
$this->data['phone2'] = array('name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2'),
);
$this->data['phone3'] = array('name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$this->data['password_confirm'] = array('name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->load->view('auth/create_user', $this->data);
}
}
function _get_csrf_nonce()
{
$this->load->helper('string');
$key = random_string('alnum', 8);
$value = random_string('alnum', 20);
$this->session->set_flashdata('csrfkey', $key);
$this->session->set_flashdata('csrfvalue', $value);
return array($key=>$value);
}
function _valid_csrf_nonce()
{
if ( $this->input->post($this->session->flashdata('csrfkey')) !== FALSE &&
$this->input->post($this->session->flashdata('csrfkey')) == $this->session->flashdata('csrfvalue'))
{
return TRUE;
}
else
{
return FALSE;
}
}
}