public ActionResult Create(DriveMappingModel driveMapping) { try { if (ModelState.IsValid) { //check if first ever item is addedd or list with entries already exists if (HttpContext.Session.GetString(sessionName) != null) { List <DriveMappingModel> driveMappings = JsonConvert.DeserializeObject <List <DriveMappingModel> >(HttpContext.Session.GetString(sessionName)); driveMapping.Id = driveMappings.Last().Id + 1; driveMappings.Add(driveMapping); HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings.OrderBy(entry => entry.Id))); } else { List <DriveMappingModel> driveMappings = new List <DriveMappingModel> { driveMapping }; HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings.OrderBy(entry => entry.Id))); } } else { return(View()); } return(RedirectToAction(indexView)); } catch (Exception ex) { HttpContext.Session.SetString(errosSession, ex.Message.ToString()); return(RedirectToAction(indexView)); } }
public ActionResult Edit(DriveMappingModel driveMapping) { try { if (ModelState.IsValid) { //haven't found better solution: so i just remove the existing entry and add the new one and do a resort of the list List <DriveMappingModel> driveMappings = JsonConvert.DeserializeObject <List <DriveMappingModel> >(HttpContext.Session.GetString(sessionName)); if (driveMapping.Id == 0) { driveMappings.RemoveAt(driveMapping.Id); } else { driveMappings.RemoveAt(driveMapping.Id - 1); } driveMappings.Add(driveMapping); HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings.OrderBy(entry => entry.Id))); } else { return(View()); } return(RedirectToAction(indexView)); } catch (Exception ex) { HttpContext.Session.SetString(errosSession, ex.Message.ToString()); return(RedirectToAction(indexView)); } }
public ActionResult Upload() { try { var file = Request.Form.Files[0]; if (file.FileName.Contains(".ps1")) { string driveMappingConfig = null; string line; System.IO.StreamReader powerShellContent = new System.IO.StreamReader(file.OpenReadStream()); while ((line = powerShellContent.ReadLine()) != null) { if (line.StartsWith(poshConfigVariable)) { driveMappingConfig = line; driveMappingConfig = driveMappingConfig.Replace(poshConfigVariable, ""); driveMappingConfig = driveMappingConfig.TrimStart('\''); driveMappingConfig = driveMappingConfig.TrimEnd('\''); } } if (driveMappingConfig != null) { List <DriveMappingModel> driveMappings = JsonConvert.DeserializeObject <List <DriveMappingModel> >(driveMappingConfig); HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings)); return(RedirectToAction(indexView)); } else { throw new SystemException("Could not find configuration in uploaded PowerShell script."); } } else if (file.FileName.Contains(".xml")) { // create xmldoc XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file.OpenReadStream()); //namespace manager & URI's needed in order to read GPP nodes XmlNamespaceManager nsmanager = new XmlNamespaceManager(xmldoc.NameTable); nsmanager.AddNamespace("q1", "http://www.microsoft.com/GroupPolicy/Settings"); nsmanager.AddNamespace("q2", "http://www.microsoft.com/GroupPolicy/Settings/DriveMaps"); XmlNodeList driveProperties = xmldoc.SelectNodes("q1:GPO/q1:User/q1:ExtensionData/q1:Extension/q2:DriveMapSettings/q2:Drive", nsmanager); //create list to store all entries List <DriveMappingModel> driveMappings = new List <DriveMappingModel>(); DriveMappingModel driveMapping; //helper index to assign id to our entries int i = 0; foreach (XmlNode property in driveProperties) { //the real drive mapping configuration is stored in the 2nd XML child-node --> index 1 driveMapping = new DriveMappingModel { Path = property.ChildNodes[1].Attributes["path"].InnerXml, DriveLetter = property.ChildNodes[1].Attributes["letter"].InnerXml, Label = property.ChildNodes[1].Attributes["label"].InnerXml, Id = (i + 1) }; //check if we have a filter applied as child node --> index 2 try { string groupFilter = property.ChildNodes[2].ChildNodes[0].Attributes["name"].InnerXml; String[] streamlinedGroupFilter = groupFilter.Split('\\'); driveMapping.GroupFilter = streamlinedGroupFilter[1]; } catch { //nothing we can do } driveMappings.Add(driveMapping); i++; } HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings)); } else { throw new NullReferenceException(); } return(RedirectToAction(indexView)); } catch (Exception ex) { HttpContext.Session.SetString(errosSession, ex.Message.ToString()); return(RedirectToAction(indexView)); } }