finish implementing permissions backend logic

This commit is contained in:
Timothy Baldridge 2019-09-29 14:53:25 -06:00
parent 6c24e4fdbb
commit 9f6fbe1692
3 changed files with 84 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack.Validation;
@ -32,6 +33,16 @@ namespace Wabbajack.Test
CanUseInOtherGames: false
";
private static string server_whitelist = @"
GoogleIDs:
- googleDEADBEEF
AllowedPrefixes:
- https://somegoodplace.com/
";
[TestInitialize]
public void TestSetup()
@ -39,6 +50,7 @@ namespace Wabbajack.Test
WorkQueue.Init((x, y, z) => { }, (min, max) => { });
validate = new ValidateModlist();
validate.LoadAuthorPermissionsFromString(permissions);
validate.LoadServerWhitelist(server_whitelist);
}
[TestMethod]
@ -174,6 +186,47 @@ namespace Wabbajack.Test
errors = validate.Validate(modlist);
Assert.AreEqual(errors.Count(), 1);
// Error due to file downloaded from 3rd party
modlist.GameType = Game.Skyrim;
modlist.Archives[0] = new DirectURLArchive()
{
URL = "https://somebadplace.com",
Hash = "DEADBEEF"
};
errors = validate.Validate(modlist);
Assert.AreEqual(errors.Count(), 1);
// Ok due to file downloaded from whitelisted 3rd party
modlist.GameType = Game.Skyrim;
modlist.Archives[0] = new DirectURLArchive()
{
URL = "https://somegoodplace.com/myfile",
Hash = "DEADBEEF"
};
errors = validate.Validate(modlist);
Assert.AreEqual(errors.Count(), 0);
// Error due to file downloaded from bad 3rd party
modlist.GameType = Game.Skyrim;
modlist.Archives[0] = new GoogleDriveMod()
{
Id = "bleg",
Hash = "DEADBEEF"
};
errors = validate.Validate(modlist);
Assert.AreEqual(errors.Count(), 1);
// Error due to file downloaded from good 3rd party
modlist.GameType = Game.Skyrim;
modlist.Archives[0] = new GoogleDriveMod()
{
Id = "googleDEADBEEF",
Hash = "DEADBEEF"
};
errors = validate.Validate(modlist);
Assert.AreEqual(errors.Count(), 0);
}
}

View File

@ -37,4 +37,11 @@ namespace Wabbajack.Validation
{
public Permissions Permissions;
}
public class ServerWhitelist
{
public List<string> GoogleIDs;
public List<string> AllowedPrefixes;
}
}

View File

@ -18,6 +18,7 @@ namespace Wabbajack.Validation
public class ValidateModlist
{
public Dictionary<string, Author> AuthorPermissions { get; set; }
public ServerWhitelist ServerWhitelist { get; set; }
public void LoadAuthorPermissionsFromString(string s)
{
@ -27,6 +28,14 @@ namespace Wabbajack.Validation
AuthorPermissions = d.Deserialize<Dictionary<string, Author>>(s);
}
public void LoadServerWhitelist(string s)
{
var d = new DeserializerBuilder()
.WithNamingConvention(new PascalCaseNamingConvention())
.Build();
ServerWhitelist = d.Deserialize<ServerWhitelist>(s);
}
/// <summary>
/// Takes all the permissions for a given Nexus mods and merges them down to a single permissions record
/// the more specific record having precedence in each field.
@ -103,6 +112,21 @@ namespace Wabbajack.Validation
.Where(m => m.GameName.ToLower() != nexus)
.Do(m => ValidationErrors.Push($"The modlist is for {nexus} but {m.Name} is for game type {m.GameName} and is not allowed to be converted to other game types"));
modlist.Archives
.OfType<GoogleDriveMod>()
.PMap(m =>
{
if (!ServerWhitelist.GoogleIDs.Contains(m.Id))
ValidationErrors.Push($"{m.Name} uses Google Drive id {m.Id} but that id is not in the file whitelist.");
});
modlist.Archives
.OfType<DirectURLArchive>()
.PMap(m =>
{
if (!ServerWhitelist.AllowedPrefixes.Any(prefix => m.URL.StartsWith(prefix)))
ValidationErrors.Push($"{m.Name} will be downloaded from {m.URL} but that URL is not in the server whitelist");
});
return ValidationErrors.ToList();
}