Fix and improve Server's privilege get/setters
authorPerttu Ahola <celeron55@gmail.com>
Thu, 15 Mar 2012 13:27:16 +0000 (15:27 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Thu, 15 Mar 2012 13:38:59 +0000 (15:38 +0200)
src/scriptapi.cpp
src/server.cpp
src/server.h

index 99c3767ae86f077b76c8154f1b45bc8dd58ea3a3..e8ebecb5a93065f767670c52038aec90b1202cea 100644 (file)
@@ -3551,10 +3551,7 @@ static int l_get_player_privs(lua_State *L)
        // Do it
        lua_newtable(L);
        int table = lua_gettop(L);
-       u64 privs_i = server->getPlayerAuthPrivs(name);
-       // Special case for the "name" setting (local player / server owner)
-       if(name == g_settings->get("name"))
-               privs_i = PRIV_ALL;
+       u64 privs_i = server->getPlayerEffectivePrivs(name);
        std::set<std::string> privs_s = privsToSet(privs_i);
        for(std::set<std::string>::const_iterator
                        i = privs_s.begin(); i != privs_s.end(); i++){
index a74a2ee758ac1fbb3cce72533e7478204a169de0..2139df957388a8b05740cdaf4440374541fdf9ec 100644 (file)
@@ -4182,6 +4182,40 @@ std::wstring Server::getStatusString()
        return os.str();
 }
 
+u64 Server::getPlayerAuthPrivs(const std::string &name)
+{
+       try{
+               return m_authmanager.getPrivs(name);
+       }
+       catch(AuthNotFoundException &e)
+       {
+               dstream<<"WARNING: Auth not found for "<<name<<std::endl;
+               return 0;
+       }
+}
+
+void Server::setPlayerAuthPrivs(const std::string &name, u64 privs)
+{
+       try{
+               return m_authmanager.setPrivs(name, privs);
+       }
+       catch(AuthNotFoundException &e)
+       {
+               dstream<<"WARNING: Auth not found for "<<name<<std::endl;
+       }
+}
+
+u64 Server::getPlayerEffectivePrivs(const std::string &name)
+{
+       // Local player gets all privileges regardless of
+       // what's set on their account.
+       if(m_simple_singleplayer_mode)
+               return PRIV_ALL;
+       if(name == g_settings->get("name"))
+               return PRIV_ALL;
+       return getPlayerAuthPrivs(name);
+}
+
 void Server::setPlayerPassword(const std::string &name, const std::wstring &password)
 {
        // Add player to auth manager
@@ -4570,16 +4604,7 @@ u64 Server::getPlayerPrivs(Player *player)
        if(player==NULL)
                return 0;
        std::string playername = player->getName();
-       // Local player gets all privileges regardless of
-       // what's set on their account.
-       if(g_settings->get("name") == playername)
-       {
-               return PRIV_ALL;
-       }
-       else
-       {
-               return getPlayerAuthPrivs(playername);
-       }
+       return getPlayerEffectivePrivs(playername);
 }
 
 void dedicated_server_loop(Server &server, bool &kill)
index 31e3ed1767d4e7d91cde4fb6b56f28d03c04a5eb..f149ac6871183248c9858274d9e3d51c323ef027 100644 (file)
@@ -465,32 +465,13 @@ public:
                m_shutdown_requested = true;
        }
 
-
        // Envlock and conlock should be locked when calling this
        void SendMovePlayer(Player *player);
        
-       u64 getPlayerAuthPrivs(const std::string &name)
-       {
-               try{
-                       return m_authmanager.getPrivs(name);
-               }
-               catch(AuthNotFoundException &e)
-               {
-                       dstream<<"WARNING: Auth not found for "<<name<<std::endl;
-                       return 0;
-               }
-       }
-
-       void setPlayerAuthPrivs(const std::string &name, u64 privs)
-       {
-               try{
-                       return m_authmanager.setPrivs(name, privs);
-               }
-               catch(AuthNotFoundException &e)
-               {
-                       dstream<<"WARNING: Auth not found for "<<name<<std::endl;
-               }
-       }
+       // Thread-safe
+       u64 getPlayerAuthPrivs(const std::string &name);
+       void setPlayerAuthPrivs(const std::string &name, u64 privs);
+       u64 getPlayerEffectivePrivs(const std::string &name);
 
        // Changes a player's password, password must be given as plaintext
        // If the player doesn't exist, a new entry is added to the auth manager