2013-04-22 2 views
0

현재 사용자가 자신의 사용자 영역을 등록 및 로그인하고 메모 스 니펫을 추가/편집/삭제할 수있는 프로젝트 작업 중입니다.Codeigniter 사용자 기능

현재 편집 클래스에서 작업 중이며 다른 사용자가 동일한 URL을 방문하여 다른 사람의 메모를 수정할 수 없도록 만드는 방법이 궁금합니다. ,

스키마 = 아이디, 제목, 설명, 미리보기 (모든 노트는 데이터베이스에서 같은 테이블에 저장됩니다) USER1 그의 user_id를 결합하는 (http://domain.com/edit/1에서 자신의 메모를 편집하고자하는 경우, 예를 들어

을 USER_ID 데이터베이스에서) user2가 동일한 URL을 방문하여 그의 노트를 편집하는 것을 어떻게 멈출 수 있습니까?

<?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(); ?> 

다른 사용자가 시도하면 내가 그래서를 제한 할 수있는 방법이 있습니다 :을 heres보기

<?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); 
} 




} 

: 여기

모델을 heres 컨트롤러

<?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); 
} 
} 

입니다 해당 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

메신저 탱크 인증을 사용하여 인증하고 데이터베이스에 저장하여 세션을 처리하면이 작업이 가능합니까? 나는 couldnt한다 위의 일을 얻는다. 신속한 대응에 감사드립니다. 필자의 게시물을 함수 – jackthedev

+0

의 전체 MVC로 업데이트했습니다. 그래도 잘 작동합니다. 그러나 모델의 get_snippet 메소드에서 user_id를 쿼리해야합니다. 지금 당장은 이드와 타이틀 만 선택하고 있습니다. 그것을 시도해보십시오 $ this-> db-> select ('id, title, user_id'); – stormdrain

+0

여전히 운이 좋으면 user_id가 같더라도/mysnippets /로 리디렉션됩니다. – jackthedev

0

당신은 당신이 로그인 한 경우 편집중인 ID가 제공하는 세션 ID와 동일 여부를 확인할 수 있습니다

이 될 수있는 일 같은 :

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

나는 또한 도움을 주셔서 감사합니다. – jackthedev

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(); 
} 

이렇게하면 정보에 액세스 할 수 없습니다. ,하지만 나는 그들에게 처음부터 시도 할 수 없도록, 즉 그들에게 선택권을주지 못하게하기 위해 무언가를 할 것입니다.

관련 문제