From a0803b51792fd7e98ce0214029cceaeebc09a882 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 11:15:43 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8Role=E7=BC=93=E5=AD=98=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=88=B0RoleHelper=E7=B1=BB=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MongoDbAccessManager.cs | 11 +++ Bootstrap.DataAccess.MongoDB/Role.cs | 85 +++++++++++++++++-- Bootstrap.DataAccess.SQLite/Role.cs | 7 +- Bootstrap.DataAccess/Helper/RoleHelper.cs | 51 +++++++++-- Bootstrap.DataAccess/Role.cs | 19 +++-- 5 files changed, 150 insertions(+), 23 deletions(-) diff --git a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs index 47d0413e..855c7ea5 100644 --- a/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs +++ b/Bootstrap.DataAccess.MongoDB/MongoDbAccessManager.cs @@ -92,6 +92,17 @@ namespace Bootstrap.DataAccess.MongoDB } } + /// + /// + /// + public static IMongoCollection Roles + { + get + { + return DBAccess.GetCollection("Roles"); + } + } + private static void InitDb() { var connectString = DbAdapterManager.GetConnectionString("ba"); diff --git a/Bootstrap.DataAccess.MongoDB/Role.cs b/Bootstrap.DataAccess.MongoDB/Role.cs index ea152838..a4114c3a 100644 --- a/Bootstrap.DataAccess.MongoDB/Role.cs +++ b/Bootstrap.DataAccess.MongoDB/Role.cs @@ -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 /// public class Role : DataAccess.Role { + /// + /// + /// + /// + /// + public override IEnumerable RetrieveRoles() + { + return MongoDbAccessManager.Roles.Find(FilterDefinition.Empty).ToList(); + } + + /// + /// + /// + /// + /// + 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.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description)); + return true; + } + } + + /// + /// + /// + /// + /// + public override bool DeleteRole(IEnumerable value) + { + var list = new List>(); + foreach (var id in value) + { + list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id))); + } + MongoDbAccessManager.Roles.BulkWrite(list); + return true; + } + /// /// /// @@ -15,23 +62,49 @@ namespace Bootstrap.DataAccess.MongoDB /// public override IEnumerable RetrieveRolesByUserName(string userName) { - return new List() { "Administrators" }; + var roles = new List(); + var user = UserHelper.RetrieveUsers().Cast().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; } + /// /// /// /// /// - public override IEnumerable RetrieveRolesByUrl(string url) => new List() { "Administrators" }; + public override IEnumerable RetrieveRolesByUrl(string url) + { + // TODO: 需要菜单完成后处理此函数 + return new List() { "Administrators" }; + } + /// /// /// - /// + /// /// - public override IEnumerable RetrieveRoles() + public override IEnumerable RetrieveRolesByUserId(string userId) { - var roles = MongoDbAccessManager.DBAccess.GetCollection("Roles"); - return roles.Find(FilterDefinition.Empty).ToList(); + var roles = RoleHelper.RetrieveRoles(); + var user = UserHelper.RetrieveUsers().Cast().FirstOrDefault(u => u.Id == userId); + roles.ToList().ForEach(r => r.Checked = user.Roles.Any(id => id == r.Id) ? "checked" : ""); + return roles; + } + + /// + /// + /// + /// + /// + /// + public override bool SaveRolesByUserId(string userId, IEnumerable roleIds) + { + MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders.Update.Set(u => u.Roles, roleIds)); + return true; } } } diff --git a/Bootstrap.DataAccess.SQLite/Role.cs b/Bootstrap.DataAccess.SQLite/Role.cs index 0168767e..4968527c 100644 --- a/Bootstrap.DataAccess.SQLite/Role.cs +++ b/Bootstrap.DataAccess.SQLite/Role.cs @@ -39,7 +39,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(userIds: new List() { userId }, roleIds: roleIds); ret = true; } catch (Exception ex) @@ -50,6 +49,7 @@ namespace Bootstrap.DataAccess.SQLite } return ret; } + /// /// 删除角色表 /// @@ -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; } + /// /// /// @@ -115,7 +115,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { menuId }); ret = true; } catch (Exception ex) @@ -126,6 +125,7 @@ namespace Bootstrap.DataAccess.SQLite } return ret; } + /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// @@ -154,7 +154,6 @@ namespace Bootstrap.DataAccess.SQLite }); transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { groupId }); ret = true; } catch (Exception ex) diff --git a/Bootstrap.DataAccess/Helper/RoleHelper.cs b/Bootstrap.DataAccess/Helper/RoleHelper.cs index ab79f27b..08dd2473 100644 --- a/Bootstrap.DataAccess/Helper/RoleHelper.cs +++ b/Bootstrap.DataAccess/Helper/RoleHelper.cs @@ -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"; + /// /// 查询所有角色 /// /// /// public static IEnumerable RetrieveRoles() => CacheManager.GetOrAdd(RetrieveRolesDataKey, key => DbAdapterManager.Create().RetrieveRoles()); + /// /// 保存用户角色关系 /// /// /// /// - public static bool SaveRolesByUserId(string userId, IEnumerable roleIds) => DbAdapterManager.Create().SaveRolesByUserId(userId, roleIds); + 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) => DbAdapterManager.Create().DeleteRole(value); + 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) => DbAdapterManager.Create().SaveRole(p); + 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 id, IEnumerable roleIds) => DbAdapterManager.Create().SavaRolesByMenuId(id, roleIds); + 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 id, IEnumerable roleIds) => DbAdapterManager.Create().SaveRolesByGroupId(id, roleIds); + 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); + /// /// /// diff --git a/Bootstrap.DataAccess/Role.cs b/Bootstrap.DataAccess/Role.cs index 05e461d9..5d16f644 100644 --- a/Bootstrap.DataAccess/Role.cs +++ b/Bootstrap.DataAccess/Role.cs @@ -18,18 +18,22 @@ namespace Bootstrap.DataAccess /// 获得/设置 角色主键ID /// public string Id { get; set; } + /// /// 获得/设置 角色名称 /// public string RoleName { get; set; } + /// /// 获得/设置 角色描述 /// public string Description { get; set; } + /// /// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联 /// public string Checked { get; set; } + /// /// 查询所有角色 /// @@ -53,6 +57,7 @@ namespace Bootstrap.DataAccess } return roles; } + /// /// 保存用户角色关系 /// @@ -89,7 +94,6 @@ namespace Bootstrap.DataAccess } transaction.CommitTransaction(); } - CacheCleanUtility.ClearCache(userIds: new List() { userId }, roleIds: roleIds); ret = true; } catch (Exception ex) @@ -100,6 +104,7 @@ namespace Bootstrap.DataAccess } return ret; } + /// /// 查询某个用户所拥有的角色 /// @@ -125,6 +130,7 @@ namespace Bootstrap.DataAccess } return roles; } + /// /// 删除角色表 /// @@ -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; } + /// /// 保存新建/更新的角色信息 /// @@ -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() : new List { p.Id }); return ret; } + /// /// 查询某个菜单所拥有的角色 /// @@ -190,6 +196,7 @@ namespace Bootstrap.DataAccess } return roles; } + /// /// /// @@ -225,7 +232,6 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(roleIds: roleIds, menuIds: new List() { menuId }); ret = true; } catch (Exception ex) @@ -236,6 +242,7 @@ namespace Bootstrap.DataAccess } return ret; } + /// /// 根据GroupId查询和该Group有关的所有Roles /// @@ -262,6 +269,7 @@ namespace Bootstrap.DataAccess } return roles; } + /// /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// @@ -297,7 +305,6 @@ namespace Bootstrap.DataAccess transaction.CommitTransaction(); } } - CacheCleanUtility.ClearCache(roleIds: roleIds, groupIds: new List() { groupId }); ret = true; } catch (Exception ex) @@ -308,12 +315,14 @@ namespace Bootstrap.DataAccess } return ret; } + /// /// /// /// /// public virtual IEnumerable RetrieveRolesByUserName(string userName) => DbHelper.RetrieveRolesByUserName(userName); + /// /// 根据菜单url查询某个所拥有的角色 /// 从NavigatorRole表查