Skip to content

Commit

Permalink
fix 快速登录无密码salt无值
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 21, 2024
1 parent b10969e commit 7df7355
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
4 changes: 1 addition & 3 deletions src/LinCms.Application.Contracts/Cms/Users/CreateUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ public class CreateUserDto : IValidatableObject
/// <summary>
/// 用户名
/// </summary>
[StringLength(10, MinimumLength = 2, ErrorMessage = "用户名长度必须在2~10之间")]
[Required(ErrorMessage = "用户名不能为空")]
public string Username { get; set; }
public string? Username { get; set; }

/// <summary>
/// 昵称
Expand Down
9 changes: 0 additions & 9 deletions src/LinCms.Application/Cms/Groups/GroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;

using FreeSql.Internal.ObjectPool;

using IGeekFan.FreeKit.Extras.FreeSql;
using LinCms.Cms.Permissions;
using LinCms.Common;
Expand Down Expand Up @@ -135,13 +133,6 @@ public async Task DeleteAsync(long id)

await groupRepository.DeleteAsync(id);
await groupPermissionRepository.DeleteAsync(r => r.GroupId == id);

//_freeSql.Transaction(() =>
//{
// _freeSql.Delete<LinGroupPermission>(new LinGroupPermission { GroupId = id }).ExecuteAffrows();
// _freeSql.Delete<LinGroup>(id).ExecuteAffrows();
//});

}

[Transactional]
Expand Down
21 changes: 10 additions & 11 deletions src/LinCms.Application/Cms/Users/UserIdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using DotNetCore.Security;
Expand Down Expand Up @@ -27,26 +28,24 @@ public async Task<bool> VerifyUserPasswordAsync(long userId, string password, st
}


public async Task ChangePasswordAsync(long userId, string newpassword, string salt)
public async Task ChangePasswordAsync(long userId, string newpassword,string salt)
{
var linUserIdentity = await GetFirstByUserIdAsync(userId); ;

await ChangePasswordAsync(linUserIdentity, newpassword, salt);
var userIdentity = await GetFirstByUserIdAsync(userId);
await ChangePasswordAsync(userIdentity, newpassword, salt);
}


public Task ChangePasswordAsync(LinUserIdentity linUserIdentity, string newpassword, string salt)
public async Task ChangePasswordAsync(LinUserIdentity linUserIdentity, string newpassword, string salt)
{
string encryptPassword = cryptographyService.Encrypt(newpassword, salt);
if (linUserIdentity == null)
{
linUserIdentity = new LinUserIdentity(LinUserIdentity.Password, "", encryptPassword, DateTime.Now);
return userIdentityRepository.InsertAsync(linUserIdentity);
await userIdentityRepository.InsertAsync(linUserIdentity);
}
else
{
linUserIdentity.Credential = encryptPassword;
return userIdentityRepository.UpdateAsync(linUserIdentity);
await userIdentityRepository.UpdateAsync(linUserIdentity);
}
}

Expand All @@ -56,7 +55,7 @@ public Task DeleteAsync(long userId)
return userIdentityRepository.Where(r => r.CreateUserId == userId).ToDelete().ExecuteAffrowsAsync();
}

public Task<LinUserIdentity> GetFirstByUserIdAsync(long userId)
public Task<LinUserIdentity?> GetFirstByUserIdAsync(long userId)
{
return userIdentityRepository
.Where(r => r.CreateUserId == userId && r.IdentityType == LinUserIdentity.Password)
Expand All @@ -75,12 +74,12 @@ public async Task<List<UserIdentityDto>> GetListAsync(long userId)
public async Task UnBind(Guid id)
{
LinUserIdentity userIdentity = await userIdentityRepository.GetAsync(id);
if (userIdentity == null || userIdentity.CreateUserId != CurrentUser.FindUserId())
if (userIdentity == null || userIdentity.CreateUserId != CurrentUser.FindUserId())
{
throw new LinCmsException("你无权解绑此账号");
}

List<LinUserIdentity> userIdentities = await userIdentityRepository.Select.Where(r => r.CreateUserId == CurrentUser.FindUserId()).ToListAsync();
List<LinUserIdentity> userIdentities = await userIdentityRepository.Select.Where(r => r.CreateUserId == CurrentUser.FindUserId()).ToListAsync();

bool hasPwd = userIdentities.Any(r => r.IdentityType == LinUserIdentity.Password);

Expand Down
11 changes: 11 additions & 0 deletions src/LinCms.Application/Cms/Users/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public async Task ChangePasswordAsync(ChangePasswordDto passwordDto)
{
throw new LinCmsException("旧密码不正确");
}
if(user.Salt.IsNullOrWhiteSpace())
{
user.Salt = Guid.NewGuid().ToString();
await userRepository.UpdateAsync(user);
}

await userIdentityService.ChangePasswordAsync(user.Id, passwordDto.NewPassword, user.Salt);
}
Expand All @@ -61,6 +66,12 @@ public async Task ResetPasswordAsync(long id, ResetPasswordDto resetPasswordDto)
throw new LinCmsException("用户不存在", ErrorCode.NotFound);
}

if (user.Salt.IsNullOrWhiteSpace())
{
user.Salt = Guid.NewGuid().ToString();
await userRepository.UpdateAsync(user);
}

await userIdentityService.ChangePasswordAsync(id, resetPasswordDto.ConfirmPassword, user.Salt);
}

Expand Down
15 changes: 14 additions & 1 deletion src/LinCms.Web/Controllers/Cms/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using IGeekFan.FreeKit.Extras.Dto;
using IGeekFan.FreeKit.Extras.Security;
using LinCms.Aop.Attributes;
using LinCms.Aop.Filter;
using LinCms.Cms.Admins;
Expand Down Expand Up @@ -36,7 +37,7 @@ public PagedResultDto<UserDto> GetUserListByGroupId([FromQuery] UserSearchDto se
/// <param name="userStatus"></param>
/// <returns></returns>
[HttpPut("user/{id}/status/{userStatus}")]
[LinCmsAuthorize("修改用户密码", "管理员")]
[LinCmsAuthorize("修改用户状态", "管理员")]
public Task ChangeStatusAsync(long id, UserStatus userStatus)
{
return userSevice.ChangeStatusAsync(id, userStatus);
Expand All @@ -56,6 +57,18 @@ public async Task<UnifyResponseDto> UpdateAsync(long id, [FromBody] UpdateUserDt
return UnifyResponseDto.Success();
}

/// <summary>
/// 根据用户Id获取用户信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("user/{id}")]
[LinCmsAuthorize("查询所有用户", "管理员")]
public Task<UserInformation> GetInformationAsync(int id)
{
return userSevice.GetInformationAsync(id);
}

/// <summary>
/// 删除用户
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/LinCms.Web/LinCms.Web.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7df7355

Please sign in to comment.