权限分为:数据权限和功能权限
数据权限:
查询提供附加表达式的支持:
//提供附加表达式的支持
List<TAggreateRoot> GetByCondition(Expression<Func<TAggreateRoot, bool>> condition,
Expression<Func<TAggreateRoot,bool>> definecondition);
//返回聚合根分页的方法
List<TAggreateRoot> GetByConditionPages(Expression<Func<TAggreateRoot, bool>> condition,
Expression<Func<TAggreateRoot, bool>> definecondition,
RequestPage request, out int totalcount);
List<TAggreateRoot> GetByConditionPages(List<Conditions> condition,
Expression<Func<TAggreateRoot, bool>> definecondition,
RequestPage request, out int totalcount);
List<TDTO> GetByCondition<TDTO>(Expression<Func<TAggreateRoot, bool>> condition,
Expression<Func<TAggreateRoot, bool>> definecondition);
List<TDTO> GetByConditionPages<TDTO>(Expression<Func<TAggreateRoot, bool>> condition,
Expression<Func<TAggreateRoot, bool>> definecondition,
RequestPage request, out int totalcount);
List<TDTO> GetByConditionPages<TDTO>(List<Conditions> condition,
Expression<Func<TAggreateRoot, bool>> definecondition,
RequestPage request, out int totalcount);
不需要查询条件的写法:
var query = orderdbcontext.Set<TAggreateRoot>().Where(condition.And(definecondition));
在工程DDD.Domain新建文件夹ModelPermission: BAS_Department类
public partial class BAS_Department:AggreateRoot
{
private IRepository<BAS_Department> irepository;
public BAS_Department(IRepository<BAS_Department> irepository)
{
this.irepository = irepository;
}
/// <summary>
/// 创建部门对象
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="con_id"></param>
public void CreateDepartment(string name,string description,Guid con_id)
{
var bas_department = new BAS_Department();
bas_department.Id = base.Id;
bas_department.Name = name;
bas_department.Description = description;
bas_department.Con_Id = con_id;
irepository.Create(bas_department);
}
/// <summary>
/// 根据部门名称返回部门对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public BAS_Department GetDepartmentByName(string name)
{
return irepository.GetByCondition(p => p.Name == name, p => true).SingleOrDefault();
}
}
BAS_DR类:
public partial class BAS_DR:AggreateRoot
{
private IRepository<BAS_DR> irepository;
public BAS_DR(IRepository<BAS_DR> irepository)
{
this.irepository = irepository;
}
public BAS_DR() { }
/// <summary>
/// 创建DR对象,将多个部门添加到角色中
/// </summary>
/// <param name="departments"></param>
/// <param name="role"></param>
public void CreateBAS_DR(List<BAS_Department> departments,BAS_Role role)
{
foreach(var department in departments)
{
var bas_dr = new BAS_DR();
bas_dr.Id = base.Id;
bas_dr.BAS_Department = department;
bas_dr.BAS_Role = role;
irepository.Create(bas_dr);
}
}
/// <summary>
/// 根据部门对象获取对应的DR信息
/// </summary>
/// <param name="department"></param>
/// <returns></returns>
public List<BAS_DR> GetDRSByDepartment(BAS_Department department)
{
return irepository.GetByCondition(p => p.BAS_Department.Id == department.Id,p=>true);
}
/// <summary>
/// 根据角色对象获取对应的DR信息
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
public List<BAS_DR> GetDRSByRole(BAS_Role role)
{
return irepository.GetByCondition(p => p.BAS_Role.Id == role.Id, p => true);
}
}
}
BAS_IdentityContainer类:
public partial class BAS_IdentityContianer:AggreateRoot
{
private IRepository<BAS_IdentityContianer> irepository;
public BAS_IdentityContianer(IRepository<BAS_IdentityContianer> irepository)
{
this.irepository = irepository;
}
/// <summary>
/// 创建标识容器对象
/// </summary>
/// <param name="con_id"></param>
public void CreateIdentityContainer(Guid con_id)
{
var bas_identitycontainer = new BAS_IdentityContianer();
bas_identitycontainer.Id = con_id;
irepository.Create(bas_identitycontainer);
}
}
BAS_Post类:
public partial class BAS_Post:AggreateRoot
{
private IRepository<BAS_Post> irepository;
public BAS_Post(IRepository<BAS_Post> irepository)
{
this.irepository = irepository;
}
/// <summary>
/// 创建岗位对象
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="con_id"></param>
public void CreatePost(string name,string description,Guid con_id)
{
var bas_post = new BAS_Post();
bas_post.Id = base.Id;
bas_post.Name = name;
bas_post.Description = description;
bas_post.Con_Id = con_id;
irepository.Create(bas_post);
}
/// <summary>
/// 根据岗位名返回岗位对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public BAS_Post GetPostByName(string name)
{
return irepository.GetByCondition(p => p.Name == name, p => true).SingleOrDefault();
}
}
BAS_PR类:
public partial class BAS_PR:AggreateRoot
{
public IRepository<BAS_PR> irepository;
public BAS_PR(IRepository<BAS_PR> irepository)
{
this.irepository = irepository;
}
public BAS_PR() { }
/// <summary>
/// 创建PR对象,将多个岗位添加到角色中
/// </summary>
/// <param name="posts"></param>
/// <param name="role"></param>
public void CreateBAS_PR(List<BAS_Post> posts,BAS_Role role)
{
foreach(var post in posts)
{
var bas_pr = new BAS_PR();
bas_pr.Id = base.Id;
bas_pr.BAS_Post = post;
bas_pr.BAS_Role = role;
irepository.Create(bas_pr);
}
}
/// <summary>
/// 根据岗位对象获取PR信息
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public List<BAS_PR> GetPRSByPost(BAS_Post post)
{
return irepository.GetByCondition(p => p.BAS_Post.Id == post.Id, p => true);
}
/// <summary>
/// 根据角色对象获取PR信息
/// </summary>
/// <param name="role"></param>
/// <returns></returns>
public List<BAS_PR> GetPRSByRole(BAS_Role role)
{
return irepository.GetByCondition(p => p.BAS_Role.Id == role.Id, p => true);
}
}
BAS_Role类:
public partial class BAS_Role:AggreateRoot
{
private IRepository<BAS_Role> irepository;
public BAS_Role(IRepository<BAS_Role> irepository)
{
this.irepository = irepository;
}
/// <summary>
/// 创建角色对象
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="con_id"></param>
public void CreateRole(string name,string description,Guid con_id)
{
var bas_role = new BAS_Role();
bas_role.Id = base.Id;
bas_role.Name = name;
bas_role.Description = description;
bas_role.Con_Id = con_id;
irepository.Create(bas_role);
}
/// <summary>
/// 根据角色名返回角色对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public BAS_Role GetRoleByName(string name)
{
return irepository.GetByCondition(p => p.Name == name, p => true).SingleOrDefault();
}
}
}
BAS_UDPSet类:
public partial class BAS_UDPSet:AggreateRoot
{
private IRepository<BAS_UDPSet> irepository;
public BAS_UDPSet(IRepository<BAS_UDPSet> irepository)
{
this.irepository = irepository;
}
public BAS_UDPSet() { }
/// <summary>
/// 创建UDP对象,将用户添加到某个部门某个岗位
/// </summary>
/// <param name="user"></param>
/// <param name="department"></param>
/// <param name="post"></param>
/// <param name="ismain">是否为主部门主岗位</param>
public void CreateDepartmentPostToUser(BAS_User user,BAS_Department department,
BAS_Post post,bool ismain)
{
var udp = new BAS_UDPSet();
udp.Id = base.Id;
udp.BAS_Department = department;
udp.BAS_Post = post;
udp.BAS_User = user;
udp.IsMain = ismain;
irepository.Create(udp);
}
/// <summary>
/// 根据部门获取对应的UDP信息
/// </summary>
/// <param name="department"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUDPByDepartment(BAS_Department department)
{
return irepository.GetByCondition(p => p.BAS_Department.Id == department.Id,
p => true);
}
/// <summary>
/// 根据用户获取对应的UDP信息
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUDPByUser(BAS_User user)
{
return irepository.GetByCondition(p => p.BAS_User.Id == user.Id, p => true);
}
/// <summary>
/// 根据岗位获取对应的UDP信息
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUDPByPost(BAS_Post post)
{
return irepository.GetByCondition(p => p.BAS_Post.Id == post.Id, p => true);
}
}
BAS_UR类:
public partial class BAS_UR:AggreateRoot
{
private IRepository<BAS_UR> irepository;
public BAS_UR(IRepository<BAS_UR> irepository)
{
this.irepository = irepository;
}
public BAS_UR() { }
/// <summary>
/// 创建UR对象,将多个用户添加到角色中
/// </summary>
/// <param name="users"></param>
/// <param name="role"></param>
public void CreateBAS_UR(List<BAS_User> users,BAS_Role role)
{
foreach(var user in users)
{
var bas_ur = new BAS_UR();
bas_ur.Id = base.Id;
bas_ur.BAS_User = user;
bas_ur.BAS_Role = role;
irepository.Create(bas_ur);
}
}
/// <summary>
/// 根据用户对象获取对应的UR信息
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public List<BAS_UR> GetURSByUser(BAS_User user)
{
return irepository.GetByCondition(p => p.BAS_User.Id == user.Id, p => true);
}
public List<BAS_UR> GetURSByRole(BAS_Role role)
{
return irepository.GetByCondition(p => p.BAS_Role.Id == role.Id, p => true);
}
}
BAS_User类:
public partial class BAS_User:AggreateRoot
{
private IRepository<BAS_User> irepository;
public BAS_User(IRepository<BAS_User> irepository)
{
this.irepository = irepository;
}
/// <summary>
/// 在仓储中创建用户对象
/// </summary>
/// <param name="no"></param>
/// <param name="name"></param>
/// <param name="mobile"></param>
/// <param name="con_id"></param>
/// <param name="password"></param>
public void CreateUser(string no,string name,string mobile,Guid con_id,string password)
{
BAS_User bas_user = new BAS_User();
bas_user.Id = base.Id;
bas_user.No = no;
bas_user.Name = name;
bas_user.Mobile = mobile;
bas_user.Con_Id = con_id;
var userlogin = new BAS_UserLogin(password);
bas_user.BAS_UserLogin = userlogin;
irepository.Create(bas_user);
}
/// <summary>
/// 根据用户NO返回用户对象
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
public BAS_User GetUserByNo(string no)
{
return irepository.GetByCondition(p => p.No == no, p => true).SingleOrDefault();
}
/// <summary>
/// 用户登录验证
/// </summary>
/// <param name="userno"></param>
/// <param name="password"></param>
/// <returns></returns>
public bool LoginVerify(string userno,string password)
{
return irepository.GetByCondition(p => p.No == No && p.BAS_UserLogin.Password == password,
p => true).Count == 1;
}
}
BAS_UserLogin类:
public partial class BAS_UserLogin:ValueObject
{
public BAS_UserLogin(string password)
{
Id = base.Id;
Password = password;
}
}
部门的领域服务:BAS_DepartmentService
public class BAS_DepartmentService
{
private IRepository<BAS_Department> irepositorydepartment;
private IRepository<BAS_IdentityContianer> irepositoryidentitycontainer;
private IRepository<BAS_UDPSet> irepositoryudp;
private IRepository<BAS_User> irepositoryuser;
public BAS_DepartmentService(IRepository<BAS_Department> irepositorydepartment,
IRepository<BAS_IdentityContianer> irepositoryidentitycontainer,
IRepository<BAS_UDPSet> irepositoryudp,
IRepository<BAS_User> irepositoryuser)
{
this.irepositorydepartment = irepositorydepartment;
this.irepositoryidentitycontainer = irepositoryidentitycontainer;
this.irepositoryudp = irepositoryudp;
this.irepositoryuser = irepositoryuser;
}
/// <summary>
/// 协调创建部门与标识容器对象
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
public void CreateDepartment(string name,string description)
{
var bas_department = new BAS_Department(irepositorydepartment);
var con_id = Guid.NewGuid();
bas_department.CreateDepartment(name, description, con_id);
var bas_identitycontainer = new BAS_IdentityContianer(irepositoryidentitycontainer);
bas_identitycontainer.CreateIdentityContainer(con_id);
}
public List<BAS_Department> GetDepartmentByUserNo(string no)
{
var udps = new BAS_UDPService(irepositoryudp, irepositoryuser,
irepositorydepartment, null).GetUdpByUser(no);
var departments = new List<BAS_Department>();
foreach(var udp in udps)
{
var department = irepositorydepartment.GetByCondition(p => p.Id == udp.BAS_Department.Id, p => true).SingleOrDefault();
departments.Add(department);
}
return departments;
}
}
中间服务UDP:BAS_UDPService
public class BAS_UDPService
{
private IRepository<BAS_UDPSet> irepositoryudp;
private IRepository<BAS_User> irepositoryuser;
private IRepository<BAS_Department> irepositorydepartment;
private IRepository<BAS_Post> irepositorypost;
BAS_UDPSet udp;
public BAS_UDPService( IRepository<BAS_UDPSet> irepositoryudp,
IRepository<BAS_User> irepositoryuser,
IRepository<BAS_Department> irepositorydepartment,
IRepository<BAS_Post> irepositorypost)
{
this.irepositoryudp = irepositoryudp;
this.irepositoryuser = irepositoryuser;
this.irepositorydepartment = irepositorydepartment;
this.irepositorypost = irepositorypost;
udp = new BAS_UDPSet(irepositoryudp);
}
/// <summary>
/// 将用户添加到部门和岗位
/// </summary>
/// <param name="userno"></param>
/// <param name="departmentname"></param>
/// <param name="postname"></param>
/// <param name="ismain"></param>
public void CreateDepartmentPostToUser(string userno,string departmentname,
string postname,bool ismain)
{
var user = irepositoryuser.GetByCondition(p => p.No == userno, p => true).SingleOrDefault();
var department = irepositorydepartment.GetByCondition(p => p.Name == departmentname,
p => true).SingleOrDefault();
var post = irepositorypost.GetByCondition(p => p.Name == postname, p => true).SingleOrDefault();
udp.CreateDepartmentPostToUser(user, department, post,ismain);
}
/// <summary>
/// 根据部门名获取UDP信息
/// </summary>
/// <param name="departmentname"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUdpByDepartment(string departmentname)
{
var department = irepositorydepartment.GetByCondition(
p => p.Name == departmentname, p => true).SingleOrDefault();
return udp.GetUDPByDepartment(department);
}
/// <summary>
/// 根据用户NO获取UDP信息
/// </summary>
/// <param name="userno"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUdpByUser(string userno)
{
var user = irepositoryuser.GetByCondition(p => p.No == userno, p => true).SingleOrDefault();
return udp.GetUDPByUser(user);
}
/// <summary>
/// 根据岗位名获取UDP信息
/// </summary>
/// <param name="postname"></param>
/// <returns></returns>
public List<BAS_UDPSet> GetUdpByPost(string postname)
{
var post =
irepositorypost.GetByCondition(p => p.Name == postname, p => true).SingleOrDefault();
return udp.GetUDPByPost(post);
}
}
岗位BAS_PostService服务:
public class BAS_PostService
{
private IRepository<BAS_Post> irepositorypost;
private IRepository<BAS_IdentityContianer> irepositoryidentitycontianer;
private IRepository<BAS_User> irepositoryuser;
private IRepository<BAS_UDPSet> irepositoryudp;
public BAS_PostService(IRepository<BAS_Post> irepositorypost
,IRepository<BAS_IdentityContianer> irepositoryidentitycontianer,
IRepository<BAS_User> irepositoryuser,
IRepository<BAS_UDPSet> irepositoryudp)
{
this.irepositorypost = irepositorypost;
this.irepositoryidentitycontianer = irepositoryidentitycontianer;
this.irepositoryuser = irepositoryuser;
this.irepositoryudp = irepositoryudp;
}
/// <summary>
/// 创建岗位
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
public void CreatePost(string name,string description)
{
var bas_post = new BAS_Post(irepositorypost);
var con_id = Guid.NewGuid();
bas_post.CreatePost(name, description, con_id);
var bas_identitycontainer = new BAS_IdentityContianer(irepositoryidentitycontianer);
bas_identitycontainer.CreateIdentityContainer(con_id);
}
/// <summary>
/// 通过用户NO获取用户岗位信息
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
public List<BAS_Post> GetPostsByUserNO(string no)
{
var udps = new BAS_UDPService(irepositoryudp, irepositoryuser, null, irepositorypost)
.GetUdpByUser(no);
var posts =new List<BAS_Post>();
foreach(var udp in udps)
{
var post = irepositorypost.GetByCondition(p => p.Id == udp.BAS_Post.Id, p => true)
.SingleOrDefault();
posts.Add(post);
}
return posts;
}
}
BAS_DRService 角色:
public class BAS_DRService
{
private IRepository<BAS_Department> irepositorydepartment;
private IRepository<BAS_Role> irepositoryrole;
private IRepository<BAS_DR> irepositorydr;
BAS_DR bas_dr;
public BAS_DRService(IRepository<BAS_Department> irepositorydepartment,
IRepository<BAS_Role> irepositoryrole,
IRepository<BAS_DR> irepositorydr)
{
this.irepositorydepartment = irepositorydepartment;
this.irepositoryrole = irepositoryrole;
this.irepositorydr = irepositorydr;
bas_dr = new BAS_DR(irepositorydr);
}
/// <summary>
/// 将多个部门添加到角色中
/// </summary>
/// <param name="departmentnames"></param>
/// <param name="rolename"></param>
public void CreateBAS_DR(string [] departmentnames,string rolename)
{
var listdepartment = new List<BAS_Department>();
for(int i=0;i<departmentnames.Length;i++)
{
var department =
irepositorydepartment.GetByCondition(p => p.Name == departmentnames[i]
, p => true).SingleOrDefault();
listdepartment.Add(department);
}
var role = irepositoryrole.GetByCondition(p => p.Name == rolename, p => true).SingleOrDefault();
bas_dr.CreateBAS_DR(listdepartment, role);
}
/// <summary>
/// 根据部门名获取DR信息
/// </summary>
/// <param name="departmentname"></param>
/// <returns></returns>
public List<BAS_DR> GetDRSByDepartmentName(string departmentname)
{
var department =
irepositorydepartment.GetByCondition(p => p.Name == departmentname, p => true)
.SingleOrDefault();
return bas_dr.GetDRSByDepartment(department);
}
/// <summary>
/// 根据角色名获取DR信息
/// </summary>
/// <param name="rolename"></param>
/// <returns></returns>
public List<BAS_DR> GetDRSbyRoleName(string rolename)
{
var role =
irepositoryrole.GetByCondition(p => p.Name == rolename, p => true).SingleOrDefault();
return bas_dr.GetDRSByRole(role);
}
}
多个用户添加到角色 BAS_URService:
public class BAS_URService
{
private IRepository<BAS_User> irepositoryuser;
private IRepository<BAS_Role> irepositoryrole;
private IRepository<BAS_UR> irepositoryur;
BAS_UR bas_ur;
public BAS_URService(IRepository<BAS_User> irepositoryuser,
IRepository<BAS_Role> irepositoryrole,
IRepository<BAS_UR> irepositoryur)
{
this.irepositoryuser = irepositoryuser;
this.irepositoryrole = irepositoryrole;
this.irepositoryur = irepositoryur;
bas_ur = new BAS_UR(irepositoryur);
}
/// <summary>
/// 将多个用户添加到角色
/// </summary>
/// <param name="usernos"></param>
/// <param name="rolename"></param>
public void CreateBAS_UR(string[] usernos,string rolename)
{
var listuser = new List<BAS_User>();
for(int i=0;i<usernos.Length;i++)
{
var user = irepositoryuser.GetByCondition(p => p.No == usernos[i], p => true)
.SingleOrDefault();
listuser.Add(user);
}
var role = irepositoryrole.GetByCondition(p => p.Name == rolename, p => true).SingleOrDefault();
bas_ur.CreateBAS_UR(listuser, role);
}
/// <summary>
/// 根据用户No获取UR
/// </summary>
/// <param name="userno"></param>
/// <returns></returns>
public List<BAS_UR> GetURSByUserNo(string userno)
{
var user = irepositoryuser.GetByCondition(p => p.No == userno, p => true).SingleOrDefault();
return bas_ur.GetURSByUser(user);
}
/// <summary>
/// 根据角色名获取UR
/// </summary>
/// <param name="rolename"></param>
/// <returns></returns>
public List<BAS_UR> GetURsByRoleName(string rolename)
{
var role = irepositoryrole.GetByCondition(p => p.Name == rolename, p => true)
.SingleOrDefault();
return bas_ur.GetURSByRole(role);
}
}