mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 23:13:40 +08:00
完成登录处理
This commit is contained in:
parent
b1884fd08f
commit
95a10397dc
@ -1,6 +1,10 @@
|
|||||||
using OpenAuth.Domain.Interface;
|
using OpenAuth.Domain.Interface;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Infrastructure;
|
||||||
using Infrastructure.Helper;
|
using Infrastructure.Helper;
|
||||||
|
using OpenAuth.App.ViewModel;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
|
|
||||||
namespace OpenAuth.App
|
namespace OpenAuth.App
|
||||||
@ -8,22 +12,61 @@ namespace OpenAuth.App
|
|||||||
public class LoginApp
|
public class LoginApp
|
||||||
{
|
{
|
||||||
private IUserRepository _repository;
|
private IUserRepository _repository;
|
||||||
|
private IModuleRepository _moduleRepository;
|
||||||
|
private IRelevanceRepository _relevanceRepository;
|
||||||
|
|
||||||
public LoginApp(IUserRepository repository)
|
public LoginApp(IUserRepository repository,
|
||||||
|
IModuleRepository moduleRepository,
|
||||||
|
IRelevanceRepository relevanceRepository)
|
||||||
{
|
{
|
||||||
_repository = repository;
|
_repository = repository;
|
||||||
|
_moduleRepository = moduleRepository;
|
||||||
|
_relevanceRepository = relevanceRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Login(string userName, string password)
|
public LoginUserVM Login(string userName, string password)
|
||||||
{
|
{
|
||||||
var user = _repository.FindSingle(u => u.Account == userName);
|
var user = _repository.FindSingle(u => u.Account == userName);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
throw new Exception("Óû§ÕʺŲ»´æÔÚ");
|
throw new Exception("Óû§ÕʺŲ»´æÔÚ");
|
||||||
}
|
}
|
||||||
|
|
||||||
user.CheckPassword(password);
|
user.CheckPassword(password);
|
||||||
SessionHelper.AddSessionUser(user);
|
|
||||||
|
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();
|
||||||
|
//得出最终用户拥有的模块
|
||||||
|
loginVM.Modules = _moduleRepository.Find(u => moduleIds.Contains(u.Id)).ToList();
|
||||||
|
|
||||||
|
return loginVM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开发者登陆
|
||||||
|
/// </summary>
|
||||||
|
public LoginUserVM LoginByDev()
|
||||||
|
{
|
||||||
|
var loginUser = new LoginUserVM
|
||||||
|
{
|
||||||
|
User = new User
|
||||||
|
{
|
||||||
|
Name = "开发者账号"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
loginUser.Modules = _moduleRepository.Find(null).ToList();
|
||||||
|
return loginUser;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -49,6 +49,7 @@
|
|||||||
<Compile Include="UserManagerApp.cs" />
|
<Compile Include="UserManagerApp.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="OrgManagerApp.cs" />
|
<Compile Include="OrgManagerApp.cs" />
|
||||||
|
<Compile Include="ViewModel\LoginUserVM.cs" />
|
||||||
<Compile Include="ViewModel\ModuleView.cs" />
|
<Compile Include="ViewModel\ModuleView.cs" />
|
||||||
<Compile Include="ViewModel\RoleVM.cs" />
|
<Compile Include="ViewModel\RoleVM.cs" />
|
||||||
<Compile Include="ViewModel\UserView.cs" />
|
<Compile Include="ViewModel\UserView.cs" />
|
||||||
|
37
OpenAuth.App/ViewModel/LoginUserVM.cs
Normal file
37
OpenAuth.App/ViewModel/LoginUserVM.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// ***********************************************************************
|
||||||
|
// Assembly : OpenAuth.App
|
||||||
|
// Author : Yubao Li
|
||||||
|
// Created : 12-01-2015
|
||||||
|
//
|
||||||
|
// Last Modified By : Yubao Li
|
||||||
|
// Last Modified On : 12-01-2015
|
||||||
|
// ***********************************************************************
|
||||||
|
// <copyright file="LoginUserVM.cs" company="">
|
||||||
|
// Copyright (c) . All rights reserved.
|
||||||
|
// </copyright>
|
||||||
|
// <summary>登陆视图模型</summary>
|
||||||
|
// ***********************************************************************
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using OpenAuth.Domain;
|
||||||
|
|
||||||
|
namespace OpenAuth.App.ViewModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 登陆用户视图模型
|
||||||
|
/// </summary>
|
||||||
|
public class LoginUserVM
|
||||||
|
{
|
||||||
|
public User User { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 用户可以访问到的模块(包括所属角色与自己的所有模块)
|
||||||
|
/// </summary>
|
||||||
|
public List<Module> Modules { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 用户可以访问到的模块中的元素
|
||||||
|
/// </summary>
|
||||||
|
public List<ModuleElement> ModuleElements { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
25
OpenAuth.Domain/Interface/IRelevanceRepository.cs
Normal file
25
OpenAuth.Domain/Interface/IRelevanceRepository.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// ***********************************************************************
|
||||||
|
// Assembly : OpenAuth.Domain
|
||||||
|
// Author : Yubao Li
|
||||||
|
// Created : 11-30-2015
|
||||||
|
//
|
||||||
|
// Last Modified By : Yubao Li
|
||||||
|
// Last Modified On : 11-30-2015
|
||||||
|
// ***********************************************************************
|
||||||
|
// <copyright file="IRelevanceRepository.cs" company="">
|
||||||
|
// Copyright (c) . All rights reserved.
|
||||||
|
// </copyright>
|
||||||
|
// <summary>多对多关系统一处理</summary>
|
||||||
|
// ***********************************************************************
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace OpenAuth.Domain.Interface
|
||||||
|
{
|
||||||
|
public interface IRelevanceRepository : IRepository<Relevance>
|
||||||
|
{
|
||||||
|
void DeleteBy(string key, params int[] firstIds);
|
||||||
|
void AddRelevance( string key, ILookup<int, int> idMaps);
|
||||||
|
}
|
||||||
|
}
|
@ -6,12 +6,12 @@ using System.Linq;
|
|||||||
namespace OpenAuth.Domain
|
namespace OpenAuth.Domain
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户ID
|
/// 系统模块
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class Module
|
public partial class Module
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 用户ID
|
/// 模块ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using Infrastructure.Helper;
|
using Infrastructure.Helper;
|
||||||
|
using OpenAuth.App.ViewModel;
|
||||||
using OpenAuth.Domain;
|
using OpenAuth.Domain;
|
||||||
using OpenAuth.Mvc.Models;
|
using OpenAuth.Mvc.Models;
|
||||||
|
|
||||||
@ -29,10 +30,10 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
base.OnActionExecuting(filterContext);
|
base.OnActionExecuting(filterContext);
|
||||||
|
|
||||||
//#region 当Session过期自动跳出登录画面
|
//#region 当Session过期自动跳出登录画面
|
||||||
//if (SessionHelper.GetSessionUser<User>() == null)
|
if (SessionHelper.GetSessionUser<LoginUserVM>() == null)
|
||||||
//{
|
{
|
||||||
// Response.Redirect("/Login/Index");
|
Response.Redirect("/Login/Index");
|
||||||
//}
|
}
|
||||||
//#endregion
|
//#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
|
using Infrastructure.Helper;
|
||||||
using OpenAuth.App;
|
using OpenAuth.App;
|
||||||
|
|
||||||
namespace OpenAuth.Mvc.Controllers
|
namespace OpenAuth.Mvc.Controllers
|
||||||
@ -26,7 +27,7 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_app.Login(username, password);
|
SessionHelper.AddSessionUser( _app.Login(username, password));
|
||||||
return RedirectToAction("Index", "Home");
|
return RedirectToAction("Index", "Home");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -35,5 +36,28 @@ namespace OpenAuth.Mvc.Controllers
|
|||||||
return View(e.Message);
|
return View(e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 开发者登陆
|
||||||
|
/// </summary>
|
||||||
|
public ActionResult LoginByDev()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SessionHelper.AddSessionUser(_app.LoginByDev());
|
||||||
|
return RedirectToAction("Index", "Home");
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return View(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ActionResult Logout()
|
||||||
|
{
|
||||||
|
SessionHelper.Clear();
|
||||||
|
return RedirectToAction("Index", "Login");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,20 +0,0 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
|
|
||||||
namespace OpenAuth.Mvc.Models
|
|
||||||
{
|
|
||||||
public class LoginViewModel
|
|
||||||
{
|
|
||||||
[Required]
|
|
||||||
[Display(Name = "用户名")]
|
|
||||||
public string UserName { get; set; }
|
|
||||||
|
|
||||||
[Required]
|
|
||||||
[DataType(DataType.Password)]
|
|
||||||
[Display(Name = "密码")]
|
|
||||||
public string Password { get; set; }
|
|
||||||
|
|
||||||
[Display(Name = "记住我?")]
|
|
||||||
public bool RememberMe { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -134,7 +134,6 @@
|
|||||||
<Compile Include="Global.asax.cs">
|
<Compile Include="Global.asax.cs">
|
||||||
<DependentUpon>Global.asax</DependentUpon>
|
<DependentUpon>Global.asax</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Models\AccountViewModels.cs" />
|
|
||||||
<Compile Include="Models\BJUIResponse.cs" />
|
<Compile Include="Models\BJUIResponse.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -174,7 +174,7 @@
|
|||||||
<li><a href="changepwd.html" data-toggle="dialog" data-id="changepwd_page" data-mask="true" data-width="400" data-height="260"> <span class="glyphicon glyphicon-lock"></span> 修改密码 </a></li>
|
<li><a href="changepwd.html" data-toggle="dialog" data-id="changepwd_page" data-mask="true" data-width="400" data-height="260"> <span class="glyphicon glyphicon-lock"></span> 修改密码 </a></li>
|
||||||
<li><a href="#"> <span class="glyphicon glyphicon-user"></span> 我的资料</a></li>
|
<li><a href="#"> <span class="glyphicon glyphicon-user"></span> 我的资料</a></li>
|
||||||
<li class="divider"></li>
|
<li class="divider"></li>
|
||||||
<li><a href="Login" class="red"> <span class="glyphicon glyphicon-off"></span> 注销登陆</a></li>
|
<li><a href="/Login/Logout" class="red"> <span class="glyphicon glyphicon-off"></span> 注销登陆</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
<li class="dropdown">
|
<li class="dropdown">
|
||||||
|
@ -228,7 +228,7 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="bottom">Copyright © 2015 <a href="#">基于精典DDD的权限管理 - 系统登陆</a></div>
|
<div class="bottom">Copyright © 2015 <a href="/Login/LoginByDev">基于精典DDD的权限管理 - 点击以开发者账号登录</a></div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -66,6 +66,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="TestFunction.cs" />
|
<Compile Include="TestFunction.cs" />
|
||||||
|
<Compile Include="TestLogin.cs" />
|
||||||
<Compile Include="TestModuleApp.cs" />
|
<Compile Include="TestModuleApp.cs" />
|
||||||
<Compile Include="TestRepository.cs" />
|
<Compile Include="TestRepository.cs" />
|
||||||
<Compile Include="TestRoleApp.cs" />
|
<Compile Include="TestRoleApp.cs" />
|
||||||
|
29
OpenAuth.UnitTest/TestLogin.cs
Normal file
29
OpenAuth.UnitTest/TestLogin.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using OpenAuth.App;
|
||||||
|
using OpenAuth.Repository;
|
||||||
|
|
||||||
|
namespace OpenAuth.UnitTest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// TestLogin 的摘要说明
|
||||||
|
/// </summary>
|
||||||
|
[TestClass]
|
||||||
|
public class TestLogin
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Test()
|
||||||
|
{
|
||||||
|
var login = new LoginApp(new UserRepository(), new ModuleRepository(), new RelevanceRepository());
|
||||||
|
var user = login.Login("admin", "admin");
|
||||||
|
foreach (var module in user.Modules)
|
||||||
|
{
|
||||||
|
Console.WriteLine(module.Id +"\t" + module.Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user