Former-commit-id: 7ad2ee20537ea384a56184ef5ae5de79e9b95dbb
Former-commit-id: 67baedc8fa35427fd8c7d4d1737ea6b53b6b7146
1.0
wanggang 4 years ago
parent 2809c37030
commit 8ee9e3c8bb

@ -7,7 +7,6 @@ namespace Application.Domain.Entities
[Display(Name = "部门用户", Order = 93)] [Display(Name = "部门用户", Order = 93)]
[Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll | ScopeType.UserRead | ScopeType.UserEdit)] [Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll | ScopeType.UserRead | ScopeType.UserEdit)]
[AdminModule] [AdminModule]
[ManyToMany]
public class DepartmentUser : BaseEntity public class DepartmentUser : BaseEntity
{ {
public string Position { get; set; } public string Position { get; set; }

@ -7,7 +7,6 @@ namespace Application.Domain.Entities
{ {
[Display(Name = "机构用户", Order = 80)] [Display(Name = "机构用户", Order = 80)]
[Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll | ScopeType.UserRead | ScopeType.UserDelete)] [Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll | ScopeType.UserRead | ScopeType.UserDelete)]
[ManyToMany]
[AdminModule] [AdminModule]
public class OrganUser : BaseEntity public class OrganUser : BaseEntity
{ {

@ -7,7 +7,6 @@ namespace Application.Domain.Entities
[Display(Name = "用户角色", Order = 81)] [Display(Name = "用户角色", Order = 81)]
[Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll)] [Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll)]
[AdminModule] [AdminModule]
[ManyToMany]
public class OrganUserRole : BaseEntity public class OrganUserRole : BaseEntity
{ {
public bool IsReadOnly { get; set; } public bool IsReadOnly { get; set; }

@ -7,7 +7,6 @@ namespace Application.Domain.Entities
[Display(Name = "角色权限", Order = 81)] [Display(Name = "角色权限", Order = 81)]
[Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll )] [Scope(ScopeAttribute.PlatformAll | ScopeAttribute.OrganAll )]
[AdminModule] [AdminModule]
[ManyToMany]
public class RolePermission : BaseEntity public class RolePermission : BaseEntity
{ {
public Guid RoleId { get; set; } public Guid RoleId { get; set; }

@ -62,6 +62,11 @@ namespace Platform.Api
.Select(o => o.Organ) .Select(o => o.Organ)
.ToList(); .ToList();
var organId = User.GetOrganId(); var organId = User.GetOrganId();
var temp = this._organUserRepo.ReadOnlyTable()
.Where(o => o.User.UserName == userName)
.Include(o=>o.User).Include(o=>o.Organ)
.Include(o => o.UserRoles).ThenInclude(o => o.OrganRole)
.ToList();
var model = new LayoutModel var model = new LayoutModel
{ {
Name = this._settingService.GetValue("name"), Name = this._settingService.GetValue("name"),
@ -75,7 +80,6 @@ namespace Platform.Api
Roles = User == null Roles = User == null
? new List<string>() ? new List<string>()
: this._organUserRepo.ReadOnlyTable() : this._organUserRepo.ReadOnlyTable()
.Include(o=>o.UserRoles).ThenInclude(o=>o.OrganRole)
.Where(o => o.User.UserName == userName) .Where(o => o.User.UserName == userName)
.Where(o => o.OrganId == organId) .Where(o => o.OrganId == organId)
.SelectMany(o => o.UserRoles) .SelectMany(o => o.UserRoles)

@ -48,7 +48,12 @@ namespace Platform.Apis
{ {
try try
{ {
var user = this._userRepo.Table().FirstOrDefault(o => o.UserName == model.UserName); var user = this._userRepo.Table()
.Include(o => o.OrganUsers)
.ThenInclude(o => o.Organ)
.FirstOrDefault(o => o.UserName == model.UserName);
var userMainOrgan = user.OrganUsers.OrderByDescending(o => o.IsDefault).ThenBy(o => o.OrganId).FirstOrDefault();
if (user == null) if (user == null)
{ {
return BadRequest(ModelState.AddModelError("用户名或密码错误")); return BadRequest(ModelState.AddModelError("用户名或密码错误"));
@ -100,7 +105,11 @@ namespace Platform.Apis
} }
} }
} }
return Ok(this.CreateToken(user.UserName)); var claims = new List<Claim> {
new Claim(this._parameters.NameClaimType, user.UserName),
new Claim("OrganId", userMainOrgan.OrganId.ToString())
};
return Ok(this.CreateToken(claims));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -130,7 +139,7 @@ namespace Platform.Apis
new Claim(this._parameters.NameClaimType, userName), new Claim(this._parameters.NameClaimType, userName),
new Claim("OrganId", userMainOrgan.OrganId.ToString()) new Claim("OrganId", userMainOrgan.OrganId.ToString())
}; };
return Ok(CreateToken(userName)); return Ok(CreateToken(claims));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -139,12 +148,17 @@ namespace Platform.Apis
} }
} }
private object CreateToken(String userName) private object CreateToken(List<Claim> claims)
{ {
if(User.Identity.IsAuthenticated)
{
this.HttpContext.JwtSignOut();
this.HttpContext.JwtSignIn(claims, false);
}
return new return new
{ {
AccessToken = Request.HttpContext.CreateJwtToken(new List<Claim> { new Claim(this._parameters.NameClaimType, userName) }, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("AccessTokenTimeout")))), AccessToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("AccessTokenTimeout")))),
RefreshToken = Request.HttpContext.CreateJwtToken(new List<Claim> { new Claim(this._parameters.NameClaimType, userName) }, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("RefreshTokenTimeout")))), RefreshToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("RefreshTokenTimeout")))),
}; };
} }
} }

@ -207,7 +207,7 @@ namespace Platform.Controllers
} }
if (this.IsJsonRequest()) if (this.IsJsonRequest())
{ {
return Json(this.CreateToken(claims)); return Json(this.CreateToken(claims,model.RememberMe));
} }
else else
{ {
@ -1029,8 +1029,13 @@ namespace Platform.Controllers
return Convert.ToInt32(this._settingService.GetValue("CaptchaSeconds")); return Convert.ToInt32(this._settingService.GetValue("CaptchaSeconds"));
} }
private object CreateToken(List<Claim> claims) private object CreateToken(List<Claim> claims,bool rememberMe=false)
{ {
if(User.Identity.IsAuthenticated)
{
HttpContext.JwtSignOut();
HttpContext.JwtSignIn(claims, rememberMe);
}
return new return new
{ {
AccessToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("AccessTokenTimeout")))), AccessToken = Request.HttpContext.CreateJwtToken(claims, DateTime.Now.AddMinutes(Convert.ToDouble(_settingService.GetValue("AccessTokenTimeout")))),

@ -255,7 +255,6 @@ namespace Platform.Data
if (scopeAttribute != null) if (scopeAttribute != null)
{ {
var scope = scopeAttribute.Scope; var scope = scopeAttribute.Scope;
var isManyToMany = item.ClrType.GetCustomAttribute<ManyToManyAttribute>() != null;
//平台权限 //平台权限
if (scope.HasFlag(ScopeType.PlatformRead)) if (scope.HasFlag(ScopeType.PlatformRead))
{ {
@ -265,7 +264,7 @@ namespace Platform.Data
{ {
permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Platform, Name = $"添加平台{name}", Number = $"Add-Platform-{number}" }); permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Platform, Name = $"添加平台{name}", Number = $"Add-Platform-{number}" });
} }
if (!isManyToMany && scope.HasFlag(ScopeType.PlatformEdit)) if (scope.HasFlag(ScopeType.PlatformEdit))
{ {
permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Platform, Name = $"修改平台{name}", Number = $"Edit-Platform-{number}" }); permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Platform, Name = $"修改平台{name}", Number = $"Edit-Platform-{number}" });
} }
@ -282,7 +281,7 @@ namespace Platform.Data
{ {
permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Organ, Name = $"添加机构{name}", Number = $"Add-Organ-{number}" }); permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Organ, Name = $"添加机构{name}", Number = $"Add-Organ-{number}" });
} }
if (!isManyToMany && scope.HasFlag(ScopeType.OrganEdit)) if (scope.HasFlag(ScopeType.OrganEdit))
{ {
permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Organ, Name = $"修改机构{name}", Number = $"Edit-Organ-{number}" }); permissionCatgegory.Permissions.Add(new Permission { IsReadOnly = true, Type = PermissionType.Organ, Name = $"修改机构{name}", Number = $"Edit-Organ-{number}" });
} }
@ -490,10 +489,10 @@ namespace Platform.Data
db.Set<Building>().FirstOrDefault(o => o.Number == "root").Update(); db.Set<Building>().FirstOrDefault(o => o.Number == "root").Update();
db.SaveChanges(); db.SaveChanges();
var defaultOrganId = db.Set<Organ>().FirstOrDefault(o => o.IsReadOnly).Id; var platformOrganId = db.Set<Organ>().FirstOrDefault(o => o.IsReadOnly).Id;
var superRole = new Role { OrganId = defaultOrganId, Number = "super", Name = "超级管理员", IsReadOnly = true }; var superRole = new Role { OrganId = platformOrganId, Number = "super", Name = "超级管理员", IsReadOnly = true };
var adminRole = new Role { OrganId = defaultOrganId, Number = "admin", Name = "管理员" }; var adminRole = new Role { OrganId = platformOrganId, Number = "admin", Name = "管理员" };
var organRole = new Role { OrganId = defaultOrganId, Number = "organ", Name = "机构管理员" }; var organRole = new Role { OrganId = platformOrganId, Number = "organ", Name = "机构管理员" };
//初始化角色 //初始化角色
var skips = new string[] { var skips = new string[] {
@ -529,7 +528,7 @@ namespace Platform.Data
new OrganUser { new OrganUser {
IsReadOnly=true, IsReadOnly=true,
Type= "", Type= "",
OrganId=defaultOrganId, OrganId=platformOrganId,
UserRoles=new List<OrganUserRole> UserRoles=new List<OrganUserRole>
{ {
new OrganUserRole{ new OrganUserRole{
@ -557,7 +556,7 @@ namespace Platform.Data
OrganUsers = new List<OrganUser> { OrganUsers = new List<OrganUser> {
new OrganUser { new OrganUser {
Type= "其他", Type= "其他",
OrganId=defaultOrganId, OrganId=platformOrganId,
UserRoles=new List<OrganUserRole> UserRoles=new List<OrganUserRole>
{ {
new OrganUserRole{ OrganRoleId=adminRole.Id} new OrganUserRole{ OrganRoleId=adminRole.Id}
@ -577,7 +576,7 @@ namespace Platform.Data
OrganUsers = new List<OrganUser> { OrganUsers = new List<OrganUser> {
new OrganUser { new OrganUser {
Type= "其他", Type= "其他",
OrganId=defaultOrganId, OrganId=platformOrganId,
UserRoles=new List<OrganUserRole> UserRoles=new List<OrganUserRole>
{ {
new OrganUserRole{ OrganRoleId=organRole.Id} new OrganUserRole{ OrganRoleId=organRole.Id}

Loading…
Cancel
Save