This commit is contained in:
2018-06-28 23:08:13 +00:00
commit 877637f1e5
69 changed files with 13521 additions and 0 deletions

86
util/FoodooImporter.cs Normal file
View File

@@ -0,0 +1,86 @@
using System;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using GZTW.Pecklist.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
namespace GZTW.Pecklist.Util
{
public static class FoodooImporter
{
public static void Import(PecklistContext ct)
{
//open and read in the GZListServerBackup.bak file into a json object
//https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm
string sfoo = GetBackupFile();
JArray jin = JArray.Parse(sfoo);
JArray jout = new JArray();
foreach (JObject i in jin)
{
JObject o = new JObject();
o["name"] = (string)i["name"];
o["id"] = Util.ShortId.Generate();
o["name_v"] = (long)i["name_v"];
o["v"] = (long)i["v"];
JArray oitems = new JArray();
foreach (JObject l in i["items"])
{
JObject oitem = new JObject();
oitem["id"] = Util.ShortId.Generate();
oitem["completed"] = (bool)l["completed"];
oitem["text"] = (string)l["text"];
oitem["v"] = (long)l["v"];
var highlight = (bool?)l["highlight"];
if (highlight == null)
{
oitem["priority"] = 1;//default priority
}
else
{
oitem["priority"] = 3;//highest priority
}
oitems.Add(oitem);
}
o["items"] = oitems;
jout.Add(o);
}
//write it into the db
ListData ldat = new ListData();
//this is the most compact way to write the json out as text
ldat.TheList = JsonConvert.SerializeObject(jout);
ct.ListData.Add(ldat);
ct.SaveChanges();
}
private static string GetBackupFile()
{
try
{ // Open the text file using a stream reader.
using (StreamReader sr = new StreamReader("./db/GZListServerBackup.bak"))
{
// Read the stream to a string, and write the string to the console.
String line = sr.ReadToEnd();
return line;
}
}
catch
{
return null;
}
}
//--------
}
}

50
util/Hasher.cs Normal file
View File

@@ -0,0 +1,50 @@
using System;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
namespace GZTW.Pecklist.Util
{
//Authentication controller
public static class Hasher
{
public static string hash(string Salt, string Password)
{
//adapted from here:
//https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing
string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: Password,
salt: Base64StringToByteArray(Salt),
prf: KeyDerivationPrf.HMACSHA512,
iterationCount: 10000,
numBytesRequested: 512 / 8));
return hashed;
}
///////////////////////////////////
// convert the salt to a byte array
public static byte[] Base64StringToByteArray(string b64)
{
return Convert.FromBase64String(b64);
}
///////////////////////////
// Generate a random salt
//
public static string GenerateSalt()
{
// generate a 128-bit salt using a secure PRNG
byte[] salt = new byte[128 / 8];
using (var rng = RandomNumberGenerator.Create())
{
rng.GetBytes(salt);
}
return Convert.ToBase64String(salt);
}
}//eoc
}//eons

141
util/Schema.cs Normal file
View File

@@ -0,0 +1,141 @@
using System;
using System.Text;
using Microsoft.EntityFrameworkCore;
using GZTW.Pecklist.Models;
namespace GZTW.Pecklist.Util
{
//Key generator controller
public static class Schema
{
private static PecklistContext ctx;
/////////////////////////////////////////////////////////////////
/////////// CHANGE THIS ON NEW SCHEMA UPDATE ////////////////////
public const int DESIRED_SCHEMA_LEVEL = 2;
/////////////////////////////////////////////////////////////////
static int startingSchema = -1;
static int currentSchema = -1;
//check and update schema
public static void CheckAndUpdate(PecklistContext context)
{
ctx = context;
bool AppSchemaExists = false;
// var ld=context.ListData;
//update schema here?
using (var command = ctx.Database.GetDbConnection().CreateCommand())
{
//first of all, do we have a schema table yet (v0?)
command.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name='appschema';";
ctx.Database.OpenConnection();
using (var result = command.ExecuteReader())
{
if (result.HasRows)
{
AppSchemaExists = true;
}
ctx.Database.CloseConnection();
}
}
//Create schema table (v1)
if (!AppSchemaExists)
{
//nope, no schema table, add it now and set to v1
using (var cmCreateappschema = ctx.Database.GetDbConnection().CreateCommand())
{
context.Database.OpenConnection();
//first of all, do we have a schema table yet (v0?)
cmCreateappschema.CommandText = "CREATE TABLE appschema (id INTEGER PRIMARY KEY, schema INTEGER NOT NULL);";
cmCreateappschema.ExecuteNonQuery();
cmCreateappschema.CommandText = "insert into appschema (schema) values (1);";
cmCreateappschema.ExecuteNonQuery();
context.Database.CloseConnection();
startingSchema = 1;
currentSchema = 1;
}
}
else
{
//get current schema level
using (var cm = ctx.Database.GetDbConnection().CreateCommand())
{
cm.CommandText = "SELECT schema FROM appschema WHERE id=1;";
ctx.Database.OpenConnection();
using (var result = cm.ExecuteReader())
{
if (result.HasRows)
{
result.Read();
currentSchema = startingSchema = result.GetInt32(0);
ctx.Database.CloseConnection();
}
else
{
ctx.Database.CloseConnection();
throw new System.Exception("Pecklist->RfSchema->CheckAndUpdate: Error reading schema version");
}
}
}
}
//Bail early no update?
if (currentSchema == DESIRED_SCHEMA_LEVEL)
return;
//************* SCHEMA UPDATES ******************
//////////////////////////////////////////////////
//schema 2 import data from Foodoo
if (currentSchema < 2)
{
//empty out the listdata table
exec("delete from listdata");
//Trigger import of foodoo data
Util.FoodooImporter.Import(ctx);
//open and read in the GZListServerBackup.bak file into a json object
//turn it into pecklist format
//write it into the db
//$profit$
currentSchema = 2;
setSchemaLevel(currentSchema);
}
//*************************************************************************************
}//eofunction
private static void setSchemaLevel(int nCurrentSchema)
{
exec("UPDATE appschema SET schema=" + nCurrentSchema.ToString());
}
//execute command query
private static void exec(string q)
{
using (var cmCreateappschema = ctx.Database.GetDbConnection().CreateCommand())
{
ctx.Database.OpenConnection();
cmCreateappschema.CommandText = q;
cmCreateappschema.ExecuteNonQuery();
ctx.Database.CloseConnection();
}
}
//eoclass
}
//eons
}

20
util/ShortId.cs Normal file
View File

@@ -0,0 +1,20 @@
using System;
namespace GZTW.Pecklist.Util
{
//Authentication controller
public static class ShortId
{
/////////////////////////////////
// Generate a random shortId
//
// Based off this: https://stackoverflow.com/a/9279005/8939
public static string Generate()
{
return Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Replace("=","").Replace("/","").Replace("+","");
}
}//eoc
}//eons