using System.Web;
using System.Web.Mvc;
using MileageTraker.Web.DAL;

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;
		}
	}
}