add access level

This commit is contained in:
2024-08-12 17:15:11 +03:30
parent d123ce205f
commit 4380d9da5c
7 changed files with 103 additions and 37 deletions

View File

@ -17,14 +17,23 @@ namespace WebApplication1.Controllers
// GET: categories // GET: categories
public ActionResult Index() 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()); return View(db.categories.ToList());
} }
// GET: categories/Details/5 // GET: categories/Details/5
public ActionResult Details(int? id) 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"]); ViewBag.uid = Convert.ToInt32(Session["Userid"]);
if (id == null) if (id == null)
{ {
@ -81,7 +90,9 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) > 0) string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("tag&catManagment"))
{ {
if (id == null) if (id == null)
{ {
@ -124,7 +135,9 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) > 0) string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("tag&catManagment"))
{ {
if (id == null) if (id == null)
{ {

View File

@ -28,7 +28,12 @@ namespace WebApplication1.Controllers
// GET: news // GET: news
public ActionResult Index() 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"]); ViewBag.uid = Convert.ToInt32(Session["Userid"]);
var models = (from user in db.users // Access Users DbSet 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 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) if (Session["User"] != null)
{ {
setLists(); string userrole = Convert.ToString(Session["Userrole"]);
//var models= new Tuple<List<category>, List<tag>>(cat, tags);
return View(); if (userrole.Contains("addNews"))
} {
if (Session["User"] != null) ViewBag.categoryTitle = db.categories.ToList();
{ return View();
ViewBag.categoryTitle = db.categories.ToList(); }
return View(); else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
} }
else else
return RedirectToAction("login", "users"); return RedirectToAction("login", "users");
@ -129,9 +137,6 @@ namespace WebApplication1.Controllers
{ {
setLists(); setLists();
if (news.title != null && news.link != null && Cat != null && Tags != null) if (news.title != null && news.link != null && Cat != null && Tags != null)
{ {
@ -218,6 +223,8 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
string userrole = Convert.ToString(Session["Userrole"]);
if (id == null) if (id == null)
{ {
@ -228,7 +235,7 @@ namespace WebApplication1.Controllers
{ {
return HttpNotFound(); 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); return View(news);
} }
@ -273,7 +280,7 @@ namespace WebApplication1.Controllers
} }
else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0) else if (news.userID == Convert.ToInt32(Session["Userid"]) || Convert.ToInt32(Session["Userrole"]) > 0)
{ {
return View(news); return View(news);
} }
else else
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);

View File

@ -16,14 +16,24 @@ namespace WebApplication1.Models
// GET: tags // GET: tags
public ActionResult Index() 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()); return View(db.tags.ToList());
} }
// GET: tags/Details/5 // GET: tags/Details/5
public ActionResult Details(int? id) 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"]); ViewBag.uid = Convert.ToInt32(Session["Userid"]);
if (id == null) if (id == null)
{ {
@ -44,7 +54,9 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) > 0) string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("tag&catManagment"))
{ {
return View(); return View();
} }
@ -80,7 +92,9 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) > 0) string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("tag&catManagment"))
{ {
if (id == null) if (id == null)
{ {
@ -123,7 +137,9 @@ namespace WebApplication1.Models
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
if (Convert.ToInt32(Session["Userrole"]) > 0) string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("tag&catManagment"))
{ {
if (id == null) if (id == null)
{ {

View File

@ -85,8 +85,14 @@ namespace WebApplication1.Controllers
// GET: users // GET: users
public ActionResult Index() public ActionResult Index() {
{ if (Convert.ToInt32(Session["Userrole"]) > 0)
string userrole = Convert.ToString(Session["Userrole"]);
if (userrole.Contains("addNews"))
{
@ViewBag.role = "jornalist";
}
if (userrole.Contains("newsManagement") || userrole.Contains("tag&catManagment") )
{ {
@ViewBag.role = "admin"; @ViewBag.role = "admin";
} }
@ -116,7 +122,8 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) 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()); return View(db.users.ToList());
} }
@ -152,6 +159,8 @@ namespace WebApplication1.Controllers
//checks username and password field in form //checks username and password field in form
if (user.usename != null && user.password != null) if (user.usename != null && user.password != null)
{ {
user.role = "none";
if (check != null) if (check != null)
{ {
ModelState.AddModelError("", " username already exists"); ModelState.AddModelError("", " username already exists");
@ -286,8 +295,9 @@ namespace WebApplication1.Controllers
{ {
if (Session["User"] != null) if (Session["User"] != null)
{ {
string userrole = Convert.ToString(Session["Userrole"]);
if (Convert.ToInt32(Session["Userrole"]) == 1) if (userrole.Contains("userManagment"))
{ {
if (id == null) if (id == null)
@ -306,7 +316,7 @@ namespace WebApplication1.Controllers
} }
else else
return View("Login"); return RedirectToAction("Login");
} }
@ -315,10 +325,21 @@ namespace WebApplication1.Controllers
// more details see https://go.microsoft.com/fwlink/?LinkId=317598. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [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<string> roles)
{ {
if (ModelState.IsValid) 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); var currentuser = db.users.Find(user.ID);
currentuser.displayname = user.displayname; currentuser.displayname = user.displayname;
currentuser.role = user.role; currentuser.role = user.role;

View File

@ -67,7 +67,7 @@
<Property Name="usename" Type="nvarchar" MaxLength="50" Nullable="false" /> <Property Name="usename" Type="nvarchar" MaxLength="50" Nullable="false" />
<Property Name="password" Type="nvarchar(max)" Nullable="false" /> <Property Name="password" Type="nvarchar(max)" Nullable="false" />
<Property Name="displayname" Type="nvarchar" MaxLength="50" /> <Property Name="displayname" Type="nvarchar" MaxLength="50" />
<Property Name="role" Type="int" Nullable="false" /> <Property Name="role" Type="nvarchar" MaxLength="70" Nullable="false" />
</EntityType> </EntityType>
<EntityType Name="viewlog"> <EntityType Name="viewlog">
<Key> <Key>
@ -152,7 +152,7 @@
<Property Name="usename" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="usename" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="password" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" /> <Property Name="password" Type="String" Nullable="false" MaxLength="Max" FixedLength="false" Unicode="true" />
<Property Name="displayname" Type="String" MaxLength="50" FixedLength="false" Unicode="true" /> <Property Name="displayname" Type="String" MaxLength="50" FixedLength="false" Unicode="true" />
<Property Name="role" Type="Int32" Nullable="false" /> <Property Name="role" Type="String" Nullable="false" />
</EntityType> </EntityType>
<EntityType Name="response"> <EntityType Name="response">
<Key> <Key>

View File

@ -18,6 +18,6 @@ namespace WebApplication1.Models
public string usename { get; set; } public string usename { get; set; }
public string password { get; set; } public string password { get; set; }
public string displayname { get; set; } public string displayname { get; set; }
public int role { get; set; } public string role { get; set; }
} }
} }

View File

@ -2,6 +2,8 @@
@{ @{
ViewBag.Title = "Edituser"; ViewBag.Title = "Edituser";
var allRoles = "addNews ,newsManagement, userManagment, tag&catManagment".Split(',');
var Roles = Model.role.Split(',');
} }
<h2>Edituser</h2> <h2>Edituser</h2>
@ -28,10 +30,17 @@
<div class="form-group"> <div class="form-group">
@Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" }) @Html.LabelFor(model => model.role, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10"> <div class="col-md-10">
<select name="role" class="form-control">
<option value="0">User</option> @foreach (var r in Roles) { @r}
<option value="2">Admin</option> @for (int i = 0; i < allRoles.Length; i++)
</select> {
var r = allRoles[i];
<div class="form-check">
<input class="form-check-input" type="checkbox" value="@r" name="roles" id="flexCheckDefault_@i" @(Roles.Contains(r) ? "checked" : "")>
<label class="form-check-label" for="flexCheckDefault_@i">@r</label>
</div>
}
@Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" }) @Html.ValidationMessageFor(model => model.role, "", new { @class = "text-danger" })
</div> </div>
</div> </div>