2016-01-05 17:14:10 +08:00
|
|
|
|
using OpenAuth.Domain;
|
2015-10-31 23:04:01 +08:00
|
|
|
|
using OpenAuth.Domain.Interface;
|
|
|
|
|
using System;
|
2015-10-26 23:55:02 +08:00
|
|
|
|
using System.Collections.Generic;
|
2015-10-28 22:49:59 +08:00
|
|
|
|
using System.Linq;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App
|
|
|
|
|
{
|
|
|
|
|
public class OrgManagerApp
|
|
|
|
|
{
|
|
|
|
|
private IOrgRepository _repository;
|
2016-01-07 11:47:43 +08:00
|
|
|
|
private IRelevanceRepository _relevanceRepository;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
2016-01-07 11:47:43 +08:00
|
|
|
|
public OrgManagerApp(IOrgRepository repository, IRelevanceRepository relevanceRepository)
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
2016-01-07 11:47:43 +08:00
|
|
|
|
_relevanceRepository = relevanceRepository;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-13 21:33:53 +08:00
|
|
|
|
public IList<Org> GetAll()
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
2015-11-13 21:33:53 +08:00
|
|
|
|
return _repository.LoadOrgs().ToList();
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
2015-10-26 23:55:02 +08:00
|
|
|
|
|
2015-11-06 00:27:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 部门的直接子部门
|
|
|
|
|
/// <para>TODO:可以根据用户的喜好决定选择LoadAllChildren或LoadDirectChildren</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="orgId">The org unique identifier.</param>
|
|
|
|
|
/// <returns>IEnumerable{Org}.</returns>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public IList<Org> LoadDirectChildren(Guid orgId)
|
2015-11-06 00:27:08 +08:00
|
|
|
|
{
|
2015-11-13 21:33:53 +08:00
|
|
|
|
return _repository.Find(u => u.ParentId == orgId).ToList();
|
2015-11-06 00:27:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 得到部门的所有子部门
|
|
|
|
|
/// <para>如果orgId为0,表示取得所有部门</para>
|
|
|
|
|
/// </summary>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public IList<Org> LoadAllChildren(Guid orgId)
|
2015-11-06 00:27:08 +08:00
|
|
|
|
{
|
2016-01-08 12:53:48 +08:00
|
|
|
|
return _repository.GetSubOrgs(orgId).ToList();
|
2015-11-06 00:27:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-29 23:51:10 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加部门
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="org">The org.</param>
|
|
|
|
|
/// <returns>System.Int32.</returns>
|
|
|
|
|
/// <exception cref="System.Exception">未能找到该组织的父节点信息</exception>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public Guid AddOrUpdate(Org org)
|
2015-10-26 23:55:02 +08:00
|
|
|
|
{
|
2016-04-16 23:25:00 +08:00
|
|
|
|
ChangeModuleCascade(org);
|
2016-09-02 18:05:17 +08:00
|
|
|
|
if (org.Id == Guid.Empty)
|
2015-10-26 23:55:02 +08:00
|
|
|
|
{
|
2015-11-24 15:49:00 +08:00
|
|
|
|
_repository.Add(org);
|
2015-10-26 23:55:02 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-11-24 15:49:00 +08:00
|
|
|
|
_repository.Update(org);
|
2015-10-26 23:55:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return org.Id;
|
|
|
|
|
}
|
2015-10-29 23:51:10 +08:00
|
|
|
|
|
2015-10-31 23:04:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除指定ID的部门及其所有子部门
|
|
|
|
|
/// </summary>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public void DelOrg(Guid id)
|
2015-10-31 23:04:01 +08:00
|
|
|
|
{
|
|
|
|
|
var delOrg = _repository.FindSingle(u => u.Id == id);
|
2015-11-05 23:41:06 +08:00
|
|
|
|
if (delOrg == null) return;
|
2015-10-31 23:04:01 +08:00
|
|
|
|
|
|
|
|
|
_repository.Delete(u => u.CascadeId.Contains(delOrg.CascadeId));
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 11:47:43 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载特定用户的角色
|
|
|
|
|
/// TODO:这里会加载用户及用户角色的所有角色,“为用户分配角色”功能会给人一种混乱的感觉,但可以接受
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId">The user unique identifier.</param>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public List<Org> LoadForUser(Guid userId)
|
2016-01-07 11:47:43 +08:00
|
|
|
|
{
|
|
|
|
|
//用户角色
|
|
|
|
|
var userRoleIds =
|
|
|
|
|
_relevanceRepository.Find(u => u.FirstId == userId && u.Key == "UserRole").Select(u => u.SecondId).ToList();
|
|
|
|
|
|
|
|
|
|
//用户角色与自己分配到的角色ID
|
|
|
|
|
var moduleIds =
|
|
|
|
|
_relevanceRepository.Find(
|
|
|
|
|
u =>
|
|
|
|
|
(u.FirstId == userId && u.Key == "UserAccessedOrg") ||
|
|
|
|
|
(u.Key == "RoleAccessedOrg" && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
|
|
|
|
|
|
|
|
|
|
if (!moduleIds.Any()) return new List<Org>();
|
|
|
|
|
return _repository.Find(u => moduleIds.Contains(u.Id)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载特定角色的角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId">The role unique identifier.</param>
|
2016-09-02 18:05:17 +08:00
|
|
|
|
public List<Org> LoadForRole(Guid roleId)
|
2016-01-07 11:47:43 +08:00
|
|
|
|
{
|
|
|
|
|
var moduleIds =
|
|
|
|
|
_relevanceRepository.Find(u => u.FirstId == roleId && u.Key == "RoleAccessedOrg")
|
|
|
|
|
.Select(u => u.SecondId)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (!moduleIds.Any()) return new List<Org>();
|
|
|
|
|
return _repository.Find(u => moduleIds.Contains(u.Id)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-31 23:04:01 +08:00
|
|
|
|
#region 私有方法
|
|
|
|
|
|
2015-11-24 15:49:00 +08:00
|
|
|
|
//修改对象的级联ID,生成类似XXX.XXX.X.XX
|
|
|
|
|
private void ChangeModuleCascade(Org org)
|
2015-10-30 23:14:31 +08:00
|
|
|
|
{
|
2015-11-24 15:49:00 +08:00
|
|
|
|
string cascadeId;
|
|
|
|
|
int currentCascadeId = 1; //当前结点的级联节点最后一位
|
|
|
|
|
var sameLevels = _repository.Find(o => o.ParentId == org.ParentId && o.Id != org.Id);
|
2015-11-22 15:55:26 +08:00
|
|
|
|
foreach (var obj in sameLevels)
|
2015-10-31 23:04:01 +08:00
|
|
|
|
{
|
2015-11-22 15:55:26 +08:00
|
|
|
|
int objCascadeId = int.Parse(obj.CascadeId.Split('.').Last());
|
2015-11-22 22:26:59 +08:00
|
|
|
|
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
|
2015-10-31 23:04:01 +08:00
|
|
|
|
}
|
2015-11-22 15:55:26 +08:00
|
|
|
|
|
2016-09-02 18:05:17 +08:00
|
|
|
|
if (org.ParentId != Guid.Empty)
|
2015-11-24 15:49:00 +08:00
|
|
|
|
{
|
|
|
|
|
var parentOrg = _repository.FindSingle(o => o.Id == org.ParentId);
|
|
|
|
|
if (parentOrg != null)
|
|
|
|
|
{
|
|
|
|
|
cascadeId = parentOrg.CascadeId + "." + currentCascadeId;
|
|
|
|
|
org.ParentName = parentOrg.Name;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("未能找到该组织的父节点信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cascadeId = "0." + currentCascadeId;
|
|
|
|
|
org.ParentName = "根节点";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
org.CascadeId = cascadeId;
|
2015-10-30 23:14:31 +08:00
|
|
|
|
}
|
2015-10-31 23:04:01 +08:00
|
|
|
|
|
|
|
|
|
#endregion 私有方法
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
2015-11-06 00:27:08 +08:00
|
|
|
|
}
|