【问题标题】:how to check user permission given to user (code igniter ) [duplicate]如何检查授予用户的用户权限(代码点火器)[重复]
【发布时间】:2012-10-01 18:14:38
【问题描述】:

可能重复:
CodeIgniter authentication + user privileges

我有 5 种用户类型和权限表,我在其中向不同的用户授予不同的权限。 is_view、is_delete、is_add 等权限。用户根据这些权限访问该功能。

我完成了数据库。我想在调用控制器之前检查每个页面上授予用户的权限。

【问题讨论】:

  • 在调用控制器之前?!但是你要如何在没有控制器的情况下检查 db 中的用户权限?
  • @BhuvanRikka 通过制作一个通用模型。并在每次控制器调用之前检查它。或使用钩子。有没有可能

标签: php database codeigniter


【解决方案1】:

您应该将您的身份验证逻辑放在控制器的构造函数中

在基本控制器的构造函数中(更干燥,因为您不必在所有控制器中重复逻辑)。

【讨论】:

    【解决方案2】:

    我将创建一个扩展核心控制器的新控制器。将此文件放在application/core/

    class MY_AuthController extends CI_Controller {
        public function __construct() {
            // Do your auth check in here, redirect if not logged in
        }
    }
    

    然后所有需要身份验证的页面都继承这个新控制器。您只需将此文件放在常规控制器文件夹中

    class Admin extends MY_AuthController {
        // All your controller goodness in here..
    }
    

    【讨论】:

      【解决方案3】:

      我建议你阅读以下两篇文章:

      1。 Phil Sturgeon 在Keeping It Dry 上的帖子。

      Phil 将向您介绍如何创建父控制器,其构造函数将包含会话和潜在的数据库逻辑。此后创建的所有控制器都应继承自您的自定义控制器,而不是本机 CI_Controller

      后面是....

      2。 Shane Pearson 的CodeIgniter Base Classes Revisited

      Shane 的文章改进了 Phil 的技术并将您的自定义控制器从 /core 重新定位到 /base,并且还使用了更好的 __autoload()'er。例如,这个实现允许我使用 CodeIgniter 的 CLI 类,而 Phil 却被排除在外。


      给你一个想法 - 一旦完成,你的代码看起来会有点像这样:

      /base/MY_In_Controller.php:

      <?php
      class MY_In_Controller extends CI_Controller{
          function __construct(){
              parent::__construct();
              //things like:
              //is the user even logged in? thank heavens I don't have to check this in every controller now. redirect if the session doesnt exist.
              //query the database and grab the permissions for the user. persist them with $this->load->vars();
              $this->data['perms'] = some_database_function();
              $this->load->vars($this->data);
          }
      }
      

      controllers/manage.php:

      <?php
      class Manage extends MY_In_Controller{
          function __construct(){
              parent::__construct();
          }
          function index(){
              $this->load->view('manage');
              //and I can still access their permissions here and in the view.
              print_r($this->data['perms']);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多