Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.28 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class Auth extends CI_Controller {
  4.  
  5.     function __construct()
  6.     {
  7.         parent::__construct();
  8.         $this->load->library('ion_auth');
  9.         $this->load->library('session');
  10.         $this->load->library('form_validation');
  11.         $this->load->helper('url');
  12.    
  13.         $this->load->database();
  14.  
  15.         $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
  16.     }
  17.  
  18.     //redirect if needed, otherwise display the user list
  19.     function index()
  20.     {
  21.  
  22.         if (!$this->ion_auth->logged_in())
  23.         {
  24.            
  25.             //redirect them to the login page
  26.             redirect('auth/login', 'refresh');
  27.         }
  28.         elseif (!$this->ion_auth->is_admin())
  29.         {
  30.             //redirect them to the home page because they must be an administrator to view this
  31.             redirect('/', 'refresh');
  32.         }
  33.         else
  34.         {
  35.             //set the flash data error message if there is one
  36.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  37.  
  38.             //list the users
  39.             $this->data['users'] = $this->ion_auth->users()->result();
  40.             foreach ($this->data['users'] as $k => $user)
  41.             {
  42.                 $this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
  43.             }
  44.  
  45.  
  46.             $this->load->view('auth/index', $this->data);
  47.         }
  48.     }
  49.  
  50.     //log the user in
  51.     function login()
  52.     {
  53.         $this->data['title'] = "Login";
  54.  
  55.         //validate form input
  56.         $this->form_validation->set_rules('identity', 'Identity', 'required');
  57.         $this->form_validation->set_rules('password', 'Password', 'required');
  58.  
  59.         if ($this->form_validation->run() == true)
  60.         {
  61.             //check to see if the user is logging in
  62.             //check for "remember me"
  63.             $remember = (bool) $this->input->post('remember');
  64.  
  65.             if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
  66.             {
  67.                 //if the login is successful
  68.                 //redirect them back to the home page
  69.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  70.                 redirect('dashboard', 'refresh');
  71.             }
  72.             else
  73.             {
  74.                 //if the login was un-successful
  75.                 //redirect them back to the login page
  76.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  77.                 redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
  78.             }
  79.         }
  80.         else
  81.         {  
  82.             //the user is not logging in so display the login page
  83.             //set the flash data error message if there is one
  84.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  85.  
  86.             $this->data['identity'] = array('name' => 'identity',
  87.                 'id' => 'identity',
  88.                 'type' => 'text',
  89.                 'value' => $this->form_validation->set_value('identity'),
  90.             );
  91.             $this->data['password'] = array('name' => 'password',
  92.                 'id' => 'password',
  93.                 'type' => 'password',
  94.             );
  95.  
  96.             $this->load->view('auth/login', $this->data);
  97.         }
  98.     }
  99.  
  100.     //log the user out
  101.     function logout()
  102.     {
  103.         $this->data['title'] = "Logout";
  104.  
  105.         //log the user out
  106.         $logout = $this->ion_auth->logout();
  107.  
  108.         //redirect them to the login page
  109.         $this->session->set_flashdata('message', $this->ion_auth->messages());
  110.         redirect('auth/login', 'refresh');
  111.     }
  112.  
  113.     //change password
  114.     function change_password()
  115.     {
  116.         $this->form_validation->set_rules('old', 'Old password', 'required');
  117.         $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]');
  118.         $this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required');
  119.  
  120.         if (!$this->ion_auth->logged_in())
  121.         {
  122.             redirect('auth/login', 'refresh');
  123.         }
  124.  
  125.         $user = $this->ion_auth->user()->row();
  126.  
  127.         if ($this->form_validation->run() == false)
  128.         {
  129.             //display the form
  130.             //set the flash data error message if there is one
  131.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  132.  
  133.             $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
  134.             $this->data['old_password'] = array(
  135.                 'name' => 'old',
  136.                 'id'   => 'old',
  137.                 'type' => 'password',
  138.             );
  139.             $this->data['new_password'] = array(
  140.                 'name' => 'new',
  141.                 'id'   => 'new',
  142.                 'type' => 'password',
  143.                 'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  144.             );
  145.             $this->data['new_password_confirm'] = array(
  146.                 'name' => 'new_confirm',
  147.                 'id'   => 'new_confirm',
  148.                 'type' => 'password',
  149.                 'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  150.             );
  151.             $this->data['user_id'] = array(
  152.                 'name'  => 'user_id',
  153.                 'id'    => 'user_id',
  154.                 'type'  => 'hidden',
  155.                 'value' => $user->id,
  156.             );
  157.  
  158.             //render
  159.             $this->load->view('auth/change_password', $this->data);
  160.         }
  161.         else
  162.         {
  163.             $identity = $this->session->userdata($this->config->item('identity', 'ion_auth'));
  164.  
  165.             $change = $this->ion_auth->change_password($identity, $this->input->post('old'), $this->input->post('new'));
  166.  
  167.             if ($change)
  168.             {
  169.                 //if the password was successfully changed
  170.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  171.                 $this->logout();
  172.             }
  173.             else
  174.             {
  175.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  176.                 redirect('auth/change_password', 'refresh');
  177.             }
  178.         }
  179.     }
  180.  
  181.     //forgot password
  182.     function forgot_password()
  183.     {
  184.         $this->form_validation->set_rules('email', 'Email Address', 'required');
  185.         if ($this->form_validation->run() == false)
  186.         {
  187.             //setup the input
  188.             $this->data['email'] = array('name' => 'email',
  189.                 'id' => 'email',
  190.             );
  191.  
  192.             //set any errors and display the form
  193.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  194.             $this->load->view('auth/forgot_password', $this->data);
  195.         }
  196.         else
  197.         {
  198.             //run the forgotten password method to email an activation code to the user
  199.             $forgotten = $this->ion_auth->forgotten_password($this->input->post('email'));
  200.  
  201.             if ($forgotten)
  202.             {
  203.                 //if there were no errors
  204.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  205.                 redirect("auth/login", 'refresh'); //we should display a confirmation page here instead of the login page
  206.             }
  207.             else
  208.             {
  209.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  210.                 redirect("auth/forgot_password", 'refresh');
  211.             }
  212.         }
  213.     }
  214.  
  215.     //reset password - final step for forgotten password
  216.     public function reset_password($code = NULL)
  217.     {
  218.         if (!$code)
  219.         {
  220.             show_404();
  221.         }
  222.  
  223.         $user = $this->ion_auth->forgotten_password_check($code);
  224.  
  225.         if ($user)
  226.         {  
  227.             //if the code is valid then display the password reset form
  228.  
  229.             $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]');
  230.             $this->form_validation->set_rules('new_confirm', 'Confirm New Password', 'required');
  231.  
  232.             if ($this->form_validation->run() == false)
  233.             {
  234.                 //display the form
  235.  
  236.                 //set the flash data error message if there is one
  237.                 $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  238.  
  239.                 $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
  240.                 $this->data['new_password'] = array(
  241.                     'name' => 'new',
  242.                     'id'   => 'new',
  243.                 'type' => 'password',
  244.                     'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  245.                 );
  246.                 $this->data['new_password_confirm'] = array(
  247.                     'name' => 'new_confirm',
  248.                     'id'   => 'new_confirm',
  249.                     'type' => 'password',
  250.                     'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  251.                 );
  252.                 $this->data['user_id'] = array(
  253.                     'name'  => 'user_id',
  254.                     'id'    => 'user_id',
  255.                     'type'  => 'hidden',
  256.                     'value' => $user->id,
  257.                 );
  258.                 $this->data['csrf'] = $this->_get_csrf_nonce();
  259.                 $this->data['code'] = $code;
  260.  
  261.                 //render
  262.                 $this->load->view('auth/reset_password', $this->data);
  263.             }
  264.             else
  265.             {
  266.                 // do we have a valid request?
  267.                 if ($this->_valid_csrf_nonce() === FALSE || $user->id != $this->input->post('user_id'))
  268.                 {
  269.  
  270.                     //something fishy might be up
  271.                     $this->ion_auth->clear_forgotten_password_code($code);
  272.  
  273.                     show_error('This form post did not pass our security checks.');
  274.  
  275.                 }
  276.                 else
  277.                 {
  278.                     // finally change the password
  279.                     $identity = $user->{$this->config->item('identity', 'ion_auth')};
  280.  
  281.                     $change = $this->ion_auth->reset_password($identity, $this->input->post('new'));
  282.  
  283.                     if ($change)
  284.                     {
  285.                         //if the password was successfully changed
  286.                         $this->session->set_flashdata('message', $this->ion_auth->messages());
  287.                         $this->logout();
  288.                     }
  289.                     else
  290.                     {
  291.                         $this->session->set_flashdata('message', $this->ion_auth->errors());
  292.                         redirect('auth/reset_password/' . $code, 'refresh');
  293.                     }
  294.                 }
  295.             }
  296.         }
  297.         else
  298.         {
  299.             //if the code is invalid then send them back to the forgot password page
  300.             $this->session->set_flashdata('message', $this->ion_auth->errors());
  301.             redirect("auth/forgot_password", 'refresh');
  302.         }
  303.     }
  304.  
  305.  
  306.     //activate the user
  307.     function activate($id, $code=false)
  308.     {
  309.         if ($code !== false)
  310.         {
  311.             $activation = $this->ion_auth->activate($id, $code);
  312.         }
  313.         else if ($this->ion_auth->is_admin())
  314.         {
  315.             $activation = $this->ion_auth->activate($id);
  316.         }
  317.  
  318.         if ($activation)
  319.         {
  320.             //redirect them to the auth page
  321.             $this->session->set_flashdata('message', $this->ion_auth->messages());
  322.             redirect("auth", 'refresh');
  323.         }
  324.         else
  325.         {
  326.             //redirect them to the forgot password page
  327.             $this->session->set_flashdata('message', $this->ion_auth->errors());
  328.             redirect("auth/forgot_password", 'refresh');
  329.         }
  330.     }
  331.  
  332.     //deactivate the user
  333.     function deactivate($id = NULL)
  334.     {
  335.         $id = $this->config->item('use_mongodb', 'ion_auth') ? (string) $id : (int) $id;
  336.  
  337.         $this->load->library('form_validation');
  338.         $this->form_validation->set_rules('confirm', 'confirmation', 'required');
  339.         $this->form_validation->set_rules('id', 'user ID', 'required|alpha_numeric');
  340.  
  341.         if ($this->form_validation->run() == FALSE)
  342.         {
  343.             // insert csrf check
  344.             $this->data['csrf'] = $this->_get_csrf_nonce();
  345.             $this->data['user'] = $this->ion_auth->user($id)->row();
  346.  
  347.             $this->load->view('auth/deactivate_user', $this->data);
  348.         }
  349.         else
  350.         {
  351.             // do we really want to deactivate?
  352.             if ($this->input->post('confirm') == 'yes')
  353.             {
  354.                 // do we have a valid request?
  355.                 if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
  356.                 {              
  357.                     show_error('This form post did not pass our security checks.');
  358.                 }
  359.  
  360.                 // do we have the right userlevel?
  361.                 if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
  362.                 {
  363.                     $this->ion_auth->deactivate($id);
  364.                 }
  365.             }
  366.  
  367.             //redirect them back to the auth page
  368.             redirect('auth', 'refresh');
  369.         }
  370.     }
  371.  
  372.     //create a new user
  373.     function create_user()
  374.     {
  375.         $this->data['title'] = "Create User";
  376.  
  377.         /*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  378.         {
  379.             redirect('auth', 'refresh');
  380.         }*/
  381.  
  382.         //validate form input
  383.         $this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
  384.         $this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
  385.         $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
  386.         $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]');
  387.         $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
  388.  
  389.         if ($this->form_validation->run() == true)
  390.         {
  391.             $username = '';
  392.             $email    = $this->input->post('email');
  393.             $password = $this->input->post('password');
  394.  
  395.             $additional_data = array(
  396.                 'first_name' => $this->input->post('first_name'),
  397.                 'last_name'  => $this->input->post('last_name'),
  398.             );
  399.         }
  400.         if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
  401.         {
  402.             //check to see if we are creating the user
  403.             //redirect them back to the admin page
  404.             $this->session->set_flashdata('message', $this->ion_auth->messages());
  405.             redirect("auth/login", 'refresh');
  406.         }
  407.         else
  408.         {
  409.             //display the create user form
  410.             //set the flash data error message if there is one
  411.             $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  412.  
  413.             $this->data['first_name'] = array(
  414.                 'name'  => 'first_name',
  415.                 'id'    => 'first_name',
  416.                 'type'  => 'text',
  417.                 'value' => $this->form_validation->set_value('first_name'),
  418.             );
  419.             $this->data['last_name'] = array(
  420.                 'name'  => 'last_name',
  421.                 'id'    => 'last_name',
  422.                 'type'  => 'text',
  423.                 'value' => $this->form_validation->set_value('last_name'),
  424.             );
  425.             $this->data['email'] = array(
  426.                 'name'  => 'email',
  427.                 'id'    => 'email',
  428.                 'type'  => 'text',
  429.                 'value' => $this->form_validation->set_value('email'),
  430.             );
  431.             $this->data['password'] = array(
  432.                 'name'  => 'password',
  433.                 'id'    => 'password',
  434.                 'type'  => 'password',
  435.                 'value' => $this->form_validation->set_value('password'),
  436.             );
  437.             $this->data['password_confirm'] = array(
  438.                 'name'  => 'password_confirm',
  439.                 'id'    => 'password_confirm',
  440.                 'type'  => 'password',
  441.                 'value' => $this->form_validation->set_value('password_confirm'),
  442.             );
  443.  
  444.             $this->load->view('auth/create_user', $this->data);
  445.         }
  446.     }
  447.  
  448.     //edit a user
  449.     function edit_user($id)
  450.     {
  451.         $this->data['title'] = "Edit User";
  452.  
  453.         if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  454.         {
  455.             redirect('auth', 'refresh');
  456.         }
  457.  
  458.         $user = $this->ion_auth->user($id)->row();
  459.  
  460.         //process the phone number
  461.         if (isset($user->phone) && !empty($user->phone))
  462.         {
  463.             $user->phone = explode('-', $user->phone);
  464.         }
  465.  
  466.         //validate form input
  467.         $this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
  468.         $this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
  469.         $this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
  470.         $this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
  471.         $this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
  472.         $this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
  473.  
  474.         if (isset($_POST) && !empty($_POST))
  475.         {
  476.             // do we have a valid request?
  477.             if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
  478.             {
  479.                 show_error('This form post did not pass our security checks.');
  480.             }
  481.  
  482.             $data = array(
  483.                 'first_name' => $this->input->post('first_name'),
  484.                 'last_name'  => $this->input->post('last_name'),
  485.                 'company'    => $this->input->post('company'),
  486.                 'phone'      => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
  487.             );
  488.  
  489.             //update the password if it was posted
  490.             if ($this->input->post('password'))
  491.             {
  492.                 $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]');
  493.                 $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required');
  494.  
  495.                 $data['password'] = $this->input->post('password');
  496.             }
  497.  
  498.             if ($this->form_validation->run() === TRUE)
  499.             {
  500.                 $this->ion_auth->update($user->id, $data);
  501.  
  502.                 //check to see if we are creating the user
  503.                 //redirect them back to the admin page
  504.                 $this->session->set_flashdata('message', "User Saved");
  505.                 redirect("auth", 'refresh');
  506.             }
  507.         }
  508.        
  509.         //display the edit user form
  510.         $this->data['csrf'] = $this->_get_csrf_nonce();
  511.  
  512.         //set the flash data error message if there is one
  513.         $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  514.  
  515.         //pass the user to the view
  516.         $this->data['user'] = $user;
  517.  
  518.         $this->data['first_name'] = array(
  519.             'name'  => 'first_name',
  520.             'id'    => 'first_name',
  521.             'type'  => 'text',
  522.             'value' => $this->form_validation->set_value('first_name', $user->first_name),
  523.         );
  524.         $this->data['last_name'] = array(
  525.             'name'  => 'last_name',
  526.             'id'    => 'last_name',
  527.             'type'  => 'text',
  528.             'value' => $this->form_validation->set_value('last_name', $user->last_name),
  529.         );
  530.         $this->data['company'] = array(
  531.             'name'  => 'company',
  532.             'id'    => 'company',
  533.             'type'  => 'text',
  534.             'value' => $this->form_validation->set_value('company', $user->company),
  535.         );
  536.         $this->data['phone1'] = array(
  537.             'name'  => 'phone1',
  538.             'id'    => 'phone1',
  539.             'type'  => 'text',
  540.             'value' => $this->form_validation->set_value('phone1', $user->phone[0]),
  541.         );
  542.         $this->data['phone2'] = array(
  543.             'name'  => 'phone2',
  544.             'id'    => 'phone2',
  545.             'type'  => 'text',
  546.             'value' => $this->form_validation->set_value('phone2', $user->phone[1]),
  547.         );
  548.         $this->data['phone3'] = array(
  549.             'name'  => 'phone3',
  550.             'id'    => 'phone3',
  551.             'type'  => 'text',
  552.             'value' => $this->form_validation->set_value('phone3', $user->phone[2]),
  553.         );
  554.         $this->data['password'] = array(
  555.             'name' => 'password',
  556.             'id'   => 'password',
  557.             'type' => 'password'
  558.         );
  559.         $this->data['password_confirm'] = array(
  560.             'name' => 'password_confirm',
  561.             'id'   => 'password_confirm',
  562.             'type' => 'password'
  563.         );
  564.  
  565.         $this->load->view('auth/edit_user', $this->data);
  566.     }
  567.  
  568.     function _get_csrf_nonce()
  569.     {
  570.         $this->load->helper('string');
  571.         $key   = random_string('alnum', 8);
  572.         $value = random_string('alnum', 20);
  573.         $this->session->set_flashdata('csrfkey', $key);
  574.         $this->session->set_flashdata('csrfvalue', $value);
  575.  
  576.         return array($key => $value);
  577.     }
  578.  
  579.     function _valid_csrf_nonce()
  580.     {
  581.         if ($this->input->post($this->session->flashdata('csrfkey')) !== FALSE &&
  582.             $this->input->post($this->session->flashdata('csrfkey')) == $this->session->flashdata('csrfvalue'))
  583.         {
  584.             return TRUE;
  585.         }
  586.         else
  587.         {
  588.             return FALSE;
  589.         }
  590.     }
  591.  
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement