Files
tbd-station-14/Content.Server/Players/JobWhitelist/JobWhitelistManager.cs
Pieter-Jan Briers 0c97520276 Fix usages of TryIndex() (#39124)
* Fix usages of TryIndex()

Most usages of TryIndex() were using it incorrectly. Checking whether prototype IDs specified in prototypes actually existed before using them. This is not appropriate as it's just hiding bugs that should be getting caught by the YAML linter and other tools. (#39115)

This then resulted in TryIndex() getting modified to log errors (94f98073b0), which is incorrect as it causes false-positive errors in proper uses of the API: external data validation. (#39098)

This commit goes through and checks every call site of TryIndex() to see whether they were correct. Most call sites were replaced with the new Resolve(), which is suitable for these "defensive programming" use cases.

Fixes #39115

Breaking change: while doing this I noticed IdCardComponent and related systems were erroneously using ProtoId<AccessLevelPrototype> for job prototypes. This has been corrected.

* fix tests

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-09-09 18:17:56 +02:00

115 lines
3.5 KiB
C#

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.Database;
using Content.Shared.CCVar;
using Content.Shared.Players.JobWhitelist;
using Content.Shared.Roles;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Serilog;
namespace Content.Server.Players.JobWhitelist;
public sealed class JobWhitelistManager : IPostInjectInit
{
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly IServerDbManager _db = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IPrototypeManager _prototypes = default!;
[Dependency] private readonly UserDbDataManager _userDb = default!;
private readonly Dictionary<NetUserId, HashSet<string>> _whitelists = new();
public void Initialize()
{
_net.RegisterNetMessage<MsgJobWhitelist>();
}
private async Task LoadData(ICommonSession session, CancellationToken cancel)
{
var whitelists = await _db.GetJobWhitelists(session.UserId, cancel);
cancel.ThrowIfCancellationRequested();
_whitelists[session.UserId] = whitelists.ToHashSet();
}
private void FinishLoad(ICommonSession session)
{
SendJobWhitelist(session);
}
private void ClientDisconnected(ICommonSession session)
{
_whitelists.Remove(session.UserId);
}
public async void AddWhitelist(NetUserId player, ProtoId<JobPrototype> job)
{
if (_whitelists.TryGetValue(player, out var whitelists))
whitelists.Add(job);
await _db.AddJobWhitelist(player, job);
if (_player.TryGetSessionById(player, out var session))
SendJobWhitelist(session);
}
public bool IsAllowed(ICommonSession session, ProtoId<JobPrototype> job)
{
if (!_config.GetCVar(CCVars.GameRoleWhitelist))
return true;
if (!_prototypes.Resolve(job, out var jobPrototype) ||
!jobPrototype.Whitelisted)
{
return true;
}
return IsWhitelisted(session.UserId, job);
}
public bool IsWhitelisted(NetUserId player, ProtoId<JobPrototype> job)
{
if (!_whitelists.TryGetValue(player, out var whitelists))
{
Log.Error("Unable to check if player {Player} is whitelisted for {Job}. Stack trace:\\n{StackTrace}",
player,
job,
Environment.StackTrace);
return false;
}
return whitelists.Contains(job);
}
public async void RemoveWhitelist(NetUserId player, ProtoId<JobPrototype> job)
{
_whitelists.GetValueOrDefault(player)?.Remove(job);
await _db.RemoveJobWhitelist(player, job);
if (_player.TryGetSessionById(new NetUserId(player), out var session))
SendJobWhitelist(session);
}
public void SendJobWhitelist(ICommonSession player)
{
var msg = new MsgJobWhitelist
{
Whitelist = _whitelists.GetValueOrDefault(player.UserId) ?? new HashSet<string>()
};
_net.ServerSendMessage(msg, player.Channel);
}
void IPostInjectInit.PostInject()
{
_userDb.AddOnLoadPlayer(LoadData);
_userDb.AddOnFinishLoad(FinishLoad);
_userDb.AddOnPlayerDisconnect(ClientDisconnected);
}
}