Former-commit-id: 2cc075688d5e2004784dde88975dc4c485f11c98
Former-commit-id: d070ee3723807edcd5bd67c67b79857172f8f84f
1.0
wanggang 5 years ago
parent 526dbb5f83
commit 635369ad3e

@ -21,7 +21,7 @@ namespace Infrastructure.Extensions
return $"jwt{httpContext.RequestServices.GetService<IConfiguration>()["jwt:cookie"]}";
}
public static void JwtSignIn(this HttpContext httpContext, string userName, bool rememberMe, string nickName = null, string organName = null)
public static void JwtSignIn(this HttpContext httpContext, string userName, bool rememberMe,string organNumber = null)
{
if (httpContext is null)
{
@ -30,13 +30,9 @@ namespace Infrastructure.Extensions
var claims = new List<Claim> {
new Claim(ClaimTypes.Name, userName),
};
if(!string.IsNullOrEmpty(nickName))
if (!string.IsNullOrEmpty(organNumber))
{
claims.Add(new Claim(ClaimTypes.GivenName, nickName));
}
if (!string.IsNullOrEmpty(organName))
{
claims.Add(new Claim(ClaimTypes.UserData, organName));
claims.Add(new Claim(ClaimTypes.UserData, organNumber));
}
var token = httpContext.CreateJwtToken(claims, DateTime.Now.AddYears(1));
var cookieOptions = new CookieOptions

@ -1,10 +0,0 @@
using System.Collections.Generic;
using System.Security.Claims;
namespace Infrastructure.Web
{
public interface IClaimService
{
List<Claim> GetClaims(string userName);
}
}

@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Security.Claims;
namespace Infrastructure.Web
{
public interface IUserService
{
List<string> GetOrganNumbers(string userName);
List<Claim> GetRoles(string userName);
}
}

@ -30,10 +30,23 @@ namespace Infrastructure.Web
validatedToken = new JwtSecurityTokenHandler().ReadJwtToken(securityToken);
var claims = (validatedToken as JwtSecurityToken).Claims.ToList();
var userName = claims.FirstOrDefault(o => o.Type == ClaimTypes.Name).Value;
using var scope = this._serviceProvider.CreateScope();
var roles = scope.ServiceProvider.GetService<IClaimService>().GetClaims(userName);
claims.Add(new Claim("organs","list"));
claims.AddRange(roles);
var userService = scope.ServiceProvider.GetService<IUserService>();
var organNumber = claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData)?.Value;
if (!string.IsNullOrEmpty(organNumber))
{
var organNumbers = userService.GetOrganNumbers(userName);
if (!organNumbers.Any(o => o == organNumber))
{
validatedToken = null;
return null;
}
}
var claims2 = userService.GetRoles(userName);
claims.AddRange(claims2);
return new ClaimsPrincipal(new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme));
}
catch (Exception ex)

@ -303,6 +303,11 @@ $(function () {
$(function () {
InitControls();
});
$('body').on('change', '#OrganNumber.submit', function () {
$(this).parents('form').submit();
});
$('body').on('click', '.cmd', function () {
$.getJSON($(this).attr('href'), function (response) {
console.log(response);

@ -35,7 +35,7 @@ namespace IoTNode
services.AddTransient<SceneTiggerService>();
services.AddTransient<ISceneTiggerService, CachedSceneTiggerService>();
services.AddTransient<IDbConfig, DbConfig>();
services.AddTransient<IClaimService, RoleService>();
services.AddTransient<IUserService, UserService>();
services.AddTransient<DataService>();
services.AddTransient<IoTNodeEventHandler>();
services.AddSingleton<IoTNodeClient>();

@ -7,16 +7,21 @@ using System.Security.Claims;
namespace IoTNode
{
public class RoleService : IClaimService
public class UserService : IUserService
{
private readonly IRepository<User> _userRepo;
public RoleService(IRepository<User> userRepo)
public UserService(IRepository<User> userRepo)
{
this._userRepo = userRepo;
}
public List<Claim> GetClaims(string userName)
public List<string> GetOrganNumbers(string userName)
{
throw new System.NotImplementedException();
}
public List<Claim> GetRoles(string userName)
{
var userPermissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName)
.SelectMany(o => o.UserRoles)

@ -136,8 +136,8 @@ namespace Platform.Controllers
try
{
var user = this._userRepo.Table()
.Include(o=>o.OrganUsers)
.ThenInclude(o=>o.Organ)
.Include(o => o.OrganUsers)
.ThenInclude(o => o.Organ)
.FirstOrDefault(o => o.UserName == userName);
if (user == null)
{
@ -198,7 +198,7 @@ namespace Platform.Controllers
}
else
{
HttpContext.JwtSignIn(model.UserName, model.RememberMe, user.NickName, mainOrgan?.Id.ToString());
HttpContext.JwtSignIn(model.UserName, model.RememberMe, mainOrgan?.Number);
if (string.IsNullOrEmpty(returnUrl))
{
returnUrl = Url.Action("Index", "Home");
@ -926,9 +926,11 @@ namespace Platform.Controllers
return Content($"var hasLogin={(User.Identity.IsAuthenticated ? "true" : "false")}");
}
public IActionResult ChangeCurrentOrgan(Guid organId,string returnUrl)
public IActionResult ChangeOrgan(string organNumber, string returnUrl)
{
return View();
this.HttpContext.JwtSignOut();
this.HttpContext.JwtSignIn(User.Identity.Name, false, organNumber);
return Redirect(returnUrl);
}
#region tools

@ -1,5 +1,6 @@
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Web;
using IoT.Shared.Application.Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -16,12 +17,14 @@ namespace Platform.Controllers
[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : Controller
{
private readonly IUserService _useService;
private readonly IRepository<User> _userRepo;
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<Building> _buildingRepo;
public HomeController(IRepository<User> userRepo,IRepository<Organ> organRepo,IRepository<Building> buildingRepo)
public HomeController(IUserService userService,IRepository<User> userRepo,IRepository<Organ> organRepo,IRepository<Building> buildingRepo)
{
this._useService = userService;
this._userRepo = userRepo;
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
@ -29,7 +32,6 @@ namespace Platform.Controllers
public IActionResult Index()
{
var organId = Guid.Parse(User.Claims.FirstOrDefault(o => o.Type == ClaimTypes.UserData).Value);
return View();
}

@ -1,42 +0,0 @@
using Infrastructure.Data;
using Infrastructure.Web;
using IoT.Shared.Application.Domain.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace Platform
{
public class RoleService : IClaimService
{
private readonly IRepository<User> _userRepo;
public RoleService(IRepository<User> userRepo)
{
this._userRepo = userRepo;
}
public List<Claim> GetOrgans(string userName)
{
return this._userRepo.ReadOnlyTable()
.Where(o => o.UserName == userName)
.SelectMany(o => o.OrganUsers)
.Select(o => o.Organ)
.ToList()
.Select(o => new Claim("", o.Name))
.ToList();
}
public List<Claim> GetClaims(string userName)
{
var permissions = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName)
.SelectMany(o => o.UserRoles)
.Select(o => o.Role)
.SelectMany(o => o.RolePermissions)
.Select(o => o.Permission.Number)
.Select(o => new Claim(ClaimTypes.Role, o))
.ToList();
return permissions;
}
}
}

@ -33,7 +33,7 @@ namespace Platform
services.AddTransient<SceneTiggerService>();
services.AddTransient<ISceneTiggerService, CachedSceneTiggerService>();
services.AddTransient<IDbConfig, DbConfig>();
services.AddTransient<IClaimService, RoleService>();
services.AddTransient<IUserService, UserService>();
if (Env.IsDevelopment() || Configuration.GetSection("AppSettings").GetValue<bool>("debug", false))
{
services.AddTransient<IEmailSender, EmptyEmailSender>();

@ -0,0 +1,49 @@
using Infrastructure.Data;
using Infrastructure.Web;
using IoT.Shared.Application.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace Platform
{
public class UserService : IUserService
{
private readonly IRepository<User> _userRepo;
public UserService(IRepository<User> userRepo)
{
this._userRepo = userRepo;
}
public List<string> GetOrganNumbers(string userName)
{
var list = this._userRepo.ReadOnlyTable().Where(o => o.UserName == userName)
.SelectMany(o => o.OrganUsers)
.Select(o => o.Organ.Number)
.ToList();
return list;
}
public List<Claim> GetRoles(string userName)
{
var user = this._userRepo.ReadOnlyTable()
.Where(o => o.UserName == userName)
.Include(o => o.OrganUsers).ThenInclude(o => o.Organ)
.Include(o => o.UserRoles).ThenInclude(o => o.Role).ThenInclude(o => o.RolePermissions).ThenInclude(o => o.Permission)
.FirstOrDefault();
List<Claim> list = null;
if (user != null)
{
list = user.UserRoles.Select(o => o.Role)
.SelectMany(o => o.RolePermissions)
.Select(o => o.Permission.Number)
.Select(o => new Claim(ClaimTypes.Role, o))
.ToList();
list.Add(new Claim(ClaimTypes.GivenName, user.NickName));
}
return list;
}
}
}

@ -1,4 +1,5 @@
@inject Infrastructure.Application.Services.Settings.ISettingService settingSerice
@using Microsoft.AspNetCore.Http.Extensions
@inject Infrastructure.Application.Services.Settings.ISettingService settingSerice
@inject IRepository<OrganUser> organUserRepo
@{
var name = settingSerice.GetValue("name");
@ -41,13 +42,16 @@
{
var userName=User.Identity.Name;
var displayName=User.Claims.FirstOrDefault(o=>o.Type== System.Security.Claims.ClaimTypes.GivenName)?.Value??userName;
var organs = organUserRepo.ReadOnlyTable().Where(o=>o.User.UserName==userName).Select(o=>new{o.Organ.Id,o.Organ.Name});
var organs = organUserRepo.ReadOnlyTable().Where(o=>o.User.UserName==userName).Select(o=>new{o.Organ.Number,o.Organ.Name});
if(organs.Any())
{
var organId = User.Claims.FirstOrDefault(o=>o.Type== System.Security.Claims.ClaimTypes.UserData)?.Value;
var list = new SelectList(organs, "Id", "Name", organId);
var organNumber = User.Claims.FirstOrDefault(o=>o.Type== System.Security.Claims.ClaimTypes.UserData)?.Value;
var list = new SelectList(organs, "Number", "Name", organNumber);
<li class="nav-item">
@Html.DropDownList("OrganId", list, "请选择", new { @class = "form-control",data-url=Url.Action("ChangeCurrentOrgan","Account",new{area=""}) })
<form method="get" action="@Url.Action("ChangeOrgan","Account",new{area=""})">
<input type="hidden" name="ReturnUrl" value="@ViewContext.HttpContext.Request.GetDisplayUrl()" />
@Html.DropDownList("OrganNumber", list,new { @class = "form-control submit" })
</form>
</li>
}
<li class="nav-item">

@ -23,8 +23,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{48C7AE84-0
D_5_41_10_4ZHXY_4projects_4lib_4package_1json__JsonSchema = http://json.schemastore.org/pyrseas-0.8
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Libraries", "Libraries", "{6044D20E-13BA-47BF-BD42-CCC5267ACCEF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IoT", "IoT", "{AE34E06D-C5C7-44BC-B168-85808318516C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebMVC", "WebMVC\WebMVC.csproj", "{C66B39B3-D863-4651-99CD-74104CA65C47}"
@ -134,7 +132,7 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{264A3E6B-80A1-488D-841D-930B810B85D2} = {6044D20E-13BA-47BF-BD42-CCC5267ACCEF}
{264A3E6B-80A1-488D-841D-930B810B85D2} = {AE34E06D-C5C7-44BC-B168-85808318516C}
{216F0A25-9F20-4235-9316-632AB94E854A} = {AE34E06D-C5C7-44BC-B168-85808318516C}
{F48CA65D-B2D6-4DB8-A396-A3FE913804FB} = {AE34E06D-C5C7-44BC-B168-85808318516C}
{BE6DEBC5-004F-4811-8BDC-67C74D9E8C2F} = {AE34E06D-C5C7-44BC-B168-85808318516C}
@ -143,7 +141,7 @@ Global
{60596088-3C4E-4EA2-933A-B66CD269845B} = {AE34E06D-C5C7-44BC-B168-85808318516C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
BuildVersion_StartDate = 2000/1/1
SolutionGuid = {0B7095FB-5E70-4EF8-805A-CB4A91AE4B0A}
BuildVersion_StartDate = 2000/1/1
EndGlobalSection
EndGlobal

Loading…
Cancel
Save