admin和super_admin的基于角色的访问控制系统


Role base access control system for admin and super_admin

我正试图得到这个结果->对两种用户类型使用访问控制逻辑:管理员和超级管理员。

管理员将具有对系统内所有记录的读取访问权限,但他们只能对自己创建的记录进行编辑/删除访问。

超级管理员将拥有对所有记录的读取/编辑/删除权限。在这种情况下,我应该使用什么?如果有人知道如何在上面的情况下以简单的方式给予回滚访问控制,那么请告诉我如何做到这一点?

从adminlogin.php登录后,我的页面会出现在这里。。。这是我的控制器页面。。

listing.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Listing extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('student');
    $this->load->helper('url');
    $this->load->helper('form');
    $s = $this->session->userdata('admin_id');
    log_message('error', 'Some variable did not contain a value.');
}
public function index()
{
    $s = $this->session->userdata('admin_id');
    $this->load->model('student',$s);
   //$data['result'] = $this->student->listing();
    $students = $this->student->listing();/////new line delete [resulet]time 5:42 29/03/16
     //$this->load->view('list_view',$data); //// change here time 5:52 29/03/16
    $this->load->view('list_view',array('students'=>$students)); /////listing->list_view name change
}   
public function delete($id)
{
    $result = $this->student->delete_operation($id);
    $s = $this->session->userdata('admin_id');// session data call.
    //$data['result'] = $this->student->listing();
    $students = $this->student->listing();///new line 30/03 1230pm// change for list_view
    $this->load->view('list_view',array('students'=>$students));///same as above//change for list_view
    //$this->load->view('list_view',$data); ////////////////////////listing->list_view name change
} 
public function edit($id)
{               

    if($id)
    {
        $s = $this->session->userdata('admin_id');
        $result = $this->student->edit_record($id);   
        $data['action'] = 'edit';
        $data['student_id'] = $result[0]->student_id;
        $data['student_name'] = $result[0]->student_name;
        $data['student_email'] = $result[0]->student_email;
        $data['student_address'] = $result[0]->student_address;
        $data['subject'] = $result[0]->subject;
        $data['marks'] = $result[0]->marks;
    }
    $this->load->view('edit_student',$data);   
}   
public function add_student()
{       
    //$s['user'] = $this->session->userdata('admin_id');//get session data // new line30/03/16
    $data['student_id'] = '';
    $data['student_name'] = '';
    $data['student_email'] = '';
    $data['student_address'] ='';
    $data['subject'] = '';
    $data['marks'] = '';
    //$data['admin_id']=''; //new line 12:39 30/03/16
    $this->load->view('edit_student',$data);           
}
public function add()
{
    $data = array(
    'student_name' => $this->input->post('txt_name'),
    'student_email' => $this->input->post('txt_email'),          
    'student_address' => $this->input->post('txt_address'),
    'subject' => $this->input->post('subject'),
    'marks' => $this->input->post('marks'),
    'admin_id' => $this->input->post('admin_id')//new line 12:39 31/03
    );
    $result = $this->student->add_record($id,$data);
    header('location:'.base_url().'index.php/listing');
}

}

最好的方法可能是在系统中使用一些角色,例如,您可以使用Ion auth库:
http://benedmunds.com/ion_auth/

有了它,您可以定义用户组(例如:用户、管理员、超级管理员)您可以查看手册的ingroup()部分,了解它的工作原理
一个让你了解如何检查记录删除的示例功能:

function hasDeleteRight($record_author_id, $logged_in_user_id) {
    // if the user has administrator role we check if he is the author of the record he can delete it
    if ($this->ion_auth->in_group('administrator', $logged_in_user_id)) {
        if($record_author_id == $logged_in_user_id) {
            return true;
        }
    // if the user has superadministrator role he anyway can delete the record
    } elseif ($this->ion_auth->in_group('superadministrator', $logged_in_user_id)) {
        return true;
    }
    // other users cannot delete the record
    return false;
}

您仍然可以使用此示例作为函数的基础。

代码中的用法:

public function delete($id)
{
    $logged_user_id = $this->session->userdata('admin_id');
    if(!hasDeleteRight($id, $logged_user_id))
    {
        return false;
    }
    //....your delete record code

更新:
权限检查无需离子身份验证,仅使用会话数据和独立登录(不是首选方式):
在超级管理员登录代码中,您可以将权限放入会话:

function super_admin_login() {
    //your log in code
    if($login_success) {
        $this->session->set_userdata('permission', 'superadministrator');
    }
}

类似于正常管理员登录:

function admin_login() {
    //your log in code
    if($login_success) {
        $this->session->set_userdata('permission', 'administrator');
    }
}

function hasDeleteRight($record_author_id, $logged_in_user_id) {
    // if the user has administrator role we check if he is the author of the record he can delete it
    if ($this->session->userdata('permission') == 'administrator') {
        if($record_author_id == $logged_in_user_id) {
            return true;
        }
    // if the user has superadministrator role he anyway can delete the record
    } elseif ($this->session->userdata('permission') == 'superadministrator') {
        return true;
    }
    // other users cannot delete the record
    return false;
}