Skip to content

Commit

Permalink
[#12] Administration des users
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Gagnier committed Oct 6, 2015
1 parent 422b027 commit bbb2ce1
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 41 deletions.
83 changes: 49 additions & 34 deletions LanAdeptAdmin/Views/User/Details.cshtml
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
@model LanAdeptData.Model.User

@{
ViewBag.Title = "Details";
ViewBag.Title = "Détails d'un utilisateur";
ViewBag.Sidebar = "_Sidebar_User";
}

<h2>Details</h2>
<h2>
Détails - @Model.CompleteName
<div class="pull-right">
@Html.ActionLink("Modifier", "Edit", new { id = Model.UserID }, new { @class = "btn btn-primary" })
@Html.ActionLink("Retour", "Index", null, new { @class = "btn btn-default" })
</div>

</h2>
<hr />

@if (!string.IsNullOrWhiteSpace(TempData["Error"] as string))
{
<div class="alert alert-danger" role="alert">
@Html.Raw(TempData["Error"])
</div>
}
@if (!string.IsNullOrWhiteSpace(TempData["Success"] as string))
{
<div class="alert alert-success" role="alert">
@Html.Raw(TempData["Success"])
</div>
}

<div>
<h4>User</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Role.Name)
</dt>

<dd>
@Html.DisplayFor(model => model.Role.Name)
</dd>

<dt>
@Html.DisplayNameFor(model => model.Email)
</dt>

<dd>
@Html.DisplayFor(model => model.Email)
</dd>

<dt>
@Html.DisplayNameFor(model => model.CompleteName)
</dt>

<dd>
@Html.DisplayFor(model => model.CompleteName)
</dd>

</dl>
<dl class="dl-horizontal">

<dt>
Nom complet
</dt>

<dd>
@Html.DisplayFor(model => model.CompleteName)
</dd>

<dt>
Email
</dt>

<dd>
@Html.DisplayFor(model => model.Email)
</dd>

<dt>
Rôle
</dt>

<dd>
@Html.DisplayFor(model => model.Role.Name)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.UserID }) |
@Html.ActionLink("Back to List", "Index")
</p>
7 changes: 3 additions & 4 deletions LanAdeptAdmin/Views/User/Edit.cshtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@model LanAdeptAdmin.Views.Users.ModelController.EditModel

@{
ViewBag.Title = "Edit";
ViewBag.Title = "Modifier un utilisateur";
ViewBag.Sidebar = "_Sidebar_User";
}

<h2>Edit</h2>
<h2>Modifier un utilisateur</h2>

@using (Html.BeginForm())
{
Expand All @@ -20,8 +20,7 @@
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @readonly="readonly" } })
</div>
</div>

Expand Down
13 changes: 13 additions & 0 deletions LanAdeptAdmin/Views/User/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
<h2>Liste des utilisateurs</h2>
<hr />

@if (!string.IsNullOrWhiteSpace(TempData["Error"] as string))
{
<div class="alert alert-danger" role="alert">
@Html.Raw(TempData["Error"])
</div>
}
@if (!string.IsNullOrWhiteSpace(TempData["Success"] as string))
{
<div class="alert alert-success" role="alert">
@Html.Raw(TempData["Success"])
</div>
}

<table class="table">
<thead>
<tr>
Expand Down
4 changes: 4 additions & 0 deletions LanAdeptAdmin/Views/User/ModelController/EditModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
Expand All @@ -18,16 +19,19 @@ public class EditModel
public string Email { get; set; }

[Required]
[DisplayName("Nom complet")]
public string CompleteName { get; set; }

[Required]
[DisplayName("Rôle")]
public int RoleID { get; set; }

public SelectList RoleList { get; set; }

public EditModel() { }
public EditModel(User user)
{
UserID = user.UserID;
Email = user.Email;
CompleteName = user.CompleteName;
RoleID = user.RoleID;
Expand Down
29 changes: 26 additions & 3 deletions LanAdeptAdmin/Views/User/ModelController/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace LanAdeptAdmin.Controllers
{
public class UserController : Controller
{
private const string ERROR_INVALID_ID = "Désolé, une erreur est survenue. Merci de réessayer dans quelques instants";

UnitOfWork uow = UnitOfWork.Current;

[Authorize]
Expand Down Expand Up @@ -63,18 +65,38 @@ public ActionResult Edit(int? id)
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserID,Email,Password,Salt,CompleteName,RoleID")] User user)
public ActionResult Edit([Bind(Include = "UserID,CompleteName,RoleID")] EditModel model)
{
User user = uow.UserRepository.GetByID(model.UserID);
if (user == null)
{
TempData["Error"] = ERROR_INVALID_ID;
return RedirectToAction("Index");
}
Role role = uow.RoleRepository.GetByID(model.RoleID);
if (role == null)
{
TempData["Error"] = ERROR_INVALID_ID;
return RedirectToAction("Details", new { id = user.UserID });
}

if (ModelState.IsValid)
{
user.CompleteName = model.CompleteName;
user.Role = role;

uow.UserRepository.Update(user);
uow.Save();
return RedirectToAction("Index");

TempData["Success"] = "Les changements ont bien été enregistré";
return RedirectToAction("Details", new { id = user.UserID });
}
ViewBag.RoleID = new SelectList(uow.RoleRepository.Get(), "RoleID", "Name", user.RoleID);

ViewBag.RoleID = new SelectList(uow.RoleRepository.Get(), "RoleID", "Name", model.RoleID);
return View(user);
}

#if DEBUG
[Authorize]
public ActionResult Delete(int? id)
{
Expand All @@ -101,5 +123,6 @@ public ActionResult DeleteConfirmed(int id)

return RedirectToAction("Index");
}
#endif
}
}

0 comments on commit bbb2ce1

Please sign in to comment.