2018-10-30 13:07:29 +08:00
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-31 11:15:43 +08:00
|
|
|
|
using System.Linq;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Role : DataAccess.Role
|
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 此角色关联的所有菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<string> Menus { get; set; }
|
|
|
|
|
|
2018-10-31 11:15:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Role> Retrieves()
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
return MongoDbAccessManager.Roles.Find(FilterDefinition<Role>.Empty).ToList();
|
2018-10-31 11:15:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Save(DataAccess.Role p)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
|
|
|
|
if (p.Id == "0")
|
|
|
|
|
{
|
|
|
|
|
p.Id = null;
|
2018-11-01 10:38:11 +08:00
|
|
|
|
MongoDbAccessManager.Roles.InsertOne(new Role()
|
|
|
|
|
{
|
|
|
|
|
RoleName = p.RoleName,
|
|
|
|
|
Description = p.Description,
|
|
|
|
|
Menus = new List<string>()
|
|
|
|
|
});
|
2018-10-31 11:15:43 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
|
2018-10-31 11:15:43 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Delete(IEnumerable<string> value)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
var list = new List<WriteModel<Role>>();
|
2018-10-31 11:15:43 +08:00
|
|
|
|
foreach (var id in value)
|
|
|
|
|
{
|
2018-10-31 14:13:37 +08:00
|
|
|
|
list.Add(new DeleteOneModel<Role>(Builders<Role>.Filter.Eq(g => g.Id, id)));
|
2018-10-31 11:15:43 +08:00
|
|
|
|
}
|
|
|
|
|
MongoDbAccessManager.Roles.BulkWrite(list);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IEnumerable<string> RetrieveRolesByUserName(string userName)
|
|
|
|
|
{
|
2018-10-31 11:15:43 +08:00
|
|
|
|
var roles = new List<string>();
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var user = UserHelper.Retrieves().Cast<User>().FirstOrDefault(u => u.UserName == userName);
|
|
|
|
|
var role = RoleHelper.Retrieves();
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
|
|
|
|
roles.AddRange(user.Roles.Select(r => role.FirstOrDefault(rl => rl.Id == r).RoleName));
|
|
|
|
|
if (roles.Count == 0) roles.Add("Default");
|
|
|
|
|
return roles;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-31 11:15:43 +08:00
|
|
|
|
/// <param name="userId"></param>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Role> RetrievesByUserId(string userId)
|
2018-10-31 11:15:43 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var roles = RoleHelper.Retrieves();
|
|
|
|
|
var user = UserHelper.Retrieves().Cast<User>().FirstOrDefault(u => u.Id == userId);
|
2018-10-31 11:15:43 +08:00
|
|
|
|
roles.ToList().ForEach(r => r.Checked = user.Roles.Any(id => id == r.Id) ? "checked" : "");
|
|
|
|
|
return roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByUserId(string userId, IEnumerable<string> roleIds)
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
2018-10-31 11:15:43 +08:00
|
|
|
|
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
|
|
|
|
|
return true;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2018-10-31 14:13:37 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menuId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Role> RetrievesByMenuId(string menuId)
|
2018-10-31 14:13:37 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var roles = RoleHelper.Retrieves().Cast<Role>().ToList();
|
2018-10-31 14:13:37 +08:00
|
|
|
|
roles.ForEach(r => r.Checked = (r.Menus != null && r.Menus.Contains(menuId)) ? "checked" : "");
|
|
|
|
|
roles.ForEach(r => r.Menus = null);
|
|
|
|
|
return roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="menuId"></param>
|
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SavaByMenuId(string menuId, IEnumerable<string> roleIds)
|
2018-10-31 14:13:37 +08:00
|
|
|
|
{
|
2018-10-31 17:08:55 +08:00
|
|
|
|
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<string>() : p.Menus.ToList();
|
|
|
|
|
menus.Remove(menuId);
|
|
|
|
|
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
roles = MongoDbAccessManager.Roles.Find(md => roleIds.Contains(md.Id)).ToList();
|
|
|
|
|
roles.ForEach(role =>
|
2018-10-31 14:13:37 +08:00
|
|
|
|
{
|
|
|
|
|
var menus = role.Menus == null ? new List<string>() : role.Menus.ToList();
|
|
|
|
|
if (!menus.Contains(menuId))
|
|
|
|
|
{
|
|
|
|
|
menus.Add(menuId);
|
2018-10-31 17:08:55 +08:00
|
|
|
|
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == role.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
|
2018-10-31 14:13:37 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 17:22:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="groupId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Role> RetrievesByGroupId(string groupId)
|
2018-10-31 17:22:06 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var roles = RoleHelper.Retrieves();
|
|
|
|
|
var group = GroupHelper.Retrieves().Cast<Group>().FirstOrDefault(u => u.Id == groupId);
|
2018-10-31 17:22:06 +08:00
|
|
|
|
roles.ToList().ForEach(r => r.Checked = group.Roles.Any(id => id == r.Id) ? "checked" : "");
|
|
|
|
|
return roles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="groupId"></param>
|
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByGroupId(string groupId, IEnumerable<string> roleIds)
|
2018-10-31 17:22:06 +08:00
|
|
|
|
{
|
|
|
|
|
MongoDbAccessManager.Groups.FindOneAndUpdate(u => u.Id == groupId, Builders<Group>.Update.Set(u => u.Roles, roleIds));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 14:13:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override IEnumerable<string> RetrieveRolesByUrl(string url)
|
|
|
|
|
{
|
2018-10-31 19:27:56 +08:00
|
|
|
|
var menu = MongoDbAccessManager.Menus.Find(md => md.Url.StartsWith(url)).FirstOrDefault();
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var ret = RoleHelper.Retrieves().Cast<Role>().Where(md => md.Menus != null && md.Menus.Contains(menu.Id)).Select(m => m.RoleName).ToList();
|
2018-10-31 19:27:56 +08:00
|
|
|
|
if (!ret.Contains("Administrators")) ret.Add("Administrators");
|
|
|
|
|
return ret;
|
2018-10-31 14:13:37 +08:00
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|