2019-06-21 15:05:31 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2019-06-21 15:05:31 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
using System;
|
2019-06-21 15:05:31 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-26 10:13:11 +02:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Serialization;
|
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Lockdown.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2019-06-21 15:05:31 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Lockdown
|
|
|
|
|
{
|
|
|
|
|
public class FeatureConfigurationBackup : IFeatureConfigurationBackup
|
|
|
|
|
{
|
2019-06-26 10:13:11 +02:00
|
|
|
|
private readonly object @lock = new object();
|
2019-06-21 15:05:31 +02:00
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
private string filePath;
|
|
|
|
|
private IModuleLogger logger;
|
|
|
|
|
|
|
|
|
|
public FeatureConfigurationBackup(string filePath, IModuleLogger logger)
|
2019-06-21 15:05:31 +02:00
|
|
|
|
{
|
2019-06-26 10:13:11 +02:00
|
|
|
|
this.filePath = filePath;
|
2019-06-21 15:05:31 +02:00
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(IFeatureConfiguration configuration)
|
|
|
|
|
{
|
2019-06-26 10:13:11 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
var configurations = LoadFromFile();
|
|
|
|
|
var obsolete = configurations.Find(c => c.Id == configuration.Id);
|
|
|
|
|
|
|
|
|
|
if (obsolete != default(IFeatureConfiguration))
|
|
|
|
|
{
|
|
|
|
|
configurations.Remove(obsolete);
|
|
|
|
|
SaveToFile(configurations);
|
|
|
|
|
logger.Info($"Successfully removed {configuration} from backup.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-27 08:32:37 +02:00
|
|
|
|
logger.Warn($"Could not delete {configuration} as it does not exist in backup!");
|
2019-06-26 10:13:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-21 15:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-26 10:13:11 +02:00
|
|
|
|
public IList<IFeatureConfiguration> GetAllConfigurations()
|
2019-06-21 15:05:31 +02:00
|
|
|
|
{
|
2019-06-26 10:13:11 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
return LoadFromFile();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<IFeatureConfiguration> GetBy(Guid groupId)
|
|
|
|
|
{
|
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
return LoadFromFile().Where(c => c.GroupId == groupId).ToList();
|
|
|
|
|
}
|
2019-06-21 15:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Save(IFeatureConfiguration configuration)
|
|
|
|
|
{
|
2019-06-26 10:13:11 +02:00
|
|
|
|
lock (@lock)
|
|
|
|
|
{
|
|
|
|
|
var configurations = LoadFromFile();
|
|
|
|
|
|
|
|
|
|
configurations.Add(configuration);
|
|
|
|
|
SaveToFile(configurations);
|
|
|
|
|
logger.Info($"Successfully added {configuration} to backup.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<IFeatureConfiguration> LoadFromFile()
|
|
|
|
|
{
|
|
|
|
|
var configurations = new List<IFeatureConfiguration>();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
var context = new StreamingContext(StreamingContextStates.All, logger);
|
|
|
|
|
|
|
|
|
|
logger.Debug($"Attempting to load backup data from '{filePath}'...");
|
|
|
|
|
|
|
|
|
|
using (var stream = File.Open(filePath, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
configurations = (List<IFeatureConfiguration>) new BinaryFormatter(null, context).Deserialize(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debug($"Backup data successfully loaded, found {configurations.Count} items.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"No backup data found under '{filePath}'.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to load backup data from '{filePath}'!", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return configurations;
|
2019-06-21 15:05:31 +02:00
|
|
|
|
}
|
2019-06-27 08:32:37 +02:00
|
|
|
|
|
|
|
|
|
private void SaveToFile(List<IFeatureConfiguration> configurations)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (configurations.Any())
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Attempting to save backup data to '{filePath}'...");
|
|
|
|
|
|
|
|
|
|
using (var stream = File.Open(filePath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
new BinaryFormatter().Serialize(stream, configurations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Debug($"Successfully saved {configurations.Count} items.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
File.Delete(filePath);
|
|
|
|
|
logger.Debug("No backup data to save, deleted backup file.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to save backup data to '{filePath}'!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-21 15:05:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|