2012-12-11 70 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