2018-10-30 13:07:29 +08:00
|
|
|
|
using Bootstrap.Security;
|
|
|
|
|
using Longbow.Security.Cryptography;
|
|
|
|
|
using MongoDB.Driver;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2018-10-30 19:01:49 +08:00
|
|
|
|
using System.Linq;
|
2018-10-30 13:07:29 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.MongoDB
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class User : DataAccess.User
|
|
|
|
|
{
|
2018-10-30 19:01:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<string> Roles { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<string> Groups { get; set; }
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override BootstrapUser RetrieveUserByUserName(string userName)
|
|
|
|
|
{
|
2018-10-30 19:01:49 +08:00
|
|
|
|
var project = Builders<User>.Projection.Include(u => u.Id)
|
|
|
|
|
.Include(u => u.UserName)
|
|
|
|
|
.Include(u => u.DisplayName)
|
|
|
|
|
.Include(u => u.Icon)
|
|
|
|
|
.Include(u => u.Css);
|
|
|
|
|
return MongoDbAccessManager.Users.Find(user => user.UserName == userName).Project<DataAccess.User>(project).FirstOrDefault();
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override bool Authenticate(string userName, string password)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false;
|
|
|
|
|
|
2018-10-30 19:01:49 +08:00
|
|
|
|
var u = MongoDbAccessManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
|
2018-10-30 13:07:29 +08:00
|
|
|
|
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
|
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 19:01:49 +08:00
|
|
|
|
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 17:02:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.User> Retrieves()
|
2018-10-30 17:02:36 +08:00
|
|
|
|
{
|
2018-10-30 19:01:49 +08:00
|
|
|
|
var project = Builders<User>.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 MongoDbAccessManager.Users.Find(user => user.ApprovedTime != DateTime.MinValue).Project<User>(project).ToList();
|
2018-10-30 17:02:36 +08:00
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 17:02:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Save(DataAccess.User user)
|
2018-10-30 17:02:36 +08:00
|
|
|
|
{
|
2018-10-30 19:01:49 +08:00
|
|
|
|
// 已经存在或者已经在新用户中了
|
|
|
|
|
if (UserHelper.RetrieveUserByUserName(user.UserName) != null || UserHelper.RetrieveNewUsers().Any(u => u.UserName == user.UserName)) return false;
|
|
|
|
|
|
2018-10-30 17:02:36 +08:00
|
|
|
|
if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500);
|
|
|
|
|
if (user.UserName.Length > 50) user.UserName = user.UserName.Substring(0, 50);
|
2018-11-01 10:34:22 +08:00
|
|
|
|
MongoDbAccessManager.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<string>(),
|
|
|
|
|
Groups = new List<string>(),
|
2019-01-11 23:20:28 +08:00
|
|
|
|
Icon = $"{DictHelper.RetrieveIconFolderPath()}default.jpg",
|
2018-11-01 10:34:22 +08:00
|
|
|
|
Description = user.Description
|
|
|
|
|
});
|
2018-10-30 17:02:36 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 17:02:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
/// <param name="displayName"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool Update(string id, string password, string displayName)
|
2018-10-30 17:02:36 +08:00
|
|
|
|
{
|
|
|
|
|
var passSalt = LgbCryptography.GenerateSalt();
|
|
|
|
|
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
|
2018-10-30 19:01:49 +08:00
|
|
|
|
var update = Builders<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName);
|
|
|
|
|
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.Id == id, update);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 19:01:49 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName"></param>
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
/// <param name="newPass"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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<User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt);
|
|
|
|
|
MongoDbAccessManager.Users.FindOneAndUpdate(u => u.UserName == UserName, update);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
2018-10-30 19:01:49 +08:00
|
|
|
|
/// <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-30 19:01:49 +08:00
|
|
|
|
{
|
|
|
|
|
var list = new List<WriteModel<User>>();
|
|
|
|
|
foreach (var id in value)
|
|
|
|
|
{
|
|
|
|
|
list.Add(new DeleteOneModel<User>(Builders<User>.Filter.Eq(u => u.Id, id)));
|
|
|
|
|
}
|
|
|
|
|
MongoDbAccessManager.Users.BulkWrite(list);
|
2018-10-30 17:02:36 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-31 15:49:30 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override IEnumerable<DataAccess.User> RetrievesByRoleId(string roleId)
|
2018-10-31 15:49:30 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var users = UserHelper.Retrieves().Cast<User>().ToList();
|
2018-10-31 15:49:30 +08:00
|
|
|
|
users.ForEach(p => p.Checked = (p.Roles != null && p.Roles.Contains(roleId)) ? "checked" : "");
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <param name="userIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByRoleId(string roleId, IEnumerable<string> userIds)
|
2018-10-31 15:49:30 +08:00
|
|
|
|
{
|
|
|
|
|
var users = MongoDbAccessManager.Users.Find(md => md.Roles != null && md.Roles.Contains(roleId)).ToList();
|
|
|
|
|
|
|
|
|
|
// Remove roles
|
|
|
|
|
users.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
|
|
|
|
roles.Remove(roleId);
|
|
|
|
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
users = MongoDbAccessManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
|
|
|
|
// Add roles
|
|
|
|
|
users.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var roles = p.Roles == null ? new List<string>() : p.Roles.ToList();
|
|
|
|
|
roles.Add(roleId);
|
|
|
|
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Roles, roles));
|
|
|
|
|
});
|
|
|
|
|
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.User> RetrievesByGroupId(string groupId)
|
2018-10-31 17:22:06 +08:00
|
|
|
|
{
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var users = UserHelper.Retrieves().Cast<User>().ToList();
|
2018-10-31 17:22:06 +08:00
|
|
|
|
users.ForEach(p => p.Checked = (p.Groups != null && p.Groups.Contains(groupId)) ? "checked" : "");
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="groupId"></param>
|
|
|
|
|
/// <param name="userIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 23:20:28 +08:00
|
|
|
|
public override bool SaveByGroupId(string groupId, IEnumerable<string> userIds)
|
2018-10-31 17:22:06 +08:00
|
|
|
|
{
|
|
|
|
|
var users = MongoDbAccessManager.Users.Find(md => md.Groups != null && md.Groups.Contains(groupId)).ToList();
|
|
|
|
|
|
|
|
|
|
// Remove roles
|
|
|
|
|
users.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
|
|
|
|
groups.Remove(groupId);
|
|
|
|
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
users = MongoDbAccessManager.Users.Find(md => userIds.Contains(md.Id)).ToList();
|
|
|
|
|
// Add roles
|
|
|
|
|
users.ForEach(p =>
|
|
|
|
|
{
|
|
|
|
|
var groups = p.Groups == null ? new List<string>() : p.Groups.ToList();
|
|
|
|
|
groups.Add(groupId);
|
|
|
|
|
MongoDbAccessManager.Users.UpdateOne(md => md.Id == p.Id, Builders<User>.Update.Set(md => md.Groups, groups));
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|