From 4380d9da5c16babc1448247d9790a927f43d5743 Mon Sep 17 00:00:00 2001 From: lianpi3 Date: Mon, 12 Aug 2024 17:15:11 +0330 Subject: [PATCH] add access level --- Controllers/categoriesController.cs | 21 +++++++++++++---- Controllers/newsController.cs | 35 +++++++++++++++++------------ Controllers/tagsController.cs | 26 ++++++++++++++++----- Controllers/usersController.cs | 33 ++++++++++++++++++++++----- Models/Model1.edmx | 4 ++-- Models/user.cs | 2 +- Views/users/Edituser.cshtml | 19 +++++++++++----- 7 files changed, 103 insertions(+), 37 deletions(-) diff --git a/Controllers/categoriesController.cs b/Controllers/categoriesController.cs index 295134a..e818dd0 100644 --- a/Controllers/categoriesController.cs +++ b/Controllers/categoriesController.cs @@ -17,14 +17,23 @@ namespace WebApplication1.Controllers // GET: categories public ActionResult Index() { - ViewBag.role = Convert.ToInt32(Session["Userrole"]); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) + ViewBag.role = 1; + else ViewBag.role = 0; return View(db.categories.ToList()); } // GET: categories/Details/5 public ActionResult Details(int? id) { - ViewBag.role = Convert.ToInt32(Session["Userrole"]); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) + ViewBag.role = 1; + else ViewBag.role = 0; + ViewBag.uid = Convert.ToInt32(Session["Userid"]); if (id == null) { @@ -81,7 +90,9 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) > 0) + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) { if (id == null) { @@ -124,7 +135,9 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) > 0) + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) { if (id == null) { diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs index cc60667..d3314cd 100644 --- a/Controllers/newsController.cs +++ b/Controllers/newsController.cs @@ -28,7 +28,12 @@ namespace WebApplication1.Controllers // GET: news public ActionResult Index() { - ViewBag.role = Convert.ToInt32(Session["Userrole"]); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) + ViewBag.role = 1; + else ViewBag.role = 0; + 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 @@ -103,14 +108,17 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { - setLists(); - //var models= new Tuple, List>(cat, tags); - return View(); - } - if (Session["User"] != null) - { - ViewBag.categoryTitle = db.categories.ToList(); - return View(); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("addNews")) + { + ViewBag.categoryTitle = db.categories.ToList(); + return View(); + } + else + { + return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); + } } else return RedirectToAction("login", "users"); @@ -128,9 +136,6 @@ namespace WebApplication1.Controllers if (Request.Files.Count > 0) { setLists(); - - - if (news.title != null && news.link != null && Cat != null && Tags != null) { @@ -218,6 +223,8 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { + string userrole = Convert.ToString(Session["Userrole"]); + if (id == null) { @@ -228,7 +235,7 @@ namespace WebApplication1.Controllers { return HttpNotFound(); } - else if ( news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0) + else if ( news.userID == Convert.ToInt32(Session["Userid"]) || userrole.Contains("newsManagement")) { return View(news); } @@ -273,7 +280,7 @@ namespace WebApplication1.Controllers } else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0) { - return View(news); + return View(news); } else return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); diff --git a/Controllers/tagsController.cs b/Controllers/tagsController.cs index 66d0aa8..02a53e1 100644 --- a/Controllers/tagsController.cs +++ b/Controllers/tagsController.cs @@ -16,14 +16,24 @@ namespace WebApplication1.Models // GET: tags public ActionResult Index() { - ViewBag.role = Convert.ToInt32(Session["Userrole"]); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) + ViewBag.role = 1; + else ViewBag.role = 0; + return View(db.tags.ToList()); } // GET: tags/Details/5 public ActionResult Details(int? id) { - ViewBag.role = Convert.ToInt32(Session["Userrole"]); + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) + ViewBag.role = 1; + else ViewBag.role = 0; + ViewBag.uid = Convert.ToInt32(Session["Userid"]); if (id == null) { @@ -44,7 +54,9 @@ namespace WebApplication1.Models { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) > 0) + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) { return View(); } @@ -80,7 +92,9 @@ namespace WebApplication1.Models { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) > 0) + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) { if (id == null) { @@ -123,7 +137,9 @@ namespace WebApplication1.Models { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) > 0) + string userrole = Convert.ToString(Session["Userrole"]); + + if (userrole.Contains("tag&catManagment")) { if (id == null) { diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs index e40b898..1d5adb6 100644 --- a/Controllers/usersController.cs +++ b/Controllers/usersController.cs @@ -85,8 +85,14 @@ namespace WebApplication1.Controllers // GET: users - public ActionResult Index() - { if (Convert.ToInt32(Session["Userrole"]) > 0) + public ActionResult Index() { + + string userrole = Convert.ToString(Session["Userrole"]); + if (userrole.Contains("addNews")) + { + @ViewBag.role = "jornalist"; + } + if (userrole.Contains("newsManagement") || userrole.Contains("tag&catManagment") ) { @ViewBag.role = "admin"; } @@ -116,7 +122,8 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { - if (Convert.ToInt32(Session["Userrole"]) == 1) + string userrole = Convert.ToString(Session["Userrole"]); + if (userrole.Contains("userManagment")) { return View(db.users.ToList()); } @@ -152,6 +159,8 @@ namespace WebApplication1.Controllers //checks username and password field in form if (user.usename != null && user.password != null) { + user.role = "none"; + if (check != null) { ModelState.AddModelError("", " username already exists"); @@ -286,8 +295,9 @@ namespace WebApplication1.Controllers { if (Session["User"] != null) { + string userrole = Convert.ToString(Session["Userrole"]); - if (Convert.ToInt32(Session["Userrole"]) == 1) + if (userrole.Contains("userManagment")) { if (id == null) @@ -306,7 +316,7 @@ namespace WebApplication1.Controllers } else - return View("Login"); + return RedirectToAction("Login"); } @@ -315,10 +325,21 @@ namespace WebApplication1.Controllers // more details see https://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] - public ActionResult Edituser([Bind(Include = "ID,usename,password,displayname,role")] user user) + public ActionResult Edituser([Bind(Include = "ID,usename,password,displayname,role")] user user, List roles) { if (ModelState.IsValid) { + user.role = ""; + foreach (string r in roles) + { + if (r != roles[0]) + { + user.role += ","; + user.role += r; + } + else + user.role += r; + } var currentuser = db.users.Find(user.ID); currentuser.displayname = user.displayname; currentuser.role = user.role; diff --git a/Models/Model1.edmx b/Models/Model1.edmx index 77ef1f7..eadaf18 100644 --- a/Models/Model1.edmx +++ b/Models/Model1.edmx @@ -67,7 +67,7 @@ - + @@ -152,7 +152,7 @@ - + diff --git a/Models/user.cs b/Models/user.cs index dab0455..b022e14 100644 --- a/Models/user.cs +++ b/Models/user.cs @@ -18,6 +18,6 @@ namespace WebApplication1.Models public string usename { get; set; } public string password { get; set; } public string displayname { get; set; } - public int role { get; set; } + public string role { get; set; } } } diff --git a/Views/users/Edituser.cshtml b/Views/users/Edituser.cshtml index 6f2654a..1c1f471 100644 --- a/Views/users/Edituser.cshtml +++ b/Views/users/Edituser.cshtml @@ -2,6 +2,8 @@ @{ ViewBag.Title = "Edituser"; + var allRoles = "addNews ,newsManagement, userManagment, tag&catManagment".Split(','); + var Roles = Model.role.Split(','); }

Edituser

@@ -9,7 +11,7 @@ @using (Html.BeginForm()) { @Html.AntiForgeryToken() - +

user


@@ -28,10 +30,17 @@
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
- + + @foreach (var r in Roles) { @r} + @for (int i = 0; i < allRoles.Length; i++) + { + var r = allRoles[i]; +
+ + +
+ } + @Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })