159 lines
4.7 KiB
C#
159 lines
4.7 KiB
C#
using BaseOUDAL;
|
|
using System.Text;
|
|
using YLErp.Providers;
|
|
|
|
namespace YLErp.Modules.SystemModule
|
|
{
|
|
/// <summary>
|
|
/// 系统角色服务
|
|
/// </summary>
|
|
public class SysRoleService : BaseService<ErpBaseContext>
|
|
{
|
|
public SysRoleService(OptUserInfo optUser) : base(optUser)
|
|
{
|
|
}
|
|
|
|
public Role SaveRoleFunctions(RoleFunctionReq req)
|
|
{
|
|
if (DbContext.Roles.Any(role => role.Name == req.Name && role.Id != req.Id))
|
|
{
|
|
throw new ServiceException("已经存在相同的角色名称!");
|
|
}
|
|
|
|
Role dbRole;
|
|
|
|
if (req.Id > 0)
|
|
{
|
|
dbRole = DbContext.Roles.FirstOrDefault(o => o.Id == req.Id);
|
|
|
|
if (dbRole == null)
|
|
{
|
|
throw new ServiceException("数据不存在!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
DbContext.Roles.Add(dbRole = new Role());
|
|
}
|
|
|
|
dbRole.Name = req.Name;
|
|
dbRole.Remark = req.Remark;
|
|
dbRole.OptDate = DateTime.Now;
|
|
|
|
//角色创建以后才能配置权限
|
|
if (req.Id > 0)
|
|
{
|
|
SetRoleFunc(req.Id, dbRole.Name, req.rights);
|
|
}
|
|
|
|
DbContext.SaveChanges();
|
|
|
|
MemoryCacheProvider.Default.FlushAll();
|
|
|
|
return dbRole;
|
|
}
|
|
|
|
private void SetRoleFunc(int roleId, string roleName, string rights)
|
|
{
|
|
var oldFuncIds = DbContext.RoleFunctions.Where(o => o.RoleId == roleId).Select(n => n.FunctionId).ToHashSet();
|
|
|
|
var newFuncIds = string.IsNullOrWhiteSpace(rights)
|
|
? new HashSet<int>()
|
|
: rights.Split(',').Select(n => int.TryParse(n, out var i) ? i : 0)
|
|
.Distinct().Where(n => n > 0 && !oldFuncIds.Remove(n)).ToHashSet();
|
|
|
|
if (oldFuncIds.Count < 1 && newFuncIds.Count < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var allFunc = oldFuncIds.Concat(newFuncIds).ToArray();
|
|
|
|
var funcDic = allFunc.Any()
|
|
? DbContext.Functions.Where(n => allFunc.Contains(n.Id)).ToDictionary(n => n.Id, m => m.Title ?? m.Name)
|
|
: null;
|
|
|
|
//解除权限
|
|
|
|
var removeFuncList = new List<(int fid, string fname)>();
|
|
|
|
foreach (var fid in oldFuncIds)
|
|
{
|
|
var rf = DbContext.RoleFunctions.Attach(new RoleFunction { RoleId = roleId, FunctionId = fid }).Entity;
|
|
|
|
DbContext.RoleFunctions.Remove(rf);
|
|
|
|
var fname = funcDic != null && funcDic.TryGetValue(fid, out var str) ? str : null;
|
|
|
|
rf.SetDataTraceKeyInfo(roleName, fname);
|
|
|
|
if (fname != null)
|
|
{
|
|
removeFuncList.Add((fid, fname));
|
|
}
|
|
}
|
|
|
|
if (removeFuncList.Count < 1 && newFuncIds.Count < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//增加权限
|
|
|
|
var sbLog = new StringBuilder(300).Append("角色:").Append(roleName).Append(';');
|
|
|
|
if (removeFuncList.Count > 0)
|
|
{
|
|
sbLog.Append("解除权限:");
|
|
foreach (var tuple in removeFuncList)
|
|
{
|
|
sbLog.Append(tuple.fname);
|
|
}
|
|
sbLog.Replace(',', ';', sbLog.Length - 1, 1);
|
|
}
|
|
|
|
if (newFuncIds.Any())
|
|
{
|
|
sbLog.Append("增加权限:");
|
|
|
|
var newRoleFuncs = newFuncIds.Select(f =>
|
|
{
|
|
var rf = new RoleFunction { RoleId = roleId, FunctionId = f };
|
|
var fname = funcDic != null && funcDic.TryGetValue(f, out var str) ? str : f.ToString();
|
|
rf.SetDataTraceKeyInfo(roleName, fname);
|
|
sbLog.Append(fname).Append(',');
|
|
return rf;
|
|
}).ToArray();
|
|
|
|
DbContext.RoleFunctions.AddRange(newRoleFuncs);
|
|
|
|
sbLog.Remove(sbLog.Length - 1, 1);
|
|
}
|
|
|
|
DbContext.SystemLogs.Add(new DBModels.SystemLog
|
|
{
|
|
EventCategory = "角色权限",
|
|
EventName = "设置权限",
|
|
EventData = JsonHelper.Serialize(new
|
|
{
|
|
RoleId = roleId,
|
|
NewFundIds = newFuncIds,
|
|
RemoveFuncIds = removeFuncList.Select(n => n.fid).ToArray()
|
|
}),
|
|
Remark = sbLog.ToString()
|
|
}).Entity.SetOpt(UserInfo);
|
|
}
|
|
}
|
|
|
|
public class RoleFunctionReq
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
public string Name { get; set; }
|
|
|
|
public string Remark { get; set; }
|
|
|
|
public string rights { get; set; }
|
|
}
|
|
}
|