载入中...
搜索中...
未找到
Authority.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Module.h"
6#include "common/Result.h"
7#include "common/Snapshot.h"
9
10#include <cstdint>
11#include <deque>
12#include <functional>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <unordered_map>
17#include <vector>
18
19namespace eve::authority {
20
25
27enum class RuleEffect { Grant, Deny };
28
35inline constexpr std::string_view kWildcard = "*";
36
38struct Rule {
39 std::string id;
40 std::string actor;
41 std::string scope;
42 std::string capability;
43 std::string source;
45 int priority = 0;
46 double duration = 0.0;
47 double remaining = -1.0;
48 uint64_t order = 0;
49};
50
52struct Decision {
53 bool allowed = false;
54 std::string reason;
55 std::string winningRuleId;
56 std::string source;
57 int priority = 0;
58};
59
62
64struct Event {
65 uint64_t sequence = 0;
67 std::string ruleId;
68 std::string actor;
69 std::string scope;
70 std::string capability;
71 std::string source;
72 std::string reason;
73};
74
77public:
82 explicit Store(eve::PersistentId instanceId = {});
83
95 [[nodiscard]] eve::Result<std::string> grant(const std::string& actor, const std::string& scope,
96 const std::string& capability, const std::string& source,
97 int priority = 0, double duration = 0.0);
109 [[nodiscard]] eve::Result<std::string> deny(const std::string& actor, const std::string& scope,
110 const std::string& capability, const std::string& source,
111 int priority = 0, double duration = 0.0);
116 [[nodiscard]] eve::Result<void> revoke(const std::string& id, const std::string& reason = "revoked");
121 [[nodiscard]] int revokeBySource(const std::string& source, const std::string& reason = "source_revoked");
126 [[nodiscard]] int revokeByActor(const std::string& actor, const std::string& reason = "actor_revoked");
131 [[nodiscard]] int revokeByScope(const std::string& scope, const std::string& reason = "scope_revoked");
136 [[nodiscard]] int revokeByCapability(const std::string& capability,
137 const std::string& reason = "capability_revoked");
142 void update(double dtSeconds);
143
148 [[nodiscard]] bool can(const std::string& actor, const std::string& scope, const std::string& capability) const;
154 [[nodiscard]] Decision explain(const std::string& actor, const std::string& scope,
155 const std::string& capability) const;
162 [[nodiscard]] const Rule* find(const std::string& id) const;
163
165 int queryActor(const std::string& actor);
167 int queryScope(const std::string& scope);
169 int queryCapability(const std::string& capability);
175 int query(const std::string& actor, const std::string& scope, const std::string& capability);
182 const Rule* queryAt(int index) const;
183
190 [[nodiscard]] eve::Result<void> setEventCapacity(int capacity);
192 [[nodiscard]] int eventCapacity() const noexcept { return eventCapacity_; }
194 int eventCount() const;
202 const Event* eventAt(int index) const;
204 void clearEvents();
206 std::string snapshotJson() const;
215 [[nodiscard]] eve::Result<void> restoreJson(const std::string& json);
216
223 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
224
231 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
232 const eve::SnapshotHashProvider& hashProvider);
233
239 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
240
247 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
248 const eve::SnapshotHashProvider& hashProvider);
249
250private:
251 [[nodiscard]] eve::Result<std::string> add(RuleEffect effect, const std::string& actor, const std::string& scope,
252 const std::string& capability, const std::string& source, int priority,
253 double duration);
254 void emit(EventKind kind, const Rule& rule, const std::string& reason = {});
255 void trimEvents();
256 void indexInsert(const Rule& rule);
257 void indexErase(const Rule& rule);
258 void rebuildIndex();
259 [[nodiscard]] static std::string exactKey(const std::string& actor, const std::string& scope,
260 const std::string& capability);
261 [[nodiscard]] static bool ruleMatches(const Rule& rule, const std::string& actor, const std::string& scope,
262 const std::string& capability);
263 [[nodiscard]] static bool hasWildcard(const Rule& rule);
264 [[nodiscard]] int revokeMatching(const std::string& reason, const std::function<bool(const Rule&)>& predicate);
265
266 uint64_t nextId_ = 1;
267 uint64_t nextOrder_ = 1;
268 uint64_t nextSequence_ = 1;
269 int eventCapacity_ = 0;
270 eve::PersistentId instanceId_;
271 eve::Revision revision_;
273 std::deque<Rule> rules_;
274 std::deque<Event> events_;
275 std::vector<const Rule*> query_;
276 std::unordered_map<std::string, std::vector<std::string>> exactIndex_;
277 std::vector<std::string> wildcardIds_;
278};
279
281std::string effectName(RuleEffect effect);
284
286class Authority : public Module {
287public:
290 Authority() = default;
292 ~Authority() override = default;
293
299 [[nodiscard]] static eve::Result<AuthorityStoreHandleRef> newStore();
305 [[nodiscard]] static bool isStale(AuthorityStoreHandleRef reference) noexcept;
306
307private:
309};
310
311} // namespace eve::authority
float duration
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
int priority
int instanceId
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::uint32_t capacity
JobScope scope
Snapshot snapshot
std::unique_ptr< ParticleEffect > effect
Move-only, checked operation results for the common layer.
std::string_view source
std::string kind
Versioned, integrity-checked persistence envelopes.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Script module factory for generic authority stores.
Definition Authority.h:286
static eve::Result< void > release(AuthorityStoreHandleRef reference)
Releases a module-owned store.
static eve::Result< AuthorityStoreHandleRef > newStore()
Allocates an authority store and returns its ownership reference.
static bool isStale(AuthorityStoreHandleRef reference) noexcept
Reports whether a store reference is stale for the current module.
static eve::script::Borrowed< Store > resolve(AuthorityStoreHandleRef reference) noexcept
Resolves a live store as a non-owning observation.
~Authority() override=default
Releases Authority resources.
Authority()=default
Constructs a Authority.
Generic, deterministic capability authority store.
Definition Authority.h:76
int eventCapacity() const noexcept
Returns the event retention capacity; 0 means unlimited.
Definition Authority.h:192
Id128 public API.
Definition Identity.h:111
Non-owning observation of a registry-owned object.
Slot/generation registry whose objects are exclusively unique-owned.
std::string effectName(RuleEffect effect)
Returns the stable lowercase name of a rule effect.
EventKind
Kind of authority lifecycle event.
Definition Authority.h:61
RuleEffect
Whether an authority rule grants or denies a capability.
Definition Authority.h:27
constexpr std::string_view kWildcard
Opaque field token that matches any actor, scope, or capability.
Definition Authority.h:35
std::string eventKindName(EventKind kind)
Returns the stable lowercase name of an event kind.
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
Handle domain for module-owned authority stores.
Definition Authority.h:22
Explainable result of an authority decision.
Definition Authority.h:52
std::string winningRuleId
Definition Authority.h:55
Deterministically sequenced authority lifecycle event.
Definition Authority.h:64
std::string capability
Definition Authority.h:70
std::string ruleId
Definition Authority.h:67
std::string actor
Definition Authority.h:68
std::string reason
Definition Authority.h:72
std::string scope
Definition Authority.h:69
std::string source
Definition Authority.h:71
One stable authority rule over an opaque actor and scope.
Definition Authority.h:38
std::string scope
Definition Authority.h:41
RuleEffect effect
Definition Authority.h:44
std::string actor
Definition Authority.h:40
std::string capability
Definition Authority.h:42
std::string source
Definition Authority.h:43
Handle plus owner-lifetime epoch for a non-ECS registry object.