private void ServerImport(string folder, MemoryStream ms) { //TODO this check is usually not called, because this option is only available while on server. if (!SiteConfigServices.IsServer) { SetMessage("Error writing file"); SetMessage("You can only import data for approval on a server"); return; } string syncFileName = string.Format(@"{0}\{1}", folder, Constants.SSEPS_FILE_SYNC); if (File.Exists(syncFileName)) { SetMessage("File found on selected medium"); SetMessage(string.Format("File name {0} ", syncFileName)); SetMessage("Importing data"); using (ZipFile zip = ZipFile.Read(syncFileName)) { foreach (ZipEntry entry in zip) { entry.Extract(ms); ms.Position = 0; StreamReader reader = new StreamReader(ms); String file = reader.ReadToEnd(); string[] splitFile = file.Split('|'); if (splitFile.Count() != 2) { SetMessage("File in wrong format"); SetMessage("This file needs to be re-exported"); break; } InstallationServices instalServices = new InstallationServices(); int siteId = int.Parse(splitFile[0]); string token = instalServices.InstallationTokenBySiteId(siteId); string realXml = Crypto.DecryptStringAES(splitFile[1], token); XDocument xDoc = XDocument.Parse(realXml); if (xDoc.Root.Name.ToString() == "Sync") { if (_syncServices.ImportDataForApproval(_configurationManager.LoggedInUser, Crypto.EncryptStringAES(xDoc.ToString(), token), siteId)) { SetMessage("Data imported successfully"); } else { SetMessage("Failed to import data, server service down"); } } else { SetMessage("Failed to import data, file of unknown type"); } } } } else { SetMessage("File not found on selected medium"); } }
public string GetApprovedDataToSyncSecure(List<int> mdaIdList, SystemUser user) { if (mdaIdList.Count == 0) return string.Empty; //TODO check that all these mda's are from the same installation if (LoginRemoteService(user)) { InstallationServices installationServices = new InstallationServices(); int siteId = installationServices.GetSiteIdFromMDAIdOnServer(mdaIdList.First()); string feedback = syncProxy.SendApprovedData(_tokenHeader, siteId); XDocument xDoc = XDocument.Parse(Crypto.DecryptStringAES(feedback, installationServices.InstallationTokenBySiteId(siteId))); if ((bool)xDoc.Element("SecuredWebService").Attribute("success")) { //TODO using .Value throws "Data at the root level is invalid. Line 1, position 1." exception //investigate whether the server is using wrong encoding which can be a pain in the ass in future. //However it seems using .ToString() solves it. //string test = xDoc.Element("SecuredWebService").ToString(); //string test1 = xDoc.Element("SecuredWebService").Value; XDocument xDocData = XDocument.Parse(xDoc.Element("SecuredWebService").Element("data").Element("Approved").ToString()); ImportApprovedData(xDocData); return Crypto.EncryptStringAES(xDocData.ToString(), installationServices.InstallationTokenByMDAId(mdaIdList.First())); } } return string.Empty; }
private bool ExportApprovedData(ref string fileToSave, ref string fileName) { //TODO this check is usually not called, because this option is only available while on server. if (!SiteConfigServices.IsServer) { SetMessage("Error writing file"); SetMessage("You can only export approved data from a server"); return false; } try { //TODO make sure that all the selected mda's are from the same site InstallationServices installationServices = new InstallationServices(); if (installationServices.MdaIdsBelongToSameInstallation(_selectedMDAS.Select(x => x.id).ToList())) { fileToSave = _syncServices.GetApprovedDataToSyncSecure(_selectedMDAS.Select(x => x.id).ToList(), _configurationManager.LoggedInUser); fileName = Constants.SSEPS_FILE_APPROVED; return true; } else { SetMessage("Error writing file"); SetMessage("Selected mda's don't belong to same site"); return false; } } catch (ArgumentNullException ex) { SetMessage("Error writing file"); switch (ex.ParamName) { case "plainText": SetMessage("Data to be exported not found 1"); break; case "cipherText": SetMessage("Data to be exported not found 2"); break; case "sharedSecret": SetMessage("Installation token not found on this machine"); break; default: SetMessage("Error type unknown on server"); break; } //TODO handle this exception return; return false; } }