2012-07-22 61 views
0

我有一个“make-do”页面身份验证器,它定义允许哪些用户组访问该页面,但是,有些脚本允许用户在该页面比如他的用户编辑页面,但不能触及任何其他用户的编辑页面。为此,我禁止访问用户组,除非您是管理员或您目前使用的用户编辑页面是您自己的。检查是否声明了另一个函数,否则更改位置

我试着创建一个函数来做到这一点,但allowOnly usergroups函数处理惩罚而不检查其他函数是否在页面的其他地方定义。

这里的“让做”的功能,我想如何他们一个例子来工作:

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功能,这对我这样做!

回答

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变量来保存该值并检查是否授权用户。加上你正在使用全球。我永远不会这样做,而是我将声明$权威作为下面的类属性是你如何做到这一点的例子。

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; 
    } 
} 

UPDATE:

而不是重定向的页面,为什么不返回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; 
} 
+0

如果之后调用“allowCurrentUser”,则标题位置仍然被执行。哪一个是我的问题。 – PwnageAtPwn 2012-07-22 09:14:02

+0

从函数内部直接重定向页面对我来说似乎是一个很有问题的方法。你真正应该做的是如果没有权限然后返回false,并且在函数调用中,你可以相应地确定和重定向。生病发布我的答案中的例子。 – 2012-07-22 09:16:58

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; 
    }