1
0

cPlayer now checks restrictions.

Fixes #1331.
This commit is contained in:
Mattes D 2015-04-25 19:40:44 +02:00
parent 9c5797f27d
commit b5998873e4
3 changed files with 69 additions and 8 deletions

View File

@ -2125,6 +2125,37 @@ static int tolua_cPlayer_GetPermissions(lua_State * tolua_S)
static int tolua_cPlayer_GetRestrictions(lua_State * tolua_S)
{
// Function signature: cPlayer:GetRestrictions() -> {restrictions-array}
// Check the params:
cLuaState L(tolua_S);
if (
!L.CheckParamUserType(1, "cPlayer") ||
!L.CheckParamEnd (2)
)
{
return 0;
}
// Get the params:
cPlayer * self = (cPlayer *)tolua_tousertype(tolua_S, 1, nullptr);
if (self == nullptr)
{
LOGWARNING("%s: invalid self (%p)", __FUNCTION__, self);
return 0;
}
// Push the permissions:
L.Push(self->GetRestrictions());
return 1;
}
static int tolua_cPlayer_OpenWindow(lua_State * tolua_S) static int tolua_cPlayer_OpenWindow(lua_State * tolua_S)
{ {
// Function signature: cPlayer:OpenWindow(Window) // Function signature: cPlayer:OpenWindow(Window)
@ -3756,6 +3787,7 @@ void ManualBindings::Bind(lua_State * tolua_S)
tolua_beginmodule(tolua_S, "cPlayer"); tolua_beginmodule(tolua_S, "cPlayer");
tolua_function(tolua_S, "GetPermissions", tolua_cPlayer_GetPermissions); tolua_function(tolua_S, "GetPermissions", tolua_cPlayer_GetPermissions);
tolua_function(tolua_S, "GetRestrictions", tolua_cPlayer_GetRestrictions);
tolua_function(tolua_S, "OpenWindow", tolua_cPlayer_OpenWindow); tolua_function(tolua_S, "OpenWindow", tolua_cPlayer_OpenWindow);
tolua_function(tolua_S, "PermissionMatches", tolua_cPlayer_PermissionMatches); tolua_function(tolua_S, "PermissionMatches", tolua_cPlayer_PermissionMatches);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);

View File

@ -1414,14 +1414,23 @@ bool cPlayer::HasPermission(const AString & a_Permission)
AStringVector Split = StringSplit(a_Permission, "."); AStringVector Split = StringSplit(a_Permission, ".");
// Iterate over all granted permissions; if any matches, then return success: // Iterate over all restrictions; if any matches, then return failure:
for (AStringVectorVector::const_iterator itr = m_SplitPermissions.begin(), end = m_SplitPermissions.end(); itr != end; ++itr) for (auto & Restriction: m_SplitRestrictions)
{ {
if (PermissionMatches(Split, *itr)) if (PermissionMatches(Split, Restriction))
{
return false;
}
} // for Restriction - m_SplitRestrictions[]
// Iterate over all granted permissions; if any matches, then return success:
for (auto & Permission: m_SplitPermissions)
{
if (PermissionMatches(Split, Permission))
{ {
return true; return true;
} }
} // for itr - m_SplitPermissions[] } // for Permission - m_SplitPermissions[]
// No granted permission matches // No granted permission matches
return false; return false;
@ -2169,15 +2178,24 @@ void cPlayer::LoadRank(void)
RankMgr->UpdatePlayerName(m_UUID, m_PlayerName); RankMgr->UpdatePlayerName(m_UUID, m_PlayerName);
} }
m_Permissions = RankMgr->GetPlayerPermissions(m_UUID); m_Permissions = RankMgr->GetPlayerPermissions(m_UUID);
m_Restrictions = RankMgr->GetPlayerRestrictions(m_UUID);
RankMgr->GetRankVisuals(m_Rank, m_MsgPrefix, m_MsgSuffix, m_MsgNameColorCode); RankMgr->GetRankVisuals(m_Rank, m_MsgPrefix, m_MsgSuffix, m_MsgNameColorCode);
// Break up the individual permissions on each dot, into m_SplitPermissions: // Break up the individual permissions on each dot, into m_SplitPermissions:
m_SplitPermissions.clear(); m_SplitPermissions.clear();
m_SplitPermissions.reserve(m_Permissions.size()); m_SplitPermissions.reserve(m_Permissions.size());
for (AStringVector::const_iterator itr = m_Permissions.begin(), end = m_Permissions.end(); itr != end; ++itr) for (auto & Permission: m_Permissions)
{ {
m_SplitPermissions.push_back(StringSplit(*itr, ".")); m_SplitPermissions.push_back(StringSplit(Permission, "."));
} // for itr - m_Permissions[] } // for Permission - m_Permissions[]
// Break up the individual restrictions on each dot, into m_SplitRestrictions:
m_SplitRestrictions.clear();
m_SplitRestrictions.reserve(m_Restrictions.size());
for (auto & Restriction: m_Restrictions)
{
m_SplitRestrictions.push_back(StringSplit(Restriction, "."));
} // for itr - m_Restrictions[]
} }

View File

@ -254,7 +254,10 @@ public:
static bool PermissionMatches(const AStringVector & a_Permission, const AStringVector & a_Template); // Exported in ManualBindings with AString params static bool PermissionMatches(const AStringVector & a_Permission, const AStringVector & a_Template); // Exported in ManualBindings with AString params
/** Returns all the permissions that the player has assigned to them. */ /** Returns all the permissions that the player has assigned to them. */
const AStringVector & GetPermissions(void) { return m_Permissions; } // Exported in ManualBindings.cpp const AStringVector & GetPermissions(void) const { return m_Permissions; } // Exported in ManualBindings.cpp
/** Returns all the restrictions that the player has assigned to them. */
const AStringVector & GetRestrictions(void) const { return m_Restrictions; } // Exported in ManualBindings.cpp
// tolua_begin // tolua_begin
@ -500,10 +503,18 @@ protected:
/** All the permissions that this player has, based on their rank. */ /** All the permissions that this player has, based on their rank. */
AStringVector m_Permissions; AStringVector m_Permissions;
/** All the restrictions that this player has, based on their rank. */
AStringVector m_Restrictions;
/** All the permissions that this player has, based on their rank, split into individual dot-delimited parts. /** All the permissions that this player has, based on their rank, split into individual dot-delimited parts.
This is used mainly by the HasPermission() function to optimize the lookup. */ This is used mainly by the HasPermission() function to optimize the lookup. */
AStringVectorVector m_SplitPermissions; AStringVectorVector m_SplitPermissions;
/** All the restrictions that this player has, based on their rank, split into individual dot-delimited parts.
This is used mainly by the HasPermission() function to optimize the lookup. */
AStringVectorVector m_SplitRestrictions;
// Message visuals: // Message visuals:
AString m_MsgPrefix, m_MsgSuffix; AString m_MsgPrefix, m_MsgSuffix;
AString m_MsgNameColorCode; AString m_MsgNameColorCode;