重构代码:根据Bootstrap.Security组件变化,更改UserHelper类,重新整理缓存配置项

This commit is contained in:
Argo-MacBookPro 2018-09-10 18:35:25 +08:00
parent a374a32e24
commit 25d7e5f86d
4 changed files with 96 additions and 38 deletions

View File

@ -1,6 +1,5 @@
using Bootstrap.Admin.Query;
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -44,9 +43,9 @@ namespace Bootstrap.Admin.Controllers.Api
if (value.UserName.Equals(User.Identity.Name, System.StringComparison.OrdinalIgnoreCase))
{
if (value.UserStatus == UserStates.ChangeDisplayName)
ret = BootstrapUser.SaveDisplayName(value.UserName, value.DisplayName);
ret = UserHelper.SaveDisplayName(value.UserName, value.DisplayName);
else if (value.UserStatus == UserStates.ChangePassword)
ret = BootstrapUser.ChangePassword(value.UserName, value.Password, value.NewPassword);
ret = UserHelper.ChangePassword(value.UserName, value.Password, value.NewPassword);
}
return ret;
}

View File

@ -46,182 +46,196 @@
"Key": "BootstrapAdminAuthorizeFilter-RetrieveRolesByUrl",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台菜单获得所有可访问角色数据缓存"
"Desc": "通过菜单获得所有角色数据"
},
{
"Enabled": true,
"Key": "BootstrapAdminGroupMiddleware-RetrieveGroups",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理所有组数据缓存"
"Desc": "所有组数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapAdminGroupMiddleware-RetrieveGroupsByUserName",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理特定用户所属所有组数据缓存"
"Desc": "指定用户所有组数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapAdminRoleMiddleware-RetrieveRoles",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理所有角色数据缓存"
"Desc": "所有角色数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapAdminRoleMiddleware-RetrieveRolesByUserName",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理特定用户所属所有角色数据缓存"
"Desc": "指定用户角色数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapDict-RetrieveDicts",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理所有字典数据缓存"
"Desc": "所有字典数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapMenu-RetrieveMenus",
"Key": "BootstrapMenu-RetrieveMenusByUserName",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理所有菜单数据缓存"
"Desc": "用户所有菜单数据缓存"
},
{
"Enabled": true,
"Key": "BootstrapUser-RetrieveUsers",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理所有用户数据缓存"
"Desc": "所有用户数据"
},
{
"Enabled": true,
"Key": "BootstrapUser-RetrieveUsersByName",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "后台管理通过登录名获得当前用户数据缓存"
"Desc": "登录用户数据"
},
{
"Enabled": true,
"Key": "BootstrapUser-RetrieveUsersByRoleId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "通过角色ID获得所有用户数据"
},
{
"Enabled": true,
"Key": "BootstrapUser-RetrieveUsersByGroupId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "通过部门ID获得所有用户数据"
},
{
"Enabled": true,
"Key": "UserHelper-RetrieveNewUsers",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "新用户数据缓存"
"Desc": "新用户数据"
},
{
"Enabled": true,
"Key": "MenuHelper-RetrieveMenusByRoleId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "角色菜单信息缓存"
"Desc": "通过角色ID获得所有菜单数据"
},
{
"Enabled": true,
"Key": "RoleHelper-RetrieveRoles",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "所有角色数据缓存"
"Desc": "所有角色数据"
},
{
"Enabled": true,
"Key": "RoleHelper-RetrieveRolesByUserId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "指定用户角色数据缓存"
"Desc": "通过用户ID获得所有角色数据"
},
{
"Enabled": true,
"Key": "RoleHelper-RetrieveRolesByMenuId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "指定菜单角色数据缓存"
"Desc": "通过菜单ID获得所有角色数据"
},
{
"Enabled": true,
"Key": "RoleHelper-RetrieveRolesByGroupId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "指定组角色数据缓存"
"Desc": "通过部门ID获得所有角色数据"
},
{
"Enabled": true,
"Key": "GroupHelper-RetrieveGroups",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "所有组数据缓存"
"Desc": "所有部门数据"
},
{
"Enabled": true,
"Key": "GroupHelper-RetrieveGroupsByUserId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "指定用户组数据缓存"
"Desc": "通过用户ID获得所有部门数据"
},
{
"Enabled": true,
"Key": "GroupHelper-RetrieveGroupsByRoleId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "指定角色组数据缓存"
"Desc": "通过角色ID获得所有部门数据"
},
{
"Enabled": true,
"Key": "LogHelper-RetrieveLogs",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "所有日志数据缓存"
"Desc": "所有日志数据"
},
{
"Enabled": true,
"Key": "DictHelper-RetrieveDictsCategory",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "字典分类数据缓存"
"Desc": "字典分类数据"
},
{
"Enabled": true,
"Key": "NotificationHelper-RetrieveNotifications",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "通知管理数据缓存"
"Desc": "通知管理数据"
},
{
"Enabled": true,
"Key": "ExceptionHelper-RetrieveExceptions",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "程序异常数据缓存"
"Desc": "程序异常数据"
},
{
"Enabled": true,
"Key": "MessageHelper-RetrieveMessages",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "站内消息数据缓存"
"Desc": "站内消息数据"
},
{
"Enabled": true,
"Key": "TaskHelper-RetrieveTasks",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "所有任务数据缓存"
"Desc": "所有任务数据"
},
{
"Enabled": true,
"Key": "WebApi",
"Interval": 21600,
"SlidingExpiration": true,
"Desc": "WebApi 数据缓存"
"Desc": "WebApi 数据"
},
{
"Enabled": true,
"Key": "Token",
"Interval": 21600,
"SlidingExpiration": true,
"Desc": "Token 数据缓存"
"Desc": "Token 数据"
},
{
"Enabled": true,

View File

@ -45,8 +45,8 @@ namespace Bootstrap.DataAccess
corsKeys.Add(BootstrapMenu.RetrieveMenusDataKey + "*");
});
cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*");
cacheKeys.Add(BootstrapUser.RetrieveUsersDataKey + "*");
corsKeys.Add(BootstrapUser.RetrieveUsersDataKey + "*");
cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
corsKeys.Add(UserHelper.RetrieveUsersDataKey + "*");
}
if (groupIds != null)
{
@ -79,6 +79,7 @@ namespace Bootstrap.DataAccess
if (cacheKey != null)
{
cacheKeys.Add(cacheKey);
corsKeys.Add(cacheKey);
}
CacheManager.Clear(cacheKeys);
CacheManager.CorsClear(corsKeys);

View File

@ -17,10 +17,11 @@ namespace Bootstrap.DataAccess
/// </summary>
public static class UserHelper
{
private const string RetrieveUsersByNameDataKey = "BootstrapUser-RetrieveUsersByName";
internal const string RetrieveUsersDataKey = "BootstrapUser-RetrieveUsers";
internal const string RetrieveUsersByRoleIdDataKey = "BootstrapUser-RetrieveUsersByRoleId";
internal const string RetrieveUsersByGroupIdDataKey = "BootstrapUser-RetrieveUsersByGroupId";
internal const string RetrieveNewUsersDataKey = "UserHelper-RetrieveNewUsers";
/// <summary>
/// 查询所有用户
/// </summary>
@ -28,7 +29,7 @@ namespace Bootstrap.DataAccess
/// <returns></returns>
public static IEnumerable<User> RetrieveUsers()
{
return CacheManager.GetOrAdd(BootstrapUser.RetrieveUsersDataKey, key =>
return CacheManager.GetOrAdd(RetrieveUsersDataKey, key =>
{
List<User> users = new List<User>();
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null");
@ -147,6 +148,32 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="newPass"></param>
/// <returns></returns>
public static bool ChangePassword(string userName, string password, string newPass)
{
bool ret = false;
if (BootstrapUser.Authenticate(userName, password))
{
string sql = "Update Users set Password = @Password, PassSalt = @PassSalt where UserName = @userName";
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(newPass, passSalt);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", newPassword));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", passSalt));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
ret = true;
}
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="rejectBy"></param>
/// <param name="reason"></param>
@ -327,13 +354,31 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
ret = true;
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public static bool SaveDisplayName(string userName, string displayName)
{
bool ret = false;
string sql = "Update Users set DisplayName = @DisplayName where UserName = @userName";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
}
return ret;
}
/// <summary>
/// 根据用户名更改用户皮肤
/// </summary>
/// <param name="userName"></param>
@ -348,8 +393,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName);
CacheCleanUtility.ClearCache(cacheKey: key);
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
ret = true;
}
return ret;