检查以确定userA是否有权访问fileB(PHP)


Check to determine if userA has access to fileB (PHP)

PHP有一个is_readable函数,它检查脚本所有者是否可读该文件。是否有相应的脚本来查看指定用户是否可以读取文件,例如

is_readable('Gavrilo Princip', 'black_hand.srj')

不是内置的。我甚至不认为有命令行实用程序可以检查某个用户是否拥有对文件的读取权限。

不过,您可以编写自己的函数来进行检查。查看fileperms()、fileowner()、filegroup()和posix_getpwuid()函数。

检查此问题检查文件权限

PHP文件排列http://php.net/manual/en/function.fileperms.php

PHP统计http://www.php.net/manual/en/function.stat.php

其中的示例适用于*nix系统。我不知道它在Windows主机上是否也能运行。有了这些,您可以获得文件的GID和UID。

我不知道是否有一个PHP等效程序可以让你获得特定系统用户的UID和/或GID。您可能需要手动获取并根据这些值进行搜索。您可以在/etc/passwd文件

中找到该值

感谢Chris和AndrewR的帮助,我提出了一个尚未测试的解决方案。此解决方案在shell中实现,并等待来自中的标准输入(设计用于与Apache RewriteMap配合使用)。然而,它可以很容易地修改为从命令行或PHP脚本调用。它比它必须的要复杂一点,因为我们将函数(getfacl)的输入管道连接到while循环。当我们这样做时,它会启动一个新的超级进程,因此在这个循环中声明或更新的任何变量(即result)都将不可用于外部世界。此外,我使用了getfacl,因为我稍后可以将其扩展为也使用ACL权限。最后,出于实现的原因,在调用此脚本之前,我已经知道文件(user)的所有者,但是,如果不是这样,可以很容易地从getfacl命令中找到它。

#!/bin/bash
#USAGE: STDIN viewer:user:file
while read line
do
   viewer=`echo $4 | cut -d ':' -f 1`
   user=`echo $4 | cut -d ':' -f 2`
   file=`echo $4 | cut -d ':' -f 3`
   result=$(
      getfacl $file 2>/dev/null | while read line
      do
         if [[ $user == $viewer ]] && [[ $line =~ ^user: ]]
         then
            permissions=`echo $line | cut -d ':' -f 3`
            if [[ $permissions =~ r ]]
            then
               echo true
               break
            fi
         elif [[ $user == $viewer ]] && [ $line =~ ^group: ]]
         then
            #NOTE: I take advantage of the fact that each user has one single group and that group has the same name as the user's name
            permissions=`echo $line | cut -d ':' -f 3`
            if [[ $permissions =~ r ]]
            then
               echo true
               break
            fi
         elif [[ $line =~ ^other: ]]
         then
            permissions=`echo $line | cut -d ':' -f 3`
            if [[ $permissions =~ r ]]
            then
               echo true
               break
            fi
         fi
      done
   )
   if [[ $result == "true" ]]
   then
      echo true
   else
      echo false
   fi
done