mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 23:13:40 +08:00
v1.1 实现用户分级授权,得到权限的用户只能在自己的权限范围内再进一步操作
This commit is contained in:
parent
ef6a274ac6
commit
fbdd6163a2
@ -57,7 +57,6 @@
|
|||||||
<Compile Include="ViewModel\LoginUserVM.cs" />
|
<Compile Include="ViewModel\LoginUserVM.cs" />
|
||||||
<Compile Include="ViewModel\ModuleElementVM.cs" />
|
<Compile Include="ViewModel\ModuleElementVM.cs" />
|
||||||
<Compile Include="ViewModel\ModuleView.cs" />
|
<Compile Include="ViewModel\ModuleView.cs" />
|
||||||
<Compile Include="ViewModel\ResourceVM.cs" />
|
|
||||||
<Compile Include="ViewModel\RoleVM.cs" />
|
<Compile Include="ViewModel\RoleVM.cs" />
|
||||||
<Compile Include="ViewModel\UserView.cs" />
|
<Compile Include="ViewModel\UserView.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -6,103 +6,49 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using OpenAuth.App.ViewModel;
|
using OpenAuth.App.ViewModel;
|
||||||
|
using OpenAuth.Domain.Service;
|
||||||
|
|
||||||
namespace OpenAuth.App
|
namespace OpenAuth.App
|
||||||
{
|
{
|
||||||
public class ResourceManagerApp
|
public class ResourceManagerApp
|
||||||
{
|
{
|
||||||
private IResourceRepository _repository;
|
private ResManagerService _resManagerService;
|
||||||
private readonly ICategoryRepository _categoryRepository;
|
|
||||||
private IRelevanceRepository _relevanceRepository;
|
|
||||||
|
|
||||||
public ResourceManagerApp(IResourceRepository repository,
|
public ResourceManagerApp(ResManagerService resManagerService)
|
||||||
ICategoryRepository categoryRepository,
|
|
||||||
IRelevanceRepository relevanceRepository)
|
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_resManagerService = resManagerService;
|
||||||
_categoryRepository = categoryRepository;
|
|
||||||
_relevanceRepository = relevanceRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetResourceCntInOrg(int orgId)
|
public int GetResourceCntInOrg(int orgId)
|
||||||
{
|
{
|
||||||
if (orgId == 0)
|
return _resManagerService.GetResourceCntInOrg(orgId);
|
||||||
{
|
|
||||||
return _repository.Find(null).Count();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return _repository.GetResourceCntInOrgs(GetSubOrgIds(orgId));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Resource> LoadAll()
|
public List<Resource> LoadAll()
|
||||||
{
|
{
|
||||||
return _repository.Find(null).ToList();
|
return _resManagerService.LoadAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 加载一个节点下面的一个或全部Resources
|
/// 加载一个节点下面的一个或全部Resources
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public dynamic Load(int categoryId, int pageindex, int pagesize)
|
public dynamic Load(string username, int categoryId, int pageindex, int pagesize)
|
||||||
{
|
{
|
||||||
IEnumerable<Resource> Resources;
|
return _resManagerService.Load(username, categoryId, pageindex, pagesize);
|
||||||
int total = 0;
|
|
||||||
if (categoryId == 0)
|
|
||||||
{
|
|
||||||
Resources = _repository.LoadResources(pageindex, pagesize);
|
|
||||||
total = _repository.GetCount();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Resources = _repository.LoadInOrgs(pageindex, pagesize,GetSubOrgIds(categoryId));
|
|
||||||
total = _repository.GetResourceCntInOrgs(categoryId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new
|
|
||||||
{
|
|
||||||
total = total,
|
|
||||||
list = Resources,
|
|
||||||
pageCurrent = pageindex
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取当前节点的所有下级节点
|
|
||||||
/// </summary>
|
|
||||||
private int[] GetSubOrgIds(int orgId)
|
|
||||||
{
|
|
||||||
var org = _categoryRepository.FindSingle(u => u.Id == orgId);
|
|
||||||
var orgs = _categoryRepository.Find(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray();
|
|
||||||
return orgs;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Resource Find(int id)
|
|
||||||
{
|
|
||||||
var resource = _repository.FindSingle(u => u.Id == id);
|
|
||||||
if (resource == null) return new Resource();
|
|
||||||
|
|
||||||
return resource;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Delete(int id)
|
public void Delete(int id)
|
||||||
{
|
{
|
||||||
_repository.Delete(id);
|
_resManagerService.Delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddOrUpdate(Resource model)
|
public void AddOrUpdate(Resource model)
|
||||||
{
|
{
|
||||||
Resource resource = new Resource();
|
Resource resource = new Resource();
|
||||||
model.CopyTo(resource);
|
model.CopyTo(resource);
|
||||||
|
_resManagerService.AddOrUpdate(resource);
|
||||||
if (resource.Id == 0)
|
|
||||||
{
|
|
||||||
_repository.Add(resource);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_repository.Update(resource);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,26 +61,9 @@ namespace OpenAuth.App
|
|||||||
/// 当为UserResource时,表示UserId
|
/// 当为UserResource时,表示UserId
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="cId">分类ID</param>
|
/// <param name="cId">分类ID</param>
|
||||||
public List<ResourceVM> LoadWithAccess(string accessType, int firstId, int cId)
|
public List<dynamic> LoadWithAccess(string username, string accessType, int firstId, int cId)
|
||||||
{
|
{
|
||||||
var listVms = new List<ResourceVM>();
|
return _resManagerService.LoadWithAccess(username, accessType, firstId, cId);
|
||||||
// if (cId == 0) return listVms;
|
|
||||||
|
|
||||||
foreach (var element in _repository.LoadInOrgs(cId))
|
|
||||||
{
|
|
||||||
var accessed = _relevanceRepository.FindSingle(u => u.Key == accessType
|
|
||||||
&& u.FirstId == firstId && u.SecondId == element.Id);
|
|
||||||
listVms.Add(new ResourceVM
|
|
||||||
{
|
|
||||||
Id = element.Id,
|
|
||||||
Name = element.Name,
|
|
||||||
IsBelongUser = accessed != null,
|
|
||||||
Description = element.Description,
|
|
||||||
Key = element.Key,
|
|
||||||
Status = element.Status
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return listVms;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,8 @@ namespace OpenAuth.App
|
|||||||
|
|
||||||
public List<RoleVM> LoadForOrgAndUser(int orgId, int userId)
|
public List<RoleVM> LoadForOrgAndUser(int orgId, int userId)
|
||||||
{
|
{
|
||||||
var roleIds = _repository.Find(u => orgId == 0 || u.OrgId == orgId).ToList();
|
var allorgs = GetSubOrgIds(orgId);
|
||||||
|
var roleIds = _repository.Find(u => orgId == 0 || allorgs.Contains(u.OrgId)).ToList();
|
||||||
var rolevms = new List<RoleVM>();
|
var rolevms = new List<RoleVM>();
|
||||||
foreach (var role in roleIds)
|
foreach (var role in roleIds)
|
||||||
{
|
{
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
namespace OpenAuth.App.ViewModel
|
|
||||||
{
|
|
||||||
public class ResourceVM
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 资源表ID
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public int Id { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
///
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public string Key { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 组织名称
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 资源分类标识
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public int Status { get; set; }
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 描述
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public string Description { get; set; }
|
|
||||||
|
|
||||||
public bool IsBelongUser { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -65,6 +65,7 @@
|
|||||||
<Compile Include="Resource.cs" />
|
<Compile Include="Resource.cs" />
|
||||||
<Compile Include="Role.cs" />
|
<Compile Include="Role.cs" />
|
||||||
<Compile Include="Service\AuthoriseService.cs" />
|
<Compile Include="Service\AuthoriseService.cs" />
|
||||||
|
<Compile Include="Service\ResManagerService.cs" />
|
||||||
<Compile Include="Service\StockManagerService.cs" />
|
<Compile Include="Service\StockManagerService.cs" />
|
||||||
<Compile Include="Stock.cs" />
|
<Compile Include="Stock.cs" />
|
||||||
<Compile Include="User.cs" />
|
<Compile Include="User.cs" />
|
||||||
|
152
OpenAuth.Domain/Service/ResManagerService.cs
Normal file
152
OpenAuth.Domain/Service/ResManagerService.cs
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using OpenAuth.Domain.Interface;
|
||||||
|
|
||||||
|
namespace OpenAuth.Domain.Service
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 领域服务
|
||||||
|
/// <para>资源管理领域</para>
|
||||||
|
/// </summary>
|
||||||
|
public class ResManagerService
|
||||||
|
{
|
||||||
|
private IResourceRepository _repository;
|
||||||
|
private readonly ICategoryRepository _categoryRepository;
|
||||||
|
private IRelevanceRepository _relevanceRepository;
|
||||||
|
private AuthoriseService _authoriseService;
|
||||||
|
|
||||||
|
public ResManagerService(IResourceRepository repository,
|
||||||
|
ICategoryRepository categoryRepository,
|
||||||
|
IRelevanceRepository relevanceRepository,
|
||||||
|
AuthoriseService authoriseService)
|
||||||
|
{
|
||||||
|
_repository = repository;
|
||||||
|
_categoryRepository = categoryRepository;
|
||||||
|
_relevanceRepository = relevanceRepository;
|
||||||
|
_authoriseService = authoriseService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetResourceCntInOrg(int orgId)
|
||||||
|
{
|
||||||
|
if (orgId == 0)
|
||||||
|
{
|
||||||
|
return _repository.Find(null).Count();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return _repository.GetResourceCntInOrgs(GetSubOrgIds(orgId));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Resource> LoadAll()
|
||||||
|
{
|
||||||
|
return _repository.Find(null).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加载用户一个节点下面的一个或全部Resources
|
||||||
|
/// </summary>
|
||||||
|
public dynamic Load(string username, int categoryId, int pageindex, int pagesize)
|
||||||
|
{
|
||||||
|
_authoriseService.GetUserAccessed(username);
|
||||||
|
if (_authoriseService.Resources.Count == 0) //用户没有任何资源
|
||||||
|
{
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
total = 0,
|
||||||
|
pageCurrent = pageindex
|
||||||
|
};
|
||||||
|
}
|
||||||
|
var subIds = GetSubOrgIds(categoryId);
|
||||||
|
var query = _authoriseService.Resources.Where(u => categoryId == 0 || subIds.Contains(u.CategoryId));
|
||||||
|
var Resources = query.Skip((pageindex - 1) * pagesize).Take(pagesize);
|
||||||
|
int total = query.Count();
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
total = total,
|
||||||
|
list = Resources,
|
||||||
|
pageCurrent = pageindex
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前节点的所有下级节点
|
||||||
|
/// </summary>
|
||||||
|
private int[] GetSubOrgIds(int orgId)
|
||||||
|
{
|
||||||
|
if (orgId == 0)
|
||||||
|
{
|
||||||
|
return _categoryRepository.Find(null).Select(u => u.Id).ToArray();
|
||||||
|
}
|
||||||
|
var org = _categoryRepository.FindSingle(u => u.Id == orgId);
|
||||||
|
var orgs = _categoryRepository.Find(u => u.CascadeId.Contains(org.CascadeId)).Select(u => u.Id).ToArray();
|
||||||
|
return orgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource Find(int id)
|
||||||
|
{
|
||||||
|
var resource = _repository.FindSingle(u => u.Id == id);
|
||||||
|
if (resource == null) return new Resource();
|
||||||
|
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Delete(int id)
|
||||||
|
{
|
||||||
|
_repository.Delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddOrUpdate(Resource resource)
|
||||||
|
{
|
||||||
|
if (resource.Id == 0)
|
||||||
|
{
|
||||||
|
_repository.Add(resource);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_repository.Update(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取带有授权状态的菜单列表
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="accessType">授权类型,当前有RoleResource/UserResource</param>
|
||||||
|
/// <param name="firstId">
|
||||||
|
/// 当为RoleResource时,表示RoleId
|
||||||
|
/// 当为UserResource时,表示UserId
|
||||||
|
/// </param>
|
||||||
|
/// <param name="cId">分类ID</param>
|
||||||
|
public List<dynamic> LoadWithAccess(string username, string accessType, int firstId, int cId)
|
||||||
|
{
|
||||||
|
var listVms = new List<dynamic>();
|
||||||
|
_authoriseService.GetUserAccessed(username);
|
||||||
|
if (_authoriseService.Resources.Count == 0) //用户没有任何资源
|
||||||
|
{
|
||||||
|
return listVms;
|
||||||
|
}
|
||||||
|
|
||||||
|
var subIds = GetSubOrgIds(cId);
|
||||||
|
var query = _authoriseService.Resources.Where(u => cId == 0 || subIds.Contains(u.CategoryId));
|
||||||
|
|
||||||
|
foreach (var element in query)
|
||||||
|
{
|
||||||
|
var accessed = _relevanceRepository.FindSingle(u => u.Key == accessType
|
||||||
|
&& u.FirstId == firstId && u.SecondId == element.Id);
|
||||||
|
listVms.Add(new
|
||||||
|
{
|
||||||
|
Id = element.Id,
|
||||||
|
Name = element.Name,
|
||||||
|
IsBelongUser = accessed != null,
|
||||||
|
Description = element.Description,
|
||||||
|
Key = element.Key,
|
||||||
|
Status = element.Status
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return listVms;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -30,11 +30,11 @@ namespace OpenAuth.Mvc
|
|||||||
{
|
{
|
||||||
var builder = new ContainerBuilder();
|
var builder = new ContainerBuilder();
|
||||||
|
|
||||||
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>
|
//注册数据库基础操作和工作单元
|
||||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
|
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>));
|
||||||
builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork));
|
builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork));
|
||||||
|
|
||||||
//Ӧ<EFBFBD>ò<EFBFBD>ע<EFBFBD><EFBFBD>
|
//注册WebConfig中的配置
|
||||||
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
|
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
|
||||||
|
|
||||||
//注册app层
|
//注册app层
|
||||||
|
@ -46,7 +46,7 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Load(int categoryId, int pageCurrent = 1, int pageSize = 30)
|
public string Load(int categoryId, int pageCurrent = 1, int pageSize = 30)
|
||||||
{
|
{
|
||||||
return JsonHelper.Instance.Serialize(_app.Load(categoryId, pageCurrent, pageSize));
|
return JsonHelper.Instance.Serialize(_app.Load(User.Identity.Name, categoryId, pageCurrent, pageSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
public string LoadForTree()
|
public string LoadForTree()
|
||||||
@ -94,7 +94,7 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
/// <returns>System.String.</returns>
|
/// <returns>System.String.</returns>
|
||||||
public string LoadWithAccess(int cId, int firstId, string key)
|
public string LoadWithAccess(int cId, int firstId, string key)
|
||||||
{
|
{
|
||||||
return JsonHelper.Instance.Serialize(_app.LoadWithAccess(key,firstId, cId));
|
return JsonHelper.Instance.Serialize(_app.LoadWithAccess(User.Identity.Name,key,firstId, cId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
|
|||||||
//
|
//
|
||||||
// 你可以指定所有值,也可以让修订版本和内部版本号采用默认值,
|
// 你可以指定所有值,也可以让修订版本和内部版本号采用默认值,
|
||||||
// 方法是按如下所示使用 "*":
|
// 方法是按如下所示使用 "*":
|
||||||
[assembly: AssemblyVersion("1.0.0.0")]
|
[assembly: AssemblyVersion("1.1.0.0")]
|
||||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
[assembly: AssemblyFileVersion("1.1.0.0")]
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<div class="bjui-pageContent tableContent" style="position: relative">
|
<div class="bjui-pageContent tableContent" style="position: relative">
|
||||||
<div class="clearfix">
|
<div class="clearfix">
|
||||||
<div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
|
<div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
|
||||||
<ul id="tree" class="ztree"></ul>
|
<ul id="orgtree" class="ztree"></ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="detail" style="margin-left: 225px;">
|
<div id="detail" style="margin-left: 225px;">
|
||||||
|
Loading…
Reference in New Issue
Block a user