2013-01-16 27 views
0

好的,爲了澄清,pawn是一種非常類似於C++的語言。我編寫的管理控制面板需要位掩碼權限(在PHP中),我實際上不知道我是否正確地將權限轉換爲PHP(因爲這是我從未做過的)。我使用define()來定義權限。下面是它最初是如何(典當):將這些位掩碼定義從典型語言轉換爲PHP有​​什麼可能的方法?

//Pawn 
enum EAdminFlags (<<= 1) { 
    EAdminFlags_None = 0, 
    EAdminFlags_BasicAdmin = 1, //kick, fine, etc 0 
    EAdminFlags_CanGiveGuns, //1 
    EAdminFlags_HouseAdmin, //2 
    EAdminFlags_IRCAdmin, //3 
    EAdminFlags_BusinessAdmin, //4 
    EAdminFlags_AdminManage, //5 can see invisible admins/manipulate admin flags 
    EAdminFlags_Scripter, //getpvars, reloadcmds, stuff only scripters should need 6 
    EAdminFlags_HelperManage, // 7 manage helpers/teachers 
    EAdminFlags_Unbannable, //unbanable under ANY circumstances 8 
    EAdminFlags_CanRefundSelf, //can refund himself 
    EAdminFlags_VehicleAdmin, //10 can manipulate player vehicles 
    EAdminFlags_AntiCheat, //immune to the anticheat 11 
    EAdminFlags_BigEars, //can /bigears, /bigfamilyears, /bigmouth 
    EAdminFlags_SetName, //13 
    EAdminFlags_CanHide, //can toggle/untoggle hidden on themselves 14 
    EAdminFlags_Invisible, //Invisible on admins list(like 9999s before) 15 
    EAdminFlags_GiveDrugs, //server manager, can restart the server/do rcon cmds, create job cars 16 
    EAdminFlags_Unban, //can unban, oban, oprison people 17 
    EAdminFlags_WeatherManage, //can manage weather/time 18 
    EAdminFlags_GiveRestrictedGun, //can give restircted guns 19 
    EAdminFlags_TeleportOthers, //can teleport other people 20 
    EAdminFlags_CanBanAdmins, //can ban other admins(or any punishments) 21 
    EAdminFlags_GiveMats, //can give materials 22 
    EAdminFlags_StatsManage, //23 /setstat, /sethp, /setarmor 
    EAdminFlags_FamilyAdmin, //24 
    EAdminFlags_FactionAdmin, //25 
    EAdminFlags_MassCmds, //26 can do /masstphere, /massmoney, /masskick, /massnuke 
    EAdminFlags_Nuke, //27 can do things like /nuke, sparta, rangeban 
    EAdminFlags_RangeBan, //28 can range ban 
    EAdminFlags_GiveMoney, //29 can /givemoney /money 
    EAdminFlags_Donations, //30 can give DPS/set donate rank 
    EAdminFlags_ServerManager, //can restart the server, etc, use /noooc, /nopms, /nonewb 
    EAdminFlags_All = -1, 
}; 

現在,這是我如何在PHP中定義它:

define("EAdminFlags_None",0); 
    define("EAdminFlags_BasicAdmin",1); 
    define("EAdminFlags_CanGiveGuns",1 << 0); 
    define("EAdminFlags_HouseAdmin",1 << 1); 
    define("EAdminFlags_IRCAdmin",1 << 2); 
    define("EAdminFlags_BusinessAdmin",1 << 3); 
    define("EAdminFlags_AdminManage",1 << 4); 
    define("EAdminFlags_Scripter",1 << 5); 
    define("EAdminFlags_HelperManage",1 << 6); 
    define("EAdminFlags_Unbannable",1 << 7); 
    define("EAdminFlags_CanRefundSelf",1 << 8); 
    define("EAdminFlags_VehicleAdmin",1 << 9); 
    define("EAdminFlags_AntiCheat",1 << 10); 
    define("EAdminFlags_BigEars",1 << 11); 
    define("EAdminFlags_SetName",1 << 12); 
    define("EAdminFlags_CanHide",1 << 13); 
    define("EAdminFlags_Invisible",1 << 14); 
    define("EAdminFlags_GiveDrugs",1 << 15); 
    define("EAdminFlags_Unban",1 << 16); 
    define("EAdminFlags_WeatherManage",1 << 17); 
    define("EAdminFlags_GiveRestrictedGun",1 << 18); 
    define("EAdminFlags_TeleportOthers",1 << 19); 
    define("EAdminFlags_CanBanAdmins",1 << 20); 
    define("EAdminFlags_GiveMats",1 << 21); 
    define("EAdminFlags_StatsManage",1 << 22); 
    define("EAdminFlags_FamilyAdmin",1 << 23); 
    define("EAdminFlags_FactionAdmin",1 << 24); 
    define("EAdminFlags_MassCmds",1 << 25); 
    define("EAdminFlags_Nuke",1 << 26); 
    define("EAdminFlags_RangeBan",1 << 27); 
    define("EAdminFlags_GiveMoney",1 << 28); 
    define("EAdminFlags_Donations",1 << 29); 
    define("EAdminFlags_ServerManager",1 << 30); 
    define("EAdminFlags_All",-1); 

我比較了bitflags和大部分時間,它只是沿着識別EAdminFlags_All許可與EAdminFlags_NoneEAdminFlags_BasicAdmin。因此,我在PHP中如何定義這個可能存在一個問題。 的權限存儲在數據庫中,將它們保存爲例如原始整數值:51531777 那麼它的加載到PHP比較像這樣:

$account = new Account(Auth::getUserID()); 
echo "Welcome ".$account->getUsername()."<br>"; 
if($account->getAdminPerms(EAdminFlags_FamilyAdmin)) { 
    echo "You are a family admin."; //testing purposes 
    //If the person has those permissions set then display he/she is a family admin but even if the permissions are set to EAdminFlags_BasicAdmin, it will still display it wrong 
} 
public function getAdminLevel() { 
    return $this->adminlevel; //This holds the integer value "51531777" 
} 
public function getAdminPerms($n) { 
    return ($this->adminlevel & (1 << $n)) != 0; 
} 

謝謝你的幫助事前,我真的很感激。

+0

「有可能是一個問題,」哪些問題?問題是什麼?一目瞭然。 – feeela

+0

作爲初學者,你是否檢查過你的常量實際上是否全部正確定義。打印他們,並確保他們從來沒有別的,但權力的2:1,2,4,8,16,32,... – Sven

+0

它顯示權限錯誤或一些權限甚至不顯示。它也不會檢查用戶是否有標誌設置。 –

回答

1

假設上的常量的移位操作是正確的,有在檢測到特定權利標誌的錯誤:

public function getAdminPerms($n) { 
    return ($this->adminlevel & (1 << $n)) != 0; 
} 

輸入是標誌值,這是隻有1位設定在正確位置。你錯誤地再次轉移,但所有你需要做的是,用一個整數值從數據庫中讀取:

public function getAdminPerms($n) { 
    return ($this->adminlevel & $n)) != 0; 
} 

回去的路上的工作原理相同。從零出發,你可以用比特的OR-ing所有的常量值不轉移添加的所有管理員權限的標誌:

$newAdmin = EAdminFlags_AntiCheat | EAdminFlags_CanGiveGuns; 
+0

謝謝,你剛剛幫我解決了我遇到的一個大問題,現在我可以開始工作了。 –

1
define("EAdminFlags_BasicAdmin",1); 
define("EAdminFlags_CanGiveGuns",1 << 0); 

這兩者是等價的... 0移位基本上是一個空操作,所以「cangiveguns」具有相同的值作爲basicadmin

+0

我想我應該改變它,所以它從一開始。 –

相關問題