重构代码:移动Role缓存处理到RoleHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-31 11:15:43 +08:00
parent dfd4913deb
commit a0803b5179
5 changed files with 150 additions and 23 deletions

View File

@ -92,6 +92,17 @@ namespace Bootstrap.DataAccess.MongoDB
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.Role> Roles
{
get
{
return DBAccess.GetCollection<DataAccess.Role>("Roles");
}
}
private static void InitDb()
{
var connectString = DbAdapterManager.GetConnectionString("ba");

View File

@ -1,5 +1,6 @@
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
@ -8,6 +9,52 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary>
public class Role : DataAccess.Role
{
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRoles()
{
return MongoDbAccessManager.Roles.Find(FilterDefinition<DataAccess.Role>.Empty).ToList();
}
/// <summary>
///
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveRole(DataAccess.Role p)
{
if (p.Id == "0")
{
p.Id = null;
MongoDbAccessManager.Roles.InsertOne(p);
return true;
}
else
{
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<DataAccess.Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
return true;
}
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteRole(IEnumerable<string> value)
{
var list = new List<WriteModel<DataAccess.Role>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<DataAccess.Role>(Builders<DataAccess.Role>.Filter.Eq(g => g.Id, id)));
}
MongoDbAccessManager.Roles.BulkWrite(list);
return true;
}
/// <summary>
///
/// </summary>
@ -15,23 +62,49 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUserName(string userName)
{
return new List<string>() { "Administrators" };
var roles = new List<string>();
var user = UserHelper.RetrieveUsers().Cast<User>().FirstOrDefault(u => u.UserName == userName);
var role = RoleHelper.RetrieveRoles();
roles.AddRange(user.Roles.Select(r => role.FirstOrDefault(rl => rl.Id == r).RoleName));
if (roles.Count == 0) roles.Add("Default");
return roles;
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUrl(string url) => new List<string>() { "Administrators" };
public override IEnumerable<string> RetrieveRolesByUrl(string url)
{
// TODO: 需要菜单完成后处理此函数
return new List<string>() { "Administrators" };
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="userId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRoles()
public override IEnumerable<DataAccess.Role> RetrieveRolesByUserId(string userId)
{
var roles = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Role>("Roles");
return roles.Find(FilterDefinition<DataAccess.Role>.Empty).ToList();
var roles = RoleHelper.RetrieveRoles();
var user = UserHelper.RetrieveUsers().Cast<User>().FirstOrDefault(u => u.Id == userId);
roles.ToList().ForEach(r => r.Checked = user.Roles.Any(id => id == r.Id) ? "checked" : "");
return roles;
}
/// <summary>
///
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public override bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds)
{
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
return true;
}
}
}

View File

@ -39,7 +39,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
ret = true;
}
catch (Exception ex)
@ -50,6 +49,7 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
/// <summary>
/// 删除角色表
/// </summary>
@ -76,7 +76,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(roleIds: value);
ret = true;
}
catch (Exception ex)
@ -88,6 +87,7 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
/// <summary>
///
/// </summary>
@ -115,7 +115,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
ret = true;
}
catch (Exception ex)
@ -126,6 +125,7 @@ namespace Bootstrap.DataAccess.SQLite
}
return ret;
}
/// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary>
@ -154,7 +154,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
ret = true;
}
catch (Exception ex)

View File

@ -1,7 +1,6 @@
using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
@ -16,67 +15,103 @@ namespace Bootstrap.DataAccess
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
/// <summary>
/// 查询所有角色
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRoles() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create<Role>().RetrieveRoles());
/// <summary>
/// 保存用户角色关系
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public static bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByUserId(userId, roleIds);
public static bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SaveRolesByUserId(userId, roleIds);
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
return ret;
}
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserId(string userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbAdapterManager.Create<Role>().RetrieveRolesByUserId(userId), RetrieveRolesByUserIdDataKey);
/// <summary>
/// 删除角色表
/// </summary>
/// <param name="value"></param>
public static bool DeleteRole(IEnumerable<string> value) => DbAdapterManager.Create<Role>().DeleteRole(value);
public static bool DeleteRole(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<Role>().DeleteRole(value);
if (ret) CacheCleanUtility.ClearCache(roleIds: value);
return ret;
}
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveRole(Role p) => DbAdapterManager.Create<Role>().SaveRole(p);
public static bool SaveRole(Role p)
{
var ret = DbAdapterManager.Create<Role>().SaveRole(p);
if (ret) CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string> { p.Id });
return ret;
}
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByMenuId(string menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbAdapterManager.Create<Role>().RetrieveRolesByMenuId(menuId), RetrieveRolesByMenuIdDataKey);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="menuId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public static bool SavaRolesByMenuId(string id, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SavaRolesByMenuId(id, roleIds);
public static bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SavaRolesByMenuId(menuId, roleIds);
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
return ret;
}
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbAdapterManager.Create<Role>().RetrieveRolesByGroupId(groupId), RetrieveRolesByGroupIdDataKey);
/// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary>
/// <param name="id"></param>
/// <param name="groupId"></param>
/// <param name="roleIds"></param>
/// <returns></returns>
public static bool SaveRolesByGroupId(string id, IEnumerable<string> roleIds) => DbAdapterManager.Create<Role>().SaveRolesByGroupId(id, roleIds);
public static bool SaveRolesByGroupId(string groupId, IEnumerable<string> roleIds)
{
var ret = DbAdapterManager.Create<Role>().SaveRolesByGroupId(groupId, roleIds);
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbAdapterManager.Create<Role>().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey);
/// <summary>
///
/// </summary>

View File

@ -18,18 +18,22 @@ namespace Bootstrap.DataAccess
/// 获得/设置 角色主键ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 获得/设置 角色名称
/// </summary>
public string RoleName { get; set; }
/// <summary>
/// 获得/设置 角色描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get; set; }
/// <summary>
/// 查询所有角色
/// </summary>
@ -53,6 +57,7 @@ namespace Bootstrap.DataAccess
}
return roles;
}
/// <summary>
/// 保存用户角色关系
/// </summary>
@ -89,7 +94,6 @@ namespace Bootstrap.DataAccess
}
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
ret = true;
}
catch (Exception ex)
@ -100,6 +104,7 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
@ -125,6 +130,7 @@ namespace Bootstrap.DataAccess
}
return roles;
}
/// <summary>
/// 删除角色表
/// </summary>
@ -138,9 +144,9 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
}
CacheCleanUtility.ClearCache(roleIds: value);
return ret;
}
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
@ -161,9 +167,9 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Description", DbAdapterManager.ToDBValue(p.Description)));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string> { p.Id });
return ret;
}
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
@ -190,6 +196,7 @@ namespace Bootstrap.DataAccess
}
return roles;
}
/// <summary>
///
/// </summary>
@ -225,7 +232,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
ret = true;
}
catch (Exception ex)
@ -236,6 +242,7 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// </summary>
@ -262,6 +269,7 @@ namespace Bootstrap.DataAccess
}
return roles;
}
/// <summary>
/// 根据GroupId更新Roles信息删除旧的Roles信息插入新的Roles信息
/// </summary>
@ -297,7 +305,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
ret = true;
}
catch (Exception ex)
@ -308,12 +315,14 @@ namespace Bootstrap.DataAccess
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public virtual IEnumerable<string> RetrieveRolesByUserName(string userName) => DbHelper.RetrieveRolesByUserName(userName);
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查