增加功能:增加菜单对角色授权功能

This commit is contained in:
Argo-MacBookPro 2018-10-31 14:13:37 +08:00
parent 06728127d3
commit 0861d4636b
2 changed files with 57 additions and 18 deletions

View File

@ -95,11 +95,11 @@ namespace Bootstrap.DataAccess.MongoDB
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public static IMongoCollection<DataAccess.Role> Roles public static IMongoCollection<Role> Roles
{ {
get get
{ {
return DBAccess.GetCollection<DataAccess.Role>("Roles"); return DBAccess.GetCollection<Role>("Roles");
} }
} }

View File

@ -9,6 +9,11 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary> /// </summary>
public class Role : DataAccess.Role public class Role : DataAccess.Role
{ {
/// <summary>
/// 此角色关联的所有菜单
/// </summary>
public IEnumerable<string> Menus { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -16,7 +21,7 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRoles() public override IEnumerable<DataAccess.Role> RetrieveRoles()
{ {
return MongoDbAccessManager.Roles.Find(FilterDefinition<DataAccess.Role>.Empty).ToList(); return MongoDbAccessManager.Roles.Find(FilterDefinition<Role>.Empty).ToList();
} }
/// <summary> /// <summary>
@ -29,12 +34,12 @@ namespace Bootstrap.DataAccess.MongoDB
if (p.Id == "0") if (p.Id == "0")
{ {
p.Id = null; p.Id = null;
MongoDbAccessManager.Roles.InsertOne(p); MongoDbAccessManager.Roles.InsertOne(p as Role);
return true; return true;
} }
else else
{ {
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<DataAccess.Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description)); MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.RoleName, p.RoleName).Set(md => md.Description, p.Description));
return true; return true;
} }
} }
@ -46,10 +51,10 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override bool DeleteRole(IEnumerable<string> value) public override bool DeleteRole(IEnumerable<string> value)
{ {
var list = new List<WriteModel<DataAccess.Role>>(); var list = new List<WriteModel<Role>>();
foreach (var id in value) foreach (var id in value)
{ {
list.Add(new DeleteOneModel<DataAccess.Role>(Builders<DataAccess.Role>.Filter.Eq(g => g.Id, id))); list.Add(new DeleteOneModel<Role>(Builders<Role>.Filter.Eq(g => g.Id, id)));
} }
MongoDbAccessManager.Roles.BulkWrite(list); MongoDbAccessManager.Roles.BulkWrite(list);
return true; return true;
@ -71,17 +76,6 @@ namespace Bootstrap.DataAccess.MongoDB
return roles; return roles;
} }
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUrl(string url)
{
// TODO: 需要菜单完成后处理此函数
return new List<string>() { "Administrators" };
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -106,5 +100,50 @@ namespace Bootstrap.DataAccess.MongoDB
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds)); MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Roles, roleIds));
return true; return true;
} }
/// <summary>
///
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Role> RetrieveRolesByMenuId(string menuId)
{
var roles = RoleHelper.RetrieveRoles().Cast<Role>().ToList();
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>
public override bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds)
{
roleIds.ToList().ForEach(rId =>
{
var role = MongoDbAccessManager.Roles.Find(md => md.Id == rId).FirstOrDefault();
var menus = role.Menus == null ? new List<string>() : role.Menus.ToList();
if (!menus.Contains(menuId))
{
menus.Add(menuId);
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == rId, Builders<Role>.Update.Set(md => md.Menus, menus));
}
});
return true;
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public override IEnumerable<string> RetrieveRolesByUrl(string url)
{
// TODO: 需要菜单完成后处理此函数
return new List<string>() { "Administrators" };
}
} }
} }