2013-04-22 45 views
0

即時工作在一個項目上,允許用戶註冊並登錄到自己的用戶區並添加/編輯/刪除筆記片段。Codeigniter用戶功能

我目前正在編輯類和即時通訊想知道我怎麼能讓其他用戶不能訪問相同的網址和編輯someones筆記? (所有音符都存儲在同一個數據庫中的表)

模式= ID,標題,描述,摘要,user_id說明

例如,如果用戶1希望在http://domain.com/edit/1編輯其說明(這勢必對他的USER_ID在數據庫中)我怎樣才能阻止user2訪問相同的網址和編輯他的筆記?

這裏是控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Mysnippets extends CI_Controller { 

function __construct() 
{ 
    parent::__construct(); 

    if (!$this->tank_auth->is_logged_in()) { 
     redirect('/login/'); 
    } 

    $this->load->model('dashboard_model'); 

    $this->data['user_id'] = $this->tank_auth->get_user_id(); 
    $this->data['username']= $this->tank_auth->get_username(); 
} 

public function index() 
{ 
    $this->data['private_snippets'] = $this->dashboard_model->private_snippets(); 
    $this->load->view('dashboard/my_snippets', $this->data);  
} 

function edit_snippet($snippet_id) { 

    $snippet = $this->dashboard_model->get_snippet($snippet_id); 

    //validate form input 
    $this->form_validation->set_rules('title', 'Title', 'required'); 

    if (isset($_POST) && !empty($_POST)) 
    {  
     $data = array(
      'title' => $this->input->post('title'), 
     ); 

     if ($this->form_validation->run() === true) 
     { 
      $this->dashboard_model->update_snippet($snippet_id, $data); 
      $this->session->set_flashdata('message', "<p>Product updated successfully.</p>");     
      redirect(base_url().'mysnippets/edit_snippet/'.$snippet_id); 
     }   
    } 

    $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message')); 

    $this->data['snippet'] = $snippet; 

    //display the edit product form 
    $this->data['title'] = array(
     'name'  => 'title', 
     'type'  => 'text', 
     'value'  => $this->form_validation->set_value('title', $snippet['title']), 
    ); 

    $this->load->view('dashboard/edit_snippet', $this->data); 
} 
} 

繼承人的模型:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Dashboard_model extends CI_Model { 

public function public_snippets() 
{ 
    $this->db->select('id, title, description, author, date_submitted'); 
    $query = $this->db->get_where('snippets', array('state' => 'public')); 
    return $query->result_array(); 
} 

public function private_snippets() 
{ 
    $this->db->select('id, title, description, date_submitted'); 
    $query = $this->db->get_where('snippets', array('user_id' => $this->tank_auth->get_user_id())); 
    return $query->result_array(); 
} 

public function add_snippet($data) 
{ 
    $this->db->insert('snippets', $data); 
    $id = $this->db->insert_id(); 
    return (isset($id)) ? $id : FALSE; 
} 

public function get_snippet($snippet_id) { 
    $this->db->select('id, title'); 
    $this->db->where('id', $snippet_id); 
    $query = $this->db->get('snippets'); 

    return $query->row_array(); 
} 

public function update_snippet($snippet_id, $data) 
{ 
    $this->db->where('id', $snippet_id); 
    $this->db->update('snippets', $data); 
} 




} 

繼承人的視圖:

<?php echo $message;?> 

    <?php $snippet_id = $snippet['id']; ?> 
    <?php echo form_open("mysnippets/edit_snippet/$snippet_id");?> 


    <?php echo form_input($title); ?> 
    <?php echo form_submit('submit', 'Submit');?> 

    <?php echo form_close(); ?> 

是有辦法可以限制它,所以如果另一個用戶嘗試去那個URL我可以重定向他們或顯示錯誤信息

回答

0

像這樣的東西可能會起作用。 。

public function edit_snippet(snippet_id) 
{ 
    $snippet = $this->dashboard_model->get_snippet($snippet_id); 

    // this depends on what you are using for sessions; 
    // recommend you use db sessions 
    if($snippet->user_id != $this->session->userdata('user_id');) 
    { 
     redirect('/mysnippets'); 
    } 
    else 
    { 
     //allow editing 
+0

即時消息使用坦克認證進行身份驗證,並通過將它們存儲在數據庫中處理會話,這將工作嗎?我無法得到上述工作。感謝您的迅速回應。我用功能 – jackthedev 2013-04-23 08:44:31

+0

Yup的完整MVC更新了我的帖子,這將工作正常。但是在你的模型的get_snippet方法中,你還需要查詢user_id。現在,你只選擇ID和標題。試用$ this-> db-> select('id,title,user_id'); – stormdrain 2013-04-23 11:27:23

+0

仍然沒有運氣只是重定向回/ mysnippets /即使user_id等於。 – jackthedev 2013-04-23 12:03:32

0

你可以檢查你正在編輯的ID是否相同,提供的會話ID,當你已經登錄

它可能是這樣的:

if ($snippet_id != $this->session->userdata('login_id')) 
{ 
    //redirect to another page 
} 
+0

我也試過這個沒有運氣,感謝您的幫助。 – jackthedev 2013-04-23 09:13:21

0

我只想在模型中添加一行到以下功能:

public function get_snippet($snippet_id) { 
    $this->db->select('id, title'); 
    $this->db->where('id', $snippet_id); 
    //users can access only their own snippets 
    $this->db->where('user_id', $this->session->userdata('user_id')); 
    $query = $this->db->get('snippets'); 
    return $query->row_array(); 
} 

阻止他們訪問信息,但我會做一些事情來阻止他們甚至能夠首先嚐試,即不給他們選擇。