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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘