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 IEnumerable Apps { get; set; }
///
///
///
///
///
public override IEnumerable Retrieves()
{
return DbManager.Roles.Find(FilterDefinition.Empty).ToList();
}
///
///
///
///
///
public override bool Save(DataAccess.Role p)
{
if (string.IsNullOrEmpty(p.Id))
{
DbManager.Roles.InsertOne(new Role()
{
RoleName = p.RoleName,
Description = p.Description,
Menus = new List(),
Apps = new List()
});
p.Id = DbManager.Roles.Find(r => r.RoleName == p.RoleName && r.Description == p.Description).FirstOrDefault().Id;
}
else
{
DbManager.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 Delete(IEnumerable value)
{
var list = new List>();
foreach (var id in value)
{
list.Add(new DeleteOneModel(Builders.Filter.Eq(g => g.Id, id)));
}
DbManager.Roles.BulkWrite(list);
return true;
}
///
///
///
///
///
public override IEnumerable RetrieveRolesByUserName(string userName)
{
var roles = new List();
var user = UserHelper.Retrieves().Cast().FirstOrDefault(u => u.UserName.ToLowerInvariant() == userName.ToLowerInvariant());
var role = RoleHelper.Retrieves();
roles.AddRange(role.Where(r => user.Roles.Any(rl => rl == r.Id)).Select(r => r.RoleName));
if (roles.Count == 0) roles.Add("Default");
return roles;
}
///
///
///
///
///
public override IEnumerable RetrievesByUserId(string userId)
{
var roles = RoleHelper.Retrieves();
var user = UserHelper.Retrieves().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 SaveByUserId(string userId, IEnumerable roleIds)
{
DbManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders.Update.Set(u => u.Roles, roleIds));
return true;
}
///
///
///
///
///
public override IEnumerable RetrievesByMenuId(string menuId)
{
var roles = RoleHelper.Retrieves().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 SavaByMenuId(string menuId, IEnumerable roleIds)
{
var roles = DbManager.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);
DbManager.Roles.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Menus, menus));
});
roles = DbManager.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);
DbManager.Roles.UpdateOne(md => md.Id == role.Id, Builders.Update.Set(md => md.Menus, menus));
}
});
return true;
}
///
///
///
///
///
public override IEnumerable RetrievesByGroupId(string groupId)
{
var roles = RoleHelper.Retrieves();
var group = GroupHelper.Retrieves().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 SaveByGroupId(string groupId, IEnumerable roleIds)
{
DbManager.Groups.FindOneAndUpdate(u => u.Id == groupId, Builders.Update.Set(u => u.Roles, roleIds));
return true;
}
///
///
///
///
///
public override IEnumerable RetrieveRolesByUrl(string url)
{
var menu = DbManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
var ret = RoleHelper.Retrieves().Cast().Where(md => md.Menus != null && md.Menus.Contains(menu.Id)).Select(m => m.RoleName).ToList();
if (!ret.Contains("Administrators")) ret.Add("Administrators");
return ret;
}
}
}