refactor: 格式化代码

This commit is contained in:
Argo-Tianyi 2022-01-26 19:46:11 +08:00
parent a946424275
commit 5c7b29ee04
9 changed files with 59 additions and 103 deletions

View File

@ -13,7 +13,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// </summary> /// </summary>
public class AppService : IApp public class AppService : IApp
{ {
private readonly IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -17,7 +17,7 @@ class DictService : IDict
{ {
private const string DictServiceCacheKey = "DictService-GetAll"; private const string DictServiceCacheKey = "DictService-GetAll";
private IDbContextFactory<BootstrapAdminContext> DbFactory { get; set; } private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
private string AppId { get; set; } private string AppId { get; set; }

View File

@ -13,7 +13,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// </summary> /// </summary>
public class ExceptionService : IException public class ExceptionService : IException
{ {
private IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -27,7 +27,7 @@ public class GroupService : IGroup, IDisposable
private CancellationTokenSource? GetGroupsByRoleIdCancellationTokenSource { get; set; } private CancellationTokenSource? GetGroupsByRoleIdCancellationTokenSource { get; set; }
private IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -13,7 +13,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// </summary> /// </summary>
public class LoginService : ILogin public class LoginService : ILogin
{ {
private IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -2,11 +2,11 @@
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. // Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone // Website: https://admin.blazor.zone
using BootstrapAdmin.Web.Core;
using BootstrapAdmin.DataAccess.Models;
using Microsoft.EntityFrameworkCore;
using BootstrapAdmin.Caching; using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.EFCore.Models; using BootstrapAdmin.DataAccess.EFCore.Models;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using Microsoft.EntityFrameworkCore;
namespace BootstrapAdmin.DataAccess.EFCore.Services namespace BootstrapAdmin.DataAccess.EFCore.Services
{ {
@ -15,7 +15,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services
/// </summary> /// </summary>
class NavigationService : INavigation class NavigationService : INavigation
{ {
private IDbContextFactory<BootstrapAdminContext> DbFactory { get; set; } private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -25,7 +25,7 @@ public class RoleService : IRole
private CancellationTokenSource? GetRolesByGroupIdCancellationTokenSource { get; set; } private CancellationTokenSource? GetRolesByGroupIdCancellationTokenSource { get; set; }
private IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -13,7 +13,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// </summary> /// </summary>
public class TraceService : ITrace public class TraceService : ITrace
{ {
private IDbContextFactory<BootstrapAdminContext> DbFactory; private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///

View File

@ -15,7 +15,7 @@ namespace BootstrapAdmin.DataAccess.EFCore.Services;
/// </summary> /// </summary>
public class UserService : IUser public class UserService : IUser
{ {
private IDbContextFactory<BootstrapAdminContext> DbFactory { get; set; } private IDbContextFactory<BootstrapAdminContext> DbFactory { get; }
/// <summary> /// <summary>
/// ///
@ -41,11 +41,9 @@ public class UserService : IUser
/// <returns></returns> /// <returns></returns>
public bool Authenticate(string userName, string password) public bool Authenticate(string userName, string password)
{ {
using var context = DbFactory.CreateDbContext();
var user = context.Users.Where(s => s.ApprovedTime != null).FirstOrDefault(x => x.UserName == userName);
var isAuth = false; var isAuth = false;
using var context = DbFactory.CreateDbContext();
var user = context.Users.Where(s => s.ApprovedTime != null).FirstOrDefault(x => x.UserName == userName);
if (user != null && !string.IsNullOrEmpty(user.PassSalt)) if (user != null && !string.IsNullOrEmpty(user.PassSalt))
{ {
isAuth = user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); isAuth = user.Password == LgbCryptography.ComputeHash(password, user.PassSalt);
@ -62,7 +60,6 @@ public class UserService : IUser
{ {
using var context = DbFactory.CreateDbContext(); 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(); 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();
} }
/// <summary> /// <summary>
@ -84,7 +81,6 @@ public class UserService : IUser
public List<string> GetRoles(string userName) public List<string> GetRoles(string userName)
{ {
using var context = DbFactory.CreateDbContext(); 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(); 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();
} }
@ -96,7 +92,6 @@ public class UserService : IUser
public List<string> GetUsersByGroupId(string? groupId) public List<string> GetUsersByGroupId(string? groupId)
{ {
using var context = DbFactory.CreateDbContext(); using var context = DbFactory.CreateDbContext();
return context.UserGroup.Where(s => s.GroupId == groupId).Select(s => s.UserId!).AsNoTracking().ToList(); return context.UserGroup.Where(s => s.GroupId == groupId).Select(s => s.UserId!).AsNoTracking().ToList();
} }
@ -108,7 +103,6 @@ public class UserService : IUser
public List<string> GetUsersByRoleId(string? roleId) public List<string> GetUsersByRoleId(string? roleId)
{ {
using var context = DbFactory.CreateDbContext(); using var context = DbFactory.CreateDbContext();
return context.UserRole.Where(s => s.RoleId == roleId).Select(s => s.UserId!).AsNoTracking().ToList(); return context.UserRole.Where(s => s.RoleId == roleId).Select(s => s.UserId!).AsNoTracking().ToList();
} }
@ -121,19 +115,10 @@ public class UserService : IUser
public bool SaveUsersByGroupId(string? groupId, IEnumerable<string> userIds) public bool SaveUsersByGroupId(string? groupId, IEnumerable<string> userIds)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
var ret = false; dbcontext.Database.ExecuteSqlRaw("delete from UserGroup where GroupId = {0}", groupId!);
try dbcontext.AddRange(userIds.Select(g => new UserGroup { UserId = g, GroupId = groupId }));
{ dbcontext.SaveChanges();
dbcontext.Database.ExecuteSqlRaw("delete from UserGroup where GroupId = {0}", groupId!); return true;
dbcontext.AddRange(userIds.Select(g => new UserGroup { UserId = g, GroupId = groupId }));
dbcontext.SaveChanges();
ret = true;
}
catch (Exception)
{
throw;
}
return ret;
} }
/// <summary> /// <summary>
@ -145,19 +130,10 @@ public class UserService : IUser
public bool SaveUsersByRoleId(string? roleId, IEnumerable<string> userIds) public bool SaveUsersByRoleId(string? roleId, IEnumerable<string> userIds)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
var ret = false; dbcontext.Database.ExecuteSqlRaw("delete from UserRole where RoleID = {0}", roleId!);
try dbcontext.AddRange(userIds.Select(g => new UserRole { UserId = g, RoleId = roleId }));
{ dbcontext.SaveChanges();
dbcontext.Database.ExecuteSqlRaw("delete from UserRole where RoleID = {0}", roleId!); return true;
dbcontext.AddRange(userIds.Select(g => new UserRole { UserId = g, RoleId = roleId }));
dbcontext.SaveChanges();
ret = true;
}
catch (Exception)
{
throw;
}
return ret;
} }
/// <summary> /// <summary>
@ -172,36 +148,27 @@ public class UserService : IUser
{ {
var ret = false; var ret = false;
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
try var salt = LgbCryptography.GenerateSalt();
var pwd = LgbCryptography.ComputeHash(code, salt);
var user = GetAll().FirstOrDefault(user => user.UserName == phone);
if (user == null)
{ {
var salt = LgbCryptography.GenerateSalt(); user = new User()
var pwd = LgbCryptography.ComputeHash(code, salt);
var user = GetAll().FirstOrDefault(user => user.UserName == phone);
if (user == null)
{ {
user = new User() ApprovedBy = "Mobile",
{ ApprovedTime = DateTime.Now,
ApprovedBy = "Mobile", DisplayName = "手机用户",
ApprovedTime = DateTime.Now, UserName = phone,
DisplayName = "手机用户", Icon = "default.jpg",
UserName = phone, Description = "手机用户",
Icon = "default.jpg", App = appId
Description = "手机用户", };
App = appId dbcontext.Add(user);
};
dbcontext.Add(user);
// Authorization // Authorization
var roleIds = dbcontext.Roles.Where(s => roles.Contains(s.RoleName)).Select(s => s.Id).ToList(); var roleIds = dbcontext.Roles.Where(s => roles.Contains(s.RoleName)).Select(s => s.Id).ToList();
dbcontext.AddRange(roleIds.Select(g => new { RoleID = g, UserID = user.Id })); dbcontext.AddRange(roleIds.Select(g => new { RoleID = g, UserID = user.Id }));
ret = dbcontext.SaveChanges() > 0; ret = dbcontext.SaveChanges() > 0;
}
ret = true;
}
catch (Exception)
{
throw;
} }
return ret; return ret;
} }
@ -214,16 +181,13 @@ public class UserService : IUser
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
public User? GetUserByUserName(string? userName) public User? GetUserByUserName(string? userName)
{ {
User? user = null;
if (userName != null) if (userName != null)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
user = dbcontext.Set<User>().FirstOrDefault(s => s.UserName == userName);
return dbcontext.Set<User>().FirstOrDefault(s => s.UserName == userName);
}
else
{
return null;
} }
return user;
} }
/// <summary> /// <summary>
@ -236,7 +200,6 @@ public class UserService : IUser
public string? GetAppIdByUserName(string userName) public string? GetAppIdByUserName(string userName)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Set<User>().FirstOrDefault(s => s.UserName == userName)?.App; return dbcontext.Set<User>().FirstOrDefault(s => s.UserName == userName)?.App;
} }
@ -285,7 +248,7 @@ public class UserService : IUser
public bool SaveTheme(string userName, string theme) public bool SaveTheme(string userName, string theme)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users set Css = {1} where UserName = {0}", userName, theme!) > 0; return dbcontext.Database.ExecuteSqlRaw("update Users set Css = {1} where UserName = {0}", userName, theme) > 0;
} }
/// <summary> /// <summary>
@ -298,7 +261,7 @@ public class UserService : IUser
public bool SaveLogo(string userName, string? logo) public bool SaveLogo(string userName, string? logo)
{ {
using var dbcontext = DbFactory.CreateDbContext(); using var dbcontext = DbFactory.CreateDbContext();
return dbcontext.Database.ExecuteSqlRaw("update Users set Icon = {1} where UserName = {0}", userName, logo!) > 0; return dbcontext.Database.ExecuteSqlRaw("update Users set Icon = {1} where UserName = {0}", userName, logo ?? "") > 0;
} }
/// <summary> /// <summary>
@ -331,28 +294,21 @@ public class UserService : IUser
bool ret; bool ret;
if (user == null) if (user == null)
{ {
try user = new User()
{ {
user = new User() ApprovedBy = "System",
{ ApprovedTime = DateTime.Now,
ApprovedBy = "System", DisplayName = "手机用户",
ApprovedTime = DateTime.Now, UserName = userName,
DisplayName = "手机用户", Icon = "default.jpg",
UserName = userName, Description = "系统默认创建",
Icon = "default.jpg", PassSalt = salt,
Description = "系统默认创建", Password = pwd
PassSalt = salt, };
Password = pwd dbcontext.Add(user);
}; // 授权 Default 角色
dbcontext.Add(user); dbcontext.Database.ExecuteSqlRaw("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = {0}", userName);
// 授权 Default 角色 ret = dbcontext.SaveChanges() > 0;
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;
}
catch (Exception)
{
throw;
}
} }
else else
{ {