2012-12-11 68 views
5

我正在使用最新的Ion Auth文件以及最新版本的CodeIgniter 2CodeIgniter 2和Ion Auth - 編輯自己的用戶帳戶

有一個在auth.php控制器文件中稱爲edit_user功能。此功能僅限於由成員「管理員」組中只使用,任何管理員成員可以編輯通過這個URL使用功能的任何其他成員...

/auth/edit_user/id 

的問題是,我不看不到任何控制器功能或視圖使常規(非管理員)用戶編輯自己賬戶細節。

這將是一個新的控制器功能,我需要寫(修改edit_user功能?)或者是這個東西,離子驗證應該已經辦?如果是這樣,怎麼樣?

這裏是股票離子驗證包含在auth.php控制器內edit_user功能...

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

除非我錯過了一些東西,否則我最終修改了auth.php控制器中的edit_user函數,如下所示。

我改變了這行檢查,看看用戶是 OR 「不是管理員」「沒有登錄」傾倒出來之前......

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

...這個,看看哪些檢查用戶是(「沒有用戶」「不是管理員」)傾倒出來之前或「未登錄」 ......

if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !($this->ion_auth->user()->row()->id == $id))) 

這似乎是工作...

  • 管理員可以編輯所有帳戶的
  • 用戶只能編輯自己的賬戶
  • ,有人沒有登錄,無法修改任何帳戶。

編輯:但是,用戶也可以訪問「組」設置,可以簡單地把自理進入「管理員」組。不好。

Ion Auth的開發人員將他提供的文件稱爲「示例」。因此,最終開發人員需要編輯Ion Auth以滿足項目需求。

爲防止用戶能夠使自己成爲「管理員」,需要對edit_user.php視圖文件進行簡單更改。

驗證用戶已經創建複選框前的「管理員」 ......

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

    // code that generates Groups checkboxes 

<?php endif ?> 

那麼你還需要進行全面測試,並根據需要進行調整。例如,編輯用戶配置文件後,您將被重定向到auth視圖。由於用戶沒有權限查看auth視圖,因此存在「必須是管理員」錯誤。在控制器文件中,當用戶不是「管理員」時,您必須添加適當的邏輯來正確重定向用戶。

+1

感謝您親自回答併發布代碼。我將其作爲拉取請求發佈,現在它已合併到Ion Auth存儲庫中。 – tagawa

1

沒有離子驗證不這樣做,因爲就是 - 它的重量很輕。但是,這並不難做到和你在正確的軌道上,只要抓住這edit_user方法,並採取了管理員檢查,並使它所以用戶只能修改自己的賬號,只是改變它,使它只更新用戶信息用於當前登錄用戶。

檢查離子AUTH文檔,必須在它的裂縫,並與一些代碼回來,如果你有任何問題。

+0

沒問題...只是尋找一個確定的答案。我成功修改了它併發布了我的答案。謝謝。 – Sparky