using Longbow.Cache; using Longbow.Data; using System.Collections.Generic; using System.Linq; 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 = "BootstrapAdminRoleMiddleware-RetrieveRolesByUserName"; public const string RetrieveRolesByUrlDataKey = "BootstrapAdminAuthorizeFilter-RetrieveRolesByUrl"; /// /// 查询所有角色 /// /// /// public static IEnumerable RetrieveRoles(int id = 0) { var ret = CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create().RetrieveRoles(id)); return id == 0 ? ret : ret.Where(t => id == t.Id); } /// /// 保存用户角色关系 /// /// /// /// public static bool SaveRolesByUserId(int userId, IEnumerable roleIds) => DbAdapterManager.Create().SaveRolesByUserId(userId, roleIds); /// /// 查询某个用户所拥有的角色 /// /// public static IEnumerable RetrieveRolesByUserId(int userId) => CacheManager.GetOrAdd($"{RetrieveRolesByUserIdDataKey}-{userId}", key => DbAdapterManager.Create().RetrieveRolesByUserId(userId), RetrieveRolesByUserIdDataKey); /// /// 删除角色表 /// /// public static bool DeleteRole(IEnumerable value) => DbAdapterManager.Create().DeleteRole(value); /// /// 保存新建/更新的角色信息 /// /// /// public static bool SaveRole(Role p) => DbAdapterManager.Create().SaveRole(p); /// /// 查询某个菜单所拥有的角色 /// /// /// public static IEnumerable RetrieveRolesByMenuId(int menuId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByMenuIdDataKey, menuId), key => DbAdapterManager.Create().RetrieveRolesByMenuId(menuId), RetrieveRolesByMenuIdDataKey); /// /// /// /// /// /// public static bool SavaRolesByMenuId(int id, IEnumerable roleIds) => DbAdapterManager.Create().SavaRolesByMenuId(id, roleIds); /// /// 根据GroupId查询和该Group有关的所有Roles /// /// /// public static IEnumerable RetrieveRolesByGroupId(int groupId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveRolesByGroupIdDataKey, groupId), key => DbAdapterManager.Create().RetrieveRolesByGroupId(groupId), RetrieveRolesByGroupIdDataKey); /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// /// /// /// public static bool SaveRolesByGroupId(int id, IEnumerable roleIds) => DbAdapterManager.Create().SaveRolesByGroupId(id, roleIds); /// /// /// /// /// 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); } }