IdCardConsoleSystem.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Content.Server.StationRecords.Systems;
  2. using Content.Shared.Access.Components;
  3. using Content.Shared.Access.Systems;
  4. using Content.Shared.Administration.Logs;
  5. using Content.Shared.Database;
  6. using Content.Shared.Roles;
  7. using Content.Shared.StationRecords;
  8. using Content.Shared.StatusIcon;
  9. using JetBrains.Annotations;
  10. using Robust.Server.GameObjects;
  11. using Robust.Shared.Containers;
  12. using Robust.Shared.Prototypes;
  13. using System.Linq;
  14. using static Content.Shared.Access.Components.IdCardConsoleComponent;
  15. using Content.Shared.Access;
  16. namespace Content.Server.Access.Systems;
  17. [UsedImplicitly]
  18. public sealed class IdCardConsoleSystem : SharedIdCardConsoleSystem
  19. {
  20. [Dependency] private readonly IPrototypeManager _prototype = default!;
  21. [Dependency] private readonly StationRecordsSystem _record = default!;
  22. [Dependency] private readonly UserInterfaceSystem _userInterface = default!;
  23. [Dependency] private readonly AccessReaderSystem _accessReader = default!;
  24. [Dependency] private readonly AccessSystem _access = default!;
  25. [Dependency] private readonly IdCardSystem _idCard = default!;
  26. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  27. public override void Initialize()
  28. {
  29. base.Initialize();
  30. SubscribeLocalEvent<IdCardConsoleComponent, WriteToTargetIdMessage>(OnWriteToTargetIdMessage);
  31. // one day, maybe bound user interfaces can be shared too.
  32. SubscribeLocalEvent<IdCardConsoleComponent, ComponentStartup>(UpdateUserInterface);
  33. SubscribeLocalEvent<IdCardConsoleComponent, EntInsertedIntoContainerMessage>(UpdateUserInterface);
  34. SubscribeLocalEvent<IdCardConsoleComponent, EntRemovedFromContainerMessage>(UpdateUserInterface);
  35. }
  36. private void OnWriteToTargetIdMessage(EntityUid uid, IdCardConsoleComponent component, WriteToTargetIdMessage args)
  37. {
  38. if (args.Actor is not { Valid: true } player)
  39. return;
  40. TryWriteToTargetId(uid, args.FullName, args.JobTitle, args.AccessList, args.JobPrototype, player, component);
  41. UpdateUserInterface(uid, component, args);
  42. }
  43. private void UpdateUserInterface(EntityUid uid, IdCardConsoleComponent component, EntityEventArgs args)
  44. {
  45. if (!component.Initialized)
  46. return;
  47. var privilegedIdName = string.Empty;
  48. List<ProtoId<AccessLevelPrototype>>? possibleAccess = null;
  49. if (component.PrivilegedIdSlot.Item is { Valid: true } item)
  50. {
  51. privilegedIdName = EntityManager.GetComponent<MetaDataComponent>(item).EntityName;
  52. possibleAccess = _accessReader.FindAccessTags(item).ToList();
  53. }
  54. IdCardConsoleBoundUserInterfaceState newState;
  55. // this could be prettier
  56. if (component.TargetIdSlot.Item is not { Valid: true } targetId)
  57. {
  58. newState = new IdCardConsoleBoundUserInterfaceState(
  59. component.PrivilegedIdSlot.HasItem,
  60. PrivilegedIdIsAuthorized(uid, component),
  61. false,
  62. null,
  63. null,
  64. null,
  65. possibleAccess,
  66. string.Empty,
  67. privilegedIdName,
  68. string.Empty);
  69. }
  70. else
  71. {
  72. var targetIdComponent = EntityManager.GetComponent<IdCardComponent>(targetId);
  73. var targetAccessComponent = EntityManager.GetComponent<AccessComponent>(targetId);
  74. var jobProto = new ProtoId<AccessLevelPrototype>(string.Empty);
  75. if (TryComp<StationRecordKeyStorageComponent>(targetId, out var keyStorage)
  76. && keyStorage.Key is {} key
  77. && _record.TryGetRecord<GeneralStationRecord>(key, out var record))
  78. {
  79. jobProto = record.JobPrototype;
  80. }
  81. newState = new IdCardConsoleBoundUserInterfaceState(
  82. component.PrivilegedIdSlot.HasItem,
  83. PrivilegedIdIsAuthorized(uid, component),
  84. true,
  85. targetIdComponent.FullName,
  86. targetIdComponent.LocalizedJobTitle,
  87. targetAccessComponent.Tags.ToList(),
  88. possibleAccess,
  89. jobProto,
  90. privilegedIdName,
  91. Name(targetId));
  92. }
  93. _userInterface.SetUiState(uid, IdCardConsoleUiKey.Key, newState);
  94. }
  95. /// <summary>
  96. /// Called whenever an access button is pressed, adding or removing that access from the target ID card.
  97. /// Writes data passed from the UI into the ID stored in <see cref="IdCardConsoleComponent.TargetIdSlot"/>, if present.
  98. /// </summary>
  99. private void TryWriteToTargetId(EntityUid uid,
  100. string newFullName,
  101. string newJobTitle,
  102. List<ProtoId<AccessLevelPrototype>> newAccessList,
  103. ProtoId<AccessLevelPrototype> newJobProto,
  104. EntityUid player,
  105. IdCardConsoleComponent? component = null)
  106. {
  107. if (!Resolve(uid, ref component))
  108. return;
  109. if (component.TargetIdSlot.Item is not { Valid: true } targetId || !PrivilegedIdIsAuthorized(uid, component))
  110. return;
  111. _idCard.TryChangeFullName(targetId, newFullName, player: player);
  112. _idCard.TryChangeJobTitle(targetId, newJobTitle, player: player);
  113. if (_prototype.TryIndex<JobPrototype>(newJobProto, out var job)
  114. && _prototype.TryIndex(job.Icon, out var jobIcon))
  115. {
  116. _idCard.TryChangeJobIcon(targetId, jobIcon, player: player);
  117. _idCard.TryChangeJobDepartment(targetId, job);
  118. }
  119. UpdateStationRecord(uid, targetId, newFullName, newJobTitle, job);
  120. if (!newAccessList.TrueForAll(x => component.AccessLevels.Contains(x)))
  121. {
  122. _sawmill.Warning($"User {ToPrettyString(uid)} tried to write unknown access tag.");
  123. return;
  124. }
  125. var oldTags = _access.TryGetTags(targetId) ?? new List<ProtoId<AccessLevelPrototype>>();
  126. oldTags = oldTags.ToList();
  127. var privilegedId = component.PrivilegedIdSlot.Item;
  128. if (oldTags.SequenceEqual(newAccessList))
  129. return;
  130. // I hate that C# doesn't have an option for this and don't desire to write this out the hard way.
  131. // var difference = newAccessList.Difference(oldTags);
  132. var difference = newAccessList.Union(oldTags).Except(newAccessList.Intersect(oldTags)).ToHashSet();
  133. // NULL SAFETY: PrivilegedIdIsAuthorized checked this earlier.
  134. var privilegedPerms = _accessReader.FindAccessTags(privilegedId!.Value).ToHashSet();
  135. if (!difference.IsSubsetOf(privilegedPerms))
  136. {
  137. _sawmill.Warning($"User {ToPrettyString(uid)} tried to modify permissions they could not give/take!");
  138. return;
  139. }
  140. var addedTags = newAccessList.Except(oldTags).Select(tag => "+" + tag).ToList();
  141. var removedTags = oldTags.Except(newAccessList).Select(tag => "-" + tag).ToList();
  142. _access.TrySetTags(targetId, newAccessList);
  143. /*TODO: ECS SharedIdCardConsoleComponent and then log on card ejection, together with the save.
  144. This current implementation is pretty shit as it logs 27 entries (27 lines) if someone decides to give themselves AA*/
  145. _adminLogger.Add(LogType.Action, LogImpact.Medium,
  146. $"{ToPrettyString(player):player} has modified {ToPrettyString(targetId):entity} with the following accesses: [{string.Join(", ", addedTags.Union(removedTags))}] [{string.Join(", ", newAccessList)}]");
  147. }
  148. /// <summary>
  149. /// Returns true if there is an ID in <see cref="IdCardConsoleComponent.PrivilegedIdSlot"/> and said ID satisfies the requirements of <see cref="AccessReaderComponent"/>.
  150. /// </summary>
  151. /// <remarks>
  152. /// Other code relies on the fact this returns false if privileged Id is null. Don't break that invariant.
  153. /// </remarks>
  154. private bool PrivilegedIdIsAuthorized(EntityUid uid, IdCardConsoleComponent? component = null)
  155. {
  156. if (!Resolve(uid, ref component))
  157. return true;
  158. if (!TryComp<AccessReaderComponent>(uid, out var reader))
  159. return true;
  160. var privilegedId = component.PrivilegedIdSlot.Item;
  161. return privilegedId != null && _accessReader.IsAllowed(privilegedId.Value, uid, reader);
  162. }
  163. private void UpdateStationRecord(EntityUid uid, EntityUid targetId, string newFullName, ProtoId<AccessLevelPrototype> newJobTitle, JobPrototype? newJobProto)
  164. {
  165. if (!TryComp<StationRecordKeyStorageComponent>(targetId, out var keyStorage)
  166. || keyStorage.Key is not { } key
  167. || !_record.TryGetRecord<GeneralStationRecord>(key, out var record))
  168. {
  169. return;
  170. }
  171. record.Name = newFullName;
  172. record.JobTitle = newJobTitle;
  173. if (newJobProto != null)
  174. {
  175. record.JobPrototype = newJobProto.ID;
  176. record.JobIcon = newJobProto.Icon;
  177. }
  178. _record.Synchronize(key);
  179. }
  180. }