using Bootstrap.Security;
using Longbow.Security.Cryptography;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
///
///
///
public class User : DataAccess.User
{
///
///
///
public IEnumerable Roles { get; set; }
///
///
///
public IEnumerable Groups { get; set; }
///
///
///
///
///
public override BootstrapUser RetrieveUserByUserName(string userName)
{
var project = Builders.Projection.Include(u => u.Id)
.Include(u => u.UserName)
.Include(u => u.DisplayName)
.Include(u => u.Icon)
.Include(u => u.Css);
return DbManager.Users.Find(user => user.UserName == userName).Project(project).FirstOrDefault();
}
///
///
///
///
///
///
public override bool Authenticate(string userName, string password)
{
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false;
var u = DbManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
}
///
///
///
///
public override IEnumerable RetrieveNewUsers() => DbManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
///
///
///
///
public override IEnumerable Retrieves()
{
var project = Builders.Projection.Include(u => u.Id)
.Include(u => u.UserName)
.Include(u => u.DisplayName)
.Include(u => u.RegisterTime)
.Include(u => u.ApprovedTime)
.Include(u => u.ApprovedBy)
.Include(u => u.Description)
.Include(u => u.Groups)
.Include(u => u.Roles);
return DbManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project(project).ToList();
}
///
///
///
///
///
public override bool Save(DataAccess.User user)
{
// 已经存在或者已经在新用户中了
if (UserHelper.RetrieveUserByUserName(user.UserName) != null || UserHelper.RetrieveNewUsers().Any(u => u.UserName == user.UserName)) return false;
if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500);
if (user.UserName.Length > 50) user.UserName = user.UserName.Substring(0, 50);
DbManager.Users.InsertOne(new User()
{
UserName = user.UserName,
DisplayName = user.DisplayName,
PassSalt = LgbCryptography.GenerateSalt(),
Password = LgbCryptography.ComputeHash(user.Password, user.PassSalt),
RegisterTime = DateTime.Now,
ApprovedTime = DateTime.Now,
ApprovedBy = user.ApprovedBy,
Roles = new List(),
Groups = new List(),
Icon = $"{DictHelper.RetrieveIconFolderPath()}default.jpg",
Description = user.Description
});
return true;
}
///
///
///
///
///
///
///
public override bool Update(string id, string password, string displayName)
{
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
var update = Builders.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName);
DbManager.Users.FindOneAndUpdate(u => u.Id == id, update);
return true;
}
///
///
///
///
///
///
///
public override bool ChangePassword(string userName, string password, string newPass)
{
bool ret = false;
if (Authenticate(userName, password))
{
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
var update = Builders.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt);
DbManager.Users.FindOneAndUpdate(u => u.UserName == UserName, update);
}
return ret;
}
///
///
///
///
///
public override bool Delete(IEnumerable value)
{
var list = new List>();
foreach (var id in value)
{
list.Add(new DeleteOneModel(Builders.Filter.Eq(u => u.Id, id)));
}
DbManager.Users.BulkWrite(list);
return true;
}
///
///
///
///
///
public override IEnumerable RetrievesByRoleId(string roleId)
{
var users = UserHelper.Retrieves().Cast().ToList();
users.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : "");
return users;
}
///
///
///
///
///
///
public override bool SaveByRoleId(string roleId, IEnumerable userIds)
{
var users = DbManager.Users.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
// Remove roles
users.ForEach(p =>
{
var roles = p.Roles == null ? new List() : p.Roles.ToList();
roles.Remove(roleId);
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Roles, roles));
});
users = DbManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
// Add roles
users.ForEach(p =>
{
var roles = p.Roles == null ? new List() : p.Roles.ToList();
roles.Add(roleId);
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Roles, roles));
});
return true;
}
///
///
///
///
///
public override IEnumerable RetrievesByGroupId(string groupId)
{
var users = UserHelper.Retrieves().Cast().ToList();
users.ForEach(p => p.Checked = (p.Groups != null && p.Groups.Contains(groupId)) ? "checked" : "");
return users;
}
///
///
///
///
///
///
public override bool SaveByGroupId(string groupId, IEnumerable userIds)
{
var users = DbManager.Users.Find(md => md.Groups != null && md.Groups.Contains(groupId)).ToList();
// Remove roles
users.ForEach(p =>
{
var groups = p.Groups == null ? new List() : p.Groups.ToList();
groups.Remove(groupId);
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Groups, groups));
});
users = DbManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
// Add roles
users.ForEach(p =>
{
var groups = p.Groups == null ? new List() : p.Groups.ToList();
groups.Add(groupId);
DbManager.Users.UpdateOne(md => md.Id == p.Id, Builders.Update.Set(md => md.Groups, groups));
});
return true;
}
}
}