我终于找到了一个声称要这样做的课程,我可以告诉我,这似乎是有效的,但我不知道这是否是最好的方法.我将以下面的示例代码发布类文件,以便正常运行.
请,如果你有经验,告诉我是否可以改进,性能或任何其他.我真的想学习这个,我一直在阅读,但是对我来说这是一个困难的事情.
班上…
<?PHP class bitmask { /** * This array is used to represent the users permission in usable format. * * You can change remove or add valuesto suit your needs. * Just ensure that each element defaults to false. Once you have started storing * users permsisions a change to the order of this array will cause the * permissions to be incorectly interpreted. * * @type Associtive array */ public $permissions = array( "read" => false,"write" => false,"delete" => false,"change_permissions" => false,"admin" => false ); /** * This function will use an integer bitmask (as created by toBitmask()) * to populate the class vaiable * $this->permissions with the users permissions as boolean values. * @param int $bitmask an integer representation of the users permisions. * This integer is created by toBitmask(); * * @return an associatve array with the users permissions. */ public function getPermissions($bitMask = 0) { $i = 0; foreach ($this->permissions as $key => $value) { $this->permissions[$key] = (($bitMask & pow(2,$i)) != 0) ? true : false; // Uncomment the next line if you would like to see what is happening. //echo $key . " i= ".strval($i)." power=" . strval(pow(2,$i)). "bitwise & = " . strval($bitMask & pow(2,$i))."<br>"; $i++; } return $this->permissions; } /** * This function will create and return and integer bitmask based on the permission values set in * the class variable $permissions. To use you would want to set the fields in $permissions to true for the permissions you want to grant. * Then call toBitmask() and store the integer value. Later you can pass that integer into getPermissions() to convert it back to an assoicative * array. * * @return int an integer bitmask represeting the users permission set. */ function toBitmask() { $bitmask = 0; $i = 0; foreach ($this->permissions as $key => $value) { if ($value) { $bitmask += pow(2,$i); } $i++; } return $bitmask; } } ?>
如何将权限设置/保存为位掩码值?
<?PHP /** * Example usage * initiate new bitmask object */ $perms = new bitmask(); /** * How to set permissions for a user */ $perms->permissions["read"] = true; $perms->permissions["write"] = true; $perms->permissions["delete"] = true; $perms->permissions["change_permissions"] = true; $perms->permissions["admin"] = false; // Converts to bitmask value to store in database or wherever $bitmask = $perms->toBitmask(); //in this example it is 15 $sql = "insert into user_permissions (userid,permission) values(1,$bitmask)"; echo $sql; //you would then execute code to insert your sql. ?>
使用位掩码值并根据位值返回每个数组项的true / false的示例….
<?PHP /** * Example usage to get the bitmask value from database or session/cache.... then put it to use. * $permarr returns an array with true/false for each array value based on the bit value */ $permarr = $perms->getPermissions($bitmask); if ($permarr["read"]) { echo 'user can read: <font color="green">TRUE</font>'; } else { echo 'user can read: <font color="red">FALSE</font>'; } //user can WRITE permission if ($permarr["write"]) { echo '<br>user can write: <font color="green">TRUE</font>'; } else { echo '<br>user can write: <font color="red">FALSE</font>'; } ?>