Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Access manager #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions scripts/include/access.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2018 SPMod Development Team
*
* This file is part of SPMod.
*
* SPMod is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#if defined _access_included
#endinput
#endif
#define _access_included

enum Group
{
INVALID_GROUP = -1
};

methodmap Group
{
public native Group(const char[] name);
public native void AttachPermission(const char[] permission);
public native void RemovePermission(const char[] permission);
}

native Group FindGroup(const char[] name);
15 changes: 15 additions & 0 deletions scripts/include/clients.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ methodmap Player
public native int GetIP(char[] buffer, int size, bool port = false);
public native int GetSteamID(char[] buffer, int size);

public native bool HasAccess(const char[] permission);
public native void AttachGroup(Group group);
public native void RemoveGroup(Group group);
public native void AttachPermission(const char[] permission);
public native void RemovePermission(const char[] permission);

property int Index {
public native get();
}
Expand Down Expand Up @@ -82,3 +88,12 @@ forward void OnClientDisconnect(Player client, bool crash, const char[] reason);
* @noreturn
*/
forward void OnClientPutInServer(Player client);

/*
* @brief Called when a client permissions changed.
*
* @param client Client id.
*
* @noreturn
*/
forward void OnClientPermissionsChanged(Player client);
7 changes: 2 additions & 5 deletions scripts/include/commands.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ typeset CmdCallback

methodmap Command
{
public native Command(const char[] cmd, CmdCallback func, const char[] info = "", bool server = false, int flags = 0);
public native Command(const char[] cmd, CmdCallback func, const char[] info = "", bool server = false, const char[] permission = "");
public native int GetInfo(char[] buffer, int size);

property int Access {
public native get();
}
public native int GetPermission(char[] buffer, int size);
};

forward PluginReturn OnClientCommand(int client);
Expand Down
1 change: 1 addition & 0 deletions scripts/include/spmod.inc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum PluginReturn
}

#include <clients>
#include <access>
#include <cvars>
#include <natives>
#include <forwards>
Expand Down
95 changes: 95 additions & 0 deletions src/AccessNatives.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2018 SPMod Development Team
*
* This file is part of SPMod.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "spmod.hpp"

// Group Group(const char[] name);
static cell_t CreateGroup(SourcePawn::IPluginContext *ctx,
const cell_t *params)
{
enum { arg_name = 1 };

const std::unique_ptr<GroupMngr> &groupMngr = gSPGlobal->getGroupManagerCore();

char *name;
ctx->LocalToString(params[arg_name], &name);

return static_cast<cell_t>(groupMngr->createGroup(name));
}

// native void Group.AttachPermission(const char[] permission);
static cell_t GroupAttachPermission(SourcePawn::IPluginContext *ctx,
const cell_t *params)
{
enum { arg_group = 1, arg_perm };

const std::unique_ptr<GroupMngr> &groupMngr = gSPGlobal->getGroupManagerCore();

char *perm;
ctx->LocalToString(params[arg_perm], &perm);

std::shared_ptr<AccessGroup> group = groupMngr->getGroup(params[arg_group]);
std::shared_ptr<std::string> p = groupMngr->createPermission(perm);
group->addPermisson(p);

return 1;
}

// native void Group.RemovePermission(const char[] permission);
static cell_t GroupRemovePermission(SourcePawn::IPluginContext *ctx,
const cell_t *params)
{
enum { arg_group = 1, arg_perm };

const std::unique_ptr<GroupMngr> &groupMngr = gSPGlobal->getGroupManagerCore();

char *perm;
ctx->LocalToString(params[arg_perm], &perm);

std::shared_ptr<AccessGroup> group = groupMngr->getGroup(params[arg_group]);
std::shared_ptr<std::string> p = groupMngr->createPermission(perm);
group->removePermission(p);

return 1;
}

// native Group FindGroup(const char[] group);
static cell_t FindGroup(SourcePawn::IPluginContext *ctx,
const cell_t *params)
{
enum { arg_group = 1 };

const std::unique_ptr<GroupMngr> &groupMngr = gSPGlobal->getGroupManagerCore();

char *group;
ctx->LocalToString(params[arg_group], &group);

return groupMngr->findGroup(group);
}

sp_nativeinfo_t gAccessNatives[] =
{
{ "Group.Group", CreateGroup },
{ "Group.AttachPermission", GroupAttachPermission },
{ "Group.RemovePermission", GroupRemovePermission },

{ "FindGroup", FindGroup },

{ nullptr, nullptr }
};
156 changes: 156 additions & 0 deletions src/AccessSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2018 SPMod Development Team
*
* This file is part of SPMod.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "spmod.hpp"

AccessGroup::AccessGroup(std::string_view name) : m_name(name)
{}

std::string_view AccessGroup::getNameCore() const
{
return m_name;
}

void AccessGroup::addPermisson(std::shared_ptr<std::string> permission)
{
m_permissions.insert(permission);
}

void AccessGroup::removePermission(std::shared_ptr<std::string> permission)
{
auto p = m_permissions.find(permission);

if(p != m_permissions.end())
{
m_permissions.erase(p);
}
}

void PlayerRole::attachGroup(std::shared_ptr<AccessGroup> group)
{
m_groups.push_back(group);
}
void PlayerRole::removeGroup(std::shared_ptr<AccessGroup> group)
{
auto iter = m_groups.begin();
while (iter != m_groups.end())
{
if (*iter == group)
{
m_groups.erase(iter);
break;
}
++iter;
}
}
void PlayerRole::attachPermission(std::shared_ptr<std::string> permission)
{
m_permissions.insert(permission);
}
void PlayerRole::removePermission(std::shared_ptr<std::string> permission)
{
auto p = m_permissions.find(permission);

if(p != m_permissions.end())
{
m_permissions.erase(p);
}
}

bool PlayerRole::hasAccess(std::string_view permission)
{
// create union of permissions from all groups
// if find role in union then true
// m_permissons + sets from groups

// TODO: if permission contain "group@name" then get access by group name

std::set<std::shared_ptr<std::string>> temp = m_permissions;

for(auto group : m_groups)
{
auto perms = group->getPerms();
temp.insert(perms.begin(), perms.end());
}

const std::unique_ptr<GroupMngr> &groupMngr = gSPGlobal->getGroupManagerCore();

std::shared_ptr<std::string> perm = groupMngr->findPermission(permission);

if(temp.find(perm) != temp.end())
{
return true;
}

return false;
}

std::size_t GroupMngr::createGroup(std::string_view name)
{
m_groups.push_back(std::make_shared<AccessGroup>(name));
return m_groups.size() - 1;
}

std::shared_ptr<std::string> GroupMngr::createPermission(std::string_view name)
{
for(auto perm : m_permissions)
{
if(!perm->compare(name))
{
return perm;
}
}
m_permissions.push_back(std::make_shared<std::string>(name));
return m_permissions[m_permissions.size() - 1];
}

std::shared_ptr<std::string> GroupMngr::findPermission(std::string_view name)
{
for(auto perm : m_permissions)
{
if(!perm->compare(name))
{
return perm;
}
}
return nullptr;
}

std::shared_ptr<AccessGroup> GroupMngr::getGroup(std::size_t index)
{
return m_groups[index];
}

int GroupMngr::findGroup(std::string_view name)
{
for(std::size_t i = 0; i < m_groups.size(); i++)
{
if(!m_groups[i]->getNameCore().compare(name))
{
return i;
}
}
return -1;
}

void GroupMngr::clear()
{
m_groups.clear();
m_permissions.clear();
}
Loading