<file_sep>﻿namespace aqh_daily_report.Domain
{
    using SharpArch.Domain.DomainModel;

    public class Employee : Entity
    {
    	public virtual string EmployeeNo { get; set; }

    	public virtual string Password { get; set; }

    	public virtual Branch Branch { get; set; }

    	public virtual Permission Permission { get; set; }
    }
}<file_sep>using System;

namespace aqh_daily_report.Domain
{
	public class ControllerActionMap : IEquatable<ControllerActionMap>
	{
		public ControllerActionMap(string controllerName, string actionName)
		{
			ControllerName = controllerName.ToLower();
			ActionName = actionName.ToLower();
		}

		public string ControllerName { get; private set; }
		public string ActionName { get; private set; }

		#region IEquatable<ControllerActionMap> Members

		public bool Equals(ControllerActionMap other)
		{
			if (ReferenceEquals(null, other)) return false;
			if (ReferenceEquals(this, other)) return true;
			return Equals(other.ControllerName, ControllerName) && Equals(other.ActionName, ActionName);
		}

		#endregion

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj)) return false;
			if (ReferenceEquals(this, obj)) return true;
			if (obj.GetType() != typeof (ControllerActionMap)) return false;
			return Equals((ControllerActionMap) obj);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				return (ControllerName.GetHashCode()*397) ^ ActionName.GetHashCode();
			}
		}
	}
}<file_sep>﻿using System.Web.Mvc;
using Machine.Specifications;
using Machine.Specifications.AutoMocking.Rhino;
using Machine.Specifications.Mvc;
using MvcContrib.TestHelper;
using NHibernate;
using Rhino.Mocks;
using aqh_daily_report.Domain;
using aqh_daily_report.Domain.Contracts.Tasks;
using aqh_daily_report.Web.Mvc.Controllers;
using aqh_daily_report.Web.Mvc.Controllers.ViewModels;

namespace MSpecTests.aqh_daily_report
{
	[Subject(typeof (LoginController))]
	public class LoginControllerSpecs : Specification<LoginController>
	{
		static IEmployeeTask employeeTask;
		static LoginController loginController;
		static LoginViewModel loginViewModel;
		static ActionResult result;
		static string EmployeeNo = "test";
		static string Password = "<PASSWORD>";
		static ICustomFormsAuthentication custom_forms_authentication;
		static IFutureValue<Employee> employeeFutureValue;

		Establish contact = () =>
		{

			{
				employeeFutureValue = DependencyOf<IFutureValue<Employee>>();
				employeeFutureValue.Expect(value => value.Value).Return(new Employee() { Branch = Branch.First ,Permission=Permission.Admin,Password = <PASSWORD>,EmployeeNo=EmployeeNo});
			}

			custom_forms_authentication = DependencyOf<ICustomFormsAuthentication>();
			employeeTask = DependencyOf<IEmployeeTask>();
			employeeTask.Expect(task => task.FindByEmployeeNoAndPassword(EmployeeNo, Password)).Return(employeeFutureValue);
			loginController = MockRepository.GenerateMock<LoginController>(employeeTask,custom_forms_authentication);
			loginViewModel = new LoginViewModel
			                 	{
			                 			EmployeeNo = EmployeeNo,
			                 			Password = <PASSWORD>
			                 	};

		};

		Because of = () => result = loginController.Index(loginViewModel);
		It should_Login_Success = () => result.ShouldRedirectToAction<HomeController>(x => x.Index());
	}
}<file_sep>﻿using System.Web.Mvc;

namespace aqh_daily_report.Domain
{
	public class CustomPermissionAttribute : ActionFilterAttribute
	{
		public override void OnActionExecuting(ActionExecutingContext filterContext)
		{
			var controllerActionMap = new ControllerActionMap(
					filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);
			if (!HasPermission(controllerActionMap))
			{
				filterContext.Result = new HttpUnauthorizedResult("没有访问权限");
			}
		}

		bool HasPermission(ControllerActionMap controllerActionMap)
		{
			if (DomainSession.Current.Employee == null)
				return PermissionRepository.HasPermission(controllerActionMap);
			return PermissionRepository.HasPermission(controllerActionMap, DomainSession.Current.Employee.Permission);
		}
	}
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MindHarbor.ClassEnum;
using aqh_daily_report.Domain;

namespace aqh_daily_report.Infrastructure.UserType
{
	[Serializable]
	public class PermissionUserType : ClassEnumUserType<Permission>
	{

	}
}
<file_sep>﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using MvcContrib.FluentHtml.Behaviors;

namespace aqh_daily_report.Domain
{
	public static class PermissionRepository
	{
		static readonly IDictionary<ControllerActionMap, ICollection<Permission>> store =
				new ThreadSafeDictionary<ControllerActionMap, ICollection<Permission>>();

		static PermissionRepository()
		{
			store.Add(new ControllerActionMap("Login", "Index"),
			          new Collection<Permission>());
			store.Add(new ControllerActionMap("Home", "Index"), new Collection<Permission>());
		}

		public static bool HasPermission(ControllerActionMap map, Permission permission = null)
		{
			if (permission == Permission.Admin)
				return true;

			if (!store.ContainsKey(map) || store[map].Count == 0)
				return true;
			return store[map].Contains(permission);
		}
	}
}<file_sep>﻿using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.Conventions
{
	public class HasManyToManyConvention : IHasManyToManyConvention
	{
		#region IHasManyToManyConvention Members

		public void Apply(IManyToManyCollectionInstance instance)
		{
			string tablename = "tbl" + instance.EntityType.Name + "_" + instance.ChildType.Name + "_Relationships";
			instance.Table(tablename);
		}

		#endregion
	}
}<file_sep>﻿using System.Collections.Generic;
using System.Web.Mvc;
using MvcContrib.Pagination;
using MvcContrib.UI.Grid;
using NHibernate;
using SharpArch.NHibernate.Web.Mvc;
using aqh_daily_report.Domain;
using aqh_daily_report.Domain.Contracts.Tasks;

namespace aqh_daily_report.Web.Mvc.Controllers
{
	public class EmployeeController : CustomControllerBase
	{
		readonly IEmployeeTask EmployeeTask;
		public EmployeeController(IEmployeeTask employeeTask)
		{
			EmployeeTask = employeeTask;
		}
		//
		// GET: /Employee/
		[Transaction]
		[HttpGet]
		public ActionResult Index(int? page,GridSortOptions sort)
		{
			ViewBag.Sort = sort;
			int pageSize = 20;

			int startRow = pageSize * ((page ?? 1) - 1);
			IEnumerable<Employee> customers = EmployeeTask.FindAll(startRow, pageSize, sort);
			IFutureValue<int> count = EmployeeTask.CountAll();

			return View(new CustomPagination<Employee>(customers, page ?? 1, pageSize, count.Value));
		}

	}
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace aqh_daily_report.Domain
{
	public interface ICustomFormsAuthentication
	{
		void SetAuthCookie(string userName, bool createPersistentCookie);
	}
}
<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace aqh_daily_report.Domain
{
	[CustomPermission]
	public class CustomControllerBase:Controller
	{
	}
}
<file_sep>﻿using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Inspections;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.Conventions
{
	public class CustomManyToManyTableNameConvention
			: ManyToManyTableNameConvention
	{
		protected override string GetBiDirectionalTableName(IManyToManyCollectionInspector collection,
		                                                    IManyToManyCollectionInspector otherSide)
		{
			return "tbl" + collection.EntityType.Name + "_" + otherSide.EntityType.Name + "_R";
		}

		protected override string GetUniDirectionalTableName(IManyToManyCollectionInspector collection)
		{
			return "tbl" + collection.EntityType.Name + "_" + collection.ChildType.Name + "_R";
		}
	}
}<file_sep>﻿using aqh_daily_report.Domain;

namespace aqh_daily_report.Web.Mvc.Controllers.ViewModels
{
	public class EmployeeViewModel
	{
		public int Id { get; set; }
		public string EmployeeNo { get; set; }
		public string Password { get; set; }
		public string BranchName { get; set; }
		public string PermissionName { get; set; }

		public Branch GetBranch()
		{
			return Branch.Parse(BranchName);
		}

		public Permission GetPermission()
		{
			return Permission.Parse(PermissionName);
		}
	}
}<file_sep>﻿using System;
using MindHarbor.ClassEnum;

namespace aqh_daily_report.Domain
{
	[Serializable]
	public class Branch : ClassEnumGeneric<Branch>
	{
		public static readonly Branch First=new Branch("first");
		public Branch(string name) : base(name)
		{}
	}
}<file_sep>﻿using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.Conventions
{

	#region Using Directives

	#endregion

	public class PrimaryKeyConvention : IIdConvention
	{
		#region IIdConvention Members

		public void Apply(IIdentityInstance instance)
		{
			instance.Column(instance.EntityType.Name + "Id");
		}

		#endregion
	}
}<file_sep>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using aqh_daily_report.Domain;
using aqh_daily_report.Infrastructure.UserType;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.CustomerMap
{
	public class EmployeeMap:IAutoMappingOverride<Employee>
	{
		public void Override(AutoMapping<Employee> mapping)
		{
			mapping.Map(x => x.Branch).CustomType<BranchUserType>();
			mapping.Map(x => x.Permission).CustomType<PermissionUserType>();
		}
	}
}
<file_sep>﻿using System;
using MindHarbor.ClassEnum;

namespace aqh_daily_report.Domain
{
	[Serializable]
	public class Permission : ClassEnumGeneric<Permission>
	{
		public static readonly Permission Receptionist = new Permission("Receptionist", "前台");
		public static readonly Permission Consultant = new Permission("Consultant", "顾问");
		public static readonly Permission Admin = new Permission("Admin", "管理员");

		public Permission(string name, string desc) : base(name)
		{
			Desc = desc;
		}

		public string Desc { get; private set; }
	}
}<file_sep>﻿using System.Collections.Generic;
using MvcContrib.UI.Grid;
using NHibernate;
using SharpArch.NHibernate;
using aqh_daily_report.Domain;
using aqh_daily_report.Domain.Contracts.Tasks;
using CommonLib.QueryOver;
namespace aqh_daily_report.Infrastructure.Queries
{
	public class EmployeeTask : NHibernateQuery, IEmployeeTask
	{
		#region IEmployeeTask Members

		public IFutureValue<Employee> FindByEmployeeNoAndPassword(string employeeNo, string password)
		{
			return
					Session.QueryOver<Employee>().Where(x => x.EmployeeNo == employeeNo && x.Password == password).FutureValue
							<Employee>();
		}

		public IEnumerable<Employee> FindAll(int startRow, int pageSize, GridSortOptions sort)
		{
			return Session.QueryOver<Employee>().OrderBy(sort).Skip(startRow).Take(pageSize).Future<Employee>();
		}


		public IFutureValue<int> CountAll()
		{
			return Session.QueryOver<Employee>().Count().FutureValue<int>();
		}


		#endregion
	}
}<file_sep>﻿using System.Web.Mvc;
using aqh_daily_report.Domain;

namespace aqh_daily_report.Web.Mvc.Controllers
{
	public class HomeController : CustomControllerBase
	{
		[HttpGet]
		public ActionResult Index()
		{
			return View();
		}
	}
}<file_sep>﻿using AutoMapper;
using NUnit.Framework;
using aqh_daily_report.Domain;
using aqh_daily_report.Web.Mvc.Controllers.ViewModels;

namespace aqh_daily_report.Tests
{
	[TestFixture]
	public class AutoMapperLearnFixture
	{
		[Test]
		public void Flattening()
		{
			var employeeViewModel = new EmployeeViewModel();
			employeeViewModel.EmployeeNo = "test";
			employeeViewModel.Password = "<PASSWORD>";
			employeeViewModel.PermissionName = Permission.Admin.Name;
			employeeViewModel.BranchName = Branch.First.Name;
			Mapper.CreateMap<EmployeeViewModel,Employee>();
			var employee = Mapper.Map<EmployeeViewModel, Employee>(employeeViewModel);
			Assert.AreEqual(employeeViewModel.EmployeeNo,employee.EmployeeNo);
			Assert.AreEqual(employeeViewModel.Password,employee.Password);
			Assert.AreEqual(employeeViewModel.BranchName,employee.Branch.Name);
			Assert.AreEqual(employeeViewModel.PermissionName,employee.Permission.Name);
		}
	}
}<file_sep>﻿using System.Web.Mvc;
using MvcContrib;
using NHibernate;
using SharpArch.NHibernate.Web.Mvc;
using aqh_daily_report.Domain;
using aqh_daily_report.Domain.Contracts.Tasks;
using aqh_daily_report.Web.Mvc.Controllers.ViewModels;

namespace aqh_daily_report.Web.Mvc.Controllers
{
	public class LoginController : CustomControllerBase
	{
		readonly ICustomFormsAuthentication CustomFormsAuthentication;
		readonly IEmployeeTask EmployeeTask;
		//
		// GET: /Login/
		public LoginController(IEmployeeTask employeeTask, ICustomFormsAuthentication customFormsAuthentication)
		{
			EmployeeTask = employeeTask;
			CustomFormsAuthentication = customFormsAuthentication;
		}

		[HttpGet]
		public ActionResult Index()
		{
			return View();
		}


		//
		// POST: /Login/Index

		[Transaction]
		[ValidateAntiForgeryToken]
		[HttpPost]
		public ActionResult Index(LoginViewModel model)
		{
			if (ModelState.IsValid)
			{
				IFutureValue<Employee> employee = EmployeeTask.FindByEmployeeNoAndPassword(model.EmployeeNo, model.Password);
				if (employee.Value != null)
				{
					DomainSession.Current.Employee = employee.Value;
					CustomFormsAuthentication.SetAuthCookie(model.EmployeeNo, false);
					return this.RedirectToAction<HomeController>(x => x.Index());
				}
				ModelState.AddModelError("", "登录失败!");
			}
			return View();
		}
	}
}<file_sep>﻿using System;
using FluentNHibernate.Automapping;
using FluentNHibernate.Conventions;
using SharpArch.Domain.DomainModel;
using SharpArch.NHibernate.FluentNHibernate;
using aqh_daily_report.Domain;
using aqh_daily_report.Infrastructure.NHibernateMaps.Conventions;

namespace aqh_daily_report.Infrastructure.NHibernateMaps
{
	#region Using Directives

	

	#endregion

	/// <summary>
	/// Generates the automapping for the domain assembly
	/// </summary>
	public class AutoPersistenceModelGenerator : IAutoPersistenceModelGenerator
	{
		#region IAutoPersistenceModelGenerator Members

		public AutoPersistenceModel Generate()
		{
			AutoPersistenceModel mappings = AutoMap.AssemblyOf<Employee>(new AutomappingConfiguration());
			mappings.IgnoreBase<Entity>();
			mappings.IgnoreBase(typeof (EntityWithTypedId<>));
			mappings.Conventions.Setup(GetConventions());
			mappings.UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

			return mappings;
		}

		#endregion

		static Action<IConventionFinder> GetConventions()
		{
			return c =>
			{
				c.Add<PrimaryKeyConvention>();
				c.Add<CustomForeignKeyConvention>();
				c.Add<HasManyConvention>();
				c.Add<TableNameConvention>();
				c.Add<CustomManyToManyTableNameConvention>();
				c.Add<IdGenerationConvention>();
				c.Add<ForeignKeyNameConvention>();
				c.Add<ReferenceConvention>();
			};
		}
	}
}<file_sep>﻿using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.Conventions
{
	#region Using Directives

	

	#endregion

	public class HasManyConvention : IHasManyConvention
	{
		#region IHasManyConvention Members

		public void Apply(IOneToManyCollectionInstance instance)
		{
			instance.Key.Column(instance.EntityType.Name + "Id");
			instance.Key.ForeignKey(instance.EntityType.Name + "_" + instance.ChildType.Name + "_FK");
			instance.Cascade.AllDeleteOrphan();
			instance.Inverse();
		}

		#endregion
	}
}<file_sep>﻿using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.Instances;

namespace aqh_daily_report.Infrastructure.NHibernateMaps.Conventions
{

	#region Using Directives

	#endregion

	public class TableNameConvention : IClassConvention
	{
		#region IClassConvention Members

		public void Apply(IClassInstance instance)
		{
			instance.Table("tbl" + instance.EntityType.Name.InflectTo().Pluralized);
		}

		#endregion
	}
}<file_sep>﻿using System;
using MindHarbor.ClassEnum;
using aqh_daily_report.Domain;

namespace aqh_daily_report.Infrastructure.UserType
{
	[Serializable]
	public class BranchUserType : ClassEnumUserType<Branch>
	{}
}<file_sep>﻿using System.Collections.Generic;
using MvcContrib.UI.Grid;
using NHibernate;

namespace aqh_daily_report.Domain.Contracts.Tasks
{
	public interface IEmployeeTask
	{
		 IFutureValue<Employee> FindByEmployeeNoAndPassword(string employeeNo, string password);
		 IEnumerable<Employee> FindAll(int startRow,int pageSize,GridSortOptions sort);
		IFutureValue<int> CountAll();
	}
}