调整部分代码结构

This commit is contained in:
yubaolee 2016-07-19 11:44:48 +08:00
parent d66fd4b25e
commit 3dea7d00f0
20 changed files with 207 additions and 186 deletions

View File

@ -63,6 +63,11 @@ namespace Infrastructure
{
right = Expression.Constant(filterObj.Value.Equals("1"));
}
else if (property.PropertyType == typeof(Guid?))
{
left = Expression.Property(left, "Value");
right = Expression.Constant(Guid.Parse(filterObj.Value));
}
else
{
throw new Exception("暂不能解析该Key的类型");

View File

@ -1,35 +1,27 @@
using System.Linq;
using System.Web;
using Infrastructure;
using OpenAuth.App.ViewModel;
using OpenAuth.App.SSO;
using OpenAuth.Domain.Service;
namespace OpenAuth.App
{
public class LoginApp
/// <summary>
/// 加载用户所有可访问的资源/机构/模块
/// <para>李玉宝新增于2016-07-19 10:53:30</para>
/// </summary>
public class AuthorizeApp
{
private AuthoriseService _service;
private readonly AuthoriseService _service;
public LoginApp(AuthoriseService service)
public AuthorizeApp(AuthoriseService service)
{
_service = service;
}
public LoginUserVM GetLoginUser()
public UserWithAccessedCtrls GetAccessedControls(string username)
{
if (!AuthUtil.CheckLogin())
{
throw new HttpException(401,"未登录");
}
return AuthUtil.GetCurrentUser();
}
public LoginUserVM GetLoginUser(string username)
{
_service.GetUserAccessed(username);
var user = new LoginUserVM
_service.LoadAuthControls(username);
var user = new UserWithAccessedCtrls
{
User = _service.User,
AccessedOrgs = _service.Orgs,

View File

@ -73,7 +73,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CategoryManagerApp.cs" />
<Compile Include="LoginApp.cs" />
<Compile Include="AuthorizeApp.cs" />
<Compile Include="ModuleElementManagerApp.cs" />
<Compile Include="ModuleManagerApp.cs" />
<Compile Include="ResourceManagerApp.cs" />
@ -94,7 +94,7 @@
<Compile Include="UserManagerApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="OrgManagerApp.cs" />
<Compile Include="ViewModel\LoginUserVM.cs" />
<Compile Include="ViewModel\UserWithAccessedCtrls.cs" />
<Compile Include="ViewModel\ModuleElementVM.cs" />
<Compile Include="ViewModel\ModuleView.cs" />
<Compile Include="ViewModel\RoleVM.cs" />

View File

@ -74,14 +74,14 @@ namespace OpenAuth.App.SSO
/// </summary>
/// <param name="remark">The remark.</param>
/// <returns>LoginUserVM.</returns>
public static LoginUserVM GetCurrentUser(string remark = "")
public static UserWithAccessedCtrls GetCurrentUser(string remark = "")
{
var requestUri = String.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
try
{
var value = _helper.Get<LoginUserVM>(null, requestUri);
var value = _helper.Get<UserWithAccessedCtrls>(null, requestUri);
return value;
}
catch (Exception ex)

View File

@ -39,7 +39,7 @@ namespace OpenAuth.App.SSO
if (string.IsNullOrEmpty(token))
{
//直接登录
filterContext.Result = SsoLoginResult(cookieSessionUserName);
filterContext.Result = LoginResult(cookieSessionUserName);
}
else
{
@ -47,14 +47,14 @@ namespace OpenAuth.App.SSO
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
{
//会话丢失,跳转到登录页面
filterContext.Result = SsoLoginResult(cookieSessionUserName);
filterContext.Result = LoginResult(cookieSessionUserName);
}
}
base.OnActionExecuting(filterContext);
}
private static ActionResult SsoLoginResult(string username)
private static ActionResult LoginResult(string username)
{
//跳转到SSO站点登陆
//return new RedirectResult(string.Format("{0}/sso/login?appkey={1}&username={2}",

View File

@ -57,7 +57,8 @@ namespace OpenAuth.App.SSO
if (string.IsNullOrEmpty(token))
{
//直接登录
filterContext.Result = SsoLoginResult(cookieSessionUserName);
filterContext.Result = LoginResult(cookieSessionUserName);
return;
}
else
{
@ -65,14 +66,15 @@ namespace OpenAuth.App.SSO
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
{
//会话丢失,跳转到登录页面
filterContext.Result = SsoLoginResult(cookieSessionUserName);
filterContext.Result = LoginResult(cookieSessionUserName);
return;
}
}
base.OnActionExecuting(filterContext);
}
private static ActionResult SsoLoginResult(string username)
private static ActionResult LoginResult(string username)
{
//跳转到SSO站点登陆
//return new RedirectResult(string.Format("{0}/sso/login?appkey={1}&username={2}",

View File

@ -9,7 +9,7 @@
// <copyright file="LoginUserVM.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>登陆视图模型</summary>
// <summary>用户及权限视图模型</summary>
// ***********************************************************************
using System.Collections.Generic;
@ -18,9 +18,11 @@ using OpenAuth.Domain;
namespace OpenAuth.App.ViewModel
{
/// <summary>
/// 登陆用户视图模型
/// 视图模型
/// <para>包括用户及用户可访问的机构/资源/模块</para>
/// <para>李玉宝修改于2016-07-19 10:57:31</para>
/// </summary>
public class LoginUserVM
public class UserWithAccessedCtrls
{
public User User { get; set; }
/// <summary>

View File

@ -70,33 +70,39 @@ namespace OpenAuth.Domain.Service
_user.CheckPassword(password);
}
/// <summary>
/// 设置开发者账号
/// </summary>
public void SetSysUser()
{
_user = new User
{
Account = "System"
};
}
public void GetUserAccessed(string name)
/// <summary>
/// 加载用户可访问的所有机构/资源/菜单
/// <para>李玉宝于2016-07-19 10:32:19</para>
/// </summary>
/// <param name="name">The name.</param>
public void LoadAuthControls(string name)
{
if (name == "System")
{
_modules = _unitWork.Find<Module>(null).ToList();
_moduleElements = _unitWork.Find<ModuleElement>(null).ToList();
_resources = _unitWork.Find<Resource>(null).OrderBy(u => u.SortNo).ToList();
_orgs = _unitWork.Find<Org>(null).OrderBy(u => u.SortNo).ToList();
_user = new User{Account = "System"};
LoadForSystem();
}
else
{
_user = _unitWork.FindSingle<User>(u => u.Account == name);
if (_user != null)
{
LoadForUser();
}
}
}
/// <summary>
/// 加载用户权限
/// <para>李玉宝于2016-07-19 10:20:16</para>
/// </summary>
/// <param name="name">The name.</param>
private void LoadForUser()
{
//用户角色
var userRoleIds = _unitWork.Find<Relevance>(u => u.FirstId == _user.Id && u.Key == "UserRole").Select(u => u.SecondId).ToList();
var userRoleIds =
_unitWork.Find<Relevance>(u => u.FirstId == _user.Id && u.Key == "UserRole").Select(u => u.SecondId).ToList();
//用户角色与自己分配到的模块ID
var moduleIds = _unitWork.Find<Relevance>(
@ -128,6 +134,19 @@ namespace OpenAuth.Domain.Service
(u.Key == "RoleAccessedOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId);
_orgs = _unitWork.Find<Org>(u => orgids.Contains(u.Id)).ToList();
}
/// <summary>
/// 加载系统管理员权限
/// <para>李玉宝于2016-07-19 10:19:31</para>
/// </summary>
private void LoadForSystem()
{
_modules = _unitWork.Find<Module>(null).ToList();
_moduleElements = _unitWork.Find<ModuleElement>(null).ToList();
_resources = _unitWork.Find<Resource>(null).OrderBy(u => u.SortNo).ToList();
_orgs = _unitWork.Find<Org>(null).OrderBy(u => u.SortNo).ToList();
}
}
}

View File

@ -47,7 +47,7 @@ namespace OpenAuth.Domain.Service
public IEnumerable<ModuleElement> LoadByModuleId(string loginuser, int id)
{
_authoriseService.GetUserAccessed(loginuser);
_authoriseService.LoadAuthControls(loginuser);
if (_authoriseService.ModuleElements.Count == 0) //用户没有任何资源
{
return new List<ModuleElement>();
@ -70,7 +70,7 @@ namespace OpenAuth.Domain.Service
public List<dynamic> LoadWithAccess(string username, string accessType, int firstId, int moduleId)
{
var listVms = new List<dynamic>();
_authoriseService.GetUserAccessed(username);
_authoriseService.LoadAuthControls(username);
if (_authoriseService.ModuleElements.Count == 0) //用户没有任何资源
{
return listVms;

View File

@ -40,7 +40,7 @@ namespace OpenAuth.Domain.Service
public dynamic Load(string loginuser, int parentId, int pageindex, int pagesize)
{
_authoriseService.GetUserAccessed(loginuser);
_authoriseService.LoadAuthControls(loginuser);
if (_authoriseService.Modules.Count == 0) //用户不能访问任何模块
{
return new

View File

@ -48,7 +48,7 @@ namespace OpenAuth.Domain.Service
/// </summary>
public dynamic Load(string username, int categoryId, int pageindex, int pagesize)
{
_authoriseService.GetUserAccessed(username);
_authoriseService.LoadAuthControls(username);
if (_authoriseService.Resources.Count == 0) //用户没有任何资源
{
return new
@ -122,7 +122,7 @@ namespace OpenAuth.Domain.Service
public List<dynamic> LoadWithAccess(string username, string accessType, int firstId, int cId)
{
var listVms = new List<dynamic>();
_authoriseService.GetUserAccessed(username);
_authoriseService.LoadAuthControls(username);
if (_authoriseService.Resources.Count == 0) //用户没有任何资源
{
return listVms;

View File

@ -29,7 +29,7 @@ namespace OpenAuth.Domain.Service
public dynamic Load(string username, int orgId, int pageindex, int pagesize)
{
_authoriseService.GetUserAccessed(username);
_authoriseService.LoadAuthControls(username);
if (_authoriseService.Orgs.Count == 0) //用户没有任何可见机构
{
return new

View File

@ -38,7 +38,7 @@ namespace OpenAuth.Mvc
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
//注册app层
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (LoginApp)));
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (UserManagerApp)));
//注册领域服务
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AuthoriseService)))

View File

@ -1,21 +1,15 @@
// ***********************************************************************
// Assembly : OpenAuth.Mvc
// Author : Administrator
// Created : 09-22-2015
// Author : yubaolee
// Created : 07-11-2016
//
// Last Modified By : Administrator
// Last Modified On : 09-22-2015
// ***********************************************************************
// <copyright file="BaseController.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>
// 基础控制器
// 继承该控制器可以防止未登录查看
// 继承该控制器后如果想访问控制器中存在但模块配置里面没有的ActionHome/Git请使用AnonymousAttribute
// </summary>
// Last Modified By : yubaolee
// Last Modified On : 07-19-2016
// Contact : www.cnblogs.com/yubaolee
// File: BaseController.cs
// ***********************************************************************
using OpenAuth.Mvc.Models;
using System;
using System.Configuration;
@ -23,11 +17,15 @@ using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using OpenAuth.App;
using OpenAuth.App.SSO;
namespace OpenAuth.Mvc.Controllers
{
/// <summary>
/// 基础控制器
/// <para>用于控制登录用户是否有权限访问指定的Action</para>
/// <para>李玉宝新增于2016-07-19 11:12:09</para>
/// </summary>
public class BaseController : SSOController
{
protected BjuiResponse BjuiResponse = new BjuiResponse();
@ -36,7 +34,7 @@ namespace OpenAuth.Mvc.Controllers
{
base.OnActionExecuting(filterContext);
var loginUser = AutofacExt.GetFromFac<LoginApp>().GetLoginUser();
if (!AuthUtil.CheckLogin()) return;
var controllername = Request.RequestContext.RouteData.Values["controller"].ToString().ToLower();
var actionname = filterContext.ActionDescriptor.ActionName.ToLower();
@ -46,8 +44,8 @@ namespace OpenAuth.Mvc.Controllers
throw new Exception("未能找到Action");
var authorize = function.GetCustomAttribute(typeof(AuthenticateAttribute));
var module = loginUser.Modules.FirstOrDefault(u => u.Url.ToLower().Contains(controllername));
//当前登录用户没有Action记录&&Action没有anonymous标识
var module = AuthUtil.GetCurrentUser().Modules.FirstOrDefault(u => u.Url.ToLower().Contains(controllername));
//当前登录用户没有Action记录&&Action有authenticate标识
if (authorize != null && module == null)
{
filterContext.Result = new RedirectResult("/Login/Index");

View File

@ -1,6 +1,7 @@
using System.Web.Mvc;
using Infrastructure;
using OpenAuth.App;
using OpenAuth.App.SSO;
using OpenAuth.App.ViewModel;
using OpenAuth.Mvc.Models;
@ -17,7 +18,7 @@ namespace OpenAuth.Mvc.Controllers
public ActionResult Index()
{
var user = AutofacExt.GetFromFac<LoginApp>().GetLoginUser();
var user = AuthUtil.GetCurrentUser();
return View(user.Modules);
}

View File

@ -4,6 +4,7 @@ using OpenAuth.Domain;
using System;
using System.Linq;
using System.Web.Mvc;
using OpenAuth.App.SSO;
using OpenAuth.App.ViewModel;
using OpenAuth.Mvc.Models;
@ -45,13 +46,13 @@ namespace OpenAuth.Mvc.Controllers
/// </summary>
public string LoadForTree()
{
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().Modules;
var orgs = AuthUtil.GetCurrentUser().Modules;
return JsonHelper.Instance.Serialize(orgs);
}
public string LoadModuleWithRoot()
{
var orgs = AutofacExt.GetFromFac<LoginApp>().GetLoginUser().Modules.MapToList<ModuleView>();
var orgs = AuthUtil.GetCurrentUser().Modules.MapToList<ModuleView>();
return JsonHelper.Instance.Serialize(orgs);
}

View File

@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using OpenAuth.App.SSO;
using OpenAuth.App.ViewModel;
namespace OpenAuth.Mvc.Controllers
@ -35,7 +36,7 @@ namespace OpenAuth.Mvc.Controllers
public string LoadOrg()
{
return JsonHelper.Instance.Serialize(AutofacExt.GetFromFac<LoginApp>().GetLoginUser().AccessedOrgs);
return JsonHelper.Instance.Serialize(AuthUtil.GetCurrentUser().AccessedOrgs);
}
public string LoadForUser(int firstId)

View File

@ -15,7 +15,7 @@ namespace OpenAuth.UnitTest
public void TestMethod1()
{
AuthoriseService _service = new AuthoriseService(new UnitWork() );
_service.GetUserAccessed("test");
_service.LoadAuthControls("test");
var orgs = _service.Orgs;
}

View File

@ -23,10 +23,10 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers
/// </summary>
public class CheckController : Controller
{
private LoginApp _app;
private AuthorizeApp _app;
public CheckController()
{
_app = AutofacExt.GetFromFac<LoginApp>();
_app = AutofacExt.GetFromFac<AuthorizeApp>();
}
public bool GetStatus(string token = "", string requestid = "")
@ -44,7 +44,7 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers
string userName = GetUserName(token, requestid);
if (!string.IsNullOrEmpty(userName))
{
return JsonHelper.Instance.Serialize(_app.GetLoginUser(userName));
return JsonHelper.Instance.Serialize(_app.GetAccessedControls(userName));
}
return string.Empty;

View File

@ -38,7 +38,7 @@ namespace OpenAuth.WebApi
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
//注册app层
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (LoginApp)));
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (AuthorizeApp)));
//注册领域服务
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(AuthoriseService)))