1 check issue #12

2 修改登录为Identity认证方式
This commit is contained in:
yubaolee 2016-04-21 10:54:05 +08:00
parent 9c8b6f8629
commit 2d13810c82
14 changed files with 233 additions and 140 deletions

View File

@ -1,115 +1,61 @@
using Infrastructure;
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
using System;
using System;
using System.Linq;
using System.Web;
using Infrastructure;
using OpenAuth.App.ViewModel;
using System.Web.Security;
using OpenAuth.Domain.Service;
namespace OpenAuth.App
{
public class LoginApp
{
private IUserRepository _repository;
private IModuleRepository _moduleRepository;
private IRelevanceRepository _relevanceRepository;
private IRepository<ModuleElement> _moduleElementRepository;
private IResourceRepository _resourceRepository;
private IOrgRepository _orgRepository;
private AuthoriseService _service;
public LoginApp(IUserRepository repository,
IModuleRepository moduleRepository,
IRelevanceRepository relevanceRepository,
IRepository<ModuleElement> moduleElementRepository,
IResourceRepository resourceRepository,
IOrgRepository orgRepository)
public LoginApp(AuthoriseService service)
{
_repository = repository;
_moduleRepository = moduleRepository;
_relevanceRepository = relevanceRepository;
_moduleElementRepository = moduleElementRepository;
_resourceRepository = resourceRepository;
_orgRepository = orgRepository;
_service = service;
}
public LoginUserVM Login(string userName, string password)
public void Login(string userName, string password)
{
var user = _repository.FindSingle(u => u.Account == userName);
if (user == null)
{
throw new Exception("用户帐号不存在");
}
user.CheckPassword(password);
_service.Check(userName, password);
FormsAuthentication.SetAuthCookie(userName, true);
var loginVM = new LoginUserVM
{
User = user
};
//用户角色
var userRoleIds =
_relevanceRepository.Find(u => u.FirstId == user.Id && u.Key == "UserRole").Select(u => u.SecondId).ToList();
//用户角色与自己分配到的模块ID
var moduleIds =
_relevanceRepository.Find(
u =>
(u.FirstId == user.Id && u.Key == "UserModule") ||
(u.Key == "RoleModule" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
//用户角色与自己分配到的菜单ID
var elementIds =
_relevanceRepository.Find(
u =>
(u.FirstId == user.Id && u.Key == "UserElement") ||
(u.Key == "RoleElement" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
//得出最终用户拥有的模块
loginVM.Modules = _moduleRepository.Find(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo).MapToList<ModuleView>();
//模块菜单权限
foreach (var module in loginVM.Modules)
{
module.Elements = _moduleElementRepository.Find(u => u.ModuleId == module.Id && elementIds.Contains(u.Id)).OrderBy(u => u.Sort).ToList();
}
//用户角色与自己分配到的资源ID
var resourceIds = _relevanceRepository.Find(
u =>
(u.FirstId == user.Id && u.Key == "UserResource") ||
(u.Key == "RoleResource" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
loginVM.Resources = _resourceRepository.Find(u => resourceIds.Contains(u.Id)).ToList();
//用户角色与自己分配到的机构ID
var orgids = _relevanceRepository.Find(
u =>
(u.FirstId == user.Id && u.Key == "UserAccessedOrg") ||
(u.Key == "RoleAccessedOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
loginVM.AccessedOrgs = _orgRepository.Find(u => orgids.Contains(u.Id)).ToList();
return loginVM;
}
/// <summary>
/// 开发者登陆
/// </summary>
public LoginUserVM LoginByDev()
public void LoginByDev()
{
var loginUser = new LoginUserVM
_service.SetSysUser();
FormsAuthentication.SetAuthCookie("System", true);
}
public LoginUserVM GetLoginUser()
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
User = new User
{
Name = "开发者账号",
Account = "System"
}
throw new Exception("未登录");
}
string username = HttpContext.Current.User.Identity.Name;
_service.GetUserAccessed(username);
var user = new LoginUserVM
{
User = _service.User,
AccessedOrgs = _service.Orgs,
Modules = _service.Modules.MapToList<ModuleView>(),
Resources = _service.Resources,
};
loginUser.Modules = _moduleRepository.Find(null).MapToList<ModuleView>();
//模块包含的菜单
foreach (var module in loginUser.Modules)
foreach (var moduleView in user.Modules)
{
module.Elements = _moduleElementRepository.Find(u => u.ModuleId == module.Id).OrderBy(u => u.Sort).ToList();
moduleView.Elements = _service.ModuleElements.Where(u => u.ModuleId == moduleView.Id).OrderBy(u => u.Sort).ToList();
}
loginUser.Resources = _resourceRepository.Find(null).OrderBy(u => u.SortNo).ToList();
loginUser.AccessedOrgs = _orgRepository.Find(null).OrderBy(u => u.SortNo).ToList();
return loginUser;
return user;
}
}
}

View File

@ -27,38 +27,38 @@ namespace OpenAuth.App
public dynamic Load(int orgId, int pageindex, int pagesize)
{
IEnumerable<Stock> Stocks;
var user = SessionHelper.GetSessionUser<LoginUserVM>();
var loginOrgs = user.AccessedOrgs.Select(u => u.Id).ToArray();
//var user = AutofacExt.GetFromFac<LoginApp>().GetLoginUser();
//var loginOrgs = user.AccessedOrgs.Select(u => u.Id).ToArray();
int total;
if (orgId == 0)
{
//int total;
//if (orgId == 0)
//{
if (loginOrgs.Length == 0) //改用户没有任何可见机构
{
Stocks = _repository.Find(pageindex, pagesize, "", u => u.User == user.User.Account);
total = _repository.GetCount(u =>u.User ==user.User.Account);
}
else
{
Stocks = _repository.LoadInOrgs(pageindex, pagesize, loginOrgs);
total = _repository.GetStockCntInOrgs(loginOrgs);
}
// if (loginOrgs.Length == 0) //改用户没有任何可见机构
// {
// Stocks = _repository.Find(pageindex, pagesize, "", u => u.User == user.User.Account);
// total = _repository.GetCount(u =>u.User ==user.User.Account);
// }
// else
// {
// Stocks = _repository.LoadInOrgs(pageindex, pagesize, loginOrgs);
// total = _repository.GetStockCntInOrgs(loginOrgs);
// }
}
else //加载选择的机构及用户可访问的所有子机构
{
var orgs = _orgRepository.GetSubOrgs(orgId).Where(u =>loginOrgs.Contains(u.Id));
List<int> orgIds = orgs.Select(u => u.Id).ToList();
orgIds.Add(orgId);
Stocks = _repository.LoadInOrgs(pageindex, pagesize, orgIds.ToArray());
total = _repository.GetStockCntInOrgs(orgIds.ToArray());
}
//}
//else //加载选择的机构及用户可访问的所有子机构
//{
// var orgs = _orgRepository.GetSubOrgs(orgId).Where(u =>loginOrgs.Contains(u.Id));
// List<int> orgIds = orgs.Select(u => u.Id).ToList();
// orgIds.Add(orgId);
// Stocks = _repository.LoadInOrgs(pageindex, pagesize, orgIds.ToArray());
// total = _repository.GetStockCntInOrgs(orgIds.ToArray());
//}
return new
{
total = total,
list = Stocks,
// total = total,
// list = Stocks,
pageCurrent = pageindex
};
}

View File

@ -63,6 +63,7 @@
<Compile Include="Relevance.cs" />
<Compile Include="Resource.cs" />
<Compile Include="Role.cs" />
<Compile Include="Service\AuthoriseService.cs" />
<Compile Include="Stock.cs" />
<Compile Include="User.cs" />
<Compile Include="UserCfg.cs" />

View File

@ -0,0 +1,147 @@
// ***********************************************************************
// Assembly : OpenAuth.Domain
// Author : yubaolee
// Created : 04-21-2016
//
// Last Modified By : yubaolee
// Last Modified On : 04-21-2016
// Contact : Microsoft
// File: AuthenService.cs
// ***********************************************************************
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenAuth.Domain.Service
{
/// <summary>
/// 领域服务
/// <para>用户授权服务</para>
/// </summary>
public class AuthoriseService
{
private IUserRepository _repository;
private IModuleRepository _moduleRepository;
private IRelevanceRepository _relevanceRepository;
private IRepository<ModuleElement> _moduleElementRepository;
private IResourceRepository _resourceRepository;
private IOrgRepository _orgRepository;
private User _user;
private List<Module> _modules; //用户可访问的模块
private List<ModuleElement> _moduleElements; //用户可访问的菜单
private List<Resource> _resources; //用户可访问的资源
private List<Org> _orgs; //用户可访问的机构
public AuthoriseService(IUserRepository repository,
IModuleRepository moduleRepository,
IRelevanceRepository relevanceRepository,
IRepository<ModuleElement> moduleElementRepository,
IResourceRepository resourceRepository,
IOrgRepository orgRepository)
{
_repository = repository;
_moduleRepository = moduleRepository;
_relevanceRepository = relevanceRepository;
_moduleElementRepository = moduleElementRepository;
_resourceRepository = resourceRepository;
_orgRepository = orgRepository;
}
public List<Module> Modules
{
get { return _modules; }
}
public List<ModuleElement> ModuleElements
{
get { return _moduleElements; }
}
public List<Resource> Resources
{
get { return _resources; }
}
public List<Org> Orgs
{
get { return _orgs; }
}
public User User
{
get { return _user; }
}
public void Check(string userName, string password)
{
var _user = _repository.FindSingle(u => u.Account == userName);
if (_user == null)
{
throw new Exception("用户帐号不存在");
}
_user.CheckPassword(password);
}
/// <summary>
/// 设置开发者账号
/// </summary>
public void SetSysUser()
{
_user = new User
{
Account = "System"
};
}
public void GetUserAccessed(string name)
{
if (name == "System")
{
_modules = _moduleRepository.Find(null).ToList();
_moduleElements = _moduleElementRepository.Find(null).ToList();
_resources = _resourceRepository.Find(null).OrderBy(u => u.SortNo).ToList();
_orgs = _orgRepository.Find(null).OrderBy(u => u.SortNo).ToList();
}
else
{
//用户角色
var userRoleIds = _relevanceRepository.Find(u => u.FirstId == _user.Id && u.Key == "UserRole").Select(u => u.SecondId).ToList();
//用户角色与自己分配到的模块ID
var moduleIds = _relevanceRepository.Find(
u =>
(u.FirstId == _user.Id && u.Key == "UserModule") ||
(u.Key == "RoleModule" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
//用户角色与自己分配到的菜单ID
var elementIds = _relevanceRepository.Find(
u =>
(u.FirstId == _user.Id && u.Key == "UserElement") ||
(u.Key == "RoleElement" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
//得出最终用户拥有的模块
_modules = _moduleRepository.Find(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo).ToList();
//模块菜单权限
_moduleElements = _moduleElementRepository.Find(u => elementIds.Contains(u.Id)).ToList();
//用户角色与自己分配到的资源ID
var resourceIds = _relevanceRepository.Find(
u =>
(u.FirstId == _user.Id && u.Key == "UserResource") ||
(u.Key == "RoleResource" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
_resources = _resourceRepository.Find(u => resourceIds.Contains(u.Id)).ToList();
//用户角色与自己分配到的机构ID
var orgids = _relevanceRepository.Find(
u =>
(u.FirstId == _user.Id && u.Key == "UserAccessedOrg") ||
(u.Key == "RoleAccessedOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
_orgs = _orgRepository.Find(u => orgids.Contains(u.Id)).ToList();
}
}
}
}

View File

@ -19,6 +19,7 @@ using OpenAuth.App;
using System.Reflection;
using System.Web.Mvc;
using OpenAuth.Domain.Interface;
using OpenAuth.Domain.Service;
using OpenAuth.Repository;
namespace OpenAuth.Mvc
@ -44,6 +45,7 @@ namespace OpenAuth.Mvc
builder.RegisterType<ResourceManagerApp>();
builder.RegisterType<StockManagerApp>();
builder.RegisterType<RevelanceManagerApp>();
builder.RegisterType<AuthoriseService>();
// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

View File

@ -24,6 +24,7 @@ using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using OpenAuth.App;
namespace OpenAuth.Mvc.Controllers
{
@ -33,8 +34,8 @@ namespace OpenAuth.Mvc.Controllers
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var loginUser = SessionHelper.GetSessionUser<LoginUserVM>();
if (loginUser == null)
var loginUser = AutofacExt.GetFromFac<LoginApp>().GetLoginUser();
if (!User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult("/Login/Index");
return;

View File

@ -25,7 +25,7 @@ namespace OpenAuth.Mvc.Controllers
[Anonymous]
public ActionResult Index()
{
var user = SessionHelper.GetSessionUser<LoginUserVM>();
var user = AutofacExt.GetFromFac<LoginApp>().GetLoginUser();
return View(user.Modules);
}

View File

@ -29,7 +29,7 @@ namespace OpenAuth.Mvc.Controllers
{
try
{
SessionHelper.AddSessionUser( _app.Login(username, password));
_app.Login(username, password);
return RedirectToAction("Index", "Home");
}
@ -51,19 +51,20 @@ namespace OpenAuth.Mvc.Controllers
{
try
{
SessionHelper.AddSessionUser(_app.LoginByDev());
_app.LoginByDev();
return RedirectToAction("Index", "Home");
}
catch (Exception e)
{
return View(e.Message);
return RedirectToAction("Index", "Login");
}
}
public ActionResult Logout()
{
SessionHelper.Clear();
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Login");
}
}

View File

@ -45,13 +45,13 @@ namespace OpenAuth.Mvc.Controllers
/// </summary>
public string LoadForTree()
{
var orgs = SessionHelper.GetSessionUser<LoginUserVM>().Modules;
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().Modules;
return JsonHelper.Instance.Serialize(orgs);
}
public string LoadModuleWithRoot()
{
var orgs = SessionHelper.GetSessionUser<LoginUserVM>().Modules.MapToList<ModuleView>();
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().Modules.MapToList<ModuleView>();
//添加根节点
orgs.Add(new Module
{

View File

@ -40,7 +40,7 @@ namespace OpenAuth.Mvc.Controllers
/// <returns>System.String.</returns>
public string LoadForTree()
{
var orgs = SessionHelper.GetSessionUser<LoginUserVM>().AccessedOrgs;
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().AccessedOrgs;
return JsonHelper.Instance.Serialize(orgs);
}
/// <summary>
@ -51,7 +51,7 @@ namespace OpenAuth.Mvc.Controllers
[Anonymous]
public string LoadOrg()
{
var orgs = SessionHelper.GetSessionUser<LoginUserVM>().AccessedOrgs.MapToList<Org>();
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().AccessedOrgs.MapToList<Org>();
//添加根节点
orgs.Add(new Org
{

View File

@ -37,7 +37,7 @@ namespace OpenAuth.Mvc.Controllers
{
var newmodel = new Stock();
model.CopyTo(newmodel);
newmodel.User = SessionHelper.GetSessionUser<LoginUserVM>().User.Account;
newmodel.User = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().User.Account;
_app.AddOrUpdate(newmodel);
}
catch (Exception ex)

View File

@ -296,6 +296,8 @@
<Content Include="Content\jqGrid\i18n\grid.locale-ua.js" />
<Content Include="Content\jqGrid\i18n\grid.locale-vi.js" />
<Content Include="Content\jqGrid\jquery.jqGrid.src.js" />
<Content Include="Content\js\purl.js" />
<Content Include="Content\js\util.js" />
<Content Include="Content\kindeditor_4.1.10\attached\test.txt" />
<Content Include="Content\kindeditor_4.1.10\editor-content.css" />
<Content Include="Content\kindeditor_4.1.10\kindeditor-all-min.js" />

View File

@ -18,6 +18,8 @@
<!--<add name="OpenAuthDBContext" connectionString="server=127.0.0.1;user id=root;persistsecurityinfo=True;database=openauth;password=123456" providerName="MySql.Data.MySqlClient" />-->
</connectionStrings>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
@ -58,10 +60,12 @@
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<authentication mode="None" />
<authentication mode="Forms" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<sessionState mode="InProc" timeout="60"/>
<customErrors mode="On" defaultRedirect="/Login/Index">
</customErrors>
</system.web>
<system.webServer>
<modules>

View File

@ -19,18 +19,7 @@ namespace OpenAuth.UnitTest
[TestMethod]
public void Test()
{
var login = new LoginApp(new UserRepository(),
new ModuleRepository(),
new RelevanceRepository(),
new BaseRepository<ModuleElement>(),
new ResourceRepository(),
new OrgRepository()
);
var user = login.Login("admin", "admin");
foreach (var module in user.Modules)
{
Console.WriteLine(module.Id +"\t" + module.Name);
}
}
}
}