重构代码:移动User缓存处理到UserHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-30 19:01:49 +08:00
parent 4f3b021eb7
commit f912f0cacb
7 changed files with 164 additions and 60 deletions

View File

@ -20,7 +20,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public QueryData<User> Get(QueryUserOption value)
public QueryData<object> Get(QueryUserOption value)
{
return value.RetrieveData();
}

View File

@ -21,7 +21,7 @@ namespace Bootstrap.Admin.Query
///
/// </summary>
/// <returns></returns>
public QueryData<User> RetrieveData()
public QueryData<object> RetrieveData()
{
// int limit, int offset, string name, string price, string sort, string order
var data = UserHelper.RetrieveUsers();
@ -33,7 +33,7 @@ namespace Bootstrap.Admin.Query
{
data = data.Where(t => t.DisplayName.Contains(DisplayName));
}
var ret = new QueryData<User>();
var ret = new QueryData<object>();
ret.total = data.Count();
switch (Sort)
{
@ -55,7 +55,16 @@ namespace Bootstrap.Admin.Query
default:
break;
}
ret.rows = data.Skip(Offset).Take(Limit);
ret.rows = data.Skip(Offset).Take(Limit).Select(u => new
{
u.Id,
u.UserName,
u.DisplayName,
u.RegisterTime,
u.ApprovedTime,
u.ApprovedBy,
u.Description
});
return ret;
}
}

View File

@ -36,6 +36,26 @@ namespace Bootstrap.DataAccess.MongoDB
return _db;
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.User> Users
{
get
{
return DBAccess.GetCollection<DataAccess.User>("Users");
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<User> Users
{
get
{
return DBAccess.GetCollection<User>("Users");
}
}
private static void InitDb()
{
@ -61,14 +81,6 @@ namespace Bootstrap.DataAccess.MongoDB
md.IdMemberMap.SetIgnoreIfDefault(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapUser)))
{
BsonClassMap.RegisterClassMap<BootstrapUser>(md =>
{
md.AutoMap();
md.SetIgnoreExtraElements(true);
});
}
if (!BsonClassMap.IsClassMapRegistered(typeof(DataAccess.User)))
{
BsonClassMap.RegisterClassMap<DataAccess.User>(md =>

View File

@ -3,6 +3,7 @@ using Longbow.Security.Cryptography;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.MongoDB
{
@ -11,6 +12,15 @@ namespace Bootstrap.DataAccess.MongoDB
/// </summary>
public class User : DataAccess.User
{
/// <summary>
///
/// </summary>
public IEnumerable<string> Roles { get; set; }
/// <summary>
///
/// </summary>
public IEnumerable<string> Groups { get; set; }
/// <summary>
///
/// </summary>
@ -18,8 +28,12 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override BootstrapUser RetrieveUserByUserName(string userName)
{
var users = MongoDbAccessManager.DBAccess.GetCollection<BootstrapUser>("Users");
return users.Find(user => user.UserName == userName).FirstOrDefault();
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();
}
/// <summary>
///
@ -31,27 +45,30 @@ namespace Bootstrap.DataAccess.MongoDB
{
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(password)) return false;
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.User>("Users");
var u = users.Find(user => user.UserName == userName).FirstOrDefault();
var u = MongoDbAccessManager.Users.Find(user => user.UserName == userName).FirstOrDefault();
return !string.IsNullOrEmpty(u.PassSalt) && u.Password == LgbCryptography.ComputeHash(password, u.PassSalt);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.User> RetrieveNewUsers()
{
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.User>("Users");
return users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
}
public override IEnumerable<DataAccess.User> RetrieveNewUsers() => MongoDbAccessManager.Users.Find(user => user.ApprovedTime == DateTime.MinValue).SortByDescending(user => user.RegisterTime).ToList();
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.User> RetrieveUsers()
{
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.User>("Users");
return users.Find(user => user.ApprovedTime != DateTime.MinValue).ToList();
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();
}
/// <summary>
///
@ -60,6 +77,9 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override bool SaveUser(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);
user.Id = null;
@ -68,8 +88,7 @@ namespace Bootstrap.DataAccess.MongoDB
user.RegisterTime = DateTime.Now;
user.ApprovedTime = DateTime.Now;
user.Icon = $"{DictHelper.RetrieveIconFolderPath().Code}default.jpg";
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.User>("Users");
users.InsertOne(user);
MongoDbAccessManager.Users.InsertOne(user as User);
return true;
}
/// <summary>
@ -83,9 +102,42 @@ namespace Bootstrap.DataAccess.MongoDB
{
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
var update = Builders<DataAccess.User>.Update.Set(u => u.Password, newPassword).Set(u => u.PassSalt, passSalt).Set(u => u.DisplayName, displayName);
var users = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.User>("Users");
users.FindOneAndUpdate(u => u.Id == id, update);
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;
}
/// <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;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteUser(IEnumerable<string> value)
{
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);
return true;
}
}

View File

@ -38,7 +38,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: value);
ret = true;
}
}
@ -86,7 +85,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
ret = true;
}
}
@ -114,7 +112,6 @@ namespace Bootstrap.DataAccess.SQLite
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", approvedBy));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
}
return ret;
}
@ -146,7 +143,6 @@ namespace Bootstrap.DataAccess.SQLite
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
ret = true;
}
}
@ -167,10 +163,6 @@ namespace Bootstrap.DataAccess.SQLite
public override bool SaveUsersByRoleId(string roleId, IEnumerable<string> userIds)
{
bool ret = false;
DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("UserID", typeof(int));
userIds.ToList().ForEach(userId => dt.Rows.Add(roleId, userId));
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{
try
@ -188,7 +180,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)
@ -225,7 +216,6 @@ namespace Bootstrap.DataAccess.SQLite
});
transaction.CommitTransaction();
}
CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<string>() { groupId });
ret = true;
}
catch (Exception ex)

View File

@ -37,13 +37,23 @@ namespace Bootstrap.DataAccess
/// 删除用户
/// </summary>
/// <param name="value"></param>
public static bool DeleteUser(IEnumerable<string> value) => DbAdapterManager.Create<User>().DeleteUser(value);
public static bool DeleteUser(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<User>().DeleteUser(value);
if (ret) CacheCleanUtility.ClearCache(userIds: value);
return ret;
}
/// <summary>
/// 保存新建
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveUser(User p) => DbAdapterManager.Create<User>().SaveUser(p);
public static bool SaveUser(User p)
{
var ret = DbAdapterManager.Create<User>().SaveUser(p);
if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
return ret;
}
/// <summary>
///
/// </summary>
@ -51,14 +61,24 @@ namespace Bootstrap.DataAccess
/// <param name="password"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public static bool UpdateUser(string id, string password, string displayName) => DbAdapterManager.Create<User>().UpdateUser(id, password, displayName);
public static bool UpdateUser(string id, string password, string displayName)
{
var ret = DbAdapterManager.Create<User>().UpdateUser(id, password, displayName);
if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(id) ? new List<string>() : new List<string>() { id });
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
public static bool ApproveUser(string id, string approvedBy) => DbAdapterManager.Create<User>().ApproveUser(id, approvedBy);
public static bool ApproveUser(string id, string approvedBy)
{
var ret = DbAdapterManager.Create<User>().ApproveUser(id, approvedBy);
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
return ret;
}
/// <summary>
///
/// </summary>
@ -74,7 +94,12 @@ namespace Bootstrap.DataAccess
/// <param name="rejectBy"></param>
/// <param name="reason"></param>
/// <returns></returns>
public static bool RejectUser(string id, string rejectBy) => DbAdapterManager.Create<User>().RejectUser(id, rejectBy);
public static bool RejectUser(string id, string rejectBy)
{
var ret = DbAdapterManager.Create<User>().RejectUser(id, rejectBy);
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
return ret;
}
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
@ -84,10 +109,15 @@ namespace Bootstrap.DataAccess
/// <summary>
/// 通过角色ID保存当前授权用户插入
/// </summary>
/// <param name="id">角色ID</param>
/// <param name="roleId">角色ID</param>
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
public static bool SaveUsersByRoleId(string id, IEnumerable<string> userIds) => DbAdapterManager.Create<User>().SaveUsersByRoleId(id, userIds);
public static bool SaveUsersByRoleId(string roleId, IEnumerable<string> userIds)
{
var ret = DbAdapterManager.Create<User>().SaveUsersByRoleId(roleId, userIds);
if (ret) CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<string>() { roleId });
return ret;
}
/// <summary>
/// 通过groupId获取所有用户
/// </summary>
@ -97,30 +127,51 @@ namespace Bootstrap.DataAccess
/// <summary>
/// 通过部门ID保存当前授权用户插入
/// </summary>
/// <param name="id">GroupID</param>
/// <param name="groupId">GroupID</param>
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
public static bool SaveUsersByGroupId(string id, IEnumerable<string> userIds) => DbAdapterManager.Create<User>().SaveUsersByGroupId(id, userIds);
public static bool SaveUsersByGroupId(string groupId, IEnumerable<string> userIds)
{
var ret = DbAdapterManager.Create<User>().SaveUsersByGroupId(groupId, userIds);
if (ret) CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<string>() { groupId });
return ret;
}
/// 根据用户名修改用户头像
/// </summary>
/// <param name="userName"></param>
/// <param name="iconName"></param>
/// <returns></returns>
public static bool SaveUserIconByName(string userName, string iconName) => DbAdapterManager.Create<User>().SaveUserIconByName(userName, iconName);
public static bool SaveUserIconByName(string userName, string iconName)
{
var ret = DbAdapterManager.Create<User>().SaveUserIconByName(userName, iconName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public static bool SaveDisplayName(string userName, string displayName) => DbAdapterManager.Create<User>().SaveDisplayName(userName, displayName);
public static bool SaveDisplayName(string userName, string displayName)
{
var ret = DbAdapterManager.Create<User>().SaveDisplayName(userName, displayName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
return ret;
}
/// <summary>
/// 根据用户名更改用户皮肤
/// </summary>
/// <param name="userName"></param>
/// <param name="cssName"></param>
/// <returns></returns>
public static bool SaveUserCssByName(string userName, string cssName) => DbAdapterManager.Create<User>().SaveUserCssByName(userName, cssName);
public static bool SaveUserCssByName(string userName, string cssName)
{
var ret = DbAdapterManager.Create<User>().SaveUserCssByName(userName, cssName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
return ret;
}
/// <summary>
///
/// </summary>

View File

@ -178,7 +178,6 @@ namespace Bootstrap.DataAccess
{
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
if (ret) CacheCleanUtility.ClearCache(userIds: value);
}
return ret;
}
@ -203,7 +202,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
}
return ret;
}
@ -227,7 +225,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Password", newPassword));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@PassSalt", passSalt));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(userIds: string.IsNullOrEmpty(id) ? new List<string>() : new List<string>() { id });
}
return ret;
}
@ -246,7 +243,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", approvedBy));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
}
return ret;
}
@ -265,7 +261,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@rejectedBy", rejectBy));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@rejectedReason", "未填写"));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
if (ret) CacheCleanUtility.ClearCache(userIds: new List<string>() { id });
}
return ret;
}
@ -327,7 +322,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<string>() { roleId });
ret = true;
}
catch (Exception ex)
@ -397,7 +391,6 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction();
}
}
CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<string>() { groupId });
ret = true;
}
catch (Exception ex)
@ -423,7 +416,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
}
return ret;
}
@ -442,7 +434,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
}
return ret;
}
@ -461,7 +452,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@cssName", DbAdapterManager.ToDBValue(cssName)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", userName));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{UserHelper.RetrieveUsersDataKey}*");
}
return ret;
}