Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多个管理员的Codeigniter角色_Php_Mysql_Codeigniter - Fatal编程技术网

Php 多个管理员的Codeigniter角色

Php 多个管理员的Codeigniter角色,php,mysql,codeigniter,Php,Mysql,Codeigniter,大家好,我需要关于codeigniter角色或权限的帮助。我有一个用户角色(管理员): 数据库中的表用户: id int(11) email varchar(100) password varchar(128) name varchar(100) 在我的管理面板中,我有(page.php控制器)=页面管理,页面顺序,(agent.php控制器)=添加,编辑,删除,(健身房)=添加、编辑、删除,(article.php控制器) 我有21个部分,每个部

大家好,我需要关于codeigniter角色或权限的帮助。我有一个用户角色(管理员):

数据库中的表用户:

id   int(11)    
email    varchar(100)   
password varchar(128)       
name     varchar(100)
在我的管理面板中,我有(page.php控制器)=页面管理,页面顺序,(agent.php控制器)=添加,编辑,删除,(健身房)=添加、编辑、删除,(article.php控制器)

我有21个部分,每个部分我有不止一个治疗,我想要的是给每个部分分配一个管理员,他可以编辑和查看他的部分。所以我将有21个分区管理员和一个(或多个)全局管理员可以管理一切

我在users表中添加了另一个名为type的字段: 类型varchar(50) 它将有两个值:分区管理或全局管理。我搜索了一下,但没有找到教我怎么做的教程

我不知道如何在我的系统中集成角色管理。有人能帮我吗

控件:user.php

    class User extends Admin_Controller
            {

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

                public function index ()
                {
                    // Fetch all users
                    $this->data['users'] = $this->user_m->get();

                    // Load view
                    $this->data['subview'] = 'admin/user/index';
                    $this->load->view('admin/_layout_main', $this->data);
                }

                public function edit ($id = NULL)
                {
                    // Fetch a user or set a new one
                    if ($id) {
                        $this->data['user'] = $this->user_m->get($id);
                        count($this->data['user']) || $this->data['errors'][] = 'User could not be found';
                    }
                    else {
                        $this->data['user'] = $this->user_m->get_new();
                    }

                    // Set up the form
                    $rules = $this->user_m->rules_admin;
                    $id || $rules['password']['rules'] .= '|required';
                    $this->form_validation->set_rules($rules);

                    // Process the form
                    if ($this->form_validation->run() == TRUE) {
                        $data = $this->user_m->array_from_post(array('name', 'email', 'password'));
                        $data['password'] = $this->user_m->hash($data['password']);
                        $this->user_m->save($data, $id);
                        redirect('admin/user');
                    }

                    // Load the view
                    $this->data['subview'] = 'admin/user/edit';
                    $this->load->view('admin/_layout_main', $this->data);
                }

                public function delete ($id)
                {
                    $this->user_m->delete($id);
                    redirect('admin/user');
                }

                public function login ()
                {
                    // Redirect a user if he's already logged in
                    $dashboard = 'admin/dashboard';
                    $this->user_m->loggedin() == FALSE || redirect($dashboard);

                    // Set form
                    $rules = $this->user_m->rules;
                    $this->form_validation->set_rules($rules);

                    // Process form
                    if ($this->form_validation->run() == TRUE) {
                        // We can login and redirect
                        if ($this->user_m->login() == TRUE) {
                            redirect($dashboard);
                        }
                        else {
                            $this->session->set_flashdata('error', 'That email/password combination does not exist');
                            redirect('admin/user/login', 'refresh');
                        }
                    }

                    // Load view
                    $this->data['subview'] = 'admin/user/login';
                    $this->load->view('admin/_layout_modal', $this->data);
                }

                public function logout ()
                {
                    $this->user_m->logout();
                    redirect('admin/user/login');
                }

                public function _unique_email ($str)
                {
                    // Do NOT validate if email already exists
                    // UNLESS it's the email for the current user

                    $id = $this->uri->segment(4);
                    $this->db->where('email', $this->input->post('email'));
                    !$id || $this->db->where('id !=', $id);
                    $user = $this->user_m->get();

                    if (count($user)) {
                        $this->form_validation->set_message('_unique_email', '%s should be unique');
                        return FALSE;
                    }

                    return TRUE;
                }
            }
模型用户_m.php:
                protected $_table_name = 'users';
                protected $_order_by = 'name';
                public $rules = array(
                    'email' => array(
                        'field' => 'email', 
                        'label' => 'Email', 
                        'rules' => 'trim|required|valid_email|xss_clean'
                    ), 
                    'password' => array(
                        'field' => 'password', 
                        'label' => 'Password', 
                        'rules' => 'trim|required'
                    )
                );
                public $rules_admin = array(
                    'name' => array(
                        'field' => 'name', 
                        'label' => 'Name', 
                        'rules' => 'trim|required|xss_clean'
                    ), 
                    'email' => array(
                        'field' => 'email', 
                        'label' => 'Email', 
                        'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
                    ), 
                    'password' => array(
                        'field' => 'password', 
                        'label' => 'Password', 
                        'rules' => 'trim|matches[password_confirm]'
                    ),
                    'password_confirm' => array(
                        'field' => 'password_confirm', 
                        'label' => 'Confirm password', 
                        'rules' => 'trim|matches[password]'
                    ),
                );

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

                public function login ()
                {
                    $user = $this->get_by(array(
                        'email' => $this->input->post('email'),
                        'password' => $this->hash($this->input->post('password')),
                    ), TRUE);

                    if (count($user)) {
                        // Log in user
                        $data = array(
                            'name' => $user->name,
                            'email' => $user->email,
                            'id' => $user->id,
                            'loggedin' => TRUE,
                        );
                        $this->session->set_userdata($data);
                    }
                }

                public function logout ()
                {
                    $this->session->sess_destroy();
                }

                public function loggedin ()
                {
                    return (bool) $this->session->userdata('loggedin');
                }

                public function get_new(){
                    $user = new stdClass();
                    $user->name = '';
                    $user->email = '';
                    $user->password = '';
                    return $user;
                }

                public function hash ($string)
                {
                    return hash('sha512', $string . config_item('encryption_key'));
                }
            }

有太多的方法可以将权限系统合并到您的项目中,这完全取决于您需要什么。我将为您的案例提供一个基本想法,如果我正确理解您的问题,我将如何做:

  • 是的,您可以向用户表中添加另一个字段并将其称为角色
  • 在分区表中添加用户id字段。这是您将用户与节连接的方式
  • 用户登录后,请确认该用户是否为section_用户,如果是,则需要根据该用户的id从db中提取正确的section
  • 如果不是,则表示它是一个全局_管理员,然后显示所有部分
  • 我不确定我是否正确理解了你的问题


    让我知道。

    省去麻烦,用这个:。例如,您将拥有所需的所有管理员类型的角色和权限。

    我不确定您到底想要实现什么,但我将大致解释一下我将做什么:

    1)定义URL方案

    例如,如果您有一个汽车爱好者网站,每个品牌可能都有自己的部分:

    somesite.com/section/honda
    somesite.com/section/ford
    somesite.com/section/toyota
    
    这些URL段塞(本田、福特、丰田等)实际上成为您试图访问的部分的标识符。每一个都是独一无二的

    然后,您需要确保/section/之后的每个slug都是一个参数,而不是一个函数调用。您可以通过进入/application/config/routes.php并定义如下路由来完成此操作:

    $route['section/(:any)'] = section_controller/$1; 
    // $1 is the placeholder variable for the (:any) regex. So anything that comes after /section will be used as a parameter in the index() function of the section_controller class. 
    
    class Section extends CI_Model
    {
        public $section_name;
        public $section_id;
    
        public function loadByName($section_name)
        {
             $query = $this->db->select('section_id', 'section_name')
                            ->from('section')
                            ->where('section_name', $section_name);
    
             $row = $query->row();
    
             $this->section_name = $row->section_name;
             $this->section_id = $row->section_id;
    
             return $row;
        }
    
        public function loadById($section_id)
        {
             $query = $this->db->select('section_id', 'section_name')
                               ->from('section')
                               ->where('section_id', $section_id);
    
             $row = $query->row();
    
             $this->section_name = $row->section_name;
             $this->section_id = $row->section_id;
    
             return $row;
        }
    }
    
    2。创建一个名为“section”的新数据库和相应的模型

    现在只需给它两个字段:*section\u id*和*section\u name*。这将存储每个独特的部分。模型的代码如下所示:

    $route['section/(:any)'] = section_controller/$1; 
    // $1 is the placeholder variable for the (:any) regex. So anything that comes after /section will be used as a parameter in the index() function of the section_controller class. 
    
    class Section extends CI_Model
    {
        public $section_name;
        public $section_id;
    
        public function loadByName($section_name)
        {
             $query = $this->db->select('section_id', 'section_name')
                            ->from('section')
                            ->where('section_name', $section_name);
    
             $row = $query->row();
    
             $this->section_name = $row->section_name;
             $this->section_id = $row->section_id;
    
             return $row;
        }
    
        public function loadById($section_id)
        {
             $query = $this->db->select('section_id', 'section_name')
                               ->from('section')
                               ->where('section_id', $section_id);
    
             $row = $query->row();
    
             $this->section_name = $row->section_name;
             $this->section_id = $row->section_id;
    
             return $row;
        }
    }
    

    3。在用户表中,创建一个名为*section\u id*的附加字段。

    这将是对他们作为管理员的部分ID的引用。例如,如果Toyota section_id为381,则使用381作为用户表中section_id字段中的数字

    4。请求页面时,根据slug名称查找节id。

    在控制器文件中,您应该在index()方法的某个地方加载节模型,如下所示:

    class Section_controller extends CI_Controller
    {
    
        public function index($section_name)
        {
              // I will assume you've already loaded your logged in User somewhere
    
              $this->load->model('Section');
              $this->Section->loadByName($section_name);
    
              if ($this->User->section_id == $this->Section->section_id)
              {
                  // Render the page with appropriate permissions
              }
              else
              {
                  // Throw an error
              }
        }
    }
    
    我不会再详细说明做这一切的细节;您必须阅读Codeigniter文档,了解如何处理路由、控制器、DB查询等

    如果您只有两个角色,那么它可以轻松实现。您知道该用户是否为admin如果user>为admin,则会激活admin拥有访问权限的所有部分。如果用户是,则他将无法>获得访问权限

    若您可以放心使用tankauth身份验证库,若您有足够的时间执行此任务,请转到tankauth

    您还可以使用篝火(HMVC)进行用户身份验证


    但我必须为每个部分都有自己的控制器,我必须有一个用户(全局用户=全局管理员),可以管理所有部分,还可以访问页面控制器和注册控制器。例如,我有22个部分,每个部分都有自己的库、程序、成员、规则、活动,解决你的网站架构是我认为有点太超出范围,所以答案。但解决问题归结起来是:1。通过其唯一ID或名称2了解您所在的分区。确定管理员应该管理的分区的ID 3。比较这两个4。当然,在if语句中包含全局管理员权限和一个简单的条件是非常容易的。链接中解释了这一点,您需要将代码集成到您的项目中。