mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2026-04-08 18:11:28 +08:00
统一注入代码
This commit is contained in:
80
OpenAuth.App/AutofacExt.cs
Normal file
80
OpenAuth.App/AutofacExt.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.Mvc
|
||||
// Author : yubaolee
|
||||
// Created : 10-26-2015
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 10-26-2015
|
||||
// ***********************************************************************
|
||||
// <copyright file="AutofacExt.cs" company="www.cnblogs.com/yubaolee">
|
||||
// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
|
||||
// </copyright>
|
||||
// <summary>IOC扩展</summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using Autofac;
|
||||
using Autofac.Integration.Mvc;
|
||||
using Autofac.Integration.WebApi;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using IContainer = Autofac.IContainer;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public static class AutofacExt
|
||||
{
|
||||
private static IContainer _container;
|
||||
|
||||
public static void InitAutofac()
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//注册数据库基础操作和工作单元
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.RegisterType(typeof(UnitWork)).As(typeof(IUnitWork)).PropertiesAutowired();
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
|
||||
|
||||
// 注册controller,使用属性注入
|
||||
builder.RegisterControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
|
||||
//注册所有的ApiControllers
|
||||
builder.RegisterApiControllers(Assembly.GetCallingAssembly()).PropertiesAutowired();
|
||||
|
||||
builder.RegisterModelBinders(Assembly.GetCallingAssembly());
|
||||
builder.RegisterModelBinderProvider();
|
||||
|
||||
// OPTIONAL: Register web abstractions like HttpContextBase.
|
||||
//builder.RegisterModule<AutofacWebTypesModule>();
|
||||
|
||||
// OPTIONAL: Enable property injection in view pages.
|
||||
builder.RegisterSource(new ViewRegistrationSource());
|
||||
|
||||
// 注册所有的Attribute
|
||||
builder.RegisterFilterProvider();
|
||||
|
||||
// Set the dependency resolver to be Autofac.
|
||||
_container = builder.Build();
|
||||
|
||||
//Set the MVC DependencyResolver
|
||||
DependencyResolver.SetResolver(new AutofacDependencyResolver(_container));
|
||||
|
||||
//Set the WebApi DependencyResolver
|
||||
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)_container);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从容器中获取对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static T GetFromFac<T>()
|
||||
{
|
||||
return _container.Resolve<T>();
|
||||
// return (T)DependencyResolver.Current.GetService(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,18 @@
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Autofac.Integration.Mvc, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Autofac.Mvc5.3.3.4\lib\net45\Autofac.Integration.Mvc.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Autofac.Integration.WebApi, Version=3.4.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Autofac.WebApi2.3.4.0\lib\net45\Autofac.Integration.WebApi.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
@@ -41,18 +53,27 @@
|
||||
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="OptimaJet.Workflow.Core, Version=1.5.5.0, Culture=neutral, PublicKeyToken=3d29392dccd464d7, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\WorkflowEngine.NET-Core.1.5.5.2\lib\net45\OptimaJet.Workflow.Core.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http.WebHost, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
@@ -81,6 +102,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AuthoriseService.cs" />
|
||||
<Compile Include="AutofacExt.cs" />
|
||||
<Compile Include="BaseApp.cs" />
|
||||
<Compile Include="AuthorizeApp.cs" />
|
||||
<Compile Include="CategoryApp.cs" />
|
||||
@@ -111,7 +133,6 @@
|
||||
<Compile Include="UserManagerApp.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="OrgManagerApp.cs" />
|
||||
<Compile Include="Response\CommandModel.cs" />
|
||||
<Compile Include="Response\UserWithAccessedCtrls.cs" />
|
||||
<Compile Include="Response\ModuleElementVM.cs" />
|
||||
<Compile Include="Response\ModuleView.cs" />
|
||||
|
||||
@@ -7,39 +7,41 @@ using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.App
|
||||
{
|
||||
public class OrgManagerApp
|
||||
public class OrgManagerApp : BaseApp<Org>
|
||||
{
|
||||
public IUnitWork _unitWork { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 添加部门
|
||||
/// </summary>
|
||||
/// <param name="org">The org.</param>
|
||||
/// <returns>System.Int32.</returns>
|
||||
/// <exception cref="System.Exception">未能找到该组织的父节点信息</exception>
|
||||
public string AddOrUpdate(Org org)
|
||||
public string Add(Org org)
|
||||
{
|
||||
ChangeModuleCascade(org);
|
||||
if (org.Id == string.Empty)
|
||||
{
|
||||
_unitWork.Add(org);
|
||||
}
|
||||
else
|
||||
{
|
||||
//获取旧的的CascadeId
|
||||
var CascadeId = _unitWork.FindSingle<Org>(o => o.Id == org.Id).CascadeId;
|
||||
//根据CascadeId查询子部门
|
||||
var orgs = _unitWork.Find<Org>(u => u.CascadeId.Contains(CascadeId) && u.Id != org.Id).OrderBy(u => u.CascadeId).ToList();
|
||||
|
||||
//更新操作
|
||||
_unitWork.Update(org);
|
||||
Repository.Add(org);
|
||||
|
||||
//更新子部门的CascadeId
|
||||
foreach (var a in orgs)
|
||||
{
|
||||
ChangeModuleCascade(a);
|
||||
_unitWork.Update(a);
|
||||
}
|
||||
return org.Id;
|
||||
}
|
||||
|
||||
public string Update(Org org)
|
||||
{
|
||||
ChangeModuleCascade(org);
|
||||
|
||||
//获取旧的的CascadeId
|
||||
var cascadeId = Repository.FindSingle(o => o.Id == org.Id).CascadeId;
|
||||
//根据CascadeId查询子部门
|
||||
var orgs = Repository.Find(u => u.CascadeId.Contains(cascadeId) && u.Id != org.Id)
|
||||
.OrderBy(u => u.CascadeId).ToList();
|
||||
|
||||
//更新操作
|
||||
UnitWork.Update(org);
|
||||
|
||||
//更新子部门的CascadeId
|
||||
foreach (var a in orgs)
|
||||
{
|
||||
ChangeModuleCascade(a);
|
||||
UnitWork.Update(a);
|
||||
}
|
||||
|
||||
return org.Id;
|
||||
@@ -50,10 +52,10 @@ namespace OpenAuth.App
|
||||
/// </summary>
|
||||
public void DelOrg(string[] ids)
|
||||
{
|
||||
var delOrg = _unitWork.Find<Org>(u => ids.Contains(u.Id)).ToList();
|
||||
var delOrg = Repository.Find(u => ids.Contains(u.Id)).ToList();
|
||||
foreach (var org in delOrg)
|
||||
{
|
||||
_unitWork.Delete<Org>(u => u.CascadeId.Contains(org.CascadeId));
|
||||
Repository.Delete(u => u.CascadeId.Contains(org.CascadeId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,17 +69,17 @@ namespace OpenAuth.App
|
||||
{
|
||||
//用户角色
|
||||
var userRoleIds =
|
||||
_unitWork.Find<Relevance>(u => u.FirstId == userId && u.Key == "UserRole").Select(u => u.SecondId).ToList();
|
||||
UnitWork.Find<Relevance>(u => u.FirstId == userId && u.Key == "UserRole").Select(u => u.SecondId).ToList();
|
||||
|
||||
//用户角色与自己分配到的角色ID
|
||||
var moduleIds =
|
||||
_unitWork.Find<Relevance>(
|
||||
UnitWork.Find<Relevance>(
|
||||
u =>
|
||||
(u.FirstId == userId && u.Key == "UserOrg") ||
|
||||
(u.Key == "RoleOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
|
||||
|
||||
if (!moduleIds.Any()) return new List<Org>();
|
||||
return _unitWork.Find<Org>(u => moduleIds.Contains(u.Id)).ToList();
|
||||
return UnitWork.Find<Org>(u => moduleIds.Contains(u.Id)).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -87,11 +89,11 @@ namespace OpenAuth.App
|
||||
public List<Org> LoadForRole(string roleId)
|
||||
{
|
||||
var moduleIds =
|
||||
_unitWork.Find<Relevance>(u => u.FirstId == roleId && u.Key == "RoleOrg")
|
||||
UnitWork.Find<Relevance>(u => u.FirstId == roleId && u.Key == "RoleOrg")
|
||||
.Select(u => u.SecondId)
|
||||
.ToList();
|
||||
if (!moduleIds.Any()) return new List<Org>();
|
||||
return _unitWork.Find<Org>(u => moduleIds.Contains(u.Id)).ToList();
|
||||
return UnitWork.Find<Org>(u => moduleIds.Contains(u.Id)).ToList();
|
||||
}
|
||||
|
||||
|
||||
@@ -114,13 +116,13 @@ namespace OpenAuth.App
|
||||
string cascadeId = "0.";
|
||||
if (!string.IsNullOrEmpty(orgId))
|
||||
{
|
||||
var org = _unitWork.FindSingle<Org>(u => u.Id == orgId);
|
||||
var org = UnitWork.FindSingle<Org>(u => u.Id == orgId);
|
||||
if (org == null)
|
||||
throw new Exception("未能找到指定对象信息");
|
||||
cascadeId = org.CascadeId;
|
||||
}
|
||||
|
||||
return _unitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId));
|
||||
return UnitWork.Find<Org>(u => u.CascadeId.Contains(cascadeId));
|
||||
}
|
||||
|
||||
#region 私有方法
|
||||
@@ -130,19 +132,19 @@ namespace OpenAuth.App
|
||||
{
|
||||
string cascadeId;
|
||||
int currentCascadeId = 1; //当前结点的级联节点最后一位
|
||||
var sameLevels = _unitWork.Find<Org>(o => o.ParentId == org.ParentId && o.Id != org.Id);
|
||||
var sameLevels = UnitWork.Find<Org>(o => o.ParentId == org.ParentId && o.Id != org.Id);
|
||||
foreach (var obj in sameLevels)
|
||||
{
|
||||
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
|
||||
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
||||
}
|
||||
|
||||
if (org.ParentId != null && org.ParentId != string.Empty)
|
||||
if (!string.IsNullOrEmpty(org.ParentId))
|
||||
{
|
||||
var parentOrg = _unitWork.FindSingle<Org>(o => o.Id == org.ParentId);
|
||||
var parentOrg = UnitWork.FindSingle<Org>(o => o.Id == org.ParentId);
|
||||
if (parentOrg != null)
|
||||
{
|
||||
cascadeId = parentOrg.CascadeId + currentCascadeId+".";
|
||||
cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
|
||||
org.ParentName = parentOrg.Name;
|
||||
}
|
||||
else
|
||||
@@ -152,7 +154,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
else
|
||||
{
|
||||
cascadeId = ".0." + currentCascadeId+".";
|
||||
cascadeId = ".0." + currentCascadeId + ".";
|
||||
org.ParentName = "根节点";
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.App
|
||||
// Author : yubaolee
|
||||
// Created : 09-05-2016
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 09-05-2016
|
||||
// Contact : Microsoft
|
||||
// File: CommandModel.cs
|
||||
// ***********************************************************************
|
||||
|
||||
using OptimaJet.Workflow.Core.Model;
|
||||
|
||||
namespace OpenAuth.App.Response
|
||||
{
|
||||
/// <summary>
|
||||
/// workflow命令
|
||||
/// </summary>
|
||||
public class CommandModel
|
||||
{
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public TransitionClassifier Classifier { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
|
||||
<package id="WorkflowEngine.NET-Core" version="1.5.5.2" targetFramework="net45" />
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Autofac" version="3.5.2" targetFramework="net45" />
|
||||
<package id="Autofac.Mvc5" version="3.3.4" targetFramework="net45" />
|
||||
<package id="Autofac.WebApi2" version="3.4.0" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
|
||||
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user