main
Paul Schneider 9 years ago
parent a8463ce344
commit 2bdaf4301e
12 changed files with 131 additions and 23 deletions

@ -144,7 +144,7 @@ namespace Yavsc.WebApi.Controllers
/// <param name="me">MyUpdate containing the new user name </param>
/// <returns>Ok when all is ok.</returns>
[HttpPut("~/api/me")]
public async Task<IActionResult> UpdateMe(MyUpdate me)
public async Task<IActionResult> UpdateMe(UserInfo me)
{
if (!ModelState.IsValid) return new BadRequestObjectResult(
new { error = "Specify some valid user update request." });

@ -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<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly ApplicationDbContext context;
public AdministrationController(UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
RoleManager<IdentityRole> roleManager,
ApplicationDbContext context)
{
_userManager = userManager;
_roleManager = roleManager;
this.context = context;
}
/// <summary>
@ -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<string> Users { get; set; }
return Ok(new { message = "you owned it." });
}
[Authorize(Roles=Constants.AdminGroupName)]
[Authorize(Roles = Constants.AdminGroupName)]
[Produces("application/json")]
public async Task<IActionResult> Index() {
public async Task<IActionResult> 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)

@ -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;

@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using Microsoft.Data.Entity.Migrations;
namespace Yavsc.Migrations

@ -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

@ -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; }

@ -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; }
}
}

@ -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; }
}
}

@ -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; }
}
}

@ -0,0 +1,22 @@
@model AdminViewModel
@{ 
ViewBag.Title = SR["Administration"];
}
<h2>@ViewBag.Title</h2>
<p>
Rôles :
<ul>
@foreach (var role in Model.Roles) {
<li>@Html.ActionLink(role.Name,"Role",new { id=role.Id }) (@role.Users.Length membres)</li>
}
</ul>
</p>
<p>
Nombre d'administrateurs :
@Model.AdminCount
</p>
<p>
Vous êtes administrateur: @(Model.YouAreAdmin?"Oui":"Non")
</p>

@ -0,0 +1,14 @@
@model RoleUserCollection
@{ 
ViewBag.Title = SR["Role"]+" "+Model.Name;
}
<h2>@ViewBag.Title</h2>
<ul>
@foreach (var user in Model.Users) {
<li> <img src="~/Avatars/@(user.UserName).xs.png" alt="avatar"/>
@user.UserName
</li>
}
</ul>

@ -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"

Loading…