Compare commits

...

6 Commits

Author SHA1 Message Date
zhangpeihang 6bd80fbe59 feat: 完善 Role 服务 2022-05-01 12:25:25 +08:00
zhangpeihang 9a6acf7223 feat: 完善 Nav 服务 2022-05-01 12:25:20 +08:00
zhangpeihang 1543c2fbd9 feat: 完善 Role 服务 2022-05-01 12:25:17 +08:00
zhangpeihang a7aca22561 feat: 完善 APP 服务 2022-05-01 12:25:12 +08:00
zhangpeihang 60b759eea1 feat: 完善 Group 服务 2022-05-01 12:25:09 +08:00
zhangpeihang e7f0d7eb77 feat: 完善用户服务 2022-05-01 12:25:00 +08:00
5 changed files with 113 additions and 40 deletions

View File

@ -3,17 +3,20 @@
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.FreeSql.Models; using BootStarpAdmin.DataAccess.FreeSql.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
namespace BootStarpAdmin.DataAccess.FreeSql.Service; namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class AppService : IApp class AppService : IApp
{ {
private const string AppServiceGetAppsByRoleIdCacheKey = "AppService-GetAppsByRoleId";
private IFreeSql FreeSql { get; } private IFreeSql FreeSql { get; }
public AppService(IFreeSql freeSql) => FreeSql = freeSql; public AppService(IFreeSql freeSql) => FreeSql = freeSql;
public List<string> GetAppsByRoleId(string? roleId) => FreeSql.Ado.Query<string>("select AppID from RoleApp where RoleID = @roleId", new { roleId }); public List<string> GetAppsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{AppServiceGetAppsByRoleIdCacheKey}-{roleId}", entry => FreeSql.Ado.Query<string>("select AppID from RoleApp where RoleID = @roleId", new { roleId }));
public bool SaveAppsByRoleId(string? roleId, IEnumerable<string> appIds) public bool SaveAppsByRoleId(string? roleId, IEnumerable<string> appIds)
{ {
@ -31,6 +34,10 @@ class AppService : IApp
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
} }

View File

@ -3,6 +3,7 @@
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.FreeSql.Models; using BootStarpAdmin.DataAccess.FreeSql.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
@ -10,15 +11,21 @@ namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class GroupService : IGroup class GroupService : IGroup
{ {
private const string GroupServiceGetAllCacheKey = "GroupService-GetAll";
private const string GroupServiceGetGroupsByUserIdCacheKey = "GroupService-GetGroupsByUserId";
private const string GroupServiceGetGroupsByRoleIdCacheKey = "GroupService-GetGroupsByRoleId";
private IFreeSql FreeSql { get; } private IFreeSql FreeSql { get; }
public GroupService(IFreeSql freeSql) => FreeSql = freeSql; public GroupService(IFreeSql freeSql) => FreeSql = freeSql;
public List<Group> GetAll() => FreeSql.Select<Group>().ToList(); public List<Group> GetAll() => CacheManager.GetOrAdd(GroupServiceGetAllCacheKey, entry => FreeSql.Select<Group>().ToList());
public List<string> GetGroupsByRoleId(string? roleId) => FreeSql.Ado.Query<string>("select GroupID from RoleGroup where RoleID = @roleId", new { roleId }); public List<string> GetGroupsByRoleId(string? roleId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByRoleIdCacheKey}-{roleId}", entry => FreeSql.Ado.Query<string>("select GroupID from RoleGroup where RoleID = @roleId", new { roleId }));
public List<string> GetGroupsByUserId(string? userId) => FreeSql.Ado.Query<string>("select GroupID from UserGroup where UserID = @userId", new { userId }); public List<string> GetGroupsByUserId(string? userId) => CacheManager.GetOrAdd($"{GroupServiceGetGroupsByUserIdCacheKey}-{userId}", entry => FreeSql.Ado.Query<string>("select GroupID from UserGroup where UserID = @userId", new { userId }));
public bool SaveGroupsByRoleId(string? roleId, IEnumerable<string> groupIds) public bool SaveGroupsByRoleId(string? roleId, IEnumerable<string> groupIds)
{ {
@ -36,6 +43,10 @@ class GroupService : IGroup
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
@ -47,7 +58,7 @@ class GroupService : IGroup
FreeSql.Transaction(() => FreeSql.Transaction(() =>
{ {
FreeSql.Ado.ExecuteNonQuery("delete from UserGroup where UserID = @userId", new { userId }); FreeSql.Ado.ExecuteNonQuery("delete from UserGroup where UserID = @userId", new { userId });
FreeSql.Insert(groupIds.Select(g => new UserGroup { GroupID = g, UserID = userId })); FreeSql.Insert(groupIds.Select(g => new UserGroup { GroupID = g, UserID = userId })).ExecuteAffrows();
ret = true; ret = true;
}); });
} }
@ -55,6 +66,10 @@ class GroupService : IGroup
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
} }

View File

@ -3,6 +3,7 @@
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.FreeSql.Models; using BootStarpAdmin.DataAccess.FreeSql.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
@ -10,16 +11,17 @@ namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class NavigationService : INavigation class NavigationService : INavigation
{ {
private const string NavigationServiceGetAllCacheKey = "NavigationService-GetAll";
private const string NavigationServiceGetMenusByRoleIdCacheKey = "NavigationService-GetMenusByRoleId";
private IFreeSql FreeSql { get; } private IFreeSql FreeSql { get; }
public NavigationService(IFreeSql freeSql) => FreeSql = freeSql; public NavigationService(IFreeSql freeSql) => FreeSql = freeSql;
public List<Navigation> GetAllMenus(string userName) public List<Navigation> GetAllMenus(string userName) => CacheManager.GetOrAdd($"{NavigationServiceGetAllCacheKey}-{userName}", entry => FreeSql.Ado.Query<Navigation>($"select n.ID, n.ParentId, n.Name, n.[order], n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = @Define left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.[order]", new { UserName = userName, Category = "菜单", RoleName = "Administrators", Define = EnumDictDefine.System }));
{
return FreeSql.Ado.Query<Navigation>($"select n.ID, n.ParentId, n.Name, n.[order], n.Icon, n.Url, n.Category, n.Target, n.IsResource, n.Application, ln.Name as ParentName from Navigations n inner join Dicts d on n.Category = d.Code and d.Category = @Category and d.Define = @Define left join Navigations ln on n.ParentId = ln.ID inner join (select nr.NavigationID from Users u inner join UserRole ur on ur.UserID = u.ID inner join NavigationRole nr on nr.RoleID = ur.RoleID where u.UserName = @UserName union select nr.NavigationID from Users u inner join UserGroup ug on u.ID = ug.UserID inner join RoleGroup rg on rg.GroupID = ug.GroupID inner join NavigationRole nr on nr.RoleID = rg.RoleID where u.UserName = @UserName union select n.ID from Navigations n where EXISTS (select UserName from Users u inner join UserRole ur on u.ID = ur.UserID inner join Roles r on ur.RoleID = r.ID where u.UserName = @UserName and r.RoleName = @RoleName)) nav on n.ID = nav.NavigationID ORDER BY n.Application, n.[order]", new { UserName = userName, Category = "菜单", RoleName = "Administrators", Define = EnumDictDefine.System });
}
public List<string> GetMenusByRoleId(string? roleId) => FreeSql.Ado.Query<string>("select NavigationID from NavigationRole where RoleID = @roleId", new { roleId }); public List<string> GetMenusByRoleId(string? roleId) => CacheManager.GetOrAdd($"{NavigationServiceGetMenusByRoleIdCacheKey}-{roleId}", entry => FreeSql.Ado.Query<string>("select NavigationID from NavigationRole where RoleID = @roleId", new { roleId }));
public bool SaveMenusByRoleId(string? roleId, List<string> menuIds) public bool SaveMenusByRoleId(string? roleId, List<string> menuIds)
{ {
@ -37,6 +39,10 @@ class NavigationService : INavigation
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
} }

View File

@ -3,6 +3,7 @@
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.FreeSql.Models; using BootStarpAdmin.DataAccess.FreeSql.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
@ -10,20 +11,25 @@ namespace BootStarpAdmin.DataAccess.FreeSql.Service;
class RoleService : IRole class RoleService : IRole
{ {
private const string RoleServiceGetAllCacheKey = "RoleService-GetAll";
private const string RoleServiceGetRolesByUserIdCacheKey = "RoleService-GetRolesByUserId";
private const string RoleServiceGetRolesByGroupIdCacheKey = "RoleService-GetRolesByGroupId";
private const string RoleServiceGetRolesByMenuIdCacheKey = "RoleService-GetRolesByMenusId";
private IFreeSql FreeSql { get; } private IFreeSql FreeSql { get; }
public RoleService(IFreeSql freeSql) => FreeSql = freeSql; public RoleService(IFreeSql freeSql) => FreeSql = freeSql;
public List<Role> GetAll() public List<Role> GetAll() => CacheManager.GetOrAdd(RoleServiceGetAllCacheKey, EntryPointNotFoundException => FreeSql.Select<Role>().ToList());
{
return FreeSql.Select<Role>().ToList();
}
public List<string> GetRolesByGroupId(string? groupId) => FreeSql.Ado.Query<string>("select RoleID from RoleGroup where GroupID = @groupId", new { groupId }); public List<string> GetRolesByGroupId(string? groupId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByGroupIdCacheKey}-{groupId}", entry => FreeSql.Ado.Query<string>("select RoleID from RoleGroup where GroupID = @groupId", new { groupId }));
public List<string> GetRolesByMenuId(string? menuId) => FreeSql.Ado.Query<string>("select RoleID from NavigationRole where NavigationID = @menuId", new { menuId }); public List<string> GetRolesByMenuId(string? menuId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByMenuIdCacheKey}-{menuId}", entry => FreeSql.Ado.Query<string>("select RoleID from NavigationRole where NavigationID = @menuId", new { menuId }));
public List<string> GetRolesByUserId(string? userId) => FreeSql.Ado.Query<string>("select RoleID from UserRole where UserID = @userId", new { userId }); public List<string> GetRolesByUserId(string? userId) => CacheManager.GetOrAdd($"{RoleServiceGetRolesByUserIdCacheKey}-{userId}", entry => FreeSql.Ado.Query<string>("select RoleID from UserRole where UserID = @userId", new { userId }));
public bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds) public bool SaveRolesByGroupId(string? groupId, IEnumerable<string> roleIds)
{ {
@ -41,6 +47,10 @@ class RoleService : IRole
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
@ -60,6 +70,10 @@ class RoleService : IRole
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
@ -79,6 +93,10 @@ class RoleService : IRole
{ {
throw; throw;
} }
if (ret)
{
CacheManager.Clear();
}
return ret; return ret;
} }
} }

View File

@ -3,6 +3,7 @@
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.FreeSql.Models; using BootStarpAdmin.DataAccess.FreeSql.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models; using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core; using BootstrapAdmin.Web.Core;
using Longbow.Security.Cryptography; using Longbow.Security.Cryptography;
@ -37,10 +38,9 @@ class UserService : IUser
return FreeSql.Select<User>().ToList(); return FreeSql.Select<User>().ToList();
} }
public List<string> GetApps(string userName) private const string UserServiceGetAppsByUserNameCacheKey = "UserService-GetAppsByUserName";
{
return FreeSql.Ado.Query<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName) r on ra.RoleId = r.ID union select Code from Dicts where Category = @Category and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName)", new { UserName = userName, Category = "应用程序", RoleName = "Administrators" }).ToList(); public List<string> GetApps(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppsByUserNameCacheKey}-{userName}", entry => FreeSql.Ado.Query<string>($"select d.Code from Dicts d inner join RoleApp ra on d.Code = ra.AppId inner join (select r.Id from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName union select r.Id from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName) r on ra.RoleId = r.ID union select Code from Dicts where Category = @Category and exists(select r.ID from Roles r inner join UserRole ur on r.ID = ur.RoleID inner join Users u on ur.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName union select r.ID from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID where u.UserName = @UserName and r.RoleName = @RoleName)", new { UserName = userName, Category = "应用程序", RoleName = "Administrators" }).ToList());
}
/// <summary> /// <summary>
/// 通过用户名获得指定的前台 AppId /// 通过用户名获得指定的前台 AppId
@ -54,22 +54,21 @@ class UserService : IUser
return FreeSql.Select<User>().Where(s => s.UserName == userName).ToOne(s => s.DisplayName); return FreeSql.Select<User>().Where(s => s.UserName == userName).ToOne(s => s.DisplayName);
} }
public List<string> GetRoles(string userName) private const string UserServiceGetRolesByUserNameCacheKey = "UserService-GetRolesByUserName";
{
return FreeSql.Ado.Query<string>($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @userName union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = @userName", new { userName }).ToList();
}
public User? GetUserByUserName(string? userName) => string.IsNullOrEmpty(userName) ? null : FreeSql.Select<User>().Where(i => i.UserName == userName).ToOne(); public List<string> GetRoles(string userName) => CacheManager.GetOrAdd($"{UserServiceGetRolesByUserNameCacheKey}-{userName}", entry => FreeSql.Ado.Query<string>($"select r.RoleName from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID = u.ID and u.UserName = @userName union select r.RoleName from Roles r inner join RoleGroup rg on r.ID = rg.RoleID inner join Groups g on rg.GroupID = g.ID inner join UserGroup ug on ug.GroupID = g.ID inner join Users u on ug.UserID = u.ID and u.UserName = @userName", new { userName }).ToList());
public List<string> GetUsersByGroupId(string? groupId) private const string UserServiceGetUserByUserNameCacheKey = "UserService-GetUserByUserName";
{
return FreeSql.Ado.Query<string>("select UserID from UserGroup where GroupID = @groupId", new { groupId }).ToList();
}
public List<string> GetUsersByRoleId(string? roleId) public User? GetUserByUserName(string? userName) => CacheManager.GetOrAdd($"{UserServiceGetUserByUserNameCacheKey}-{userName}", entry => string.IsNullOrEmpty(userName) ? null : FreeSql.Select<User>().Where(i => i.UserName == userName).ToOne());
{
return FreeSql.Ado.Query<string>("select UserID from UserRole where RoleID = @roleId", new { roleId }).ToList(); private const string UserServiceGetUsersByGroupIdCacheKey = "UserService-GetUsersByGroupId";
}
public List<string> GetUsersByGroupId(string? groupId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByGroupIdCacheKey}-{groupId}", entry => FreeSql.Ado.Query<string>("select UserID from UserGroup where GroupID = @groupId", new { groupId }).ToList());
private const string UserServiceGetUsersByRoleIdCacheKey = "UserService-GetUsersByRoleId";
public List<string> GetUsersByRoleId(string? roleId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByRoleIdCacheKey}-{roleId}", entry => FreeSql.Ado.Query<string>("select UserID from UserRole where RoleID = @roleId", new { roleId }).ToList());
public bool SaveUser(string userName, string displayName, string password) public bool SaveUser(string userName, string displayName, string password)
{ {
@ -185,7 +184,7 @@ class UserService : IUser
FreeSql.Insert(user).ExecuteAffrows(); FreeSql.Insert(user).ExecuteAffrows();
// Authorization // Authorization
var roleIds = FreeSql.Ado.Query<string>("select ID from Roles where RoleName in (@roles)", new { roles }); var roleIds = FreeSql.Ado.Query<string>("select ID from Roles where RoleName in (@roles)", new { roles });
FreeSql.Insert(roleIds.Select(g => new UserRole { RoleID = g, UserID = user.Id })); FreeSql.Insert(roleIds.Select(g => new UserRole { RoleID = g, UserID = user.Id })).ExecuteAffrows();
}); });
} }
else else
@ -205,26 +204,54 @@ class UserService : IUser
public bool SaveApp(string userName, string app) public bool SaveApp(string userName, string app)
{ {
throw new NotImplementedException(); var ret = FreeSql.Ado.ExecuteNonQuery("update users set App = @App Where UserName = @UserName", new { App = app, UserName = userName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
} }
public bool ChangePassword(string userName, string password, string newPassword) public bool ChangePassword(string userName, string password, string newPassword)
{ {
throw new NotImplementedException(); var ret = false;
if (Authenticate(userName, password))
{
var passSalt = LgbCryptography.GenerateSalt();
password = LgbCryptography.ComputeHash(newPassword, passSalt);
string sql = "update users set Password = @Password, PassSalt = @PassSalt where UserName = @UserName";
ret = FreeSql.Ado.ExecuteNonQuery(sql, new { Password = password, PassSalt = passSalt, UserName = userName }) == 1;
}
return ret;
} }
public bool SaveDisplayName(string userName, string displayName) public bool SaveDisplayName(string userName, string displayName)
{ {
throw new NotImplementedException(); var ret = FreeSql.Ado.ExecuteNonQuery("update users set DisplayName = @DisplayName where UserName = @UserName", new { DisplayName = displayName, UserName = userName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
} }
public bool SaveTheme(string userName, string theme) public bool SaveTheme(string userName, string theme)
{ {
throw new NotImplementedException(); var ret = FreeSql.Ado.ExecuteNonQuery("update users set Css = @Css where UserName = @UserName", new { Css = theme, UserName = userName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
} }
public bool SaveLogo(string userName, string? logo) public bool SaveLogo(string userName, string? logo)
{ {
throw new NotImplementedException(); var ret = FreeSql.Ado.ExecuteNonQuery("update users set Icon = @Icon where UserName = @UserName", new { Icon = logo, UserName = userName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
} }
} }