Teaspeak-Server/server/src/PermissionCalculator.h

114 lines
5.0 KiB
C
Raw Normal View History

2021-02-28 19:03:41 +01:00
#pragma once
#include <Definitions.h>
#include <PermissionManager.h>
#include <vector>
#include <memory>
namespace ts {
class BasicChannel;
}
namespace ts::server {
class DataClient;
class VirtualServer;
namespace groups {
class ChannelGroup;
class ServerGroup;
class GroupManager;
}
/**
* Helper for calculating the client permissions for a certain channel.
* Note: All functions are not thread save!
*/
class ClientPermissionCalculator {
public:
2021-04-14 16:00:02 +02:00
/* When providing the pointer to the channel the channel tree **should** not be locked in any way! */
2021-02-28 19:03:41 +01:00
explicit ClientPermissionCalculator(DataClient* /* client */, const std::shared_ptr<BasicChannel>& /* target channel */);
explicit ClientPermissionCalculator(DataClient* /* client */, ChannelId /* target channel id */);
explicit ClientPermissionCalculator(
const std::shared_ptr<VirtualServer>& /* server */,
ClientDbId /* client database id */,
ClientType /* client type */,
ChannelId /* target channel id */
);
/**
* Calculate the given permissions.
* This method can be called from everywhere without any locking needed.
* @param granted
* @return
*/
2021-04-14 14:57:04 +02:00
[[nodiscard]] permission::v2::PermissionFlaggedValue calculate_permission(
2021-02-28 19:03:41 +01:00
permission::PermissionType,
bool granted = false
);
/**
* Calculate the given permissions.
* This method can be called from everywhere without any locking needed.
* @param channel
* @param calculate_granted
* @return
*/
2021-04-14 14:57:04 +02:00
[[nodiscard]] std::vector<std::pair<permission::PermissionType, permission::v2::PermissionFlaggedValue>> calculate_permissions(
2021-02-28 19:03:41 +01:00
const std::deque<permission::PermissionType>&,
bool calculate_granted = false
);
2021-04-14 14:57:04 +02:00
/**
* Test if the target has the target permission granted.
* If the client does not have any value assigned for the permission `false` will be returned.
*/
[[nodiscard]] bool permission_granted(
const permission::PermissionType& /* target permission */,
const permission::PermissionValue& /* required value */,
bool /* granted permission */ = false
);
/**
* Test if the target has the target permission granted.
* If the client does not have any value assigned for the permission,
* `true` will be returned if the `required value` contains no value
* otherwise false will be returned.
*
* This method should be used when testing permissions which are allowed by default except if they're
* specified otherwise. An example permission would be if we're testing against the channel needed join power.
*/
[[nodiscard]] bool permission_granted(
const permission::PermissionType& /* target permission */,
const permission::v2::PermissionFlaggedValue& /* required value */,
bool /* granted permission */ = false
);
//const PermissionValue& required, const PermissionFlaggedValue& given, bool requires_given = true
2021-02-28 19:03:41 +01:00
private:
/* given fields */
ServerId virtual_server_id;
ClientDbId client_database_id;
ClientType client_type;
ChannelId channel_id_;
std::shared_ptr<groups::GroupManager> group_manager_{};
std::shared_ptr<permission::v2::PermissionManager> channel_permissions{};
std::function<std::shared_ptr<groups::ChannelGroup>()> default_channel_group{[]{ return nullptr; }};
std::function<std::shared_ptr<groups::ServerGroup>()> default_server_group{[]{ return nullptr; }};
2021-02-28 19:03:41 +01:00
/* fields which will be set when calculating permissions */
2021-04-14 14:57:04 +02:00
std::shared_ptr<permission::v2::PermissionManager> client_permissions_{};
2021-02-28 19:03:41 +01:00
2021-04-14 14:57:04 +02:00
std::optional<bool> skip_enabled{};
2021-02-28 19:03:41 +01:00
std::optional<std::shared_ptr<groups::ChannelGroup>> assigned_channel_group_{};
std::optional<std::vector<std::shared_ptr<groups::ServerGroup>>> assigned_server_groups_{};
void initialize_client(DataClient* /* client */);
2021-03-22 20:46:19 +01:00
void initialize_default_groups(const std::shared_ptr<VirtualServer>& /* server */);
2021-02-28 19:03:41 +01:00
[[nodiscard]] const std::vector<std::shared_ptr<groups::ServerGroup>>& assigned_server_groups();
[[nodiscard]] const std::shared_ptr<groups::ChannelGroup>& assigned_channel_group();
2021-04-14 14:57:04 +02:00
[[nodiscard]] const std::shared_ptr<permission::v2::PermissionManager>& client_permissions();
[[nodiscard]] bool has_global_skip_permission();
2021-02-28 19:03:41 +01:00
};
}