int CLuaACLDefs::hasObjectPermissionTo ( lua_State* luaVM ) { // bool hasObjectPermissionTo ( string / element theObject, string theAction [, bool defaultPermission = true ] ) CResource* pResource = NULL; CElement* pElement = NULL; SString strObject; SString strRightName; bool bDefault; CAccessControlListGroupObject::EObjectType eObjectType; CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsUserDataOfType < CResource > () ) argStream.ReadUserData ( pResource ); else if ( argStream.NextIsUserDataOfType < CElement > () ) argStream.ReadUserData ( pElement ); else argStream.ReadString ( strObject ); argStream.ReadString ( strRightName ); argStream.ReadBool ( bDefault, true ); if ( !argStream.HasErrors () ) { if ( pResource ) { // Grab the resource's name strObject = pResource->GetName (); eObjectType = CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE; } else if ( pElement ) { // Grab the client this player/console/whatever is CClient* pClient = pElement->GetClient (); if ( pClient ) { // Get his account CAccount* pAccount = pClient->GetAccount (); if ( pAccount ) { // Grab the username strObject = pAccount->GetName (); eObjectType = CAccessControlListGroupObject::OBJECT_TYPE_USER; } } } else { // Extract the object name itself including the type const char * szName = CAccessControlListManager::ExtractObjectName ( strObject.c_str (), eObjectType ); strObject = szName ? szName : ""; } // Got a string? if ( !strObject.empty () ) { // Extract the right name itself including the type CAccessControlListRight::ERightType eRightType; strRightName = CAccessControlListManager::ExtractRightName ( strRightName, eRightType ); // Did we get a right name without the prefix? if ( strRightName ) { bool bHasPermission = m_pACLManager->CanObjectUseRight ( strObject, eObjectType, strRightName, eRightType, bDefault ); // Return whether we had access or not lua_pushboolean ( luaVM, bHasPermission ); return 1; } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushnil ( luaVM ); return 1; }
int CLuaACLDefs::hasObjectPermissionTo ( lua_State* luaVM ) { // What object name we're going to check CAccessControlListGroupObject::EObjectType eObjectType; std::string strObject; // Got a pointer argument? if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA ) { // Grab it CResource* pResource = lua_toresource ( luaVM, 1 ); CElement* pElement = lua_toelement ( luaVM, 1 ); // Is it a valid resource? if ( pResource ) { // Grab the resource's name strObject = pResource->GetName (); eObjectType = CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE; } // Is this a valid element? else if ( pElement ) { // Grab the client this player/console/whatever is CClient* pClient = pElement->GetClient (); if ( pClient ) { // Get his account CAccount* pAccount = pClient->GetAccount (); if ( pAccount ) { // Grab the username strObject = pAccount->GetName (); eObjectType = CAccessControlListGroupObject::OBJECT_TYPE_USER; } } } } // Got a string argument? else if ( lua_type ( luaVM, 1 ) == LUA_TSTRING ) { // Change the pointer to point to our argument strObject = lua_tostring ( luaVM, 1 ); // Extract the object name itself including the type const char * szName = CAccessControlListManager::ExtractObjectName ( strObject.c_str (), eObjectType ); strObject = szName ? szName : ""; } // Got a string? if ( !strObject.empty () ) { // Got a string with the action to check for permission? if ( lua_type ( luaVM, 2 ) == LUA_TSTRING ) { // Grab the right name we should've gotten passed const char* szRightName = lua_tostring ( luaVM, 2 ); // Extract the right name itself including the type CAccessControlListRight::ERightType eRightType; szRightName = CAccessControlListManager::ExtractRightName ( szRightName, eRightType ); // Did we get a right name without the prefix? if ( szRightName ) { // Third argument to specify what the return defaults to. // This is if no ACL could be found. bool bDefault = true; if ( lua_type ( luaVM, 3 ) == LUA_TBOOLEAN ) { bDefault = lua_toboolean ( luaVM, 3 ) ? true:false; } // Check whether he has permissions to do that bool bHasPermission = m_pACLManager->CanObjectUseRight ( strObject.c_str (), eObjectType, szRightName, eRightType, bDefault ); // Return whether we had access or not lua_pushboolean ( luaVM, bHasPermission ); return 1; } else m_pScriptDebugging->LogBadType ( luaVM, "hasObjectPermissionTo" ); } else m_pScriptDebugging->LogBadType ( luaVM, "hasObjectPermissionTo" ); } else m_pScriptDebugging->LogBadType ( luaVM, "hasObjectPermissionTo" ); // Failed lua_pushnil ( luaVM ); return 1; }