feat: 完善用户服务

This commit is contained in:
zhangpeihang 2022-04-26 11:17:47 +08:00
parent 21253a1ea0
commit a290a63267
1 changed files with 258 additions and 41 deletions

View File

@ -1,12 +1,13 @@
using BootstrapAdmin.DataAccess.Models;
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone
using BootStarpAdmin.DataAccess.SqlSugar.Models;
using BootstrapAdmin.Caching;
using BootstrapAdmin.DataAccess.Models;
using BootstrapAdmin.Web.Core;
using Longbow.Security.Cryptography;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BootStarpAdmin.DataAccess.SqlSugar.Service;
@ -40,7 +41,7 @@ public class UserService : IUser
/// <returns></returns>
public bool Authenticate(string userName, string password)
{
var user = Client.Ado.SqlQuery<User>("select DisplayName, Password, PassSalt from Users where ApprovedTime is not null and UserName = @UserName").First();
var user = Client.Ado.SqlQuery<User>("select DisplayName, Password, PassSalt from Users where ApprovedTime is not null and UserName = @UserName", new { UserName = userName }).First();
var isAuth = false;
if (user != null && !string.IsNullOrEmpty(user.PassSalt))
@ -70,90 +71,306 @@ public class UserService : IUser
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public string? GetAppIdByUserName(string userName)
{
return Client.Queryable<User>().Where(s => s.UserName == userName).First()?.App;
}
private const string UserServiceGetAppIdByUserNameCacheKey = "UserService-GetAppIdByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public List<string> GetApps(string userName)
{
return Client.Ado.SqlQuery<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" });
}
public string? GetAppIdByUserName(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppIdByUserNameCacheKey}-{userName}", entry => Client.Queryable<User>().Where(s => s.UserName == userName).First()?.App);
private const string UserServiceGetAppsByUserNameCacheKey = "UserService-GetAppsByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public List<string> GetRoles(string userName)
{
return Client.Ado.SqlQuery<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 = @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=@UserName", new { UserName = userName });
}
public List<string> GetApps(string userName) => CacheManager.GetOrAdd($"{UserServiceGetAppsByUserNameCacheKey}-{userName}", entry => Client.Ado.SqlQuery<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" }));
private const string UserServiceGetRolesByUserNameCacheKey = "UserService-GetRolesByUserName";
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public User? GetUserByUserName(string? userName) => string.IsNullOrEmpty(userName) ? null : Client.Queryable<User>().Where(s => s.UserName == userName).First();
public List<string> GetRoles(string userName) => CacheManager.GetOrAdd($"{UserServiceGetRolesByUserNameCacheKey}-{userName}", entry => Client.Ado.SqlQuery<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 = @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=@UserName", new { UserName = userName }));
public List<string> GetUsersByGroupId(string? groupId)
{
throw new NotImplementedException();
}
private const string UserServiceGetUserByUserNameCacheKey = "UserService-GetUserByUserName";
public List<string> GetUsersByRoleId(string? roleId)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public User? GetUserByUserName(string? userName) => CacheManager.GetOrAdd($"{UserServiceGetUserByUserNameCacheKey}-{userName}", entry => string.IsNullOrEmpty(userName) ? null : Client.Queryable<User>().Where(s => s.UserName == userName).First());
private const string UserServiceGetUsersByGroupIdCacheKey = "UserService-GetUsersByGroupId";
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetUsersByGroupId(string? groupId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByGroupIdCacheKey}-{groupId}", entry => Client.Ado.SqlQuery<string>("select UserID from UserGroup where GroupID = @GroupID", new { GroupID = groupId }));
private const string UserServiceGetUsersByRoleIdCacheKey = "UserService-GetUsersByRoleId";
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public List<string> GetUsersByRoleId(string? roleId) => CacheManager.GetOrAdd($"{UserServiceGetUsersByRoleIdCacheKey}-{roleId}", entry => Client.Ado.SqlQuery<string>("select UserID from UserRole where RoleID = @RoleID", new { RoleID = roleId }));
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="app"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveApp(string userName, string app)
{
throw new NotImplementedException();
var ret = Client.Ado.ExecuteCommand("update users set App = @App Where UserName = @UserName", new { App = app, UserName = userName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveDisplayName(string userName, string displayName)
{
throw new NotImplementedException();
var ret = Client.Ado.ExecuteCommand("update users set DisplayName = @DisplayName where UserName = @UserName", new { UserName = userName, DisplayName = displayName }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="logo"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveLogo(string userName, string? logo)
{
throw new NotImplementedException();
var ret = Client.Ado.ExecuteCommand("update users set Icon = @Icon where UserName = @UserName", new { UserName = userName, Icon = logo }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="theme"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveTheme(string userName, string theme)
{
throw new NotImplementedException();
var ret = Client.Ado.ExecuteCommand("update users set Css = @Css where UserName = @UserName", new { UserName = userName, Css = theme }) == 1;
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <param name="password"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUser(string userName, string displayName, string password)
{
throw new NotImplementedException();
var salt = LgbCryptography.GenerateSalt();
var pwd = LgbCryptography.ComputeHash(password, salt);
var user = Client.Queryable<User>().First(s => s.UserName == userName);
bool ret;
if (user == null)
{
try
{
// 开始事务
Client.UseTran();
user = new User()
{
ApprovedBy = "System",
ApprovedTime = DateTime.Now,
DisplayName = "手机用户",
UserName = userName,
Icon = "default.jpg",
Description = "系统默认创建",
PassSalt = salt,
Password = pwd
};
Client.Insertable(user).ExecuteCommand();
// 授权 Default 角色
Client.Ado.ExecuteCommand("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @userName", new { UserName = userName });
// 结束事务
Client.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
throw;
}
}
else
{
user.DisplayName = displayName;
user.PassSalt = salt;
user.Password = pwd;
Client.Updateable(user).ExecuteCommand();
ret = true;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="groupId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUsersByGroupId(string? groupId, IEnumerable<string> userIds)
{
throw new NotImplementedException();
var ret = false;
try
{
Client.BeginTran();
Client.Ado.ExecuteCommand("delete from UserGroup where GroupId = @GroupId", new { GroupId = groupId });
Client.Insertable<UserGroup>(userIds.Select(g => new { UserID = g, GroupID = groupId })).ExecuteCommand();
Client.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
throw;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="roleId"></param>
/// <param name="userIds"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool SaveUsersByRoleId(string? roleId, IEnumerable<string> userIds)
{
throw new NotImplementedException();
var ret = false;
try
{
Client.UseTran();
Client.Ado.ExecuteCommand("delete from UserRole where RoleID = @RoleID", new { RoleID = roleId });
Client.Insertable<UserRole>(userIds.Select(g => new { UserID = g, RoleID = roleId })).ExecuteCommand();
Client.CommitTran();
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
throw;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="phone"></param>
/// <param name="code"></param>
/// <param name="appId"></param>
/// <param name="roles"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool TryCreateUserByPhone(string phone, string code, string appId, ICollection<string> roles)
{
throw new NotImplementedException();
var ret = false;
try
{
var salt = LgbCryptography.GenerateSalt();
var pwd = LgbCryptography.ComputeHash(code, salt);
var user = Client.Queryable<User>().First(s => s.UserName == phone);
if (user == null)
{
Client.UseTran();
// 插入用户
user = new User()
{
ApprovedBy = "Mobile",
ApprovedTime = DateTime.Now,
DisplayName = "手机用户",
UserName = phone,
Icon = "default.jpg",
Description = "手机用户",
PassSalt = salt,
Password = LgbCryptography.ComputeHash(code, salt),
App = appId
};
Client.Insertable(user).ExecuteCommand();
// Authorization
var roleIds = Client.Ado.SqlQuery<string>("select ID from Roles where RoleName in (@roles)", new { roles = roles });
Client.Insertable<UserRole>(roleIds.Select(g => new { RoleID = g, UserID = user.Id })).ExecuteCommand();
Client.CommitTran();
}
else
{
user.PassSalt = salt;
user.Password = pwd;
Client.Updateable(user).ExecuteCommand();
}
ret = true;
}
catch (Exception)
{
Client.RollbackTran();
throw;
}
if (ret)
{
CacheManager.Clear();
}
return ret;
}
}