Estimate Owner implementation

vnext
Paul Schneider 8 years ago
parent 6278732d85
commit 02c7905b28
2 changed files with 63 additions and 12 deletions

@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims;
using Microsoft.AspNet.Authorization; using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc;
@ -19,12 +19,26 @@ namespace Yavsc.Controllers
{ {
_context = context; _context = context;
} }
bool UserIsAdminOrThis(string uid)
// GET: api/Estimate {
if (User.IsInRole(Constants.AdminGroupName)) return true;
return uid == User.GetUserId();
}
bool UserIsAdminOrInThese (string oid, string uid)
{
if (User.IsInRole(Constants.AdminGroupName)) return true;
var cuid = User.GetUserId();
return cuid == uid || cuid == oid;
}
// GET: api/Estimate{?ownerId=User.GetUserId()}
[HttpGet] [HttpGet]
public IEnumerable<Estimate> GetEstimates() public IActionResult GetEstimates(string ownerId=null)
{ {
return _context.Estimates; if ( ownerId == null ) ownerId = User.GetUserId();
else if (!UserIsAdminOrThis(ownerId)) // throw new Exception("Not authorized") ;
// or just do nothing
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
return Ok(_context.Estimates.Where(e=>e.OwnerId == ownerId));
} }
// GET: api/Estimate/5 // GET: api/Estimate/5
@ -43,7 +57,9 @@ namespace Yavsc.Controllers
return HttpNotFound(); return HttpNotFound();
} }
if (UserIsAdminOrInThese(estimate.ClientId,estimate.OwnerId))
return Ok(estimate); return Ok(estimate);
return new HttpStatusCodeResult(StatusCodes.Status403Forbidden);
} }
// PUT: api/Estimate/5 // PUT: api/Estimate/5
@ -59,7 +75,15 @@ namespace Yavsc.Controllers
{ {
return HttpBadRequest(); return HttpBadRequest();
} }
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only modify your own estimates");
return HttpBadRequest(ModelState);
}
}
_context.Entry(estimate).State = EntityState.Modified; _context.Entry(estimate).State = EntityState.Modified;
try try
@ -89,7 +113,15 @@ namespace Yavsc.Controllers
{ {
return HttpBadRequest(ModelState); return HttpBadRequest(ModelState);
} }
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
_context.Estimates.Add(estimate); _context.Estimates.Add(estimate);
try try
{ {
@ -120,11 +152,20 @@ namespace Yavsc.Controllers
} }
Estimate estimate = _context.Estimates.Single(m => m.Id == id); Estimate estimate = _context.Estimates.Single(m => m.Id == id);
if (estimate == null) if (estimate == null)
{ {
return HttpNotFound(); return HttpNotFound();
} }
var uid = User.GetUserId();
if (!User.IsInRole(Constants.AdminGroupName))
{
if (uid != estimate.OwnerId)
{
ModelState.AddModelError("OwnerId","You can only create your own estimates");
return HttpBadRequest(ModelState);
}
}
_context.Estimates.Remove(estimate); _context.Estimates.Remove(estimate);
_context.SaveChanges(); _context.SaveChanges();

@ -35,8 +35,10 @@ namespace Yavsc.Models.Billing
[NotMapped] [NotMapped]
public List<string> AttachedGraphics { get; set; } public List<string> AttachedGraphics { get; set; }
public string AttachedGraphicsString { get { return string.Join(":", AttachedGraphics); } public string AttachedGraphicsString {
set { AttachedGraphics = value.Split(':').ToList(); } } get { return string.Join(":", AttachedGraphics); }
set { AttachedGraphics = value.Split(':').ToList(); }
}
/// <summary> /// <summary>
/// List of attached files /// List of attached files
/// to this estimate, as relative pathes to /// to this estimate, as relative pathes to
@ -46,7 +48,15 @@ namespace Yavsc.Models.Billing
/// <returns></returns> /// <returns></returns>
[NotMapped] [NotMapped]
public List<string> AttachedFiles { get; set; } public List<string> AttachedFiles { get; set; }
public string AttachedFilesString { get { return string.Join(":", AttachedFiles); } public string AttachedFilesString {
set { AttachedFiles = value.Split(':').ToList(); } } get { return string.Join(":", AttachedFiles); }
set { AttachedFiles = value.Split(':').ToList(); }
}
[Required]
public string OwnerId { get; set; }
[Required]
public string ClientId { get; set; }
} }
} }

Loading…