drfb52000 2015-01-08 06:21
浏览 37
已采纳

PHP匿名函数,使用关键字和变量范围问题

For reference I'm using the SLIM framework

My issue is where you see the commented out print_r commands in the code below. For some reason the variable is showing the correct values right before the !in_array function. I'm not sure what is going on, but when you pass the variable $allowedUserTypes into that function it seems to be using (I'm guessing an older version of the variable from somewhere) incorrect values. Not sure how this is possible though since it works right before that function??. I'm guessing there is some kind of scoping issue or I'm misunderstanding the use of the 'USE' keyword in an anonymous function.

$validate_user = function ($allowedUserTypes, $templateFolder = 'api'){
    return function() use ($allowedUserTypes, $templateFolder){
        global $app, $settings, $user, $device;

        set_template_directory($templateFolder);

        $errors = array();

        $validated = true;//assumed valid until proven false.

        //check session variables only if not token api call
        if($app->request()->params('token') == ''){

            //Check for expiration date hack
            if($_SESSION['remember']==false){
                $now = new DateTime();
                $now->modify("-30 minutes");
            }else{
                $now = new DateTime();
                $now->modify("-14 days");
            }

            //If the cookie still exists then it might have a time value in it.  See if it's set.
            if(isset($_SESSION['time'])){
                //If time now (minus minute) is greater than the session time then hack attempted.
                if($now > $_SESSION['time']){ 
                    $errors["errors"]["generic"][] = "Permission denied.  Cookie expired.";
                    $validated = false;
                    unset($_SESSION['time']);
                    unset($_SESSION['remember']);
                    unset($_SESSION['userid']);
                    unset($user);
                }
            }
        }

        if(isset($user)){
            $usertype = Usertype::find_by_id($user->usertype_id);//get all usertypes

            //print_r($allowedUserTypes); --> shows Admin, Manager, Franchise Admin, Franchise Manager
            if(!in_array($usertype->name,$allowedUserTypes)){
                //print_r($allowedUserTypes); --> shows only Admin, Manager ??

                $errors["errors"]["generic"][] = "Permission denied for user type :".$usertype->name;
                $validated = false;
            }
        }else{
            $errors["errors"]["generic"][] = "Permission denied.  User not logged in.  Please log in and try again.";
            $validated = false;
        }

        if($validated==false){
            $errors["command"] = "Error";
            $errors['message'] = "User could not be validated.";

            if($templateFolder=='templates'){
                $app->render('shared/header.php',       array('settings' => $settings));
                $app->render($device.'/header.php',     array('settings' => $settings, 'pagetitle'  => 'Pool Service USA | Error Page', 'user' => $user));
                $app->render($device.'/error.php',      array('settings' => $settings, 'errors' => $errors,'device' => $device));
                $app->render($device.'/footer.php',     array('settings' => $settings));
                $app->render('shared/footer.php',       array('settings' => $settings));
            }else{ //API Based Errors
                $app->render('shared/error.php', array(
                    'settings'  => $settings,
                    'errors'    => $errors,
                    'device'    => $device
                ));
            }

            $app->stop();//stop rendering to this point.
        }
    };
};

I'll show the 2 lines I use to call to this function right before I call it and after incase that has anything to do with it.

$app->map('/api/remove-user'                        ,'get_user',$validate_user(array('Admin','Manager','Franchise Admin','Franchise Manager')),$remove_record_for_class('User'))                                ->via('GET', 'POST');
$app->map('/api/view-user'                          ,'get_user',$validate_user(array('Admin','Manager','Franchise Admin','Franchise Manager')),$view_results_for_class('User'))                                 ->via('GET', 'POST');

Any suggestions are appreciated!

  • 写回答

1条回答 默认 最新

  • dongzuozhu66776 2015-01-08 22:47
    关注

    Turns out I was running the $app->map('') command twice by accident, in one case sending only 'Admin' and 'Manager' which caused the error. So was a stupid error on my part :(. Just goes to show to watch out for this stuff.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?