mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-05-04 20:58:01 +08:00
Merge branch 'master' of https://git.oschina.net/yubaolee/OpenAuth.Net
This commit is contained in:
commit
4f77ceaac2
@ -13,15 +13,68 @@
|
|||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
namespace Infrastructure
|
namespace Infrastructure
|
||||||
{
|
{
|
||||||
public class AutoMapperExt
|
public static class AutoMapperExt
|
||||||
{
|
{
|
||||||
public static TResult ConvertTo<T, TResult>(T source)
|
/// <summary>
|
||||||
|
/// 类型映射
|
||||||
|
/// </summary>
|
||||||
|
public static T MapTo<T>(this object obj)
|
||||||
{
|
{
|
||||||
var mapper = Mapper.CreateMap<T, TResult>();
|
if (obj == null) return default(T);
|
||||||
return Mapper.Map<TResult>(source);
|
Mapper.CreateMap(obj.GetType(), typeof(T));
|
||||||
|
return Mapper.Map<T>(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 集合列表类型映射
|
||||||
|
/// </summary>
|
||||||
|
public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
|
||||||
|
{
|
||||||
|
foreach (var first in source)
|
||||||
|
{
|
||||||
|
var type = first.GetType();
|
||||||
|
Mapper.CreateMap(type, typeof(TDestination));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Mapper.Map<List<TDestination>>(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 集合列表类型映射
|
||||||
|
/// </summary>
|
||||||
|
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
|
||||||
|
{
|
||||||
|
//IEnumerable<T> 类型需要创建元素的映射
|
||||||
|
Mapper.CreateMap<TSource, TDestination>();
|
||||||
|
return Mapper.Map<List<TDestination>>(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 类型映射
|
||||||
|
/// </summary>
|
||||||
|
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
|
||||||
|
where TSource : class
|
||||||
|
where TDestination : class
|
||||||
|
{
|
||||||
|
if (source == null) return destination;
|
||||||
|
Mapper.CreateMap<TSource, TDestination>();
|
||||||
|
return Mapper.Map(source, destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DataReader映射
|
||||||
|
/// </summary>
|
||||||
|
public static IEnumerable<T> DataReaderMapTo<T>(this IDataReader reader)
|
||||||
|
{
|
||||||
|
Mapper.Reset();
|
||||||
|
Mapper.CreateMap<IDataReader, IEnumerable<T>>();
|
||||||
|
return Mapper.Map<IDataReader, IEnumerable<T>>(reader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,14 +14,17 @@ namespace OpenAuth.App
|
|||||||
private IUserRepository _repository;
|
private IUserRepository _repository;
|
||||||
private IModuleRepository _moduleRepository;
|
private IModuleRepository _moduleRepository;
|
||||||
private IRelevanceRepository _relevanceRepository;
|
private IRelevanceRepository _relevanceRepository;
|
||||||
|
private IRepository<ModuleElement> _moduleElementRepository;
|
||||||
|
|
||||||
public LoginApp(IUserRepository repository,
|
public LoginApp(IUserRepository repository,
|
||||||
IModuleRepository moduleRepository,
|
IModuleRepository moduleRepository,
|
||||||
IRelevanceRepository relevanceRepository)
|
IRelevanceRepository relevanceRepository,
|
||||||
|
IRepository<ModuleElement> moduleElementRepository )
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
_moduleRepository = moduleRepository;
|
_moduleRepository = moduleRepository;
|
||||||
_relevanceRepository = relevanceRepository;
|
_relevanceRepository = relevanceRepository;
|
||||||
|
_moduleElementRepository = moduleElementRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoginUserVM Login(string userName, string password)
|
public LoginUserVM Login(string userName, string password)
|
||||||
@ -48,7 +51,7 @@ namespace OpenAuth.App
|
|||||||
(u.FirstId == user.Id && u.Key == "UserModule") ||
|
(u.FirstId == user.Id && u.Key == "UserModule") ||
|
||||||
(u.Key == "RoleModule" && userRoleIds.Contains(u.FirstId))).Select(u =>u.SecondId).ToList();
|
(u.Key == "RoleModule" && userRoleIds.Contains(u.FirstId))).Select(u =>u.SecondId).ToList();
|
||||||
//得出最终用户拥有的模块
|
//得出最终用户拥有的模块
|
||||||
loginVM.Modules = _moduleRepository.Find(u => moduleIds.Contains(u.Id)).ToList();
|
loginVM.Modules = _moduleRepository.Find(u => moduleIds.Contains(u.Id)).MapToList<ModuleView>();
|
||||||
|
|
||||||
return loginVM;
|
return loginVM;
|
||||||
}
|
}
|
||||||
@ -65,7 +68,12 @@ namespace OpenAuth.App
|
|||||||
Name = "开发者账号"
|
Name = "开发者账号"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
loginUser.Modules = _moduleRepository.Find(null).ToList();
|
loginUser.Modules = _moduleRepository.Find(null).MapToList<ModuleView>();
|
||||||
|
//Ä£¿é°üº¬µÄ²Ëµ¥
|
||||||
|
foreach (var module in loginUser.Modules)
|
||||||
|
{
|
||||||
|
module.Elements = _moduleElementRepository.Find(u => u.ModuleId == module.Id).ToList();
|
||||||
|
}
|
||||||
return loginUser;
|
return loginUser;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,12 +26,7 @@ namespace OpenAuth.App.ViewModel
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户可以访问到的模块(包括所属角色与自己的所有模块)
|
/// 用户可以访问到的模块(包括所属角色与自己的所有模块)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Module> Modules { get; set; }
|
public List<ModuleView> Modules { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 用户可以访问到的模块中的元素
|
|
||||||
/// </summary>
|
|
||||||
public List<ModuleElement> ModuleElements { get; set; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using Infrastructure;
|
||||||
using Infrastructure;
|
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenAuth.App.ViewModel
|
namespace OpenAuth.App.ViewModel
|
||||||
{
|
{
|
||||||
@ -39,17 +39,21 @@ namespace OpenAuth.App.ViewModel
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 子节点
|
/// 子节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<ModuleView> Childern = new List<ModuleView>();
|
public List<ModuleView> Childern = new List<ModuleView>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模块中的元素
|
||||||
|
/// </summary>
|
||||||
|
public List<ModuleElement> Elements = new List<ModuleElement>();
|
||||||
|
|
||||||
public static implicit operator ModuleView(Module module)
|
public static implicit operator ModuleView(Module module)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<Module, ModuleView>(module);
|
return module.MapTo<ModuleView>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator Module(ModuleView view)
|
public static implicit operator Module(ModuleView view)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<ModuleView, Module>(view);
|
return view.MapTo<Module>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -52,12 +52,12 @@ namespace OpenAuth.App.ViewModel
|
|||||||
|
|
||||||
public static implicit operator RoleVM(Role role)
|
public static implicit operator RoleVM(Role role)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<Role,RoleVM>(role);
|
return role.MapTo<RoleVM>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator Role(RoleVM rolevm)
|
public static implicit operator Role(RoleVM rolevm)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<RoleVM, Role>(rolevm);
|
return rolevm.MapTo<Role>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -71,12 +71,12 @@ namespace OpenAuth.App.ViewModel
|
|||||||
|
|
||||||
public static implicit operator UserView(User user)
|
public static implicit operator UserView(User user)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<User, UserView>(user);
|
return user.MapTo<UserView>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator User(UserView view)
|
public static implicit operator User(UserView view)
|
||||||
{
|
{
|
||||||
return AutoMapperExt.ConvertTo<UserView, User>(view);
|
return view.MapTo<User>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,10 @@
|
|||||||
// <summary>基础控制器,设置权限</summary>
|
// <summary>基础控制器,设置权限</summary>
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using Infrastructure.Helper;
|
using Infrastructure.Helper;
|
||||||
using OpenAuth.App.ViewModel;
|
using OpenAuth.App.ViewModel;
|
||||||
using OpenAuth.Mvc.Models;
|
using OpenAuth.Mvc.Models;
|
||||||
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
|
||||||
namespace OpenAuth.Mvc.Controllers
|
namespace OpenAuth.Mvc.Controllers
|
||||||
@ -34,18 +33,22 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Request.Url != null)
|
string url = Request.Url.LocalPath;
|
||||||
|
if (url != "/"
|
||||||
|
&& !url.Contains("Main")
|
||||||
|
&& !url.Contains("Error")
|
||||||
|
&& !url.Contains("Git"))
|
||||||
{
|
{
|
||||||
string url = Request.Url.LocalPath;
|
var module = loginUser.Modules.FirstOrDefault(u => url.Contains(u.Url));
|
||||||
if(url !="/"
|
if (module == null)
|
||||||
&& !url.Contains("Main")
|
{
|
||||||
&& !url.Contains("Error")
|
filterContext.Result = new RedirectResult("/Login/Index");
|
||||||
&& !url.Contains("Git")
|
return;
|
||||||
&& !loginUser.Modules.Any(u => url.Contains(u.Url)))
|
}
|
||||||
{
|
else
|
||||||
filterContext.Result = new RedirectResult("/Login/Index");
|
{
|
||||||
return;
|
ViewBag.Module = module;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
base.OnActionExecuting(filterContext);
|
base.OnActionExecuting(filterContext);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
@model List<OpenAuth.Domain.Module>
|
@model List<OpenAuth.App.ViewModel.ModuleView>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="zh">
|
<html lang="zh">
|
||||||
<head>
|
<head>
|
||||||
|
@ -29,6 +29,15 @@
|
|||||||
$('#@_treeDetail').empty()
|
$('#@_treeDetail').empty()
|
||||||
.append('<table id="@_gridId" class="table table-bordered"></table>');
|
.append('<table id="@_gridId" class="table table-bordered"></table>');
|
||||||
|
|
||||||
|
@{
|
||||||
|
string strBtns = string.Empty;
|
||||||
|
foreach (var element in ViewBag.Module.Elements)
|
||||||
|
{
|
||||||
|
strBtns +="<button type=\"button\" class =\"btn btn-green\">"+element.Name +"</button>";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
grid = $(gridid).datagrid({
|
grid = $(gridid).datagrid({
|
||||||
gridTitle: '用户列表',
|
gridTitle: '用户列表',
|
||||||
showToolbar: true,
|
showToolbar: true,
|
||||||
@ -37,8 +46,8 @@
|
|||||||
toolbarCustom: '<a href="/UserManager/Add" class="btn btn-green" data-icon ="plus" ' +
|
toolbarCustom: '<a href="/UserManager/Add" class="btn btn-green" data-icon ="plus" ' +
|
||||||
'data-toggle="dialog" data-id="dialog-mask" data-mask="true" data-on-close="refreshGrid">添加</a>' +
|
'data-toggle="dialog" data-id="dialog-mask" data-mask="true" data-on-close="refreshGrid">添加</a>' +
|
||||||
'<button class=" btn-green" onclick="editOrg()" data-icon="pencil" type="button">编辑</button>' +
|
'<button class=" btn-green" onclick="editOrg()" data-icon="pencil" type="button">编辑</button>' +
|
||||||
'<button type="button" class="btn btn-green" onclick="openModuleAccess(this)">为用户分配模块</button>'+
|
'<button type="button" class="btn btn-green" onclick="openModuleAccess(this)">为用户分配模块</button>' +
|
||||||
'<button type="button" class ="btn btn-green" onclick="openRoleAccess(this)">为用户分配角色</button>',
|
'<button type="button" class ="btn btn-green" onclick="openRoleAccess(this)">为用户分配角色</button>' + '@strBtns',
|
||||||
columns: [
|
columns: [
|
||||||
{
|
{
|
||||||
name: 'Id',
|
name: 'Id',
|
||||||
|
@ -3,6 +3,7 @@ using System.Text;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using OpenAuth.App;
|
using OpenAuth.App;
|
||||||
|
using OpenAuth.Domain;
|
||||||
using OpenAuth.Repository;
|
using OpenAuth.Repository;
|
||||||
|
|
||||||
namespace OpenAuth.UnitTest
|
namespace OpenAuth.UnitTest
|
||||||
@ -18,7 +19,11 @@ namespace OpenAuth.UnitTest
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Test()
|
public void Test()
|
||||||
{
|
{
|
||||||
var login = new LoginApp(new UserRepository(), new ModuleRepository(), new RelevanceRepository());
|
var login = new LoginApp(new UserRepository(),
|
||||||
|
new ModuleRepository(),
|
||||||
|
new RelevanceRepository(),
|
||||||
|
new BaseRepository<ModuleElement>()
|
||||||
|
);
|
||||||
var user = login.Login("admin", "admin");
|
var user = login.Login("admin", "admin");
|
||||||
foreach (var module in user.Modules)
|
foreach (var module in user.Modules)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user