2012-12-11 2 views
5

최신 이온 인증 파일과 최신 버전 CodeIgniter 2을 사용하고 있습니다.CodeIgniter 2 및 Ion Auth - 자신의 사용자 계정 편집

컨트롤러 파일에는 edit_user이라는 기능이 있습니다. 이 기능은 "관리자"그룹 내 멤버 만 사용하도록 제한되며, 모든 관리자 멤버

/auth/edit_user/id 

문제는 내가 돈 '이다 ... 이 URL을 통해 기능을 사용하는 다른 멤버 편집 할 수 있습니다 일반 (비 관리자) 사용자가 자신의 계정 세부 정보를 편집 할 수있게하는 모든 컨트롤러 기능 또는보기를 참조하십시오.

이 컨트롤러는 필자가 작성해야 할 새로운 컨트롤러 기능 (edit_user 기능 수정)입니까, 아니면 Ion Auth가 이미 수행해야하는 것입니까? 그렇다면 어떻게? 꽤 경량입니다 - 여기

function edit_user($id) 
{ 
    $this->data['title'] = "Edit User"; 

    if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) 
    { 
     redirect('auth', 'refresh'); 
    } 

    $user = $this->ion_auth->user($id)->row(); 

    //process the phone number 
    if (isset($user->phone) && !empty($user->phone)) 
    { 
     $user->phone = explode('-', $user->phone); 
    } 

    //validate form input 
    $this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean'); 
    $this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean'); 
    $this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]'); 
    $this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]'); 
    $this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]'); 
    $this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean'); 

    if (isset($_POST) && !empty($_POST)) 
    { 
     // do we have a valid request? 
     if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id')) 
     { 
      show_error('This form post did not pass our security checks.'); 
     } 

     $data = array(
      'first_name' => $this->input->post('first_name'), 
      'last_name' => $this->input->post('last_name'), 
      'company' => $this->input->post('company'), 
      'phone'  => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'), 
     ); 

     //update the password if it was posted 
     if ($this->input->post('password')) 
     { 
      $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]'); 
      $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'required'); 

      $data['password'] = $this->input->post('password'); 
     } 

     if ($this->form_validation->run() === TRUE) 
     { 
      $this->ion_auth->update($user->id, $data); 

      //check to see if we are creating the user 
      //redirect them back to the admin page 
      $this->session->set_flashdata('message', "User Saved"); 
      redirect("auth", 'refresh'); 
     } 
    } 

    //display the edit user form 
    $this->data['csrf'] = $this->_get_csrf_nonce(); 

    //set the flash data error message if there is one 
    $this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message'))); 

    //pass the user to the view 
    $this->data['user'] = $user; 

    $this->data['first_name'] = array(
     'name' => 'first_name', 
     'id' => 'first_name', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('first_name', $user->first_name), 
    ); 
    $this->data['last_name'] = array(
     'name' => 'last_name', 
     'id' => 'last_name', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('last_name', $user->last_name), 
    ); 
    $this->data['company'] = array(
     'name' => 'company', 
     'id' => 'company', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('company', $user->company), 
    ); 
    $this->data['phone1'] = array(
     'name' => 'phone1', 
     'id' => 'phone1', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('phone1', $user->phone[0]), 
    ); 
    $this->data['phone2'] = array(
     'name' => 'phone2', 
     'id' => 'phone2', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('phone2', $user->phone[1]), 
    ); 
    $this->data['phone3'] = array(
     'name' => 'phone3', 
     'id' => 'phone3', 
     'type' => 'text', 
     'value' => $this->form_validation->set_value('phone3', $user->phone[2]), 
    ); 
    $this->data['password'] = array(
     'name' => 'password', 
     'id' => 'password', 
     'type' => 'password' 
    ); 
    $this->data['password_confirm'] = array(
     'name' => 'password_confirm', 
     'id' => 'password_confirm', 
     'type' => 'password' 
    ); 

    $this->load->view('auth/edit_user', $this->data);  
} 

답변

4

내가 뭔가를 놓치지 않는 한 컨트롤러 내에서 edit_user 기능을 다음과 같이 수정했습니다.

나는이에 ... 사용자가 가 그들을 덤핑 전에 또는 "가 아니라 관리자", "로그인하지 않은"것을 확인합니다이 줄을 ...

if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) 
{ 
    redirect('auth', 'refresh'); 
} 

변경 어느 검사가 ... 사용자가 가 ("사용자가 아닌""가 아니라 관리자") 그들을 밖으로 덤프 전에 OR "로그인하지 않은"것을 볼 수

모두를 편집 할 수 있습니다

이 작동하는 것 같군 ...

  • 관리자는
  • 사용자는 자신의 계정
  • 누군가 어떤 계정을 편집 할 수 없습니다, 로그인하지를 편집 할 수 있습니다 차지하고있다.

편집 : 그러나, 사용자는 설정 "그룹"에 대한 액세스 권한이 단순히 "관리자"그룹에 스스로가를 둘 수 있었다. 안좋다.

이온 인증 기관의 개발자는 "예제"로 제공되는 파일을 참조합니다. 따라서 프로젝트의 필요에 맞게 이온 인증을 편집하는 것은 최종 개발자의 몫입니다.

사용자가 자신을 "관리자"로 만들 수 없도록하려면 edit_user.php보기 파일을 간단히 변경해야합니다.

<?php if ($this->ion_auth->is_admin()): ?> 

    // code that generates Groups checkboxes 

<?php endif ?> 

그런 다음 당신은 또한 철저하게 테스트하고 필요에 따라 조정해야합니다 ... 사용자가 이미 체크 박스를 만들기 전에 "admin"입니다 확인합니다. 예를 들어 사용자 프로필을 수정하면 auth보기로 리디렉션됩니다. 사용자에게 auth보기를 볼 수있는 권한이 없으므로 "관리자 여야 함"오류가 발생합니다. 컨트롤러 파일에서 사용자가 "admin"이 아닌 경우 사용자를 올바르게 리디렉션 할 수있는 적절한 로직을 추가해야합니다.

+1

직접 답변 해 주시고 코드를 게시 해 주셔서 감사합니다. 나는 풀 요청으로 그것을 올렸고 이제는 Ion Auth 저장소에 병합되었습니다. – tagawa

1

없음 이온 인증은 그대로이 작업을 수행하지 않는 주식 이온을 정식 auth.php 컨트롤러에 포함 된 edit_user 기능 ...입니다. 그러나 올바른 방법으로 수행하는 것이 어렵지 않습니다. edit_user 메소드를 가져 와서 관리자 체크를 꺼내서 사용자가 자신의 계정 만 편집 할 수 있도록하고, 현재 사용자 정보 만 업데이트하도록 변경하십시오 로그인 한 사용자.

이온 인증 문서를 확인하고 문제가있을 경우 코드를 입력하십시오.

+0

아무런 문제가 없습니다 ... 단지 결정적인 대답을 찾고 있습니다. 나는 그것을 성공적으로 수정하고 내 대답을 올렸다. 고맙습니다. – Sparky

관련 문제