Skip to content

Commit

Permalink
Merge pull request #1 from nicolonsky/dev
Browse files Browse the repository at this point in the history
Added support to edit existing configurations
  • Loading branch information
nicolonsky authored Aug 24, 2019
2 parents f9aabd2 + 4fc73a5 commit 0bbc0e4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 39 deletions.
74 changes: 51 additions & 23 deletions IntuneDriveMapping/Controllers/DriveMappingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Text;
using System.IO;

namespace IntuneDriveMapping.Controllers
{
Expand All @@ -15,23 +16,21 @@ public class DriveMappingController : Controller
//Http session configs
const string sessionName = "driveMappingList";
const string errosSession = "lastError";
const string aadAppRegSession = "appReg";

//Configuration params for the generated PowerShell script
const string poshInsertString = "!INTUNEDRIVEMAPPINGJSON!";
const string poshTemplateName = "IntuneDriveMappingTemplate.ps1";
const string poshExportName = "DriveMapping.ps1";
const string poshConfigVariable = "$driveMappingJson=";

//default view where everything comes together
const string indexView = "Index";


public ActionResult Index()
{
//don't display any table data if no content is available
ViewBag.ShowList = false;


//get version
try
{
Expand All @@ -48,7 +47,6 @@ public ActionResult Index()
//SunFunNothingTodo
}


//check if error message is stored in session & forward to view
if (HttpContext.Session.GetString(errosSession) != null)
{
Expand All @@ -59,7 +57,6 @@ public ActionResult Index()

}


//check if a drivemapping list exists and display it
if (HttpContext.Session.GetString(sessionName)==null)
{
Expand All @@ -68,34 +65,26 @@ public ActionResult Index()

else
{

List<DriveMappingModel> driveMappings = JsonConvert.DeserializeObject<List<DriveMappingModel>>(HttpContext.Session.GetString(sessionName));

ViewBag.ShowList = true;

return View(driveMappings);

}

}

public ActionResult Create()
public ActionResult Create()
{

return View();

}

[HttpPost]
[ValidateAntiForgeryToken]
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)
{
Expand All @@ -119,11 +108,9 @@ public ActionResult Create(DriveMappingModel driveMapping)
else
{
return View();

}

return RedirectToAction(indexView);

}

catch (Exception ex)
Expand All @@ -133,17 +120,53 @@ public ActionResult Create(DriveMappingModel driveMapping)
return RedirectToAction(indexView);

}

}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload()
{
try
{
var file = Request.Form.Files[0];

if (file != null && file.Length > 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();
Expand All @@ -159,7 +182,7 @@ public ActionResult Upload()
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>();
List<DriveMappingModel> driveMappings = new List<DriveMappingModel>();

DriveMappingModel driveMapping;

Expand All @@ -180,7 +203,7 @@ public ActionResult Upload()
//check if we have a filter applied as child node --> index 2
try
{
string groupFilter= property.ChildNodes[2].ChildNodes[0].Attributes["name"].InnerXml;
string groupFilter = property.ChildNodes[2].ChildNodes[0].Attributes["name"].InnerXml;

String[] streamlinedGroupFilter = groupFilter.Split('\\');

Expand All @@ -198,6 +221,11 @@ public ActionResult Upload()

HttpContext.Session.SetString(sessionName, JsonConvert.SerializeObject(driveMappings));
}
else
{
throw new NullReferenceException();
}

return RedirectToAction(indexView);
}
catch (Exception ex)
Expand Down Expand Up @@ -237,14 +265,14 @@ public ActionResult Edit(int? Id)
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(DriveMappingModel driveMapping)
{
try
{
if (ModelState.IsValid)
{
//haven't found better solution --> improvement needed!
//so i just remove the existing entry and add the new one and do a resort of the list
//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));

Expand Down
2 changes: 1 addition & 1 deletion IntuneDriveMapping/Views/DriveMapping/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$(".input-file").before(
function () {
if (!$(this).prev().hasClass('input-ghost')) {
var element = $("<input type='file' class='input-ghost' accept='.xml' style='visibility:hidden; height:0'>");
var element = $("<input type='file' class='input-ghost' accept='.xml,.ps1' style='visibility:hidden; height:0'>");
element.attr("name", $(this).attr("name"));
element.change(function () {
element.next(element).find('input').val((element.val()).split('\\').pop());
Expand Down
35 changes: 20 additions & 15 deletions IntuneDriveMapping/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,49 @@
<title>@ViewData["Title"]nicolonsky tools</title>
<!-- inside <head> -->
<link rel="stylesheet" href="~/vendor.min.css" />

<style>
.nav-item a{
.nav-item a {
color: white !important;
cursor: pointer;
}
.nav-item a:hover {
color: white !important;
cursor: pointer;
}
.nav-item a:hover {
color: white !important;
cursor: pointer;
}
</style>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-108122616-3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-108122616-3', { 'anonymize_ip': true });
</script>
</head>
<body>

<nav class="navbar navbar-expand-sm navbar-dark fixed-top bg-dark">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"></button>
<a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">nicolonsky tools</a>

<div class="navbar-collapse collapse" id="navbarSupportedContent">
<ul class="nav navbar-nav mr-auto">
<li class="nav-item"><a asp-area="" asp-controller="DriveMapping" asp-action="">intune-drive-mapping-generator</a></li>
<li class="nav-item"><a href="/" class="nav-link">home</a></li>
<li class="nav-item"><a asp-area="" asp-controller="DriveMapping" asp-action="" class="nav-link">intune-drive-mapping-generator</a></li>
</ul>
</div>
</div>
</nav>

</nav>
<div class="container body-content pt-5">
<br />
<br />

@RenderBody()

<footer class="pt-4">
<hr />
<p>&copy; 2019 - nicolonsky tech.</p>
</footer>
</div>

<script src="~/vendor.min.js"></script>
</body>
</html>

0 comments on commit 0bbc0e4

Please sign in to comment.