feat: public/private lookup flags, consistency

This commit is contained in:
spiral
2021-12-06 00:32:54 -05:00
parent aacf5909a9
commit 455830a2b5
16 changed files with 44 additions and 40 deletions

View File

@@ -133,14 +133,29 @@ public class Context
await Reply($"{Emojis.Warn} This command is deprecated and will be removed soon. In the future, please use `pk;{commandDef.Key}`.");
}
public LookupContext LookupContextFor(PKSystem target) =>
System?.Id == target.Id ? LookupContext.ByOwner : LookupContext.ByNonOwner;
public LookupContext LookupContextFor(SystemId systemId)
{
var hasPrivateOverride = this.MatchFlag("private", "priv");
var hasPublicOverride = this.MatchFlag("public", "pub");
public LookupContext LookupContextFor(SystemId systemId) =>
System?.Id == systemId ? LookupContext.ByOwner : LookupContext.ByNonOwner;
if (hasPrivateOverride && hasPublicOverride)
throw new PKError("Cannot match both public and private flags at the same time.");
public LookupContext LookupContextFor(PKMember target) =>
System?.Id == target.System ? LookupContext.ByOwner : LookupContext.ByNonOwner;
if (System.Id != systemId)
{
if (hasPrivateOverride)
throw Errors.NotOwnInfo;
return LookupContext.ByNonOwner;
}
if (hasPrivateOverride)
return LookupContext.ByOwner;
if (hasPublicOverride)
return LookupContext.ByNonOwner;
// todo: add config defaults
return LookupContext.ByOwner;
}
public IComponentContext Services => _provider;
}

View File

@@ -23,7 +23,7 @@ public static class ContextChecksExt
public static Context CheckSystemPrivacy(this Context ctx, PKSystem target, PrivacyLevel level)
{
if (level.CanAccess(ctx.LookupContextFor(target))) return ctx;
if (level.CanAccess(ctx.LookupContextFor(target.Id))) return ctx;
throw new PKError("You do not have permission to access this information.");
}

View File

@@ -48,13 +48,4 @@ public static class ContextPrivacyExt
ctx.PopArgument();
return subject;
}
public static bool MatchPrivateFlag(this Context ctx, LookupContext pctx)
{
var privacy = true;
if (ctx.MatchFlag("a", "all")) privacy = false;
if (pctx == LookupContext.ByNonOwner && !privacy) throw Errors.LookupNotAllowed;
return privacy;
}
}