diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs index 1b449ef..fa3c55e 100644 --- a/Controllers/usersController.cs +++ b/Controllers/usersController.cs @@ -17,23 +17,33 @@ namespace WebApplication1.Controllers // GET: users public ActionResult Index() { - return View(db.users.ToList()); + if (Session["User"] != null) + { + if (Session["Userid"] == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(Session["Userid"]); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + else + return RedirectToAction("Login"); + }// GET: users + public ActionResult allusers() + { + if (Session["User"] != null) + { + return View(db.users.ToList()); + } + else + return RedirectToAction("Login"); } - // GET: users/Details/5 - public ActionResult Details(int? id) - { - if (id == null) - { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); - } - user user = db.users.Find(id); - if (user == null) - { - return HttpNotFound(); - } - return View(user); - } + //registeration // GET: users/Create public ActionResult Register() @@ -50,28 +60,49 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + + //check if the username exists var check = db.users.FirstOrDefault(u => u.usename == user.usename); + //checks username and password field in form if (user.usename != null && user.password != null) { if (check != null) { ModelState.AddModelError("", " username already exists"); } + //if username is valid create user add to db else { db.users.Add(user); db.SaveChanges(); + + //session start + Session["User"] = user.usename; + Session["Userid"] = user.ID; + return RedirectToAction("Index"); } } - else { - ModelState.AddModelError(""," username and password field can not be empty"); - } + else + { + ModelState.AddModelError("", + "username and password field can not be empty"); + } } - return View(user); } + // GET: users/logout + public ActionResult Logout() + { + //session check, if exists -> destroy session + if (Session["User"] != null) + { + Session.Abandon(); + } + return RedirectToAction("Index"); + } + // GET: users/Login public ActionResult Login() { @@ -85,9 +116,14 @@ namespace WebApplication1.Controllers { if (ModelState.IsValid) { + //check if user exists var existingUser = db.users.FirstOrDefault(u => u.usename == user.usename ); if (existingUser != null && existingUser.password == user.password) { + //session start + Session["User"] = existingUser.usename; + Session["Userid"] = existingUser.ID; + return RedirectToAction("Index"); } else @@ -102,16 +138,22 @@ namespace WebApplication1.Controllers // GET: users/Edit/5 public ActionResult Edit(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + + if (Session["Userid"] == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(Session["Userid"]); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - user user = db.users.Find(id); - if (user == null) - { - return HttpNotFound(); - } - return View(user); + else + return View("Login"); } // POST: users/Edit/5 @@ -135,16 +177,21 @@ namespace WebApplication1.Controllers // GET: users/Delete/5 public ActionResult Delete(int? id) { - if (id == null) + if (Session["User"] != null) { - return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + if (Session["Userid"] == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + user user = db.users.Find(Session["Userid"]); + if (user == null) + { + return HttpNotFound(); + } + return View(user); } - user user = db.users.Find(id); - if (user == null) - { - return HttpNotFound(); - } - return View(user); + else + return View("Login"); } // POST: users/Delete/5 diff --git a/Views/Account/Login.cshtml b/Views/Account/Login.cshtml deleted file mode 100644 index e97d1e5..0000000 --- a/Views/Account/Login.cshtml +++ /dev/null @@ -1,26 +0,0 @@ - -@{ - ViewBag.Title = "Login"; -} - -

Login

- -
-
-
- - - -
-
- - - - -
-
- - -
\ No newline at end of file diff --git a/Views/Account/Register.cshtml b/Views/Account/Register.cshtml deleted file mode 100644 index 6499c5d..0000000 --- a/Views/Account/Register.cshtml +++ /dev/null @@ -1,51 +0,0 @@ -@model WebApplication1.Models.user - -@{ - ViewBag.Title = "Register"; -} - -

Create a new user

- -@using (Html.BeginForm()) -{ - @Html.AntiForgeryToken() - -
-

user

-
- @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
- @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) -
-
- -
- @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) -
-
- -
- @Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" }) -
- @Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) -
-
-
-
-
- -
-
-
-} - -
- @Html.ActionLink("Back to List", "Index") -
diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml index 90e1b63..69b8c0c 100644 --- a/Views/Shared/_Layout.cshtml +++ b/Views/Shared/_Layout.cshtml @@ -20,9 +20,8 @@
  • @Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("About", "About", "Home", new { area = "" }, new { @class = "nav-link" })
  • @Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { @class = "nav-link" })
  • -
  • @Html.ActionLink("Login", "Login", "users", new { area = "" }, new { @class = "nav-link" })
  • -
  • @Html.ActionLink("Register", "Register", "users", new { area = "" }, new { @class = "nav-link" })
  • - +
  • @Html.ActionLink("user setting", "index", "users", new { area = "" }, new { @class = "nav-link" })
  • + diff --git a/Views/users/Delete.cshtml b/Views/users/Delete.cshtml index a529c8c..23a597c 100644 --- a/Views/users/Delete.cshtml +++ b/Views/users/Delete.cshtml @@ -40,9 +40,11 @@ @using (Html.BeginForm()) { @Html.AntiForgeryToken() -
    - | - @Html.ActionLink("Back to List", "Index") -
    +
    + + @Html.ActionLink("back to account managment", "index", null, new { @class = "btn btn-outline-dark" }) + + +
    } diff --git a/Views/users/Details.cshtml b/Views/users/Details.cshtml deleted file mode 100644 index 3956034..0000000 --- a/Views/users/Details.cshtml +++ /dev/null @@ -1,42 +0,0 @@ -@model WebApplication1.Models.user - -@{ - ViewBag.Title = "Details"; -} - -

    Details

    - -
    -

    user

    -
    -
    -
    - @Html.DisplayNameFor(model => model.usename) -
    - -
    - @Html.DisplayFor(model => model.usename) -
    - -
    - @Html.DisplayNameFor(model => model.password) -
    - -
    - @Html.DisplayFor(model => model.password) -
    - -
    - @Html.DisplayNameFor(model => model.displayname) -
    - -
    - @Html.DisplayFor(model => model.displayname) -
    - -
    -
    -

    - @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) | - @Html.ActionLink("Back to List", "Index") -

    diff --git a/Views/users/Edit.cshtml b/Views/users/Edit.cshtml index 2dbd952..30fbacf 100644 --- a/Views/users/Edit.cshtml +++ b/Views/users/Edit.cshtml @@ -1,15 +1,15 @@ @model WebApplication1.Models.user @{ - ViewBag.Title = "Edit"; + ViewBag.Title = "Account managment"; } - -

    Edit

    +

    Account managment

    +

    View and Edit account details

    @using (Html.BeginForm()) { @Html.AntiForgeryToken() - +

    user


    @@ -19,7 +19,7 @@
    @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" })
    - @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) + @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } }) @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
    @@ -39,15 +39,17 @@ @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" })
    +
    - + + @Html.ActionLink("back to account managment", "index", null, new { @class = "btn btn-outline-dark" }) + + +
    } -
    - @Html.ActionLink("Back to List", "Index") -
    diff --git a/Views/users/Index.cshtml b/Views/users/Index.cshtml index 53f1d1f..9ae891f 100644 --- a/Views/users/Index.cshtml +++ b/Views/users/Index.cshtml @@ -1,45 +1,53 @@ -@model IEnumerable +@model WebApplication1.Models.user @{ - ViewBag.Title = "Index"; + ViewBag.Title = "Account managment"; } +

    Account managment

    +

    View and Edit account details

    -

    Index

    +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() -

    - @Html.ActionLink("Create New", "Create") -

    - - - - - - - + -@foreach (var item in Model) { - - - - - - -} +
    + @Html.DisplayFor(model => model.displayname) +
    + + +
    + @Html.ActionLink("edit account details", "Edit", null, new { @class = "btn btn-outline-dark" }) + + @Html.ActionLink("Logout", "LogOut", null, new { @class = "btn btn-outline-dark" }) +
    +
    +
    + @Html.ActionLink("Delete Account", "Delete", null, new { @class = "btn btn-outline-dark" }) +
    + + } -
    +
    +

    user

    +
    +
    +
    @Html.DisplayNameFor(model => model.usename) -
    + + +
    + @Html.DisplayFor(model => model.usename) +
    + +
    @Html.DisplayNameFor(model => model.password) -
    + + +
    + @Html.DisplayFor(model => model.password) +
    + +
    @Html.DisplayNameFor(model => model.displayname) -
    - @Html.DisplayFor(modelItem => item.usename) - - @Html.DisplayFor(modelItem => item.password) - - @Html.DisplayFor(modelItem => item.displayname) - - @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | - @Html.ActionLink("Details", "Details", new { id=item.ID }) | - @Html.ActionLink("Delete", "Delete", new { id=item.ID }) -
    diff --git a/Views/users/Login.cshtml b/Views/users/Login.cshtml index 7435db3..103d33e 100644 --- a/Views/users/Login.cshtml +++ b/Views/users/Login.cshtml @@ -39,5 +39,5 @@ }
    - @Html.ActionLink("Back to List", "Index") + @Html.ActionLink("don't have an account? register now", "Register")
    diff --git a/Views/users/Register.cshtml b/Views/users/Register.cshtml index 7d92fa1..89d465f 100644 --- a/Views/users/Register.cshtml +++ b/Views/users/Register.cshtml @@ -10,42 +10,44 @@ { @Html.AntiForgeryToken() -
    -

    user

    -
    - @Html.ValidationSummary(true, "", new { @class = "text-danger" }) -
    - @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) -
    - @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" }) -
    -
    - -
    - @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) -
    - @Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) -
    -
    - -
    - @Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" }) -
    - @Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } }) - @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) -
    -
    -
    -
    -
    - -
    +
    +

    user

    +
    + @Html.ValidationSummary(true, "", new { @class = "text-danger" }) +
    + @Html.LabelFor(model => model.usename, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.usename, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.usename, "", new { @class = "text-danger" })
    + +
    + @Html.LabelFor(model => model.password, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.PasswordFor(model => model.password, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.password, "", new { @class = "text-danger" }) +
    +
    + + + +
    + @Html.LabelFor(model => model.displayname, htmlAttributes: new { @class = "control-label col-md-2" }) +
    + @Html.EditorFor(model => model.displayname, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.displayname, "", new { @class = "text-danger" }) +
    +
    +
    +
    +
    + +
    +
    +
    }
    - @Html.ActionLink("Back to List", "Index") + @Html.ActionLink("already have an account? Login", "Login")
    diff --git a/Views/users/allusers.cshtml b/Views/users/allusers.cshtml new file mode 100644 index 0000000..5d7bbe7 --- /dev/null +++ b/Views/users/allusers.cshtml @@ -0,0 +1,45 @@ +@model IEnumerable + +@{ + ViewBag.Title = "allusers"; +} + +

    allusers

    + +

    + @Html.ActionLink("Create New", "Create") +

    + + + + + + + + +@foreach (var item in Model) { + + + + + + +} + +
    + @Html.DisplayNameFor(model => model.usename) + + @Html.DisplayNameFor(model => model.password) + + @Html.DisplayNameFor(model => model.displayname) +
    + @Html.DisplayFor(modelItem => item.usename) + + @Html.DisplayFor(modelItem => item.password) + + @Html.DisplayFor(modelItem => item.displayname) + + @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | + @Html.ActionLink("Details", "Details", new { id=item.ID }) | + @Html.ActionLink("Delete", "Delete", new { id=item.ID }) +
    diff --git a/WebApplication1.csproj b/WebApplication1.csproj index f4191f0..a7f9555 100644 --- a/WebApplication1.csproj +++ b/WebApplication1.csproj @@ -251,14 +251,12 @@ - - - +