2018-10-30 13:07:29 +08:00
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-31 09:51:28 +08:00
|
|
|
|
using System.Linq;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Group : DataAccess.Group
|
|
|
|
|
{
|
2018-10-31 16:03:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<string> Roles { get; set; }
|
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Group> Retrieves()
|
2018-10-30 13:07:29 +08:00
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
return DbManager.Groups.Find(FilterDefinition<Group>.Empty).ToList();
|
2018-10-31 09:51:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Save(DataAccess.Group p)
|
2018-10-31 09:51:28 +08:00
|
|
|
|
{
|
|
|
|
|
if (p.Id == "0")
|
|
|
|
|
{
|
|
|
|
|
p.Id = null;
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Groups.InsertOne(new Group() {
|
2018-11-01 10:39:31 +08:00
|
|
|
|
GroupName = p.GroupName,
|
|
|
|
|
Description = p.Description,
|
|
|
|
|
Roles = new List<string>()
|
|
|
|
|
});
|
2018-10-31 09:51:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.GroupName, p.GroupName).Set(md => md.Description, p.Description));
|
2018-10-31 09:51:28 +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 09:51:28 +08:00
|
|
|
|
{
|
2018-10-31 16:03:49 +08:00
|
|
|
|
var list = new List<WriteModel<Group>>();
|
2018-10-31 09:51:28 +08:00
|
|
|
|
foreach (var id in value)
|
|
|
|
|
{
|
2018-10-31 16:03:49 +08:00
|
|
|
|
list.Add(new DeleteOneModel<Group>(Builders<Group>.Filter.Eq(g => g.Id, id)));
|
2018-10-31 09:51:28 +08:00
|
|
|
|
}
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Groups.BulkWrite(list);
|
2018-10-31 09:51:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Group> RetrievesByUserId(string userId)
|
2018-10-31 09:51:28 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var groups = GroupHelper.Retrieves();
|
|
|
|
|
var user = UserHelper.Retrieves().Cast<User>().FirstOrDefault(u => u.Id == userId);
|
2018-10-31 09:51:28 +08:00
|
|
|
|
groups.ToList().ForEach(g => g.Checked = user.Groups.Any(id => id == g.Id) ? "checked" : "");
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <param name="groupIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByUserId(string userId, IEnumerable<string> groupIds)
|
2018-10-31 09:51:28 +08:00
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Users.FindOneAndUpdate(u => u.Id == userId, Builders<User>.Update.Set(u => u.Groups, groupIds));
|
2018-10-31 09:51:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 16:03:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.Group> RetrievesByRoleId(string roleId)
|
2018-10-31 16:03:49 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var groups = GroupHelper.Retrieves().Cast<Group>().ToList();
|
2018-10-31 16:03:49 +08:00
|
|
|
|
groups.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : "");
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 09:51:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <param name="groupIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByRoleId(string roleId, IEnumerable<string> groupIds)
|
2018-10-31 09:51:28 +08:00
|
|
|
|
{
|
2019-01-15 12:52:09 +08:00
|
|
|
|
var groups = DbManager.Groups.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
2018-10-31 16:03:49 +08:00
|
|
|
|
|
|
|
|
|
// Remove roles
|
|
|
|
|
groups.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
|
|
|
|
roles.Remove(roleId);
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
2018-10-31 16:03:49 +08:00
|
|
|
|
});
|
|
|
|
|
|
2019-01-15 12:52:09 +08:00
|
|
|
|
groups = DbManager.Groups.Find(md => groupIds.Contains(md.Id)).ToList();
|
2018-10-31 16:03:49 +08:00
|
|
|
|
// Add roles
|
|
|
|
|
groups.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
|
|
|
|
roles.Add(roleId);
|
2019-01-15 12:52:09 +08:00
|
|
|
|
DbManager.Groups.UpdateOne(md => md.Id == p.Id, Builders<Group>.Update.Set(md => md.Roles, roles));
|
2018-10-31 16:03:49 +08:00
|
|
|
|
});
|
|
|
|
|
return true;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2018-10-31 17:47:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<string> RetrievesByUserName(string userName)
|
2018-10-31 17:47:50 +08:00
|
|
|
|
{
|
|
|
|
|
var groups = new List<string>();
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var user = UserHelper.Retrieves().Cast<User>().FirstOrDefault(u => u.UserName == userName);
|
|
|
|
|
var group = GroupHelper.Retrieves();
|
2018-10-31 17:47:50 +08:00
|
|
|
|
|
|
|
|
|
groups.AddRange(user.Groups.Select(r => group.FirstOrDefault(rl => rl.Id == r).GroupName));
|
|
|
|
|
if (groups.Count == 0) groups.Add("Default");
|
|
|
|
|
return groups;
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|