Phân quyền Permissions bằng code PHP CI

Phân quyền Permissions bằng code PHP CI
  1. controller
<?php
/**
 *--Project: lavarel
  --Design by: ATC PRO
  -- Date time: 26/4/2022 10:05:32 AM
 * This controller handles project management.
 */
class Permissions extends CI_Controller
{
    public $user_id;
    public $user_name;

    public function __construct(Type $foo = null)
    {

        parent::__construct();
        
        if (!$this->session->userdata('logged_in')) {
            $this->session->set_flashdata('flash_danger', 'Vui lòng đăng nhập để xem trang');
            header('location: ' .base_url().'backend/login');
        }
        else
        {
         $user_id = $this->session->userdata('user_id');
         $user_name = $this->session->userdata('user_name');
        
        }
        $this->load->model('permissions_model');
       
    }

    public function index()
    {
       
        $this->load->view('back_end_template/content', [
            'template' => 'backend/permissions/index','sl'=>1,
            'data' => $this->permissions_model->get_permissions()
             
        ]);
    }

    public function create()
    {
 $data = $this->permissions_model->get_where_permissions('0');
            $this->load->view('back_end_template/content', ['template' => 'backend/permissions/create','data' => $data  ]);
        
      }
    public function add()
    {
       
       
        
            $data = $this->permissions_model->create_permissions([
            'id' => $this->input->post('id'),
            'title' => $this->input->post('title'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

               
            ]);

            if ($data) {
                $this->session->set_flashdata('flash_success', 'Your permissions has been created');
                return header('location: ' .base_url().'backend/permissions/show/'.$data->id);
            }
        
    }

     public function search(){
        $data['template'] = 'backend/permissions/index';
        $keyword = $this->input->post('keyword');   
        $column = $this->input->post('column');   
        $list_col = array('id' => $keyword,'title' => $keyword,'created_at' => $keyword,'updated_at' => $keyword,'deleted_at' => $keyword,); 
        if($column!="")
        {
          $columns="permissions.$column";
          $list_col =array($columns => $keyword);
         }
        $config['$keyword'] = $keyword;
        $config['$columnsearch'] = $column;  
        $config['base_url'] = site_url('backend/permissions/search/');
        $config['total_rows'] = $this->get_model->atc_get_count_search('permissions',$list_col);
        $data['sl'] = $config['total_rows'];
        $config['per_page'] = 10;  
        $config['uri_segment'] = 3;
        $config['cur_tag_open'] = '<a class="current_page" href="backend/permissions/">';
        $config['cur_tag_close'] = '</a>';
        $this->pagination->initialize($config);
        
        $data['data'] = $this->get_model->atc_get_search('permissions',$list_col,$config['per_page'], $this->uri->segment($config['uri_segment'])); 
        $data['link'] = $this->pagination->create_links();
        
    
        
        $this->load->view('back_end_template/content', $data);
        
    }
    public function show($id)
    {
        $data = $this->permissions_model->get_where_permissions($id);
        if (!$data) {
            $this->denied_message();

           return header('location: ' .base_url().'backend/permissions');
        }
        $this->load->view('back_end_template/content', [
            'template' => 'backend/permissions/show',
            'data' => $data 
         
        ]);
    }

    public function edit($id)
    {
        $data = $this->permissions_model->get_where_permissions($id);
        if (!$data) {
            $this->denied_message();
         	return	header('location: ' .base_url().'backend/permissions');
           
        }
     
     
        
            $this->load->view('back_end_template/content', ['template' => 'backend/permissions/edit', 'data' => $data ]);
       
    }

    public function update()
    {
        
            $data = [
            'id' => $this->input->post('id'),
            'title' => $this->input->post('title'),
            'created_at' => $this->input->post('created_at'),
            'updated_at' => $this->input->post('updated_at'),
            'deleted_at' => $this->input->post('deleted_at'),

              
            ];
         	$ID=$this->input->post('id');
            $url=$this->input->post('urlre');
            if ($this->permissions_model->update_permissions( $data,$ID)) {
                $this->session->set_flashdata('flash_success', 'Your permissions has been updated');
                if($url=="save")
               {
                   return header('location: ' .base_url().'backend/permissions/');
               }
                return header('location: ' .base_url().'backend/permissions/show/'.$ID);
                }
           
        
    }
     public function deleteall()
    {
    
         $con = $this->input->post('id_check');
         $num_array = count($con);

            if($num_array>0){
                     for($i=1;$i<=$num_array;$i++)
                     {
                     $id = $con[$i-1];
                     $this->permissions_model->delete_permissions($id);
                     }
                      $this->session->set_flashdata('flash_success', 'Tất cả đã xóa thành công !');
              }
               return  header('location: ' .base_url().'backend/permissions');
    
    }
    public function delete($id)
    {
       if(isset($_POST['id']))
       {
          $id=$_POST['id'];
        }
        $data= $this->permissions_model->get_where_permissions($id);
        if (!$data) {
            $this->denied_message();

          return  header('location: ' .base_url().'backend/permissions');
        }
        if ($this->permissions_model->delete_permissions($id)) {
            $this->session->set_flashdata('flash_success', 'Your permissions has been deleted');
        } else {
            $this->denied_message();
        }
         return   header('location: ' .base_url().'backend/permissions');

    }

    private function denied_message()
    {
        $this->session->set_flashdata('flash_danger', 'That permissions does not exist or you do not have permission');
    }
}

2. View

 
 <!-- Content Header  -->
<section class="content-header">
      <h1>
       <a href="/admin/permissions/index">
         <button type="button" class="btn btn-flat btn-success"><i class="fa fa-mail-reply-all"></i>&nbsp;Trở về</button>
       </a>
 permissions Index
        
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">permissions</a></li>
        <li class="active"> permissions Index</li>
      </ol>
</section>
 <!-- Main content -->
 <section class="content">
    
  <div class="row">
  @if(ViewBag.alert!=null) {
    <div class="alert [email protected] alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa [email protected]"></i> Thông báo!</h4>
  @ViewBag.alert
</div>
}
    <!--  column -->
     <div class="box">
        <div class="box-body">
            <div class="panel panel-primary">
                <div class="panel-heading">
                  <h3 class="panel-title"><i class="fa fa-plus"></i> permissions Index</h3>
                </div>
              <div class="box-body">
<form action='<?=base_url()?>backend/permissions/Add' method='post' id='form_permissions'>
<div class="box-body">
<input   id='urlre' value='' name='urlre'  type='hidden'  />    <div class="form-group">
    <label for="Input_id">id</label>
  <input  placeholder="id" class="form-control" id="id" name="id"   required='required' value='<?php if(isset($data[0]->id)) echo $data[0]->id;?>' type="text" >
  </div>
    <div class="form-group">
    <label for="Input_title">title</label>
  <input  placeholder="title" class="form-control" id="title" name="title"   required='required'   value='<?php if(isset($data[0]->title)) echo $data[0]->title;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_created_at">created_at</label>
  <input  placeholder="created_at" class="form-control" id="created_at" name="created_at"   required='required'   value='<?php if(isset($data[0]->created_at)) echo $data[0]->created_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_updated_at">updated_at</label>
  <input  placeholder="updated_at" class="form-control" id="updated_at" name="updated_at"   required='required'   value='<?php if(isset($data[0]->updated_at)) echo $data[0]->updated_at;?>'  type="text" >
  </div>
    <div class="form-group">
    <label for="Input_deleted_at">deleted_at</label>
  <input  placeholder="deleted_at" class="form-control" id="deleted_at" name="deleted_at"   required='required'   value='<?php if(isset($data[0]->deleted_at)) echo $data[0]->deleted_at;?>'  type="text" >
  </div>

<div class="box-footer"> <button type="submit" class="btn btn-primary">Save changes</button></div>
</form>
              </div>
            </div>
        </div>
      </div>
 </section>

3. Model

<?php

/**
 *--Project: lavarel
  --Design by: ATC PRO
  -- Date time: 26/4/2022 10:07:12 AM
 */
class Permissions_model extends CI_Model
{
    public function create_Permissions($data)
    {
        $this->db->insert('Permissions', $data);
        $id=$this->db->insert_id();
        if($id==0)
        {
            return base_url().'backend/Permissions';
        }
        return $this->db->get_where('Permissions', ['id' => $id])->row();
    }

    public function get_Permissions()
    {
        $this->db->select('Permissions'.'.*', FALSE);
        $this->db->from('Permissions');
        $query = $this->db->get();  
        return $query->result();
        
    }
    public function get_where_Permissions($id)
    {
        return $this->db->get_where('Permissions', ['id' => $id])->result();
    }

    public function get_Permissions_where( $id)
    {
        return $this->db->get_where('Permissions', ['id' => $id])->row();
    }

    public function update_Permissions( $data, $id)
    {
        return $this->db->where(['id' => $id])->update('Permissions', $data);
    }

    public function delete_Permissions( $id)
    {
       
        return $this->db->where(['id' => $id])->delete('Permissions');
    }

    
}

Chúc các bạn thành công

nguyen tran

Leave a Reply

Your email address will not be published. Required fields are marked *