增加功能:角色管理页面增加对部门授权

This commit is contained in:
Argo-MacBookPro 2018-10-31 16:03:49 +08:00
parent 5661df8535
commit 82abbc30c1
3 changed files with 48 additions and 13 deletions

View File

@ -62,21 +62,21 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="type"></param>
/// <returns></returns>
[HttpPost("{id}")]
public IEnumerable<Group> Post(string id, [FromQuery]string type)
public IEnumerable<object> Post(string id, [FromQuery]string type)
{
var ret = new List<Group>();
IEnumerable<Group> ret = new List<Group>();
switch (type)
{
case "user":
ret = GroupHelper.RetrieveGroupsByUserId(id).ToList();
ret = GroupHelper.RetrieveGroupsByUserId(id);
break;
case "role":
ret = GroupHelper.RetrieveGroupsByRoleId(id).ToList();
ret = GroupHelper.RetrieveGroupsByRoleId(id);
break;
default:
break;
}
return ret;
return ret.Select(p => new { p.Id, p.Checked, p.GroupName, p.Description });
}
/// <summary>

View File

@ -9,13 +9,18 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary>
public class Group : DataAccess.Group
{
/// <summary>
///
/// </summary>
public IEnumerable<string> Roles { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroups()
{
return MongoDbAccessManager.Groups.Find(FilterDefinition<DataAccess.Group>.Empty).ToList();
return MongoDbAccessManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
}
/// <summary>
@ -28,12 +33,12 @@ namespace Bootstrap.DataAccess.MongoDB
if (p.Id == "0")
{
p.Id = null;
MongoDbAccessManager.Groups.InsertOne(p);
MongoDbAccessManager.Groups.InsertOne(p as Group);
return true;
}
else
{
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<DataAccess.Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
return true;
}
}
@ -45,10 +50,10 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override bool DeleteGroup(IEnumerable<string> value)
{
var list = new List<WriteModel<DataAccess.Group>>();
var list = new List<WriteModel<Group>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<DataAccess.Group>(Builders<DataAccess.Group>.Filter.Eq(g => g.Id, id)));
list.Add(new DeleteOneModel<Group>(Builders<Group>.Filter.Eq(g => g.Id, id)));
}
MongoDbAccessManager.Groups.BulkWrite(list);
return true;
@ -79,6 +84,18 @@ namespace Bootstrap.DataAccess.MongoDB
return true;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public override IEnumerable<DataAccess.Group> RetrieveGroupsByRoleId(string roleId)
{
var groups = GroupHelper.RetrieveGroups().Cast<Group>().ToList();
groups.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : "");
return groups;
}
/// <summary>
///
/// </summary>
@ -87,7 +104,25 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override bool SaveGroupsByRoleId(string roleId, IEnumerable<string> groupIds)
{
return base.SaveGroupsByRoleId(roleId, groupIds);
var groups = MongoDbAccessManager.Groups.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
// Remove roles
groups.ForEach(p =>
{
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
roles.Remove(roleId);
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
});
groups = MongoDbAccessManager.Groups.Find(md => groupIds.Contains(md.Id)).ToList();
// Add roles
groups.ForEach(p =>
{
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
roles.Add(roleId);
MongoDbAccessManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
});
return true;
}
}
}

View File

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