【问题标题】:Codeigniter roles for more than one admin多个管理员的 Codeigniter 角色
【发布时间】:2013-07-20 00:36:10
【问题描述】:

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

数据库中的表用户:

id   int(11)    
email    varchar(100)   
password varchar(128)       
name     varchar(100)

在我的管理面板中,我有 (page.php controller)=page management, page order, (agent.php controller) = add,edit,delete... , (gyms) = add,edit,delete... ,(article.php 控制器)

我有 21 个部分,对于每个部分我有多个处理,我想要为每个部分分配一个管理员,而不是只能编辑和查看他的部分。所以我将拥有 21 个 section_admin 和一个(或更多) global_admin 来管理所有内容

我在名为 type 的 users 表中添加了另一个字段: 输入 varchar(50) 它将有两个值 section_admin 或 global_admin。我进行了搜索,但没有找到任何教程告诉我如何做到这一点。

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

控制器: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;
                }
            }

模型 user_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'));
                }
            }

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    您可以通过多种方式将权限系统整合到您的项目中,这完全取决于您的需求。如果我理解你的问题,我会给你一个关于你的案例的基本想法:

    1. 是的,您可以在用户表中添加另一个字段并将其命名为角色
    2. 在您的部分表中添加一个 user_id 字段。这就是您将用户与部分联系起来的方式。
    3. 一旦用户登录,验证该用户是否为 section_user,如果是,您需要根据该 user_id 从 db 中提取正确的部分。
    4. 如果不是,则表示它是 global_admin,然后显示所有部分。

    我不确定我是否理解你的问题。

    告诉我。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 链接中有说明,需要在项目中集成代码。
      【解决方案3】:

      我不确定你想要达到什么目的,但我会大致解释一下我会做什么:

      1) 定义 URL 方案

      例如,如果您有一个面向汽车爱好者的网站,则每个品牌可能都有自己的版块:

      somesite.com/section/honda
      somesite.com/section/ford
      somesite.com/section/toyota
      

      那些 URL slug(honda、ford、toyota 等)实际上成为您尝试访问的部分的标识符。每一个都是独一无二的。

      然后,您需要确保 /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. 
      

      2。创建一个名为“section”的新数据库,以及相应的模型

      现在只需给它两个字段:*section_id* 和 *section_name*。这将存储每个独特的部分。模型的代码是这样的:

      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_id*

      的附加字段

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

      4.请求页面时,根据 slug 名称查找 section_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 文档以掌握如何处理路由、控制器、数据库查询等。

      【讨论】:

      • 但我必须为每个部分拥有他自己的控制器,并且我必须有一个用户(全局用户 = 全局管理员)可以管理所有部分并且还可以访问页面控制器和注册控制器,例如.
      • 我有 22 个版块,每个版块都有自己的画廊、节目、成员、规则、活动、档案。
      • 解决您的网站架构问题我认为有点超出了 SO 答案的范围。但解决问题归结为: 1. 通过唯一 ID 或名称了解您所在的部分 2. 确定您的管理员应该管理的部分的 ID 3. 比较两者 4. 当然在 if 语句中包含一个简单条件的全局管理员权限非常容易。
      【解决方案4】:

      如果您只有 2 个角色,那么它可以轻松实现。您知道用户是否是管理员,如果用户 > 是管理员,那么它会激活管理员有权访问的所有部分。如果用户是,那么他将无法>获得访问权限。

      如果您愿意使用 tankauth 身份验证库,如果您有足够的时间来完成任务,请转到 tankauth。

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

      【讨论】:

        猜你喜欢
        • 2017-02-15
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        相关资源
        最近更新 更多