Compare commits

...

6 Commits

Author SHA1 Message Date
zhangpeihang 9ad060d2c3 feat: 改为 EFCore 工程 2022-05-25 17:13:07 +08:00
zhangpeihang ac707dd9ec feat: 添加服务 2022-05-25 17:12:46 +08:00
zhangpeihang 680bcf621c fix: 修复创建用户失败 2022-05-25 17:12:15 +08:00
zhangpeihang d4aadfa5b8 chore: 引用 EFCore Sqlite 包 2022-05-25 17:11:38 +08:00
zhangpeihang 2fc7e621f0 feat: 添加中间表实体主键 2022-05-25 17:10:58 +08:00
zhangpeihang d7d38d9e2e feat: 完善 User 服务 2022-05-01 13:05:39 +08:00
9 changed files with 129 additions and 49 deletions

View File

@ -56,7 +56,10 @@ public static class ServicesExtensions
services.AddSingleton<IRole, RoleService>();
services.AddSingleton<IGroup, GroupService>();
services.AddSingleton<ILogin, LoginService>();
services.AddSingleton<ITrace, TraceService>();
services.AddSingleton<IApp, AppService>();
return services;
}
}

View File

@ -9,6 +9,11 @@ namespace BootstrapAdmin.DataAccess.EFCore.Models;
/// </summary>
public class NavigationRole
{
/// <summary>
///
/// </summary>
public string? ID { get; set; }
/// <summary>
///
/// </summary>

View File

@ -9,6 +9,11 @@ namespace BootstrapAdmin.DataAccess.EFCore.Models;
/// </summary>
public class RoleApp
{
/// <summary>
///
/// </summary>
public string? ID { get; set; }
/// <summary>
///
/// </summary>

View File

@ -9,6 +9,11 @@ namespace BootstrapAdmin.DataAccess.EFCore.Models;
/// </summary>
public class RoleGroup
{
/// <summary>
///
/// </summary>
public string? ID { get; set; }
/// <summary>
///
/// </summary>

View File

@ -9,6 +9,11 @@ namespace BootstrapAdmin.DataAccess.EFCore.Models;
/// </summary>
public class UserGroup
{
/// <summary>
///
/// </summary>
public string? ID { get; set; }
/// <summary>
///
/// </summary>

View File

@ -9,6 +9,11 @@ namespace BootstrapAdmin.DataAccess.EFCore.Models;
/// </summary>
public class UserRole
{
/// <summary>
///
/// </summary>
public string? ID { get; set; }
/// <summary>
///
/// </summary>

View File

@ -2,6 +2,7 @@
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.EFCore.Models;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
@ -51,60 +52,57 @@ public class UserService : IUser
return isAuth;
}
private const string UserServiceGetAppsByUserNameCacheKey = "UserService-GetAppsByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public List<string> GetApps(string userName)
public List<string> GetApps(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppsByUserNameCacheKey}-{userName}", entry =>
{
using var context = DbFactory.CreateDbContext();
return context.Dicts.FromSqlRaw("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 = {0} 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 = {0}) r on ra.RoleId = r.ID union select Code from Dicts where Category = {1} 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 = {0} and r.RoleName = {2} 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 = {0} and r.RoleName = {2})", new[] { userName, "应用程序", "Administrators" }).Select(s => s.Code).AsNoTracking().ToList();
}
});
private const string UserServiceGetRolesByUserNameCacheKey = "UserService-GetRolesByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetDisplayName(string? userName)
{
using var context = DbFactory.CreateDbContext();
return string.IsNullOrEmpty(userName) ? "" : context.Users.FirstOrDefault(s => s.UserName == userName)?.DisplayName;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public List<string> GetRoles(string userName)
public List<string> GetRoles(string userName) => CacheManager.GetOrAdd($"{UserServiceGetRolesByUserNameCacheKey}-{userName}", entry =>
{
using var context = DbFactory.CreateDbContext();
return context.Roles.FromSqlRaw("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 = {0} 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 = {0}", userName).Select(s => s.RoleName).AsNoTracking().ToList();
}
});
private const string UserServiceGetUsersByGroupIdCacheKey = "UserService-GetUsersByGroupId";
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
public List<string> GetUsersByGroupId(string? groupId)
public List<string> GetUsersByGroupId(string? groupId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByGroupIdCacheKey}-{groupId}", entry =>
{
using var context = DbFactory.CreateDbContext();
return context.UserGroup.Where(s => s.GroupId == groupId).Select(s => s.UserId!).AsNoTracking().ToList();
}
});
private const string UserServiceGetUsersByRoleIdCacheKey = "UserService-GetUsersByRoleId";
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public List<string> GetUsersByRoleId(string? roleId)
public List<string> GetUsersByRoleId(string? roleId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByRoleIdCacheKey}-{roleId}", entry =>
{
using var context = DbFactory.CreateDbContext();
return context.UserRole.Where(s => s.RoleId == roleId).Select(s => s.UserId!).AsNoTracking().ToList();
}
});
/// <summary>
///
@ -117,8 +115,12 @@ public class UserService : IUser
using var dbcontext = DbFactory.CreateDbContext();
dbcontext.Database.ExecuteSqlRaw("delete from UserGroup where GroupId = {0}", groupId!);
dbcontext.AddRange(userIds.Select(g => new UserGroup { UserId = g, GroupId = groupId }));
dbcontext.SaveChanges();
return true;
var ret = dbcontext.SaveChanges() > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -132,8 +134,12 @@ public class UserService : IUser
using var dbcontext = DbFactory.CreateDbContext();
dbcontext.Database.ExecuteSqlRaw("delete from UserRole where RoleID = {0}", roleId!);
dbcontext.AddRange(userIds.Select(g => new UserRole { UserId = g, RoleId = roleId }));
dbcontext.SaveChanges();
return true;
var ret = dbcontext.SaveChanges() > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -170,16 +176,22 @@ public class UserService : IUser
dbcontext.AddRange(roleIds.Select(g => new { RoleID = g, UserID = user.Id }));
ret = dbcontext.SaveChanges() > 0;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
private const string UserServiceGetUserByUserNameCacheKey = "UserService-GetUserByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public User? GetUserByUserName(string? userName)
public User? GetUserByUserName(string? userName) => CacheManager.GetOrAdd($"{UserServiceGetUserByUserNameCacheKey}-{userName}", entry =>
{
User? user = null;
if (userName != null)
@ -188,7 +200,7 @@ public class UserService : IUser
user = dbcontext.Set<User>().FirstOrDefault(s => s.UserName == userName);
}
return user;
}
});
/// <summary>
///
@ -222,6 +234,10 @@ public class UserService : IUser
string sql = "update Users set Password = {0}, PassSalt = {1} where UserName = {2}";
ret = dbcontext.Database.ExecuteSqlRaw(sql, new[] { password, passSalt, userName }) > 0;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
@ -235,7 +251,12 @@ public class UserService : IUser
public bool SaveDisplayName(string userName, string displayName)
{
using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users set DisplayName = {1} where UserName = {0}", userName, displayName!) > 0;
var ret = dbcontext.Database.ExecuteSqlRaw("update Users set DisplayName = {1} where UserName = {0}", userName, displayName!) > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -248,7 +269,12 @@ public class UserService : IUser
public bool SaveTheme(string userName, string theme)
{
using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users set Css = {1} where UserName = {0}", userName, theme) > 0;
var ret = dbcontext.Database.ExecuteSqlRaw("update Users set Css = {1} where UserName = {0}", userName, theme) > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -261,7 +287,12 @@ public class UserService : IUser
public bool SaveLogo(string userName, string? logo)
{
using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users set Icon = {1} where UserName = {0}", userName, logo ?? "") > 0;
var ret = dbcontext.Database.ExecuteSqlRaw("update Users set Icon = {1} where UserName = {0}", userName, logo ?? "") > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -274,7 +305,12 @@ public class UserService : IUser
public bool SaveApp(string userName, string app)
{
using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users Set App = {1} Where UserName = {0}", userName, app) > 0;
var ret = dbcontext.Database.ExecuteSqlRaw("update Users Set App = {1} Where UserName = {0}", userName, app) > 0;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
@ -305,11 +341,20 @@ public class UserService : IUser
PassSalt = salt,
Password = pwd
};
dbcontext.Add(user);
ret = dbcontext.SaveChanges() > 0;
// 授权 Default 角色
dbcontext.Database.ExecuteSqlRaw("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = {0}", userName);
ret = dbcontext.SaveChanges() > 0;
try
{
dbcontext.Database.BeginTransaction();
dbcontext.Add(user);
// 授权 Default 角色
dbcontext.Database.ExecuteSqlRaw("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = {0}", userName);
ret = dbcontext.SaveChanges() > 0;
dbcontext.Database.CommitTransaction();
}
catch (Exception)
{
dbcontext.Database.RollbackTransaction()
}
}
else
{
@ -317,7 +362,11 @@ public class UserService : IUser
user.PassSalt = salt;
user.Password = pwd;
dbcontext.Update(user);
ret = true;
ret = dbcontext.SaveChanges() > 0;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}

View File

@ -8,9 +8,11 @@
<PackageReference Include="BootstrapBlazor.Middleware" Version="1.0.0" />
<PackageReference Include="Longbow.Logging" Version="6.0.2" />
<PackageReference Include="Longbow.Tasks" Version="5.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BootstrapAdmin.DataAccess.EFCore\BootstrapAdmin.DataAccess.EFCore.csproj" />
<ProjectReference Include="..\BootstrapAdmin.DataAccess.PetaPoco\BootstrapAdmin.DataAccess.PetaPoco.csproj" />
</ItemGroup>

View File

@ -11,6 +11,7 @@ using BootstrapAdmin.Web.Services.SMS.Tencent;
using BootstrapAdmin.Web.Utils;
using PetaPoco;
using PetaPoco.Providers;
using Microsoft.EntityFrameworkCore;
namespace Microsoft.Extensions.DependencyInjection
{
@ -52,13 +53,13 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddScoped<BootstrapAppContext>();
// 增加 EFCore 数据服务
//services.AddEFCoreDataAccessServices((provider, option) =>
//{
// var configuration = provider.GetRequiredService<IConfiguration>();
// var connString = configuration.GetConnectionString("bb");
// option.UseSqlite(connString);
// option.EnableSensitiveDataLogging();
//});
services.AddEFCoreDataAccessServices((provider, option) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
option.UseSqlite(connString);
option.EnableSensitiveDataLogging();
});
// 增加 FreeSql 数据服务
// services.AddFreeSql((provider, builder) =>
@ -73,13 +74,13 @@ namespace Microsoft.Extensions.DependencyInjection
// });
// 增加 PetaPoco 数据服务
services.AddPetaPocoDataAccessServices((provider, builder) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var connString = configuration.GetConnectionString("bb");
builder.UsingProvider<SQLiteDatabaseProvider>()
.UsingConnectionString(connString);
});
//services.AddPetaPocoDataAccessServices((provider, builder) =>
//{
// var configuration = provider.GetRequiredService<IConfiguration>();
// var connString = configuration.GetConnectionString("bb");
// builder.UsingProvider<SQLiteDatabaseProvider>()
// .UsingConnectionString(connString);
//});
return services;
}