add edit users for admin

This commit is contained in:
2024-08-10 17:10:45 +03:30
parent f71b11c131
commit 4ffc4197aa
15 changed files with 116 additions and 25 deletions

View File

@ -45,7 +45,7 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
return View(); return View();
} }
@ -81,7 +81,7 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
if (id == null) if (id == null)
{ {
@ -124,7 +124,7 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
if (id == null) if (id == null)
{ {

View File

@ -128,7 +128,10 @@ namespace WebApplication1.Controllers
if (Request.Files.Count > 0) if (Request.Files.Count > 0)
{ {
setLists(); setLists();
/*
string x = news.tag;
var y = news.tag[1];
*/
if (news.title != null && news.link != null && news.cat != null && news.tag != null) if (news.title != null && news.link != null && news.cat != null && news.tag != null)
{ {
var image = Request.Files[0]; var image = Request.Files[0];
@ -202,7 +205,7 @@ namespace WebApplication1.Controllers
{ {
return HttpNotFound(); return HttpNotFound();
} }
else if ( news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) == 1) else if ( news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0)
{ {
return View(news); return View(news);
} }
@ -245,7 +248,7 @@ namespace WebApplication1.Controllers
{ {
return HttpNotFound(); return HttpNotFound();
} }
else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) == 1) else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0)
{ {
return View(news); return View(news);
} }

View File

@ -44,7 +44,7 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
return View(); return View();
} }
@ -80,7 +80,7 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
if (id == null) if (id == null)
{ {
@ -123,7 +123,7 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) == 1) if (Convert.ToInt32(Session["Userrole"]) > 0)
{ {
if (id == null) if (id == null)
{ {

View File

@ -9,6 +9,7 @@ using System.Web.Mvc;
using WebApplication1.Models; using WebApplication1.Models;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Web.UI.WebControls;
namespace WebApplication1.Controllers namespace WebApplication1.Controllers
{ {
@ -281,7 +282,52 @@ namespace WebApplication1.Controllers
return View(user); return View(user);
} }
public ActionResult Edituser(int? id)
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
user user = db.users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
else
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
else
return View("Login");
}
// POST: tickets/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edituser([Bind(Include = "ID,usename,password,displayname,role")] user user)
{
if (ModelState.IsValid)
{
var currentuser = db.users.Find(user.ID);
currentuser.displayname = user.displayname;
currentuser.role = user.role;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: users/Delete/5 // GET: users/Delete/5
public ActionResult Delete(int? id) public ActionResult Delete(int? id)

BIN
Media/news/kmcZj0sRbt.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

View File

@ -34,7 +34,7 @@
</td> </td>
<td> <td>
@Html.ActionLink("Read", "../news/Details", new { id = item.ID })| @Html.ActionLink("Read", "../news/Details", new { id = item.ID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID) @if (ViewBag.role > 0 || ViewBag.uid == item.userID)
{ {
@Html.ActionLink("Edit", "../news/Edit", new { id = item.ID }) @Html.ActionLink("Edit", "../news/Edit", new { id = item.ID })

View File

@ -31,7 +31,7 @@
<td> <td>
@Html.ActionLink("Details", "Details", new { id = item.ID }) | @Html.ActionLink("Details", "Details", new { id = item.ID }) |
@if (ViewBag.role == 1) @if (ViewBag.role > 0)
{ {
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) @Html.ActionLink("Edit", "Edit", new { id = item.ID })
<span>|</span> <span>|</span>

View File

@ -59,7 +59,7 @@
<td> <td>
@Html.ActionLink("Read", "Details", new { id = item.NewsID })| @Html.ActionLink("Read", "Details", new { id = item.NewsID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID) @if (ViewBag.role > 0 || ViewBag.uid == item.userID)
{ {
@Html.ActionLink("Edit", "Edit", new { id = item.NewsID }) @Html.ActionLink("Edit", "Edit", new { id = item.NewsID })

View File

@ -42,9 +42,7 @@
@Html.DisplayFor(modelItem => item.views) @Html.DisplayFor(modelItem => item.views)
</td> </td>
<td> <td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) | @Html.ActionLink("Details", "Details", new { id = item.ID })
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td> </td>
</tr> </tr>
} }

View File

@ -34,7 +34,7 @@
</td> </td>
<td> <td>
@Html.ActionLink("Read", "../news/Details", new { id = item.ID })| @Html.ActionLink("Read", "../news/Details", new { id = item.ID })|
@if (ViewBag.role == 1 || ViewBag.uid == item.userID) @if (ViewBag.role > 0 || ViewBag.uid == item.userID)
{ {
@Html.ActionLink("Edit", "../news/Edit", new { id = item.ID }) @Html.ActionLink("Edit", "../news/Edit", new { id = item.ID })

View File

@ -30,7 +30,7 @@
</td> </td>
<td> <td>
@Html.ActionLink("Details", "Details", new { id = item.code }) | @Html.ActionLink("Details", "Details", new { id = item.code }) |
@if (ViewBag.role == 1) @if (ViewBag.role > 0)
{ {
@Html.ActionLink("Edit", "Edit", new { id = item.code }) @Html.ActionLink("Edit", "Edit", new { id = item.code })
<span>|</span> <span>|</span>

View File

@ -27,7 +27,7 @@
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
<input type="password", name="password" , class="form-control"/> <input type="password" name="password" class="form-control"/>
@Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" })
</div> </div>
</div> </div>

View File

@ -0,0 +1,49 @@
@model WebApplication1.Models.user
@{
ViewBag.Title = "Edituser";
}
<h2>Edituser</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>user</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ID)
<div class="form-group">
@Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select name="role" class="form-control">
<option value="0">User</option>
<option value="2">Admin</option>
</select>
@Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>

View File

@ -14,9 +14,6 @@
<th> <th>
@Html.DisplayNameFor(model => model.usename) @Html.DisplayNameFor(model => model.usename)
</th> </th>
<th>
@Html.DisplayNameFor(model => model.password)
</th>
<th> <th>
@Html.DisplayNameFor(model => model.displayname) @Html.DisplayNameFor(model => model.displayname)
</th> </th>
@ -28,9 +25,6 @@
<td> <td>
@Html.DisplayFor(modelItem => item.usename) @Html.DisplayFor(modelItem => item.usename)
</td> </td>
<td>
@Html.DisplayFor(modelItem => item.password)
</td>
<td> <td>
@Html.DisplayFor(modelItem => item.displayname) @Html.DisplayFor(modelItem => item.displayname)
</td> </td>

View File

@ -295,6 +295,7 @@
<Content Include="Views\responses\Respond.cshtml" /> <Content Include="Views\responses\Respond.cshtml" />
<Content Include="Views\responses\Index.cshtml" /> <Content Include="Views\responses\Index.cshtml" />
<Content Include="Views\responses\test.cshtml" /> <Content Include="Views\responses\test.cshtml" />
<Content Include="Views\users\Edituser.cshtml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="App_Data\" /> <Folder Include="App_Data\" />