Codeigniter:使用继承来控制登录权限 [英] Codeigniter: Controlling log in privileges with inheritance

查看:125
本文介绍了Codeigniter:使用继承来控制登录权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有些注意事项:



1 )有两个外部的控制器,不需要验证。一个用于信息(splash_pages等),另一个用于创建登录会话。



2)所有其他控制器从主控制器继承,作为其构造函数的一部分



到目前为止,上面的2适用于登录和注销。



在代码方面:



主控制器正在描述2)
这是位于Core文件夹Codeigniter

  class MY_Controller extends CI_Controller 
{
public function __construct()
{
parent :: __ construct();
$ this-> load-> library('cart');
$ this-> load-> library('session');
$ this-> load-> helper('form');
$ this-> load-> library('form_validation');
if(!$ this-> session-> userdata('loggedin')){
redirect('/ sessions / log_in /','refresh');
}
}

使用登录系统的类:

  class Records extends MY_Controller {

public function __construct()
{
parent :: __构造();
$ this-> load-> model('some_model');
$ this-> load-> library('some_library');
}

想法是在对象构造时,它将检查用户是否记录是否正确构建对象或重定向到屏幕中的日志。



但是,项目的需求已经改变了一点。现在的要求陈述了布置了大约6个不同的用户组,其特权可以被布置成子集。 A可以做我,B可以做A + II,C可以做B + III,等等。有一些小小的暗示,可能有一些特权不是严格的子集(IE只有B可以做任务IV),但这还没有被确认,所以我想保持我的选项打开。



我的设想是从MY_Controller继承自MY_Controller的一系列控制器。



例如在Core文件夹中:

  class MY_AsController extends MY_Controller {
public function __construct(){
parent :: __ construct
$ accountType = $ this-> session-> userdata('accountType');
if(!($ accountType == declaredConstant)){
redirect('/ someController / someMethod','refresh');
}
}

然后在控制器文件夹中:

  class AControlPage extends MY_AsController {
//插入页面函数,只能访问此处
}

不幸的是,在实践中应用它不会产生任何错误,只有一个空白页。

解决方案

结束不更改父构造函数或使用更多继承:



向My_Controller添加了以下内容:

 函数allowedToView($ userAccountType,$ requiredAccountTypes){
//如果用户不在允许的用户组
if(!(in_array($ userAccountType,$ requiredAccountTypes))){
redirect / not_allowed /','refresh');
}
}

已将子构造函数更改为:

  public function __construct(){
parent :: __ construct();
$ accountType = $ this-> session-> userdata('accountType');
$ allowedTypes = array(declaredConstant1,declaredConstant2,...);
$ this-> allowedToView($ accountType,$ allowedTypes);
}

谢谢你,约瑟夫的见解让我远离了我的疯狂! >

I'm looking to do log ins with my codeigniter project.

Some considerations:

1) There are two controllers that are outside that do not require authentication. One for information (splash_pages and such), and the other to create a logged in session.

2) All other controllers inherit from a master Controller that as part of its constructor, requires you to be logged in or it kicks you to the log in screen.

So far the above 2 works fine for logged in vs logged out.

In terms of code:

The master controller I was describing for 2) This is located in the Core folder of Codeigniter

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('cart');
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->library('form_validation');
        if (!$this->session->userdata('loggedin')){
            redirect('/sessions/log_in/','refresh');
        }
    }

A class using the login system:

class Records extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('some_model');
        $this->load->library('some_library');
    }

The idea is that on object construction, it will check whether the user is logged in or not and either construct the object correctly or redirect to the log in screen.

However, the needs of the project have changed a bit. The requirements now state that there are around 6 different user groups arranged whose privileges can be arranged into subsets. A can do I, B can do A + II, C can do B + III, and so on. There has been slight hints that there may be privileges that are not strictly subset (IE only B can do task IV), but this has not been confirmed yet so I want to keep my options open.

How I am envisioning doing it is having a bunch of Controllers from MY_Controller that inherit from MY_Controller.

For example in the Core folder:

class MY_AsController extends MY_Controller {
    public function __construct(){
        parent::__construct();
        $accountType = $this->session->userdata('accountType');
        if(!($accountType == declaredConstant)){
            redirect('/someController/someMethod','refresh');
        }
    }

Then in the controllers folder:

class AControlPage extends MY_AsController {
     //Insert page functions that only As have access to here
}

Unfortunately, applying it in practice doesn't generate any errors, only a blank page. I'm not sure what to after that though.

解决方案

Ended up not changing the parent constructor or using more inheritance at all:

Added the following to My_Controller:

public function allowedToView($userAccountType, $requiredAccountTypes){
    //If user not in allowed userGroup
    if(!(in_array($userAccountType,$requiredAccountTypes))){
        redirect('/sessions/not_allowed/','refresh');
    }
}

Changed child constructor to:

public function __construct() {
    parent::__construct();
    $accountType = $this->session->userdata('accountType');
    $allowedTypes = array(declaredConstant1,declaredConstant2,...);
    $this->allowedToView($accountType,$allowedTypes);
}

Thanks, Joseph for the insight leading me away from my craziness!

这篇关于Codeigniter:使用继承来控制登录权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆