Move Access & AccessReader to shared. (#5798)

* git mv

* Move Access Component & system.

- Name space changes
- Rename AccessReader to AccessReaderComponent
- Also need an abstract TryGetSlot function for SharedInventoryComponent

* better TryGetSlot

* Ah yes, tests exist.
This commit is contained in:
Leon Friedrich
2021-12-26 17:07:28 +13:00
committed by GitHub
parent 32d752bfa6
commit b675bdb789
27 changed files with 90 additions and 81 deletions

View File

@@ -1,25 +0,0 @@
using System.Collections.Generic;
using Content.Server.Access.Systems;
using Content.Shared.Access;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
/// <summary>
/// Simple mutable access provider found on ID cards and such.
/// </summary>
[RegisterComponent]
[Friend(typeof(AccessSystem))]
public class AccessComponent : Component
{
public override string Name => "Access";
[DataField("tags", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<AccessLevelPrototype>))]
[ViewVariables]
public HashSet<string> Tags = new();
}
}

View File

@@ -1,48 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Hands.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Shared.Access;
using Content.Shared.Inventory;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Access.Components
{
/// <summary>
/// Stores access levels necessary to "use" an entity
/// and allows checking if something or somebody is authorized with these access levels.
/// </summary>
[RegisterComponent]
public class AccessReader : Component
{
public override string Name => "AccessReader";
/// <summary>
/// Whether this reader is enabled or not. If disabled, all access
/// checks will pass.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool Enabled = true;
/// <summary>
/// The set of tags that will automatically deny an allowed check, if any of them are present.
/// </summary>
public HashSet<string> DenyTags = new();
/// <summary>
/// List of access lists to check allowed against. For an access check to pass
/// there has to be an access list that is a subset of the access in the checking list.
/// </summary>
[DataField("access")]
[ViewVariables]
public List<HashSet<string>> AccessLists = new();
}
}

View File

@@ -5,6 +5,7 @@ using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.Access;
using Content.Shared.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Containers.ItemSlots;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
@@ -29,7 +30,7 @@ namespace Content.Server.Access.Components
{
base.Initialize();
Owner.EnsureComponentWarn<AccessReader>();
Owner.EnsureComponentWarn<AccessReaderComponent>();
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
if (UserInterface != null)
@@ -66,11 +67,11 @@ namespace Content.Server.Access.Components
}
/// <summary>
/// Returns true if there is an ID in <see cref="PrivilegedIdSlot"/> and said ID satisfies the requirements of <see cref="AccessReader"/>.
/// Returns true if there is an ID in <see cref="PrivilegedIdSlot"/> and said ID satisfies the requirements of <see cref="AccessReaderComponent"/>.
/// </summary>
private bool PrivilegedIdIsAuthorized()
{
if (!_entities.TryGetComponent(Owner, out AccessReader? reader))
if (!_entities.TryGetComponent(Owner, out AccessReaderComponent? reader))
{
return true;
}

View File

@@ -1,120 +0,0 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Access.Components;
using Content.Server.Inventory.Components;
using Content.Server.Items;
using Content.Shared.Access;
using Content.Shared.Hands.Components;
using Content.Shared.Inventory;
using Content.Shared.PDA;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
namespace Content.Server.Access.Systems
{
public class AccessReaderSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AccessReader, ComponentInit>(OnInit);
}
private void OnInit(EntityUid uid, AccessReader reader, ComponentInit args)
{
var allTags = reader.AccessLists.SelectMany(c => c).Union(reader.DenyTags);
foreach (var level in allTags)
{
if (!_prototypeManager.HasIndex<AccessLevelPrototype>(level))
{
Logger.ErrorS("access", $"Invalid access level: {level}");
}
}
}
/// <summary>
/// Searches an <see cref="AccessComponent"/> in the entity itself, in its active hand or in its ID slot.
/// Then compares the found access with the configured access lists to see if it is allowed.
/// </summary>
/// <remarks>
/// If no access is found, an empty set is used instead.
/// </remarks>
/// <param name="entity">The entity to be searched for access.</param>
public bool IsAllowed(AccessReader reader, EntityUid entity)
{
var tags = FindAccessTags(entity);
return IsAllowed(reader, tags);
}
public bool IsAllowed(AccessReader reader, ICollection<string> accessTags)
{
if (reader.DenyTags.Overlaps(accessTags))
{
// Sec owned by cargo.
return false;
}
return !reader.Enabled || reader.AccessLists.Count == 0 || reader.AccessLists.Any(a => a.IsSubsetOf(accessTags));
}
public ICollection<string> FindAccessTags(EntityUid uid)
{
// check entity itself
if (FindAccessTagsItem(uid, out var tags))
return tags;
// maybe access component inside its hands?
if (EntityManager.TryGetComponent(uid, out SharedHandsComponent? hands))
{
if (hands.TryGetActiveHeldEntity(out var heldItem) &&
FindAccessTagsItem(heldItem, out tags))
{
return tags;
}
}
// maybe its inside an inventory slot?
if (EntityManager.TryGetComponent(uid, out InventoryComponent? inventoryComponent))
{
if (inventoryComponent.HasSlot(EquipmentSlotDefines.Slots.IDCARD) &&
inventoryComponent.TryGetSlotItem(EquipmentSlotDefines.Slots.IDCARD, out ItemComponent? item) &&
FindAccessTagsItem(item.Owner, out tags)
)
{
return tags;
}
}
return Array.Empty<string>();
}
/// <summary>
/// Try to find <see cref="AccessComponent"/> on this item
/// or inside this item (if it's pda)
/// </summary>
private bool FindAccessTagsItem(EntityUid uid, [NotNullWhen(true)] out HashSet<string>? tags)
{
if (EntityManager.TryGetComponent(uid, out AccessComponent? access))
{
tags = access.Tags;
return true;
}
if (EntityManager.TryGetComponent(uid, out PDAComponent? pda) &&
pda.ContainedID?.Owner is {Valid: true} id)
{
tags = EntityManager.GetComponent<AccessComponent>(id).Tags;
return true;
}
tags = null;
return false;
}
}
}

View File

@@ -1,24 +0,0 @@
using Content.Server.Access.Components;
using Robust.Shared.GameObjects;
using System.Collections.Generic;
namespace Content.Server.Access.Systems
{
public class AccessSystem : EntitySystem
{
/// <summary>
/// Replaces the set of access tags we have with the provided set.
/// </summary>
/// <param name="newTags">The new access tags</param>
public bool TrySetTags(EntityUid uid, IEnumerable<string> newTags, AccessComponent? access = null)
{
if (!Resolve(uid, ref access))
return false;
access.Tags.Clear();
access.Tags.UnionWith(newTags);
return true;
}
}
}

View File

@@ -1,4 +1,5 @@
using Content.Server.Access.Components;
using Content.Shared.Access.Systems;
using Content.Shared.Roles;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;