完成登录处理

This commit is contained in:
yubaolee 2015-12-01 17:30:24 +08:00
parent b1884fd08f
commit 95a10397dc
13 changed files with 174 additions and 34 deletions

View File

@ -1,6 +1,10 @@
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using Infrastructure.Helper;
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
namespace OpenAuth.App
@ -8,22 +12,61 @@ namespace OpenAuth.App
public class LoginApp
{
private IUserRepository _repository;
private IModuleRepository _moduleRepository;
private IRelevanceRepository _relevanceRepository;
public LoginApp(IUserRepository repository)
public LoginApp(IUserRepository repository,
IModuleRepository moduleRepository,
IRelevanceRepository relevanceRepository)
{
_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);
if (user == null)
{
throw new Exception("Óû§ÕʺŲ»´æÔÚ");
}
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;
}
}
}

View File

@ -49,6 +49,7 @@
<Compile Include="UserManagerApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="OrgManagerApp.cs" />
<Compile Include="ViewModel\LoginUserVM.cs" />
<Compile Include="ViewModel\ModuleView.cs" />
<Compile Include="ViewModel\RoleVM.cs" />
<Compile Include="ViewModel\UserView.cs" />

View 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; }
}
}

View 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);
}
}

View File

@ -6,12 +6,12 @@ using System.Linq;
namespace OpenAuth.Domain
{
/// <summary>
/// 用户ID
/// 系统模块
/// </summary>
public partial class Module
{
/// <summary>
/// 用户ID
/// 模块ID
/// </summary>
/// <returns></returns>
public int Id { get; set; }

View File

@ -14,6 +14,7 @@
using System.Web.Mvc;
using Infrastructure.Helper;
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
using OpenAuth.Mvc.Models;
@ -29,10 +30,10 @@ namespace OpenAuth.Mvc.Controllers
base.OnActionExecuting(filterContext);
//#region 当Session过期自动跳出登录画面
//if (SessionHelper.GetSessionUser<User>() == null)
//{
// Response.Redirect("/Login/Index");
//}
if (SessionHelper.GetSessionUser<LoginUserVM>() == null)
{
Response.Redirect("/Login/Index");
}
//#endregion
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infrastructure.Helper;
using OpenAuth.App;
namespace OpenAuth.Mvc.Controllers
@ -26,7 +27,7 @@ namespace OpenAuth.Mvc.Controllers
{
try
{
_app.Login(username, password);
SessionHelper.AddSessionUser( _app.Login(username, password));
return RedirectToAction("Index", "Home");
}
@ -35,5 +36,28 @@ namespace OpenAuth.Mvc.Controllers
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");
}
}
}

View File

@ -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; }
}
}

View File

@ -134,7 +134,6 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\BJUIResponse.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

View File

@ -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">&nbsp;<span class="glyphicon glyphicon-lock"></span> 修改密码&nbsp;</a></li>
<li><a href="#">&nbsp;<span class="glyphicon glyphicon-user"></span> 我的资料</a></li>
<li class="divider"></li>
<li><a href="Login" class="red">&nbsp;<span class="glyphicon glyphicon-off"></span> 注销登陆</a></li>
<li><a href="/Login/Logout" class="red">&nbsp;<span class="glyphicon glyphicon-off"></span> 注销登陆</a></li>
</ul>
</li>
<li class="dropdown">

View File

@ -228,7 +228,7 @@
</form>
</div>
</div>
<div class="bottom">Copyright &copy; 2015 <a href="#">基于精典DDD的权限管理 - 系统登陆</a></div>
<div class="bottom">Copyright &copy; 2015 <a href="/Login/LoginByDev">基于精典DDD的权限管理 - 点击以开发者账号登录</a></div>
</div>
</body>
</html>

View File

@ -66,6 +66,7 @@
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFunction.cs" />
<Compile Include="TestLogin.cs" />
<Compile Include="TestModuleApp.cs" />
<Compile Include="TestRepository.cs" />
<Compile Include="TestRoleApp.cs" />

View 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);
}
}
}
}