Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.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.     public function __construct()
  6.     {
  7.         parent::__construct();
  8.         $this->load->database();
  9.         $this->load->library(array('ion_auth','form_validation'));
  10.         $this->load->helper(array('url','language'));
  11.  
  12.         $this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
  13.  
  14.         $this->lang->load('auth');
  15.     }
  16.  
  17.     // redirect if needed, otherwise display the user list
  18.     public function index()
  19.     {
  20.  
  21.         if (!$this->ion_auth->logged_in())
  22.         {
  23.             // redirect them to the login page
  24.             redirect('auth/login', 'refresh');
  25.         }
  26.         elseif (!$this->ion_auth->is_admin()) // remove this elseif if you want to enable this for non-admins
  27.         {
  28.             // redirect them to the home page because they must be an administrator to view this
  29.             return show_error('You must be an administrator to view this page.');
  30.         }
  31.         else
  32.         {
  33.             // set the flash data error message if there is one
  34.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  35.  
  36.             //list the users
  37.             $this->data['users'] = $this->ion_auth->users()->result();
  38.             foreach ($this->data['users'] as $k => $user)
  39.             {
  40.                 $this->data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
  41.             }
  42.  
  43.  
  44.             $this->load->model('Ion_auth_model');
  45.             $data['sidebar'] = $this->Ion_auth_model->sidebar();
  46.  
  47.             $this->load->view('header');
  48.             $this->_render_page('auth/index', $this->data);
  49.             $this->load->view('sidebar', $data);
  50.             $this->load->view('footer');
  51.  
  52.  
  53.         }
  54.     }
  55.  
  56.     // log the user in
  57.     public function login()
  58.     {
  59.         $this->data['title'] = $this->lang->line('login_heading');
  60.  
  61.         //validate form input
  62.         $this->form_validation->set_rules('identity', str_replace(':', '', $this->lang->line('login_identity_label')), 'required');
  63.         $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
  64.  
  65.         if ($this->form_validation->run() == true)
  66.         {
  67.             // check to see if the user is logging in
  68.             // check for "remember me"
  69.             $remember = (bool) $this->input->post('remember');
  70.  
  71.             if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
  72.             {
  73.                 //if the login is successful
  74.                 //redirect them back to the home page
  75.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  76.                 redirect('/', 'refresh');
  77.             }
  78.             else
  79.             {
  80.                 // if the login was un-successful
  81.                 // redirect them back to the login page
  82.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  83.                 redirect('auth/login', 'refresh'); // use redirects instead of loading views for compatibility with MY_Controller libraries
  84.             }
  85.         }
  86.         else
  87.         {
  88.             // the user is not logging in so display the login page
  89.             // set the flash data error message if there is one
  90.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  91.  
  92.             $this->data['identity'] = array('name' => 'identity',
  93.                 'id'    => 'identity',
  94.                 'type'  => 'text',
  95.                 'value' => $this->form_validation->set_value('identity'),
  96.             );
  97.             $this->data['password'] = array('name' => 'password',
  98.                 'id'   => 'password',
  99.                 'type' => 'password',
  100.             );
  101.  
  102.  
  103.  
  104.             $this->load->model('Ion_auth_model');
  105.             $data['sidebar'] = $this->Ion_auth_model->sidebar();
  106.  
  107.             $this->load->view('header');
  108.             $this->_render_page('auth/login', $this->data);
  109.             $this->load->view('sidebar', $data);
  110.             $this->load->view('footer');
  111.         }
  112.     }
  113.  
  114.     // log the user out
  115.     public function logout()
  116.     {
  117.         $this->data['title'] = "Logout";
  118.  
  119.         // log the user out
  120.         $logout = $this->ion_auth->logout();
  121.  
  122.         // redirect them to the login page
  123.         $this->session->set_flashdata('message', $this->ion_auth->messages());
  124.         redirect('auth/login', 'refresh');
  125.     }
  126.  
  127.     // change password
  128.     public function change_password()
  129.     {
  130.         $this->form_validation->set_rules('old', $this->lang->line('change_password_validation_old_password_label'), 'required');
  131.         $this->form_validation->set_rules('new', $this->lang->line('change_password_validation_new_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[new_confirm]');
  132.         $this->form_validation->set_rules('new_confirm', $this->lang->line('change_password_validation_new_password_confirm_label'), 'required');
  133.  
  134.         if (!$this->ion_auth->logged_in())
  135.         {
  136.             redirect('auth/login', 'refresh');
  137.         }
  138.  
  139.         $user = $this->ion_auth->user()->row();
  140.  
  141.         if ($this->form_validation->run() == false)
  142.         {
  143.             // display the form
  144.             // set the flash data error message if there is one
  145.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  146.  
  147.             $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
  148.             $this->data['old_password'] = array(
  149.                 'name' => 'old',
  150.                 'id'   => 'old',
  151.                 'type' => 'password',
  152.             );
  153.             $this->data['new_password'] = array(
  154.                 'name'    => 'new',
  155.                 'id'      => 'new',
  156.                 'type'    => 'password',
  157.                 'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  158.             );
  159.             $this->data['new_password_confirm'] = array(
  160.                 'name'    => 'new_confirm',
  161.                 'id'      => 'new_confirm',
  162.                 'type'    => 'password',
  163.                 'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  164.             );
  165.             $this->data['user_id'] = array(
  166.                 'name'  => 'user_id',
  167.                 'id'    => 'user_id',
  168.                 'type'  => 'hidden',
  169.                 'value' => $user->id,
  170.             );
  171.  
  172.             // render
  173.             $this->_render_page('auth/change_password', $this->data);
  174.         }
  175.         else
  176.         {
  177.             $identity = $this->session->userdata('identity');
  178.  
  179.             $change = $this->ion_auth->change_password($identity, $this->input->post('old'), $this->input->post('new'));
  180.  
  181.             if ($change)
  182.             {
  183.                 //if the password was successfully changed
  184.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  185.                 $this->logout();
  186.             }
  187.             else
  188.             {
  189.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  190.                 redirect('auth/change_password', 'refresh');
  191.             }
  192.         }
  193.     }
  194.  
  195.     // forgot password
  196.     public function forgot_password()
  197.     {
  198.         // setting validation rules by checking whether identity is username or email
  199.         if($this->config->item('identity', 'ion_auth') != 'email' )
  200.         {
  201.            $this->form_validation->set_rules('identity', $this->lang->line('forgot_password_identity_label'), 'required');
  202.         }
  203.         else
  204.         {
  205.            $this->form_validation->set_rules('identity', $this->lang->line('forgot_password_validation_email_label'), 'required|valid_email');
  206.         }
  207.  
  208.  
  209.         if ($this->form_validation->run() == false)
  210.         {
  211.             $this->data['type'] = $this->config->item('identity','ion_auth');
  212.             // setup the input
  213.             $this->data['identity'] = array('name' => 'identity',
  214.                 'id' => 'identity',
  215.             );
  216.  
  217.             if ( $this->config->item('identity', 'ion_auth') != 'email' ){
  218.                 $this->data['identity_label'] = $this->lang->line('forgot_password_identity_label');
  219.             }
  220.             else
  221.             {
  222.                 $this->data['identity_label'] = $this->lang->line('forgot_password_email_identity_label');
  223.             }
  224.  
  225.             // set any errors and display the form
  226.             $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  227.             $this->_render_page('auth/forgot_password', $this->data);
  228.         }
  229.         else
  230.         {
  231.             $identity_column = $this->config->item('identity','ion_auth');
  232.             $identity = $this->ion_auth->where($identity_column, $this->input->post('identity'))->users()->row();
  233.  
  234.             if(empty($identity)) {
  235.  
  236.                         if($this->config->item('identity', 'ion_auth') != 'email')
  237.                         {
  238.                             $this->ion_auth->set_error('forgot_password_identity_not_found');
  239.                         }
  240.                         else
  241.                         {
  242.                            $this->ion_auth->set_error('forgot_password_email_not_found');
  243.                         }
  244.  
  245.                         $this->session->set_flashdata('message', $this->ion_auth->errors());
  246.                         redirect("auth/forgot_password", 'refresh');
  247.                     }
  248.  
  249.             // run the forgotten password method to email an activation code to the user
  250.             $forgotten = $this->ion_auth->forgotten_password($identity->{$this->config->item('identity', 'ion_auth')});
  251.  
  252.             if ($forgotten)
  253.             {
  254.                 // if there were no errors
  255.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  256.                 redirect("auth/login", 'refresh'); //we should display a confirmation page here instead of the login page
  257.             }
  258.             else
  259.             {
  260.                 $this->session->set_flashdata('message', $this->ion_auth->errors());
  261.                 redirect("auth/forgot_password", 'refresh');
  262.             }
  263.         }
  264.     }
  265.  
  266.     // reset password - final step for forgotten password
  267.     public function reset_password($code = NULL)
  268.     {
  269.         if (!$code)
  270.         {
  271.             show_404();
  272.         }
  273.  
  274.         $user = $this->ion_auth->forgotten_password_check($code);
  275.  
  276.         if ($user)
  277.         {
  278.             // if the code is valid then display the password reset form
  279.  
  280.             $this->form_validation->set_rules('new', $this->lang->line('reset_password_validation_new_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[new_confirm]');
  281.             $this->form_validation->set_rules('new_confirm', $this->lang->line('reset_password_validation_new_password_confirm_label'), 'required');
  282.  
  283.             if ($this->form_validation->run() == false)
  284.             {
  285.                 // display the form
  286.  
  287.                 // set the flash data error message if there is one
  288.                 $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
  289.  
  290.                 $this->data['min_password_length'] = $this->config->item('min_password_length', 'ion_auth');
  291.                 $this->data['new_password'] = array(
  292.                     'name' => 'new',
  293.                     'id'   => 'new',
  294.                     'type' => 'password',
  295.                     'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  296.                 );
  297.                 $this->data['new_password_confirm'] = array(
  298.                     'name'    => 'new_confirm',
  299.                     'id'      => 'new_confirm',
  300.                     'type'    => 'password',
  301.                     'pattern' => '^.{'.$this->data['min_password_length'].'}.*$',
  302.                 );
  303.                 $this->data['user_id'] = array(
  304.                     'name'  => 'user_id',
  305.                     'id'    => 'user_id',
  306.                     'type'  => 'hidden',
  307.                     'value' => $user->id,
  308.                 );
  309.                 $this->data['csrf'] = $this->_get_csrf_nonce();
  310.                 $this->data['code'] = $code;
  311.  
  312.                 // render
  313.                 $this->_render_page('auth/reset_password', $this->data);
  314.             }
  315.             else
  316.             {
  317.                 // do we have a valid request?
  318.                 if ($this->_valid_csrf_nonce() === FALSE || $user->id != $this->input->post('user_id'))
  319.                 {
  320.  
  321.                     // something fishy might be up
  322.                     $this->ion_auth->clear_forgotten_password_code($code);
  323.  
  324.                     show_error($this->lang->line('error_csrf'));
  325.  
  326.                 }
  327.                 else
  328.                 {
  329.                     // finally change the password
  330.                     $identity = $user->{$this->config->item('identity', 'ion_auth')};
  331.  
  332.                     $change = $this->ion_auth->reset_password($identity, $this->input->post('new'));
  333.  
  334.                     if ($change)
  335.                     {
  336.                         // if the password was successfully changed
  337.                         $this->session->set_flashdata('message', $this->ion_auth->messages());
  338.                         redirect("auth/login", 'refresh');
  339.                     }
  340.                     else
  341.                     {
  342.                         $this->session->set_flashdata('message', $this->ion_auth->errors());
  343.                         redirect('auth/reset_password/' . $code, 'refresh');
  344.                     }
  345.                 }
  346.             }
  347.         }
  348.         else
  349.         {
  350.             // if the code is invalid then send them back to the forgot password page
  351.             $this->session->set_flashdata('message', $this->ion_auth->errors());
  352.             redirect("auth/forgot_password", 'refresh');
  353.         }
  354.     }
  355.  
  356.  
  357.     // activate the user
  358.     public function activate($id, $code=false)
  359.     {
  360.         if ($code !== false)
  361.         {
  362.             $activation = $this->ion_auth->activate($id, $code);
  363.         }
  364.         else if ($this->ion_auth->is_admin())
  365.         {
  366.             $activation = $this->ion_auth->activate($id);
  367.         }
  368.  
  369.         if ($activation)
  370.         {
  371.             // redirect them to the auth page
  372.             $this->session->set_flashdata('message', $this->ion_auth->messages());
  373.             redirect("auth", 'refresh');
  374.         }
  375.         else
  376.         {
  377.             // redirect them to the forgot password page
  378.             $this->session->set_flashdata('message', $this->ion_auth->errors());
  379.             redirect("auth/forgot_password", 'refresh');
  380.         }
  381.     }
  382.  
  383.     // deactivate the user
  384.     public function deactivate($id = NULL)
  385.     {
  386.         if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  387.         {
  388.             // redirect them to the home page because they must be an administrator to view this
  389.             return show_error('You must be an administrator to view this page.');
  390.         }
  391.  
  392.         $id = (int) $id;
  393.  
  394.         $this->load->library('form_validation');
  395.         $this->form_validation->set_rules('confirm', $this->lang->line('deactivate_validation_confirm_label'), 'required');
  396.         $this->form_validation->set_rules('id', $this->lang->line('deactivate_validation_user_id_label'), 'required|alpha_numeric');
  397.  
  398.         if ($this->form_validation->run() == FALSE)
  399.         {
  400.             // insert csrf check
  401.             $this->data['csrf'] = $this->_get_csrf_nonce();
  402.             $this->data['user'] = $this->ion_auth->user($id)->row();
  403.  
  404.             $this->_render_page('auth/deactivate_user', $this->data);
  405.         }
  406.         else
  407.         {
  408.             // do we really want to deactivate?
  409.             if ($this->input->post('confirm') == 'yes')
  410.             {
  411.                 // do we have a valid request?
  412.                 if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
  413.                 {
  414.                     show_error($this->lang->line('error_csrf'));
  415.                 }
  416.  
  417.                 // do we have the right userlevel?
  418.                 if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
  419.                 {
  420.                     $this->ion_auth->deactivate($id);
  421.                 }
  422.             }
  423.  
  424.             // redirect them back to the auth page
  425.             redirect('auth', 'refresh');
  426.         }
  427.     }
  428.  
  429.     // create a new user
  430.     public function create_user()
  431.     {
  432.         $this->data['title'] = $this->lang->line('create_user_heading');
  433.  
  434.         if ($this->ion_auth->logged_in() || $this->ion_auth->is_admin())
  435.         {
  436.             redirect('auth', 'refresh');
  437.         }
  438.  
  439.         $tables = $this->config->item('tables','ion_auth');
  440.         $identity_column = $this->config->item('identity','ion_auth');
  441.         $this->data['identity_column'] = $identity_column;
  442.  
  443.         // validate form input
  444.         $this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required');
  445.         $this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required');
  446.         if($identity_column!=='email')
  447.         {
  448.             $this->form_validation->set_rules('identity',$this->lang->line('create_user_validation_identity_label'),'required|is_unique['.$tables['users'].'.'.$identity_column.']');
  449.             $this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email');
  450.         }
  451.         else
  452.         {
  453.             $this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique[' . $tables['users'] . '.email]');
  454.         }
  455.         $this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'trim');
  456.         $this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'trim');
  457.         $this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
  458.         $this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
  459.  
  460.         if ($this->form_validation->run() == true)
  461.         {
  462.             $email    = strtolower($this->input->post('email'));
  463.             $identity = ($identity_column==='email') ? $email : $this->input->post('identity');
  464.             $password = $this->input->post('password');
  465.  
  466.             $additional_data = array(
  467.                 'first_name' => $this->input->post('first_name'),
  468.                 'last_name'  => $this->input->post('last_name'),
  469.                 'company'    => $this->input->post('company'),
  470.                 'phone'      => $this->input->post('phone'),
  471.             );
  472.         }
  473.         if ($this->form_validation->run() == true && $this->ion_auth->register($identity, $password, $email, $additional_data))
  474.         {
  475.             // check to see if we are creating the user
  476.             // redirect them back to the admin page
  477.             $this->session->set_flashdata('message', $this->ion_auth->messages());
  478.             redirect("auth", 'refresh');
  479.         }
  480.         else
  481.         {
  482.             // display the create user form
  483.             // set the flash data error message if there is one
  484.             $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  485.  
  486.             $this->data['first_name'] = array(
  487.                 'name'  => 'first_name',
  488.                 'id'    => 'first_name',
  489.                 'type'  => 'text',
  490.                 'value' => $this->form_validation->set_value('first_name'),
  491.             );
  492.             $this->data['last_name'] = array(
  493.                 'name'  => 'last_name',
  494.                 'id'    => 'last_name',
  495.                 'type'  => 'text',
  496.                 'value' => $this->form_validation->set_value('last_name'),
  497.             );
  498.             $this->data['identity'] = array(
  499.                 'name'  => 'identity',
  500.                 'id'    => 'identity',
  501.                 'type'  => 'text',
  502.                 'value' => $this->form_validation->set_value('identity'),
  503.             );
  504.             $this->data['email'] = array(
  505.                 'name'  => 'email',
  506.                 'id'    => 'email',
  507.                 'type'  => 'text',
  508.                 'value' => $this->form_validation->set_value('email'),
  509.             );
  510.             $this->data['company'] = array(
  511.                 'name'  => 'company',
  512.                 'id'    => 'company',
  513.                 'type'  => 'text',
  514.                 'value' => $this->form_validation->set_value('company'),
  515.             );
  516.             $this->data['phone'] = array(
  517.                 'name'  => 'phone',
  518.                 'id'    => 'phone',
  519.                 'type'  => 'text',
  520.                 'value' => $this->form_validation->set_value('phone'),
  521.             );
  522.             $this->data['password'] = array(
  523.                 'name'  => 'password',
  524.                 'id'    => 'password',
  525.                 'type'  => 'password',
  526.                 'value' => $this->form_validation->set_value('password'),
  527.             );
  528.             $this->data['password_confirm'] = array(
  529.                 'name'  => 'password_confirm',
  530.                 'id'    => 'password_confirm',
  531.                 'type'  => 'password',
  532.                 'value' => $this->form_validation->set_value('password_confirm'),
  533.             );
  534.  
  535.             $this->_render_page('auth/create_user', $this->data);
  536.         }
  537.     }
  538.  
  539.     // edit a user
  540.     public function edit_user($id)
  541.     {
  542.         $this->data['title'] = $this->lang->line('edit_user_heading');
  543.  
  544.         if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id)))
  545.         {
  546.             redirect('auth', 'refresh');
  547.         }
  548.  
  549.         $user = $this->ion_auth->user($id)->row();
  550.         $groups=$this->ion_auth->groups()->result_array();
  551.         $currentGroups = $this->ion_auth->get_users_groups($id)->result();
  552.  
  553.         // validate form input
  554.         $this->form_validation->set_rules('first_name', $this->lang->line('edit_user_validation_fname_label'), 'required');
  555.         $this->form_validation->set_rules('last_name', $this->lang->line('edit_user_validation_lname_label'), 'required');
  556.         $this->form_validation->set_rules('phone', $this->lang->line('edit_user_validation_phone_label'), 'required');
  557.         $this->form_validation->set_rules('company', $this->lang->line('edit_user_validation_company_label'), 'required');
  558.  
  559.         if (isset($_POST) && !empty($_POST))
  560.         {
  561.             // do we have a valid request?
  562.             if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
  563.             {
  564.                 show_error($this->lang->line('error_csrf'));
  565.             }
  566.  
  567.             // update the password if it was posted
  568.             if ($this->input->post('password'))
  569.             {
  570.                 $this->form_validation->set_rules('password', $this->lang->line('edit_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
  571.                 $this->form_validation->set_rules('password_confirm', $this->lang->line('edit_user_validation_password_confirm_label'), 'required');
  572.             }
  573.  
  574.             if ($this->form_validation->run() === TRUE)
  575.             {
  576.                 $data = array(
  577.                     'first_name' => $this->input->post('first_name'),
  578.                     'last_name'  => $this->input->post('last_name'),
  579.                     'company'    => $this->input->post('company'),
  580.                     'phone'      => $this->input->post('phone'),
  581.                 );
  582.  
  583.                 // update the password if it was posted
  584.                 if ($this->input->post('password'))
  585.                 {
  586.                     $data['password'] = $this->input->post('password');
  587.                 }
  588.  
  589.  
  590.  
  591.                 // Only allow updating groups if user is admin
  592.                 if ($this->ion_auth->is_admin())
  593.                 {
  594.                     //Update the groups user belongs to
  595.                     $groupData = $this->input->post('groups');
  596.  
  597.                     if (isset($groupData) && !empty($groupData)) {
  598.  
  599.                         $this->ion_auth->remove_from_group('', $id);
  600.  
  601.                         foreach ($groupData as $grp) {
  602.                             $this->ion_auth->add_to_group($grp, $id);
  603.                         }
  604.  
  605.                     }
  606.                 }
  607.  
  608.             // check to see if we are updating the user
  609.                if($this->ion_auth->update($user->id, $data))
  610.                 {
  611.                     // redirect them back to the admin page if admin, or to the base url if non admin
  612.                     $this->session->set_flashdata('message', $this->ion_auth->messages() );
  613.                     if ($this->ion_auth->is_admin())
  614.                     {
  615.                         redirect('auth', 'refresh');
  616.                     }
  617.                     else
  618.                     {
  619.                         redirect('/', 'refresh');
  620.                     }
  621.  
  622.                 }
  623.                 else
  624.                 {
  625.                     // redirect them back to the admin page if admin, or to the base url if non admin
  626.                     $this->session->set_flashdata('message', $this->ion_auth->errors() );
  627.                     if ($this->ion_auth->is_admin())
  628.                     {
  629.                         redirect('auth', 'refresh');
  630.                     }
  631.                     else
  632.                     {
  633.                         redirect('/', 'refresh');
  634.                     }
  635.  
  636.                 }
  637.  
  638.             }
  639.         }
  640.  
  641.         // display the edit user form
  642.         $this->data['csrf'] = $this->_get_csrf_nonce();
  643.  
  644.         // set the flash data error message if there is one
  645.         $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  646.  
  647.         // pass the user to the view
  648.         $this->data['user'] = $user;
  649.         $this->data['groups'] = $groups;
  650.         $this->data['currentGroups'] = $currentGroups;
  651.  
  652.         $this->data['first_name'] = array(
  653.             'name'  => 'first_name',
  654.             'id'    => 'first_name',
  655.             'type'  => 'text',
  656.             'value' => $this->form_validation->set_value('first_name', $user->first_name),
  657.         );
  658.         $this->data['last_name'] = array(
  659.             'name'  => 'last_name',
  660.             'id'    => 'last_name',
  661.             'type'  => 'text',
  662.             'value' => $this->form_validation->set_value('last_name', $user->last_name),
  663.         );
  664.         $this->data['company'] = array(
  665.             'name'  => 'company',
  666.             'id'    => 'company',
  667.             'type'  => 'text',
  668.             'value' => $this->form_validation->set_value('company', $user->company),
  669.         );
  670.         $this->data['phone'] = array(
  671.             'name'  => 'phone',
  672.             'id'    => 'phone',
  673.             'type'  => 'text',
  674.             'value' => $this->form_validation->set_value('phone', $user->phone),
  675.         );
  676.         $this->data['password'] = array(
  677.             'name' => 'password',
  678.             'id'   => 'password',
  679.             'type' => 'password'
  680.         );
  681.         $this->data['password_confirm'] = array(
  682.             'name' => 'password_confirm',
  683.             'id'   => 'password_confirm',
  684.             'type' => 'password'
  685.         );
  686.  
  687.         $this->_render_page('auth/edit_user', $this->data);
  688.     }
  689.  
  690.     // create a new group
  691.     public function create_group()
  692.     {
  693.         $this->data['title'] = $this->lang->line('create_group_title');
  694.  
  695.         if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  696.         {
  697.             redirect('auth', 'refresh');
  698.         }
  699.  
  700.         // validate form input
  701.         $this->form_validation->set_rules('group_name', $this->lang->line('create_group_validation_name_label'), 'required|alpha_dash');
  702.  
  703.         if ($this->form_validation->run() == TRUE)
  704.         {
  705.             $new_group_id = $this->ion_auth->create_group($this->input->post('group_name'), $this->input->post('description'));
  706.             if($new_group_id)
  707.             {
  708.                 // check to see if we are creating the group
  709.                 // redirect them back to the admin page
  710.                 $this->session->set_flashdata('message', $this->ion_auth->messages());
  711.                 redirect("auth", 'refresh');
  712.             }
  713.         }
  714.         else
  715.         {
  716.             // display the create group form
  717.             // set the flash data error message if there is one
  718.             $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  719.  
  720.             $this->data['group_name'] = array(
  721.                 'name'  => 'group_name',
  722.                 'id'    => 'group_name',
  723.                 'type'  => 'text',
  724.                 'value' => $this->form_validation->set_value('group_name'),
  725.             );
  726.             $this->data['description'] = array(
  727.                 'name'  => 'description',
  728.                 'id'    => 'description',
  729.                 'type'  => 'text',
  730.                 'value' => $this->form_validation->set_value('description'),
  731.             );
  732.  
  733.             $this->_render_page('auth/create_group', $this->data);
  734.         }
  735.     }
  736.  
  737.     // edit a group
  738.     public function edit_group($id)
  739.     {
  740.         // bail if no group id given
  741.         if(!$id || empty($id))
  742.         {
  743.             redirect('auth', 'refresh');
  744.         }
  745.  
  746.         $this->data['title'] = $this->lang->line('edit_group_title');
  747.  
  748.         if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
  749.         {
  750.             redirect('auth', 'refresh');
  751.         }
  752.  
  753.         $group = $this->ion_auth->group($id)->row();
  754.  
  755.         // validate form input
  756.         $this->form_validation->set_rules('group_name', $this->lang->line('edit_group_validation_name_label'), 'required|alpha_dash');
  757.  
  758.         if (isset($_POST) && !empty($_POST))
  759.         {
  760.             if ($this->form_validation->run() === TRUE)
  761.             {
  762.                 $group_update = $this->ion_auth->update_group($id, $_POST['group_name'], $_POST['group_description']);
  763.  
  764.                 if($group_update)
  765.                 {
  766.                     $this->session->set_flashdata('message', $this->lang->line('edit_group_saved'));
  767.                 }
  768.                 else
  769.                 {
  770.                     $this->session->set_flashdata('message', $this->ion_auth->errors());
  771.                 }
  772.                 redirect("auth", 'refresh');
  773.             }
  774.         }
  775.  
  776.         // set the flash data error message if there is one
  777.         $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
  778.  
  779.         // pass the user to the view
  780.         $this->data['group'] = $group;
  781.  
  782.         $readonly = $this->config->item('admin_group', 'ion_auth') === $group->name ? 'readonly' : '';
  783.  
  784.         $this->data['group_name'] = array(
  785.             'name'    => 'group_name',
  786.             'id'      => 'group_name',
  787.             'type'    => 'text',
  788.             'value'   => $this->form_validation->set_value('group_name', $group->name),
  789.             $readonly => $readonly,
  790.         );
  791.         $this->data['group_description'] = array(
  792.             'name'  => 'group_description',
  793.             'id'    => 'group_description',
  794.             'type'  => 'text',
  795.             'value' => $this->form_validation->set_value('group_description', $group->description),
  796.         );
  797.  
  798.         $this->_render_page('auth/edit_group', $this->data);
  799.     }
  800.  
  801.  
  802.     public function _get_csrf_nonce()
  803.     {
  804.         $this->load->helper('string');
  805.         $key   = random_string('alnum', 8);
  806.         $value = random_string('alnum', 20);
  807.         $this->session->set_flashdata('csrfkey', $key);
  808.         $this->session->set_flashdata('csrfvalue', $value);
  809.  
  810.         return array($key => $value);
  811.     }
  812.  
  813.     public function _valid_csrf_nonce()
  814.     {
  815.         $csrfkey = $this->input->post($this->session->flashdata('csrfkey'));
  816.         if ($csrfkey && $csrfkey == $this->session->flashdata('csrfvalue'))
  817.         {
  818.             return TRUE;
  819.         }
  820.         else
  821.         {
  822.             return FALSE;
  823.         }
  824.     }
  825.  
  826.     public function _render_page($view, $data=null, $returnhtml=false)//I think this makes more sense
  827.     {
  828.  
  829.         $this->viewdata = (empty($data)) ? $this->data: $data;
  830.  
  831.         $view_html = $this->load->view($view, $this->viewdata, $returnhtml);
  832.  
  833.         if ($returnhtml) return $view_html;//This will return html on 3rd argument being true
  834.     }
  835.  
  836. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement