From 2bdaf4301ed009dd7bc42dc745eda7403d42e64d Mon Sep 17 00:00:00 2001
From: Paul Schneider
Date: Fri, 9 Dec 2016 18:14:01 +0100
Subject: [PATCH] roles
---
Yavsc/ApiControllers/AccountController.cs | 2 +-
Yavsc/Controllers/AdministrationController.cs | 61 ++++++++++++++-----
...20161209121035_bookQueryReason.Designer.cs | 1 -
.../20161209121035_bookQueryReason.cs | 2 -
.../ApplicationDbContextModelSnapshot.cs | 2 -
Yavsc/ViewModels/Account/Me.cs | 3 +-
.../Administration/AdminViewModel.cs | 9 +++
Yavsc/ViewModels/Administration/RoleInfo.cs | 22 +++++++
.../Administration/RoleUserCollection.cs | 15 +++++
Yavsc/Views/Administration/Index.cshtml | 22 +++++++
Yavsc/Views/Administration/Role.cshtml | 14 +++++
Yavsc/Views/_ViewImports.cshtml | 1 +
12 files changed, 131 insertions(+), 23 deletions(-)
create mode 100644 Yavsc/ViewModels/Administration/AdminViewModel.cs
create mode 100644 Yavsc/ViewModels/Administration/RoleInfo.cs
create mode 100644 Yavsc/ViewModels/Administration/RoleUserCollection.cs
create mode 100644 Yavsc/Views/Administration/Index.cshtml
create mode 100644 Yavsc/Views/Administration/Role.cshtml
diff --git a/Yavsc/ApiControllers/AccountController.cs b/Yavsc/ApiControllers/AccountController.cs
index cf28fd5c..bbc82cb8 100644
--- a/Yavsc/ApiControllers/AccountController.cs
+++ b/Yavsc/ApiControllers/AccountController.cs
@@ -144,7 +144,7 @@ namespace Yavsc.WebApi.Controllers
/// MyUpdate containing the new user name
/// Ok when all is ok.
[HttpPut("~/api/me")]
- public async Task UpdateMe(MyUpdate me)
+ public async Task UpdateMe(UserInfo me)
{
if (!ModelState.IsValid) return new BadRequestObjectResult(
new { error = "Specify some valid user update request." });
diff --git a/Yavsc/Controllers/AdministrationController.cs b/Yavsc/Controllers/AdministrationController.cs
index ae3d2a48..fc41879e 100644
--- a/Yavsc/Controllers/AdministrationController.cs
+++ b/Yavsc/Controllers/AdministrationController.cs
@@ -1,5 +1,4 @@
-using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
@@ -7,7 +6,10 @@ using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Mvc;
+using Microsoft.Data.Entity;
using Yavsc.Models;
+using Yavsc.Models.Auth;
+using Yavsc.ViewModels.Administration;
namespace Yavsc.Controllers
{
@@ -17,11 +19,15 @@ namespace Yavsc.Controllers
private readonly UserManager _userManager;
private readonly RoleManager _roleManager;
+ private readonly ApplicationDbContext context;
+
public AdministrationController(UserManager userManager,
- RoleManager roleManager)
+ RoleManager roleManager,
+ ApplicationDbContext context)
{
_userManager = userManager;
_roleManager = roleManager;
+ this.context = context;
}
///
@@ -47,30 +53,53 @@ namespace Yavsc.Controllers
AddErrors(addToRoleResult);
return new BadRequestObjectResult(ModelState);
}
- return Ok(new {message="you owned it."});
- }
- public class RoleInfo {
- public string Name { get; set; }
- public IEnumerable Users { get; set; }
+ return Ok(new { message = "you owned it." });
}
- [Authorize(Roles=Constants.AdminGroupName)]
+
+ [Authorize(Roles = Constants.AdminGroupName)]
[Produces("application/json")]
- public async Task Index() {
+ public async Task Index()
+ {
var adminCount = await _userManager.GetUsersInRoleAsync(
Constants.AdminGroupName);
var youAreAdmin = await _userManager.IsInRoleAsync(
await _userManager.FindByIdAsync(User.GetUserId()),
Constants.AdminGroupName);
- var roles = _roleManager.Roles.Select(x=>
- new RoleInfo {
- Name = x.Name,
- Users = x.Users.Select( u=>u.UserId )
- } );
- return Ok (new { Roles = roles, AdminCount = adminCount.Count,
- YouAreAdmin = youAreAdmin
+ var roles = _roleManager.Roles.Include(
+ x => x.Users
+ ).Select(x => new RoleInfo {
+ Id = x.Id,
+ Name = x.Name,
+ Users = x.Users.Select(u=>u.UserId).ToArray()
+ });
+ return View(new AdminViewModel
+ {
+ Roles = roles.ToArray(),
+ AdminCount = adminCount.Count,
+ YouAreAdmin = youAreAdmin
});
}
+ public IActionResult Role(string id)
+ {
+ IdentityRole role = _roleManager.Roles
+ .Include(r=>r.Users).FirstOrDefault
+ ( r=> r.Id == id );
+ var ri = GetRoleUserCollection(role);
+ return View("Role",ri);
+ }
+
+ public RoleUserCollection GetRoleUserCollection(IdentityRole role)
+ {
+ var result = new RoleUserCollection {
+ Id = role.Id,
+ Name = role.Name,
+ Users = context.Users.Where(u=>role.Users.Any(ru => u.Id == ru.UserId))
+ .Select( u => new UserInfo { UserName = u.UserName, Avatar = u.Avatar, UserId = u.Id } )
+ .ToArray()
+ };
+ return result;
+ }
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
diff --git a/Yavsc/Migrations/20161209121035_bookQueryReason.Designer.cs b/Yavsc/Migrations/20161209121035_bookQueryReason.Designer.cs
index 9280e3db..3726ab32 100644
--- a/Yavsc/Migrations/20161209121035_bookQueryReason.Designer.cs
+++ b/Yavsc/Migrations/20161209121035_bookQueryReason.Designer.cs
@@ -1,7 +1,6 @@
using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
-using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Migrations;
using Yavsc.Models;
diff --git a/Yavsc/Migrations/20161209121035_bookQueryReason.cs b/Yavsc/Migrations/20161209121035_bookQueryReason.cs
index 8b65f538..d1126ce3 100644
--- a/Yavsc/Migrations/20161209121035_bookQueryReason.cs
+++ b/Yavsc/Migrations/20161209121035_bookQueryReason.cs
@@ -1,5 +1,3 @@
-using System;
-using System.Collections.Generic;
using Microsoft.Data.Entity.Migrations;
namespace Yavsc.Migrations
diff --git a/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs b/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs
index 8ccd11ee..3c7b4b91 100644
--- a/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs
+++ b/Yavsc/Migrations/ApplicationDbContextModelSnapshot.cs
@@ -1,8 +1,6 @@
using System;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Infrastructure;
-using Microsoft.Data.Entity.Metadata;
-using Microsoft.Data.Entity.Migrations;
using Yavsc.Models;
namespace Yavsc.Migrations
diff --git a/Yavsc/ViewModels/Account/Me.cs b/Yavsc/ViewModels/Account/Me.cs
index 4b0e498d..cd6bea20 100644
--- a/Yavsc/ViewModels/Account/Me.cs
+++ b/Yavsc/ViewModels/Account/Me.cs
@@ -30,7 +30,8 @@ namespace Yavsc.Models.Auth
public string Address { get; set; }
}
-public class MyUpdate {
+public class UserInfo {
+ public string UserId { get; set; }
public string UserName { get; set; }
public string Avatar { get; set; }
diff --git a/Yavsc/ViewModels/Administration/AdminViewModel.cs b/Yavsc/ViewModels/Administration/AdminViewModel.cs
new file mode 100644
index 00000000..24bc52f4
--- /dev/null
+++ b/Yavsc/ViewModels/Administration/AdminViewModel.cs
@@ -0,0 +1,9 @@
+
+namespace Yavsc.ViewModels.Administration {
+
+public class AdminViewModel {
+ public RoleInfo [] Roles { get; set; }
+ public int AdminCount { get; set; }
+ public bool YouAreAdmin { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Yavsc/ViewModels/Administration/RoleInfo.cs b/Yavsc/ViewModels/Administration/RoleInfo.cs
new file mode 100644
index 00000000..86bddde1
--- /dev/null
+++ b/Yavsc/ViewModels/Administration/RoleInfo.cs
@@ -0,0 +1,22 @@
+using System.Linq;
+using Microsoft.AspNet.Identity.EntityFramework;
+
+namespace Yavsc.ViewModels.Administration
+{
+ public class RoleInfo
+ {
+ public RoleInfo ()
+ {
+
+ }
+ public RoleInfo ( IdentityRole role)
+ {
+ Name = role.Name;
+ Id = role.Id;
+ Users = role.Users.Select(u => u.UserId).ToArray();
+ }
+ public string Id { get; set; }
+ public string Name { get; set; }
+ public string[] Users { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Yavsc/ViewModels/Administration/RoleUserCollection.cs b/Yavsc/ViewModels/Administration/RoleUserCollection.cs
new file mode 100644
index 00000000..93de88a8
--- /dev/null
+++ b/Yavsc/ViewModels/Administration/RoleUserCollection.cs
@@ -0,0 +1,15 @@
+using Yavsc.Models.Auth;
+
+namespace Yavsc.ViewModels.Administration
+{
+ public class RoleUserCollection
+ {
+ public RoleUserCollection()
+ {
+
+ }
+ public string Id { get; set; }
+ public string Name { get; set; }
+ public UserInfo[] Users { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Yavsc/Views/Administration/Index.cshtml b/Yavsc/Views/Administration/Index.cshtml
new file mode 100644
index 00000000..a6ea5893
--- /dev/null
+++ b/Yavsc/Views/Administration/Index.cshtml
@@ -0,0 +1,22 @@
+@model AdminViewModel
+
+@{
+ ViewBag.Title = SR["Administration"];
+}
+
+@ViewBag.Title
+
+Rôles :
+
+@foreach (var role in Model.Roles) {
+- @Html.ActionLink(role.Name,"Role",new { id=role.Id }) (@role.Users.Length membres)
+}
+
+
+
+Nombre d'administrateurs :
+@Model.AdminCount
+
+
+Vous êtes administrateur: @(Model.YouAreAdmin?"Oui":"Non")
+
\ No newline at end of file
diff --git a/Yavsc/Views/Administration/Role.cshtml b/Yavsc/Views/Administration/Role.cshtml
new file mode 100644
index 00000000..c93f800d
--- /dev/null
+++ b/Yavsc/Views/Administration/Role.cshtml
@@ -0,0 +1,14 @@
+@model RoleUserCollection
+@{
+ ViewBag.Title = SR["Role"]+" "+Model.Name;
+}
+
+@ViewBag.Title
+
+
+@foreach (var user in Model.Users) {
+ -
+ @user.UserName
+
+}
+
diff --git a/Yavsc/Views/_ViewImports.cshtml b/Yavsc/Views/_ViewImports.cshtml
index 4154c7e2..e591f181 100755
--- a/Yavsc/Views/_ViewImports.cshtml
+++ b/Yavsc/Views/_ViewImports.cshtml
@@ -18,6 +18,7 @@
@using Yavsc.ViewModels.Manage;
@using Yavsc.ViewModels.Calendar;
@using Yavsc.ViewModels.Auth;
+@using Yavsc.ViewModels.Administration;
@inject IViewLocalizer LocString
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"