代码之家  ›  专栏  ›  技术社区  ›  Mr. ED

代码点火器路由和重定向

  •  1
  • Mr. ED  · 技术社区  · 10 年前

    当我键入基本url时 http://localhost/myproject/admin 它不断地将我发送到我的权限页面。这个 http://localhost/myproject/admin 是base_url()。

    我的核心/Controller.php是如何工作的,它检查是否可以访问控制器,如果不在忽略列表中,则被重定向到权限,否则可以访问页面。

    我想知道的是,如果有可能 base_url() 所以它忽略了它,让我可以访问它。我不确定在下面的代码中添加它的最佳位置。

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    // I am not using MY_Controller works with Controller fine.
    class Controller extends CI_Controller { 
    
    public function __construct() {
        parent::__construct();
    
        $url = $this->uri->segment(1).'/'.$this->uri->segment(2);
    
        if (isset($url)) {
            $route = '';
    
            $segment = explode('/', $url);
    
            if (isset($segment[0])) {
                $route .= $segment[0];
            }
    
            if (isset($segment[1])) {
                $route .= '/' . $segment[1];
            }
    
            // $route would equal example: common/dashboard
    
            // $segment[0] folder i.e common
            // $segment[1] controller 
    
            $ignore = array(
                'common/dashboard',
                'common/login',
                'common/forgotten',
                'common/reset',
                'error/not_found',
                'error/permission'
            );
    
            if (!in_array($route, $ignore)) {
                redirect('permission');
            }
        }
    }
    }
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   JC Sama    10 年前

    使用Hook检查权限:

    1-创建配置文件 config/acl.php :

    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    $acl = array(
        'role_permission' => array(
            'role/index' => 'access_show_roles_list',
            'role/add' => 'access_add_role',
            'role/edit' => 'access_edit_role',
            'role/delete' => 'access_delete_role',
            'permission/index' => 'access_permission_list',
         ),
        'users' => array(
            'user/index' => 'access_show_users_list',
            'user/add' => 'access_add_user',
            'user/edit' => 'access_edit_user',
            'user/delete' => 'access_delete_user',
            'user/profil' => 'access_profil_user',
            'user/showpasswd' => 'access_show_password',
        ),
    );
    $config['acl'] = $acl;
    

    2-创建挂钩 Hooks/Autorization.php :

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    
    class Authorization {
    
        private $ci;
    
        public function __construct(){
            $this->ci = &get_instance();
        }
    
        public function authorize()
        {
            if (!$this->_has_access()) {
                if ($this->ci->input->is_ajax_request())
                    die('-9');
    
                show_404();
            }
        }
    
        private function _has_access() {
            $class = $this->ci->router->class;
            $action = $this->ci->router->method;
            $full_action = $class . '/' . $action;
            // --> Start
            $acl = $this->ci->config->item('acl');
            $arr_acl = array();
    
            array_map(function($value) use (&$arr_acl){
                $arr_acl = array_merge($arr_acl, $value);
            }, array_values($acl));
            // --> End
    
            if (isset($arr_acl[$full_action])
                && !in_array($full_action, $this->ci->user->permissions))
                return false;
    
            return true;
        }
    }
    

    3-通过设置激活挂钩 enable_hooks TRUE 在里面 config/config.php :

    $config['enable_hooks'] = TRUE;
    

    4-设置 Autorization config/hooks.php :

    $hook['post_controller_constructor'][] = array(
        'class'    => 'Authorization',
        'function' => 'authorize',
        'filename' => 'Authorization.php',
        'filepath' => 'hooks',
        'params'   => array()
    );
    

    5-添加权限的翻译, language/english/permissions_lang.php :

    /* ROLE */
    $lang['access_show_roles_list'] = "Show all roles.";
    $lang['access_add_role'] = "Add new role.";
    $lang['access_edit_role'] = "Update a role.";
    $lang['access_delete_role'] = "Delete a role.";
    $lang['access_change_role_status'] = "Change role stat Enabled/Disabled.";
    $lang['access_permission_list'] = "Access to the permissions list.";
    

    6-添加 acl.php 自动加载文件,在 config.autoload.php :

    $autoload['config'] = array('acl');
    

    就是这样。

        2
  •  1
  •   Tpojka    10 年前

    没有人提到,但你使用 reserved name 您的控制器。 改变它,看看是否有效。