dongnan1899 2013-03-25 18:38
浏览 19

什么是防止未经身份验证的用户访问控制器方法的好方法?

I have an event(s) controller:

class Event extends CI_Controller{

   public function index(){
   }

   public function foo(){
   }

   //Shouldn't be able to use this method unless logged in
   public function bar(){
   }


}

And I'm trying to organise my code so it's fairly tidy and straightforward. Just now I have a controller named MY_Controller so that only authenticated users can access the methods(edit_event(),add_event()) of any controllers extending it.

However, some of the methods in my controller need to be accessed by unauthenticated users (such as get_event()).

What is a good way of handling this? Should I make two completely separate controllers or extend from the basic event controller and add authenticated methods?

Previously I've had a manager controller that handled all methods which required authentication such as add_user,delete_user,add_doc,delete_doc. But it became blotted very quickly and wasn't easy to update or modify the controller (plus it was messy and didn't seem to follow good programming etiquette).

  • 写回答

1条回答 默认 最新

  • doutany76678 2013-03-26 17:01
    关注

    usually i use hooks

    read about them.

    for example i created a url_hook.php and it controls everytime a page load if uri is allowed for the user:

    class url_hook{
    
    function allowed_urls(){
    
    $allow = array('login','logout','search');
    
    if(in_array($allow,$this->uri->segment(2)) && $this->session->userdata('user_id')){
    
     //ok user allowed
    
    return true;
    
    }else{
    
    //user not allowed
    
     redirect();
    

    } } }

    then in config/config.php i do:

    $config['enable_hooks'] = TRUE;
    

    and in config/hooks.php somenthing like this:

     $hook['pre_controller'][] = array(
                                    'class'    => 'url_hook',
                                    'function' => 'allowed_urls',
                                    'filename' => 'url_hook.php',
                                    'filepath' => 'hooks'
    
                                    );
    

    all this will run automatically everytime a url is called in your app

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助