// *********************************************************************** // 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 System; using System.Collections.Generic; using System.Linq; using OpenAuth.Domain; using OpenAuth.Domain.Interface; namespace OpenAuth.App { /// /// 领域服务 /// 用户授权服务 /// public class AuthoriseService { public IUnitWork _unitWork { get; set; } protected User _user; private List _userRoleIds; //用户角色GUID public List Modules { get { return GetModulesQuery().ToList(); } } public List Roles { get { return GetRolesQuery().ToList(); } } public List ModuleElements { get { return GetModuleElementsQuery().ToList(); } } public List Resources { get { return GetResourcesQuery().ToList(); } } public List Orgs { get { return GetOrgsQuery().ToList(); } } public User User { get { return _user; } set { _user = value; _userRoleIds = _unitWork.Find(u => u.FirstId == _user.Id && u.Key == "UserRole").Select(u => u.SecondId).ToList(); } } public void Check(string userName, string password) { var _user = _unitWork.FindSingle(u => u.Account == userName); if (_user == null) { throw new Exception("用户帐号不存在"); } _user.CheckPassword(password); } /// /// 用户可访问的机构 /// /// IQueryable<Org>. public virtual IQueryable GetOrgsQuery() { var orgids = _unitWork.Find( u => (u.FirstId == _user.Id && u.Key == "UserOrg") || (u.Key == "RoleOrg" && _userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId); return _unitWork.Find(u => orgids.Contains(u.Id)); } /// /// 获取用户可访问的资源 /// /// IQueryable<Resource>. public virtual IQueryable GetResourcesQuery() { var resourceIds = _unitWork.Find( u => (u.FirstId == _user.Id && u.Key == "UserResource") || (u.Key == "RoleResource" && _userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId); return _unitWork.Find(u => resourceIds.Contains(u.Id)); } /// /// 模块菜单权限 /// public virtual IQueryable GetModuleElementsQuery() { var elementIds = _unitWork.Find( u => (u.FirstId == _user.Id && u.Key == "UserElement") || (u.Key == "RoleElement" && _userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId); return _unitWork.Find(u => elementIds.Contains(u.Id)); } /// /// 得出最终用户拥有的模块 /// public virtual IQueryable GetModulesQuery() { var moduleIds = _unitWork.Find( u => (u.FirstId == _user.Id && u.Key == "UserModule") || (u.Key == "RoleModule" && _userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId); return _unitWork.Find(u => moduleIds.Contains(u.Id)).OrderBy(u => u.SortNo); } //用户角色 public virtual IQueryable GetRolesQuery() { return _unitWork.Find(u => _userRoleIds.Contains(u.Id)); } } }