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