Allow password change with no restrictions + code reorg (#1894)

This commit is contained in:
FrancescoUK
2018-03-25 19:18:03 +01:00
parent 4595ab5c35
commit 4079e44572
11 changed files with 115 additions and 115 deletions

View File

@@ -84,113 +84,86 @@ class Employees extends Persons
*/
public function save($employee_id = -1)
{
if($this->input->post('current_password') != '')
{
if($this->Employee->check_password($this->input->post('username'), $this->input->post('current_password')))
{
$employee_data = array(
'username' => $this->input->post('username'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'hash_version' => 2
);
$first_name = $this->xss_clean($this->input->post('first_name'));
$last_name = $this->xss_clean($this->input->post('last_name'));
$email = $this->xss_clean(strtolower($this->input->post('email')));
if($this->Employee->change_password($employee_data, $employee_id))
{
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('employees_successful_change_password'), 'id' => $employee_id));
}
else//failure
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_unsuccessful_change_password'), 'id' => -1));
}
}
else
// format first and last name properly
$first_name = $this->nameize($first_name);
$last_name = $this->nameize($last_name);
$person_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'gender' => $this->input->post('gender'),
'email' => $email,
'phone_number' => $this->input->post('phone_number'),
'address_1' => $this->input->post('address_1'),
'address_2' => $this->input->post('address_2'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zip' => $this->input->post('zip'),
'country' => $this->input->post('country'),
'comments' => $this->input->post('comments'),
);
$grants_array = array();
foreach($this->Module->get_all_permissions()->result() as $permission)
{
$grants = array();
$grant = $this->input->post('grant_'.$permission->permission_id) != NULL ? $this->input->post('grant_'.$permission->permission_id) : '';
if($grant == $permission->permission_id)
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_current_password_invalid'), 'id' => -1));
$grants['permission_id'] = $permission->permission_id;
$grants['menu_group'] = $this->input->post('menu_group_'.$permission->permission_id) != NULL ? $this->input->post('menu_group_'.$permission->permission_id) : '--';
$grants_array[] = $grants;
}
}
else
//Password has been changed OR first time password set
if($this->input->post('password') != '')
{
$first_name = $this->xss_clean($this->input->post('first_name'));
$last_name = $this->xss_clean($this->input->post('last_name'));
$email = $this->xss_clean(strtolower($this->input->post('email')));
// format first and last name properly
$first_name = $this->nameize($first_name);
$last_name = $this->nameize($last_name);
$person_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'gender' => $this->input->post('gender'),
'email' => $email,
'phone_number' => $this->input->post('phone_number'),
'address_1' => $this->input->post('address_1'),
'address_2' => $this->input->post('address_2'),
'city' => $this->input->post('city'),
'state' => $this->input->post('state'),
'zip' => $this->input->post('zip'),
'country' => $this->input->post('country'),
'comments' => $this->input->post('comments'),
$exploded = explode(":", $this->input->post('language'));
$employee_data = array(
'username' => $this->input->post('username'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => $exploded[0],
'language' => $exploded[1]
);
}
else //Password not changed
{
$exploded = explode(":", $this->input->post('language'));
$employee_data = array(
'username' => $this->input->post('username'),
'language_code' => $exploded[0],
'language' => $exploded[1]
);
}
$grants_array = array();
foreach($this->Module->get_all_permissions()->result() as $permission)
if($this->Employee->save_employee($person_data, $employee_data, $grants_array, $employee_id))
{
// New employee
if($employee_id == -1)
{
$grants = array();
$grant = $this->input->post('grant_'.$permission->permission_id) != NULL ? $this->input->post('grant_'.$permission->permission_id) : '';
if($grant == $permission->permission_id)
{
$grants['permission_id'] = $permission->permission_id;
$grants['menu_group'] = $this->input->post('menu_group_'.$permission->permission_id) != NULL ? $this->input->post('menu_group_'.$permission->permission_id) : '--';
$grants_array[] = $grants;
}
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('employees_successful_adding') . ' ' . $first_name . ' ' . $last_name,
'id' => $this->xss_clean($employee_data['person_id'])));
}
//Password has been changed OR first time password set
if($this->input->post('password') != '')
else // Existing employee
{
$exploded = explode(":", $this->input->post('language'));
$employee_data = array(
'username' => $this->input->post('username'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'hash_version' => 2,
'language_code' => $exploded[0],
'language' => $exploded[1]
);
}
else //Password not changed
{
$exploded = explode(":", $this->input->post('language'));
$employee_data = array(
'username' => $this->input->post('username'),
'language_code' => $exploded[0],
'language' => $exploded[1]
);
}
if($this->Employee->save_employee($person_data, $employee_data, $grants_array, $employee_id))
{
// New employee
if($employee_id == -1)
{
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('employees_successful_adding') . ' ' . $first_name . ' ' . $last_name,
'id' => $this->xss_clean($employee_data['person_id'])));
}
else // Existing employee
{
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('employees_successful_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => $employee_id));
}
}
else // Failure
{
echo json_encode(array('success' => FALSE,
'message' => $this->lang->line('employees_error_adding_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => -1));
echo json_encode(array('success' => TRUE,
'message' => $this->lang->line('employees_successful_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => $employee_id));
}
}
else // Failure
{
echo json_encode(array('success' => FALSE,
'message' => $this->lang->line('employees_error_adding_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => -1));
}
}
/*

View File

@@ -11,7 +11,7 @@ class Home extends Secure_Controller
public function index()
{
$this->load->view('home');
$this->load->view('home/home');
}
public function logout()
@@ -20,7 +20,7 @@ class Home extends Secure_Controller
}
/*
Loads the change password form
Loads the change employee password form
*/
public function change_password($employee_id = -1)
{
@@ -31,7 +31,42 @@ class Home extends Secure_Controller
}
$data['person_info'] = $person_info;
$this->load->view('employees/form_change_password', $data);
$this->load->view('home/form_change_password', $data);
}
/*
Change employee password
*/
public function save($employee_id = -1)
{
if($this->input->post('current_password') != '' && $employee_id != -1)
{
if($this->Employee->check_password($this->input->post('username'), $this->input->post('current_password')))
{
$employee_data = array(
'username' => $this->input->post('username'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'hash_version' => 2
);
if($this->Employee->change_password($employee_data, $employee_id))
{
echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('employees_successful_change_password'), 'id' => $employee_id));
}
else//failure
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_unsuccessful_change_password'), 'id' => -1));
}
}
else
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_current_password_invalid'), 'id' => -1));
}
}
else
{
echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('employees_current_password_invalid'), 'id' => -1));
}
}
}
?>

View File

@@ -11,7 +11,7 @@ class Office extends Secure_Controller
public function index()
{
$this->load->view('office');
$this->load->view('home/office');
}
public function logout()

View File

@@ -421,9 +421,9 @@ class Employee extends Person
$row = $this->db->get()->row();
// If no grants are assigned yet then set the default to 'home'
if ($row == null)
if ($row == NULL)
{
return "home";
return 'home';
}
else
{

View File

@@ -123,6 +123,5 @@ class Module extends CI_Model
$this->db->from('modules');
return $this->db->get()->row()->sort;
}
}
?>

View File

@@ -565,7 +565,7 @@ class Sale extends CI_Model
$sales_data = array(
'sale_time' => date('Y-m-d H:i:s'),
'customer_id' => $this->Customer->exists($customer_id) ? $customer_id : null,
'customer_id' => $this->Customer->exists($customer_id) ? $customer_id : NULL,
'employee_id' => $employee_id,
'comment' => $comment,
'sale_status' => $sale_status,

View File

@@ -41,7 +41,7 @@
'name' => 'msg_src',
'id' => 'msg_src',
'class' => 'form-control input-sm required',
'value'=>$this->config->item('msg_src') == null ? $this->config->item('company') : $this->config->item('msg_src')));?>
'value'=>$this->config->item('msg_src') == NULL ? $this->config->item('company') : $this->config->item('msg_src')));?>
</div>
</div>
</div>

View File

@@ -2,7 +2,7 @@
<ul id="error_message_box" class="error_message_box"></ul>
<?php echo form_open('employees/save/'.$person_info->person_id, array('id'=>'employee_form', 'class'=>'form-horizontal')); ?>
<?php echo form_open('home/save/'.$person_info->person_id, array('id'=>'employee_form', 'class'=>'form-horizontal')); ?>
<div class="tab-content">
<div class="tab-pane fade in active" id="employee_login_info">
<fieldset>
@@ -87,14 +87,7 @@ $(document).ready(function()
success:function(response)
{
dialog_support.hide();
if(!response.success)
{
$.notify(response.message, {type: 'danger' });
}
else
{
$.notify(response.message, {type: 'success' });
}
$.notify(response.message, { type: response.success ? 'success' : 'danger'} );
},
dataType:'json'
});
@@ -131,4 +124,4 @@ $(document).ready(function()
}
}, form_support.error));
});
</script>
</script>

View File

@@ -332,7 +332,7 @@
{
?>
<?php
if($this->config->item('custom'.$i.'_name') != null)
if($this->config->item('custom'.$i.'_name') != NULL)
{
$item_arr = (array)$item_info;
?>