Fix list filtering and matching issues
This commit is contained in:
parent
584a55cd60
commit
8e05a00136
2 changed files with 23 additions and 19 deletions
|
@ -78,7 +78,7 @@ public class EntityList : IEnumerable<EntityName> {
|
||||||
foreach (var entry in this) {
|
foreach (var entry in this) {
|
||||||
if (entry.Type == EntityType.Role) {
|
if (entry.Type == EntityType.Role) {
|
||||||
if (entry.Id.HasValue) {
|
if (entry.Id.HasValue) {
|
||||||
return authorRoles.Any(r => r.Id == entry.Id.Value);
|
if (authorRoles.Any(r => r.Id == entry.Id.Value)) return true;
|
||||||
} else {
|
} else {
|
||||||
foreach (var r in authorRoles) {
|
foreach (var r in authorRoles) {
|
||||||
if (!string.Equals(r.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
if (!string.Equals(r.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
||||||
|
@ -88,16 +88,15 @@ public class EntityList : IEnumerable<EntityName> {
|
||||||
}
|
}
|
||||||
} else if (entry.Type == EntityType.Channel) {
|
} else if (entry.Type == EntityType.Channel) {
|
||||||
if (entry.Id.HasValue) {
|
if (entry.Id.HasValue) {
|
||||||
return entry.Id.Value == channel.Id;
|
if (entry.Id.Value == channel.Id) return true;
|
||||||
} else {
|
} else {
|
||||||
if (!string.Equals(channel.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
if (!string.Equals(channel.Name, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
||||||
if (keepId) entry.SetId(channel.Id);
|
if (keepId) entry.SetId(channel.Id);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else // User
|
} else { // User
|
||||||
{
|
|
||||||
if (entry.Id.HasValue) {
|
if (entry.Id.HasValue) {
|
||||||
return entry.Id.Value == author.Id;
|
if (entry.Id.Value == author.Id) return true;
|
||||||
} else {
|
} else {
|
||||||
if (!string.Equals(author.Username, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
if (!string.Equals(author.Username, entry.Name, StringComparison.OrdinalIgnoreCase)) break;
|
||||||
if (keepId) entry.SetId(author.Id);
|
if (keepId) entry.SetId(author.Id);
|
||||||
|
|
|
@ -49,22 +49,23 @@ public class FilterList {
|
||||||
public FilterList(JObject config, string whitelistKey = "Whitelist", string blacklistKey = "Blacklist", string exemptKey = "Exempt") {
|
public FilterList(JObject config, string whitelistKey = "Whitelist", string blacklistKey = "Blacklist", string exemptKey = "Exempt") {
|
||||||
if (whitelistKey != null && config[whitelistKey] != null &&
|
if (whitelistKey != null && config[whitelistKey] != null &&
|
||||||
blacklistKey != null && config[blacklistKey] != null) {
|
blacklistKey != null && config[blacklistKey] != null) {
|
||||||
|
// User has defined both keys at once, which is not allowed
|
||||||
throw new FormatException($"Cannot have both '{whitelistKey}' and '{blacklistKey}' defined at once.");
|
throw new FormatException($"Cannot have both '{whitelistKey}' and '{blacklistKey}' defined at once.");
|
||||||
}
|
}
|
||||||
|
|
||||||
JToken? valueSrc = null;
|
JToken? incoming = null;
|
||||||
if (whitelistKey != null) {
|
if (whitelistKey != null) {
|
||||||
// Try getting a whitelist
|
// Try getting a whitelist
|
||||||
valueSrc = config[whitelistKey];
|
incoming = config[whitelistKey];
|
||||||
Mode = FilterMode.Whitelist;
|
Mode = FilterMode.Whitelist;
|
||||||
}
|
}
|
||||||
if (valueSrc != null && blacklistKey != null) {
|
if (incoming == null && blacklistKey != null) {
|
||||||
// Try getting a blacklist
|
// Try getting a blacklist
|
||||||
valueSrc = config[blacklistKey];
|
incoming = config[blacklistKey];
|
||||||
Mode = FilterMode.Blacklist;
|
Mode = FilterMode.Blacklist;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valueSrc == null) {
|
if (incoming == null) {
|
||||||
// Got neither. Have an empty list.
|
// Got neither. Have an empty list.
|
||||||
Mode = FilterMode.None;
|
Mode = FilterMode.None;
|
||||||
FilteredList = new EntityList();
|
FilteredList = new EntityList();
|
||||||
|
@ -72,18 +73,22 @@ public class FilterList {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify that the specified array is actually an array.
|
if (incoming.Type != JTokenType.Array)
|
||||||
if (valueSrc != null && valueSrc.Type != JTokenType.Array)
|
throw new FormatException("Filtering list must be a JSON array.");
|
||||||
throw new ArgumentException("Given list must be a JSON array.");
|
FilteredList = new EntityList((JArray)incoming, true);
|
||||||
FilteredList = new EntityList((JArray)valueSrc!, true);
|
|
||||||
|
|
||||||
// Verify the same for the exemption list.
|
// Verify the same for the exemption list.
|
||||||
FilterExemptions = new EntityList();
|
|
||||||
if (exemptKey != null) {
|
if (exemptKey != null) {
|
||||||
var exc = config[exemptKey];
|
var incomingEx = config[exemptKey];
|
||||||
if (exc != null && exc.Type == JTokenType.Array) {
|
if (incomingEx == null) {
|
||||||
FilterExemptions = new EntityList(exc, true);
|
FilterExemptions = new EntityList();
|
||||||
|
} else if (incomingEx.Type != JTokenType.Array) {
|
||||||
|
throw new FormatException("Filtering exemption list must be a JSON array.");
|
||||||
|
} else {
|
||||||
|
FilterExemptions = new EntityList(incomingEx, true);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
FilterExemptions = new EntityList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue