alternative to function within function in php -
so have function call/return to/from ajax. can see, there multiple places return data , stop script if conditionals met. (obviously) far less concise if had function passed error message , returned/encoded json, while stopping main function continuing. problem not know how best structure sort of thing in php, given functions within functions aren't used.
sorry long code piece... not sure how else describe issue.
sincere (or other unrelated tips even). appreciated.
public function mask_as_user() { $this->load->model('user_model', '', true); $email = $this->input->post('email'); //there should front-end validation here too, if not permitter, dont allow proceed. if (strpos($this->session->userdata('user_type'), 'permitter') === false) { $return['status'] = 'error'; $return['message']= 'you not admin.'; header('content-type: application/json'); echo json_encode($return); return false; } //save current admin account, know switch $admin_account_email = $this->session->userdata('user_email'); //logout current user not have overlapping session data //$this->logout(false, true); $user_data = $this->user_model->get_user_data($email); if ($user_data[0]){ $user_data = $this->clean_user_data($user_data[0]); } else{ $return['status'] = 'error'; $return['message']= 'this not active client account.'; header('content-type: application/json'); echo json_encode($return); return false; } //get userdata user-mask account , remove unneccessary data (such login credentials) session array //prevent switching admin accounts. not sensitive data rest of company can't access elsewhere, maybe someday there be. if (strpos($user_data['user_type'], 'permitter') !== false) { $return['status'] = 'error'; $return['message']= 'you cannot switch admin account.'; header('content-type: application/json'); echo json_encode($return); return false; } //foreach column loaded database, create session value. $this->session->set_userdata($user_data); //set user loggedin. $this->session->set_userdata('loggedin', true); //set current admin account mask being applied to. need returning admin account without having logout. $this->session->set_userdata('admin_account_email', $admin_account_email); $return['status'] = 'success'; $return['redir_url'] = '/site_client/dashboard'; header('content-type: application/json'); echo json_encode($return); }
you use try/catch statement, :
public function mask_as_user() { $this->load->model('user_model', '', true); $email = $this->input->post('email'); try { //there should front-end validation here too, if not permitter, dont allow proceed. if (strpos($this->session->userdata('user_type'), 'permitter') === false) { throw new exception('you not admin.'); } //save current admin account, know switch $admin_account_email = $this->session->userdata('user_email'); //logout current user not have overlapping session data //$this->logout(false, true); $user_data = $this->user_model->get_user_data($email); if ($user_data[0]){ $user_data = $this->clean_user_data($user_data[0]); } else { throw new exception('this not active client account.'); } //get userdata user-mask account , remove unneccessary data (such login credentials) session array //prevent switching admin accounts. not sensitive data rest of company can't access elsewhere, maybe someday there be. if (strpos($user_data['user_type'], 'permitter') !== false) { throw new exception('you cannot switch admin account.'); } //foreach column loaded database, create session value. $this->session->set_userdata($user_data); //set user loggedin. $this->session->set_userdata('loggedin', true); //set current admin account mask being applied to. need returning admin account without having logout. $this->session->set_userdata('admin_account_email', $admin_account_email); $return['status'] = 'success'; $return['redir_url'] = '/site_client/dashboard'; header('content-type: application/json'); echo json_encode($return); return true; } catch (exception $e) { $return['message']= $e->getmessage(); $return['status'] = 'error'; header('content-type: application/json'); echo json_encode($return); return false; } }
Comments
Post a Comment