Security for user editing only their own content

This commit is contained in:
2014-01-22 21:48:15 -05:00
parent 0ef5199048
commit a441de1a45
4 changed files with 38 additions and 1 deletions
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.Mvc;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
namespace MileageTraker.Web.Attributes
{
public class LogOwnerAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var authorized = base.AuthorizeCore(httpContext);
if (!authorized)
{
return false;
}
var rd = httpContext.Request.RequestContext.RouteData;
var id = int.Parse(rd.Values["id"].ToString());
var dataService = new DataService();
var log = dataService.GetLog(id);
var userName = httpContext.User.Identity.Name;
return log.User.Username == userName;
}
}
}
+4
View File
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Security;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
@@ -36,6 +37,7 @@ namespace MileageTraker.Web.Controllers
return View(model);
}
[LogOwnerAuthorize]
public ActionResult EditPast(int id)
{
var log = DataService.GetLog(id);
@@ -53,6 +55,8 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
var log = DataService.GetLog(viewModel.LogId);
if (log.User.Username != User.Identity.Name)
throw new SecurityException();
viewModel.SetProperties(log);
if (viewModel.Purpose != null)
+1 -1
View File
@@ -16,7 +16,7 @@
@Html.EditorForModel()
<div class="form-actions">
<input type="submit" value="Update" class="btn btn-primary" />
@Html.ActionLink("Cancel", "Index", new {}, new { @class = "btn btn-mini" })
@Html.ActionLink("Cancel", "Index", new {}, new { @class = "btn" })
</div>
</fieldset>
}
+1
View File
@@ -112,6 +112,7 @@
<Compile Include="Attributes\FormatHintAttribute.cs" />
<Compile Include="Attributes\HttpParamActionAttribute.cs" />
<Compile Include="Attributes\InputSizeAttribute.cs" />
<Compile Include="Attributes\LogOwnerAuthorizeAttribute.cs" />
<Compile Include="Attributes\NoEditLabelAttribute.cs" />
<Compile Include="Attributes\UnitsAttribute.cs" />
<Compile Include="Attributes\UserActivityAttribute.cs" />