2018-10-28 15:03:59 +08:00
|
|
|
|
using Longbow.Cache;
|
|
|
|
|
using Longbow.Data;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class RoleHelper
|
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
public const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
|
|
|
|
|
public const string RetrieveRolesByUserIdDataKey = "RoleHelper-RetrieveRolesByUserId";
|
|
|
|
|
public const string RetrieveRolesByMenuIdDataKey = "RoleHelper-RetrieveRolesByMenuId";
|
|
|
|
|
public const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId";
|
2018-10-28 21:14:03 +08:00
|
|
|
|
public const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
|
|
|
|
|
public const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<Role> Retrieves() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbContextManager.Create<Role>().Retrieves());
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存用户角色关系
|
|
|
|
|
/// </summary>
|
2018-10-28 15:03:59 +08:00
|
|
|
|
/// <param name="userId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool SaveByUserId(string userId, IEnumerable<string> roleIds)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Role>().SaveByUserId(userId, roleIds);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { userId }, roleIds: roleIds);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询某个用户所拥有的角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<Role> RetrievesByUserId(string userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbContextManager.Create<Role>().RetrievesByUserId(userId), RetrieveRolesByUserIdDataKey);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除角色表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool Delete(IEnumerable<string> value)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Role>().Delete(value);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(roleIds: value);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新建/更新的角色信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool Save(Role p)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Role>().Save(p);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(roleIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string> { p.Id });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询某个菜单所拥有的角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menuId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<Role> RetrievesByMenuId(string menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbContextManager.Create<Role>().RetrievesByMenuId(menuId), RetrieveRolesByMenuIdDataKey);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 11:15:43 +08:00
|
|
|
|
/// <param name="menuId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool SavaByMenuId(string menuId, IEnumerable<string> roleIds)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Role>().SavaByMenuId(menuId, roleIds);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List<string>() { menuId });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据GroupId查询和该Group有关的所有Roles
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="groupId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<Role> RetrievesByGroupId(string groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbContextManager.Create<Role>().RetrievesByGroupId(groupId), RetrieveRolesByGroupIdDataKey);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息
|
|
|
|
|
/// </summary>
|
2018-10-31 11:15:43 +08:00
|
|
|
|
/// <param name="groupId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static bool SaveByGroupId(string groupId, IEnumerable<string> roleIds)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = DbContextManager.Create<Role>().SaveByGroupId(groupId, roleIds);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
if (ret) CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List<string>() { groupId });
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<string> RetrieveRolesByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, userName), key => DbContextManager.Create<Role>().RetrieveRolesByUserName(userName), RetrieveRolesByUserNameDataKey);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public static IEnumerable<string> RetrieveRolesByUrl(string url) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByUrlDataKey, url), key => DbContextManager.Create<Role>().RetrieveRolesByUrl(url), RetrieveRolesByUrlDataKey);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|