检查是否声明了其他功能,否则更改位置

时间:2012-07-22 08:41:49

标签: php

我有一个“make-do”页面身份验证器,用于定义允许哪些用户组访问该页面,但是,我的一些脚本允许用户通过该页面,例如,他的用户编辑页面,但不是触摸任何其他用户编辑页面。为此,我禁止访问用户组,除非您是管理员或您当前使用的用户编辑页面是您自己的。

我尝试创建一个函数来执行此操作,但allowOnly usergroups函数处理惩罚而不检查是否在页面的其他位置定义了其他函数。

以下是“make-do”功能以及我希望它们如何工作的示例:

    public function allowOnly($officer, $administrator, $superuser)
{
    $authority = 0;
    if ($officer == true && $this->session->isOfficer()) {
        $authority++;
    }
    elseif ($administrator == true & $this->session->isAdmin()) {
        $authority++;
    }
    elseif ($superuser == true & $this->session->isSuperuser()) {
        $authority++;
    }
    if ($authority != 0) {
        return true;
    }
    else {
        header("Location: ../incorrectRights.php");
        exit;
    }
}
function allowCurrentUser()
{
    global $authority;
    $authority++;
}

如果用户位置不是任何允许的用户组,则会更改用户位置,但由于该代码在“allowCurrentUser”之前执行,因此在函数有机会允许用户通过之前更改位置。

我希望它能像这样工作:

<?php
     include("functions.php");
     $functions->allowOnly(false, false, true);
     if($session->username == $allowedUserName) {
          $functions->allowCurrentUser();
      }

如果我的描述性不够,或者我的代码缺乏效率,我很抱歉,即使我错过了为我这样做的内置php函数!

2 个答案:

答案 0 :(得分:0)

你应该查看PHP的function_exists(),这将告诉你这个功能是否已经存在。

你的代码也有错误。

$administrator == true & $this->session->isAdmin()

应该是

$administrator == true && $this->session->isAdmin()

因为您只使用了单&,而应该是&&

并且还要改变

$superuser == true & $this->session->isSuperuser()

$superuser == true && $this->session->isSuperuser()

阅读完代码后,我意识到您正在使用$authority变量来保存该值并检查是否授权用户。而且你正在使用全球。我永远不会那样做,相反,我会声明$ authority作为类属性,下面是你如何做到这一点的例子。

class functions 
{
    //declare class propert and set default value to 0
    protected $_authority = 0;

    public function allowOnly($officer, $administrator, $superuser)
    {
        if ($officer == true && $this->session->isOfficer()) {
            $this->_authority++;
        }
        elseif ($administrator == true && $this->session->isAdmin()) {
            $this->_authority++;
        }
        elseif ($superuser == true && $this->session->isSuperuser()) {
            $this->_authority++;
        }
        if ($this->_authority != 0) {
            return true;
        }
        else {
            header("Location: ../incorrectRights.php");
            exit;
        }
    }

    public function allowCurrentUser()
    {
        $this->_authority++;
        return $this->_authority;
    }
}

<强>更新

而不是重定向页面,为什么不在函数调用期间返回false和重定向,你可以这样做。

class functions 
{
    //declare class propert and set default value to 0
    protected $_authority = 0;

    public function allowOnly($officer, $administrator, $superuser)
    {
        if ($officer == true && $this->session->isOfficer()) {
            $this->_authority++;
        }
        elseif ($administrator == true && $this->session->isAdmin()) {
            $this->_authority++;
        }
        elseif ($superuser == true && $this->session->isSuperuser()) {
            $this->_authority++;
        }

        return ($this->_authority != 0) ? true : false;
    }

    public function allowCurrentUser()
    {
        $this->_authority++;
        return $this->_authority;
    }
}

并且在函数调用中。

include("functions.php");
if($functions->allowOnly(false, false, true)) {
    //person is allowed access
} 
//else allow current user
elseif($session->username == $allowedUserName) {
    $functions->allowCurrentUser();
} 
else {
    //redirect here
    header("Location: ../incorrectRights.php");
    exit;
}

答案 1 :(得分:0)

我不完全确定这是否是您根据标题寻找的答案,但这就是我得到您所要求的印象。

假设发生的事情是你对allowOnly()的检查将用户带到“../incorrectRights.php"-page,然后你检查用户登录的是否与页面看到的相同,你是什么需要做的是将检查放在allowOnly函数中,或者至少在检查$authority != 0之前。

以下是如何解决此问题的简单示例:

public function allowOnly($officer, $administrator, $superuser)
{
    $authority = 0;
    if ($officer == true && $this->session->isOfficer()) {
        $authority++;
    }
    elseif ($administrator == true && $this->session->isAdmin()) {
        $authority++;
    }
    elseif ($superuser == true && $this->session->isSuperuser()) {
        $authority++;
    }

    if(function_exists('allowCurrentUser')){
        if (allowCurrentUser()) {
            return true;
        }
    }
    if ($authority != 0) {
        return true;
    }
    else {
        header("Location: ../incorrectRights.php");
        exit;
    }
}
function allowCurrentUser()
{
    if($session->username == $allowedUserName){
        return true; 
    }
    else {
        return false;
    }
}

然后你的用法会产生更像

的东西
 <?php
     include("functions.php");
     $functions->allowOnly(false, false, true);
 ?>

正如你所看到的那样,我也提出了问题标题中似乎要求的function_exists('functionnamehere')调用,因为我们实际上声明了该函数,因此知道它存在,你也可以这样做:

    if ($authority != 0 || allowCurrentUser()) {
        return true;
    }