roles
This commit is contained in:
parent
ec08905e9e
commit
1aaf2bd102
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>
|
/// <param name="me">MyUpdate containing the new user name </param>
|
||||||
/// <returns>Ok when all is ok.</returns>
|
/// <returns>Ok when all is ok.</returns>
|
||||||
[HttpPut("~/api/me")]
|
[HttpPut("~/api/me")]
|
||||||
public async Task<IActionResult> UpdateMe(MyUpdate me)
|
public async Task<IActionResult> UpdateMe(UserInfo me)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid) return new BadRequestObjectResult(
|
if (!ModelState.IsValid) return new BadRequestObjectResult(
|
||||||
new { error = "Specify some valid user update request." });
|
new { error = "Specify some valid user update request." });
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
@ -7,7 +6,10 @@ using Microsoft.AspNet.Authorization;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
using Yavsc.Models.Auth;
|
||||||
|
using Yavsc.ViewModels.Administration;
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -17,11 +19,15 @@ namespace Yavsc.Controllers
|
||||||
private readonly UserManager<ApplicationUser> _userManager;
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
private readonly RoleManager<IdentityRole> _roleManager;
|
private readonly RoleManager<IdentityRole> _roleManager;
|
||||||
|
|
||||||
|
private readonly ApplicationDbContext context;
|
||||||
|
|
||||||
public AdministrationController(UserManager<ApplicationUser> userManager,
|
public AdministrationController(UserManager<ApplicationUser> userManager,
|
||||||
RoleManager<IdentityRole> roleManager)
|
RoleManager<IdentityRole> roleManager,
|
||||||
|
ApplicationDbContext context)
|
||||||
{
|
{
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_roleManager = roleManager;
|
_roleManager = roleManager;
|
||||||
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -47,30 +53,53 @@ namespace Yavsc.Controllers
|
||||||
AddErrors(addToRoleResult);
|
AddErrors(addToRoleResult);
|
||||||
return new BadRequestObjectResult(ModelState);
|
return new BadRequestObjectResult(ModelState);
|
||||||
}
|
}
|
||||||
return Ok(new {message="you owned it."});
|
return Ok(new { message = "you owned it." });
|
||||||
}
|
}
|
||||||
public class RoleInfo {
|
|
||||||
public string Name { get; set; }
|
[Authorize(Roles = Constants.AdminGroupName)]
|
||||||
public IEnumerable<string> Users { get; set; }
|
|
||||||
}
|
|
||||||
[Authorize(Roles=Constants.AdminGroupName)]
|
|
||||||
[Produces("application/json")]
|
[Produces("application/json")]
|
||||||
public async Task<IActionResult> Index() {
|
public async Task<IActionResult> Index()
|
||||||
|
{
|
||||||
var adminCount = await _userManager.GetUsersInRoleAsync(
|
var adminCount = await _userManager.GetUsersInRoleAsync(
|
||||||
Constants.AdminGroupName);
|
Constants.AdminGroupName);
|
||||||
var youAreAdmin = await _userManager.IsInRoleAsync(
|
var youAreAdmin = await _userManager.IsInRoleAsync(
|
||||||
await _userManager.FindByIdAsync(User.GetUserId()),
|
await _userManager.FindByIdAsync(User.GetUserId()),
|
||||||
Constants.AdminGroupName);
|
Constants.AdminGroupName);
|
||||||
var roles = _roleManager.Roles.Select(x=>
|
var roles = _roleManager.Roles.Include(
|
||||||
new RoleInfo {
|
x => x.Users
|
||||||
Name = x.Name,
|
).Select(x => new RoleInfo {
|
||||||
Users = x.Users.Select( u=>u.UserId )
|
Id = x.Id,
|
||||||
} );
|
Name = x.Name,
|
||||||
return Ok (new { Roles = roles, AdminCount = adminCount.Count,
|
Users = x.Users.Select(u=>u.UserId).ToArray()
|
||||||
YouAreAdmin = youAreAdmin
|
});
|
||||||
|
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)
|
private void AddErrors(IdentityResult result)
|
||||||
{
|
{
|
||||||
foreach (var error in result.Errors)
|
foreach (var error in result.Errors)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.Infrastructure;
|
using Microsoft.Data.Entity.Infrastructure;
|
||||||
using Microsoft.Data.Entity.Metadata;
|
|
||||||
using Microsoft.Data.Entity.Migrations;
|
using Microsoft.Data.Entity.Migrations;
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.Data.Entity.Migrations;
|
using Microsoft.Data.Entity.Migrations;
|
||||||
|
|
||||||
namespace Yavsc.Migrations
|
namespace Yavsc.Migrations
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.Infrastructure;
|
using Microsoft.Data.Entity.Infrastructure;
|
||||||
using Microsoft.Data.Entity.Metadata;
|
|
||||||
using Microsoft.Data.Entity.Migrations;
|
|
||||||
using Yavsc.Models;
|
using Yavsc.Models;
|
||||||
|
|
||||||
namespace Yavsc.Migrations
|
namespace Yavsc.Migrations
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,8 @@ namespace Yavsc.Models.Auth
|
||||||
public string Address { get; set; }
|
public string Address { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MyUpdate {
|
public class UserInfo {
|
||||||
|
public string UserId { get; set; }
|
||||||
|
|
||||||
public string UserName { get; set; }
|
public string UserName { get; set; }
|
||||||
public string Avatar { get; set; }
|
public string Avatar { get; set; }
|
||||||
|
|
|
||||||
9
Yavsc/ViewModels/Administration/AdminViewModel.cs
Normal file
9
Yavsc/ViewModels/Administration/AdminViewModel.cs
Normal file
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
22
Yavsc/ViewModels/Administration/RoleInfo.cs
Normal file
22
Yavsc/ViewModels/Administration/RoleInfo.cs
Normal file
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Yavsc/ViewModels/Administration/RoleUserCollection.cs
Normal file
15
Yavsc/ViewModels/Administration/RoleUserCollection.cs
Normal file
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
22
Yavsc/Views/Administration/Index.cshtml
Normal file
22
Yavsc/Views/Administration/Index.cshtml
Normal file
|
|
@ -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>
|
||||||
14
Yavsc/Views/Administration/Role.cshtml
Normal file
14
Yavsc/Views/Administration/Role.cshtml
Normal file
|
|
@ -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.Manage;
|
||||||
@using Yavsc.ViewModels.Calendar;
|
@using Yavsc.ViewModels.Calendar;
|
||||||
@using Yavsc.ViewModels.Auth;
|
@using Yavsc.ViewModels.Auth;
|
||||||
|
@using Yavsc.ViewModels.Administration;
|
||||||
|
|
||||||
@inject IViewLocalizer LocString
|
@inject IViewLocalizer LocString
|
||||||
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
|
@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue