using MongoDB.Driver; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess.MongoDB { /// /// /// public class Role : DataAccess.Role { /// /// 此角色关联的所有菜单 /// public IEnumerable Menus { get; set; } /// /// /// /// /// 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 as Role); 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; } /// /// /// /// /// public override IEnumerable RetrieveRolesByUserName(string userName) { 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 RetrieveRolesByUserId(string userId) { 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; } /// /// /// /// /// public override IEnumerable RetrieveRolesByMenuId(string menuId) { var roles = RoleHelper.RetrieveRoles().Cast().ToList(); roles.ForEach(r => r.Checked = (r.Menus != null && r.Menus.Contains(menuId)) ? "checked" : ""); roles.ForEach(r => r.Menus = null); return roles; } /// /// /// /// /// /// public override bool SavaRolesByMenuId(string menuId, IEnumerable roleIds) { var roles = MongoDbAccessManager.Roles.Find(md => md.Menus != null && md.Menus.Contains(menuId)).ToList(); // Remove roles roles.ForEach(p => { var menus = p.Menus == null ? new List() : p.Menus.ToList(); menus.Remove(menuId); MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Menus, menus)); }); roles = MongoDbAccessManager.Roles.Find(md => roleIds.Contains(md.Id)).ToList(); roles.ForEach(role => { var menus = role.Menus == null ? new List() : role.Menus.ToList(); if (!menus.Contains(menuId)) { menus.Add(menuId); MongoDbAccessManager.Roles.UpdateOne(md => md.Id == role.Id, Builders.Update.Set(md => md.Menus, menus)); } }); return true; } /// /// /// /// /// public override IEnumerable RetrieveRolesByGroupId(string groupId) { var roles = RoleHelper.RetrieveRoles(); var group = GroupHelper.RetrieveGroups().Cast().FirstOrDefault(u => u.Id == groupId); roles.ToList().ForEach(r => r.Checked = group.Roles.Any(id => id == r.Id) ? "checked" : ""); return roles; } /// /// /// /// /// /// public override bool SaveRolesByGroupId(string groupId, IEnumerable roleIds) { MongoDbAccessManager.Groups.FindOneAndUpdate(u => u.Id == groupId, Builders.Update.Set(u => u.Roles, roleIds)); return true; } /// /// /// /// /// public override IEnumerable RetrieveRolesByUrl(string url) { // TODO: 需要菜单完成后处理此函数 return new List() { "Administrators" }; } } }