diff --git a/Controllers/categoriesController.cs b/Controllers/categoriesController.cs index 087918b..555d33a 100644 --- a/Controllers/categoriesController.cs +++ b/Controllers/categoriesController.cs @@ -17,12 +17,15 @@ namespace WebApplication1.Controllers // GET: categories public ActionResult Index() { + ViewBag.role = Convert.ToInt32(Session["Userrole"]); return View(db.categories.ToList()); } // GET: categories/Details/5 public ActionResult Details(int? id) { + ViewBag.role = Convert.ToInt32(Session["Userrole"]); + ViewBag.uid = Convert.ToInt32(Session["Userid"]); if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); @@ -76,16 +79,28 @@ namespace WebApplication1.Controllers // GET: categories/Edit/5 public ActionResult Edit(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + category category = db.categories.Find(id); + if (category == null) + { + return HttpNotFound(); + } + return View(category); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - category category = db.categories.Find(id); - if (category == null) - { - return HttpNotFound(); - } - return View(category); + else + return RedirectToAction("index", "home"); } // POST: categories/Edit/5 @@ -107,16 +122,28 @@ namespace WebApplication1.Controllers // GET: categories/Delete/5 public ActionResult Delete(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + category category = db.categories.Find(id); + if (category == null) + { + return HttpNotFound(); + } + return View(category); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - category category = db.categories.Find(id); - if (category == null) - { - return HttpNotFound(); - } - return View(category); + else + return RedirectToAction("index", "home"); } // POST: categories/Delete/5 diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs index 5f83fe0..07f883e 100644 --- a/Controllers/newsController.cs +++ b/Controllers/newsController.cs @@ -28,6 +28,8 @@ namespace WebApplication1.Controllers // GET: news public ActionResult Index() { + ViewBag.role = Convert.ToInt32(Session["Userrole"]); + ViewBag.uid = Convert.ToInt32(Session["Userid"]); var models = (from user in db.users // Access Users DbSet join news in db.news on user.ID equals news.userID into newsGroup // Join News DbSet from news in newsGroup.DefaultIfEmpty() // Left outer join @@ -35,6 +37,7 @@ namespace WebApplication1.Controllers select new newsModel { DisplayName = user != null ? user.displayname : null, + userID = user != null ? user.ID : 0, NewsID = news.ID, // Use null-conditional operator for missing news Title = news.title, Image = news.image, @@ -185,16 +188,34 @@ namespace WebApplication1.Controllers // GET: news/Edit/5 public ActionResult Edit(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + news news = db.news.Find(id); + if (news == null) + { + return HttpNotFound(); + } + else if ( news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) == 1) + { + return View(news); + } + else + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - news news = db.news.Find(id); - if (news == null) - { - return HttpNotFound(); - } - return View(news); + else + return RedirectToAction("index", "home"); + } // POST: news/Edit/5 @@ -216,16 +237,33 @@ namespace WebApplication1.Controllers // GET: news/Delete/5 public ActionResult Delete(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + news news = db.news.Find(id); + if (news == null) + { + return HttpNotFound(); + } + else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) == 1) + { + return View(news); + } + else + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - news news = db.news.Find(id); - if (news == null) - { - return HttpNotFound(); - } - return View(news); + else + return RedirectToAction("index", "home"); } // POST: news/Delete/5 diff --git a/Controllers/tagsController.cs b/Controllers/tagsController.cs index 8842eca..88460bc 100644 --- a/Controllers/tagsController.cs +++ b/Controllers/tagsController.cs @@ -16,12 +16,15 @@ namespace WebApplication1.Models // GET: tags public ActionResult Index() { + ViewBag.role = Convert.ToInt32(Session["Userrole"]); return View(db.tags.ToList()); } // GET: tags/Details/5 public ActionResult Details(int? id) { + ViewBag.role = Convert.ToInt32(Session["Userrole"]); + ViewBag.uid = Convert.ToInt32(Session["Userid"]); if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); @@ -75,16 +78,28 @@ namespace WebApplication1.Models // GET: tags/Edit/5 public ActionResult Edit(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + tag tags = db.tags.Find(id); + if (tags == null) + { + return HttpNotFound(); + } + return View(tags); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - tag tag = db.tags.Find(id); - if (tag == null) - { - return HttpNotFound(); - } - return View(tag); + else + return RedirectToAction("index", "home"); } // POST: tags/Edit/5 @@ -106,16 +121,28 @@ namespace WebApplication1.Models // GET: tags/Delete/5 public ActionResult Delete(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Convert.ToInt32(Session["Userrole"]) == 1) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + tag tags = db.tags.Find(id); + if (tags == null) + { + return HttpNotFound(); + } + return View(tags); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } - tag tag = db.tags.Find(id); - if (tag == null) - { - return HttpNotFound(); - } - return View(tag); + else + return RedirectToAction("index", "home"); } // POST: tags/Delete/5 diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs index bd84e0e..c9b8746 100644 --- a/Controllers/usersController.cs +++ b/Controllers/usersController.cs @@ -261,15 +261,20 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { - // Hash the password - using (var sha256 = SHA256.Create()) - { - var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password)); - user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + var u = db.users.Find(user.ID); + + if (user.password != null){ + // Hash the password + using (var sha256 = SHA256.Create()) + { + var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password)); + user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower(); + u.password = user.password; + } } - var u= db.users.Find(user.ID); - u.password = user.password; + u.displayname = user.displayname; + db.SaveChanges(); return RedirectToAction("Index"); } diff --git a/Models/newsModel.cs b/Models/newsModel.cs index db2eaab..45587a0 100644 --- a/Models/newsModel.cs +++ b/Models/newsModel.cs @@ -9,6 +9,7 @@ namespace WebApplication1.Models { public string DisplayName { get; set; } public int NewsID { get; set; } + public int userID { get; set; } public string Title { get; set; } public string Image { get; set; } public string category { get; set; } diff --git a/Views/categories/Details.cshtml b/Views/categories/Details.cshtml index 043b08f..c2fe6b4 100644 --- a/Views/categories/Details.cshtml +++ b/Views/categories/Details.cshtml @@ -11,9 +11,6 @@