Advertisement
gundambison

perbaikan 84- controller

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