fix category,tag, news list

fix edit account details bug
This commit is contained in:
2024-08-06 16:34:13 +03:30
parent 936222ab07
commit 07ffa3303d
10 changed files with 199 additions and 82 deletions

View File

@ -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);
@ -75,6 +78,10 @@ namespace WebApplication1.Controllers
// GET: categories/Edit/5
public ActionResult Edit(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
@ -87,6 +94,14 @@ namespace WebApplication1.Controllers
}
return View(category);
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("index", "home");
}
// POST: categories/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
@ -106,6 +121,10 @@ namespace WebApplication1.Controllers
// GET: categories/Delete/5
public ActionResult Delete(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
@ -118,6 +137,14 @@ namespace WebApplication1.Controllers
}
return View(category);
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("index", "home");
}
// POST: categories/Delete/5
[HttpPost, ActionName("Delete")]

View File

@ -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,
@ -184,6 +187,10 @@ namespace WebApplication1.Controllers
// GET: news/Edit/5
public ActionResult Edit(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
@ -194,8 +201,22 @@ namespace WebApplication1.Controllers
{
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);
}
}
else
return RedirectToAction("index", "home");
}
// POST: news/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
@ -215,6 +236,10 @@ namespace WebApplication1.Controllers
// GET: news/Delete/5
public ActionResult Delete(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
@ -225,8 +250,21 @@ namespace WebApplication1.Controllers
{
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);
}
}
else
return RedirectToAction("index", "home");
}
// POST: news/Delete/5
[HttpPost, ActionName("Delete")]

View File

@ -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);
@ -74,17 +77,29 @@ namespace WebApplication1.Models
// GET: tags/Edit/5
public ActionResult Edit(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tag tag = db.tags.Find(id);
if (tag == null)
tag tags = db.tags.Find(id);
if (tags == null)
{
return HttpNotFound();
}
return View(tag);
return View(tags);
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("index", "home");
}
// POST: tags/Edit/5
@ -105,17 +120,29 @@ namespace WebApplication1.Models
// GET: tags/Delete/5
public ActionResult Delete(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tag tag = db.tags.Find(id);
if (tag == null)
tag tags = db.tags.Find(id);
if (tags == null)
{
return HttpNotFound();
}
return View(tag);
return View(tags);
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("index", "home");
}
// POST: tags/Delete/5

View File

@ -261,15 +261,20 @@ namespace WebApplication1.Controllers
{
if (ModelState.IsValid)
{
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();
}
var u= db.users.Find(user.ID);
u.password = user.password;
}
}
u.displayname = user.displayname;
db.SaveChanges();
return RedirectToAction("Index");
}

View File

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

View File

@ -11,9 +11,6 @@
<th>
@Html.DisplayNameFor(model => model.title)
</th>
<th>
@Html.DisplayNameFor(model => model.link)
</th>
<th>
@Html.DisplayNameFor(model => model.tag)
</th>
@ -29,9 +26,6 @@
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.link)
</td>
<td>
@Html.DisplayFor(modelItem => item.tag)
</td>
@ -39,9 +33,14 @@
@Html.DisplayFor(modelItem => item.cat)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
@Html.ActionLink("Read", "../news/Details", new { id = item.ID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID)
{
@Html.ActionLink("Edit", "../news/Edit", new { id = item.ID })
<span>|</span>
@Html.ActionLink("Delete", "../news/Delete", new { id = item.ID })
}
</td>
</tr>
}

View File

@ -29,9 +29,15 @@
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@if (ViewBag.role == 1)
{
@Html.ActionLink("Edit", "Edit", new { id = item.ID })
<span>|</span>
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
}
</td>
</tr>
}

View File

@ -4,6 +4,7 @@
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ -56,9 +57,17 @@
@Html.DisplayFor(modelItem => item.views)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.NewsID }) |
@Html.ActionLink("Details", "Details", new { id = item.NewsID }) |
@Html.ActionLink("Read", "Details", new { id = item.NewsID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID)
{
@Html.ActionLink("Edit", "Edit", new { id = item.NewsID })
<span>|</span>
@Html.ActionLink("Delete", "Delete", new { id = item.NewsID })
}
</td>
</tr>
}

View File

@ -11,9 +11,6 @@
<th>
@Html.DisplayNameFor(model => model.title)
</th>
<th>
@Html.DisplayNameFor(model => model.link)
</th>
<th>
@Html.DisplayNameFor(model => model.tag)
</th>
@ -29,9 +26,6 @@
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.link)
</td>
<td>
@Html.DisplayFor(modelItem => item.tag)
</td>
@ -39,9 +33,15 @@
@Html.DisplayFor(modelItem => item.cat)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
@Html.ActionLink("Read", "../news/Details", new { id = item.ID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID)
{
@Html.ActionLink("Edit", "../news/Edit", new { id = item.ID })
<span>|</span>
@Html.ActionLink("Delete", "../news/Delete", new { id = item.ID })
}
</td>
</tr>
}

View File

@ -29,9 +29,14 @@
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.code }) |
@Html.ActionLink("Details", "Details", new { id=item.code }) |
@Html.ActionLink("Delete", "Delete", new { id=item.code })
@Html.ActionLink("Details", "Details", new { id = item.code }) |
@if (ViewBag.role == 1)
{
@Html.ActionLink("Edit", "Edit", new { id = item.code })
<span>|</span>
@Html.ActionLink("Delete", "Delete", new { id = item.code })
}
</td>
</tr>
}