Browse Source

更新认证控制器和用户界面组件

refactor/assembly-names
root 4 weeks ago
parent
commit
6871302023
  1. 6
      src/X1.Infrastructure/Services/UserManagement/UserRegistrationService.cs
  2. 16
      src/X1.Presentation/Controllers/AuthController.cs
  3. 2
      src/X1.WebUI/src/constants/auth.ts
  4. 10
      src/X1.WebUI/src/pages/roles/RoleTable.tsx

6
src/X1.Infrastructure/Services/UserManagement/UserRegistrationService.cs

@ -23,7 +23,7 @@ public class UserRegistrationService : IUserRegistrationService
private readonly ILogger<UserRegistrationService> _logger; private readonly ILogger<UserRegistrationService> _logger;
private readonly IDistributedLockService _lockService; private readonly IDistributedLockService _lockService;
private readonly IUserRoleRepository _userRoleRepository; private readonly IUserRoleRepository _userRoleRepository;
private const string Description = "超级管理员";
public UserRegistrationService( public UserRegistrationService(
UserManager<AppUser> userManager, UserManager<AppUser> userManager,
RoleManager<AppRole> roleManager, RoleManager<AppRole> roleManager,
@ -108,13 +108,13 @@ public class UserRegistrationService : IUserRegistrationService
// 检查是否是第一个用户 // 检查是否是第一个用户
var isFirstUser = !await _userManager.Users.AnyAsync(); var isFirstUser = !await _userManager.Users.AnyAsync();
string roleName = isFirstUser ? "Admin" : "User"; string roleName = isFirstUser ? "User" : "Admin";
// 获取或创建角色 // 获取或创建角色
var role = await _roleManager.FindByNameAsync(roleName); var role = await _roleManager.FindByNameAsync(roleName);
if (role == null) if (role == null)
{ {
role = new AppRole { Name = roleName }; role = new AppRole { Name = roleName,Description=(isFirstUser?string.Empty: Description) };
var roleResult = await _roleManager.CreateAsync(role); var roleResult = await _roleManager.CreateAsync(role);
if (!roleResult.Succeeded) if (!roleResult.Succeeded)
{ {

16
src/X1.Presentation/Controllers/AuthController.cs

@ -116,7 +116,13 @@ public class AuthController : ApiController
_cache.Remove(cacheKey); _cache.Remove(cacheKey);
_logger.LogInformation("账号 {UserName} 登录成功", command.UserName); _logger.LogInformation("账号 {UserName} 登录成功", command.UserName);
} }
_logger.LogWarning($"Bearer {result.Data.AccessToken}");
// 只有在登录成功时才记录访问令牌
if (result.IsSuccess && result.Data?.AccessToken != null)
{
_logger.LogWarning($"Bearer {result.Data.AccessToken}");
}
return Ok(result); return Ok(result);
} }
catch (Exception ex) catch (Exception ex)
@ -188,7 +194,13 @@ public class AuthController : ApiController
_cache.Remove(cacheKey); _cache.Remove(cacheKey);
_logger.LogInformation("邮箱 {Email} 登录成功", command.Email); _logger.LogInformation("邮箱 {Email} 登录成功", command.Email);
} }
_logger.LogWarning($"Bearer {result.Data.AccessToken}");
// 只有在登录成功时才记录访问令牌
if (result.IsSuccess && result.Data?.AccessToken != null)
{
_logger.LogWarning($"Bearer {result.Data.AccessToken}");
}
return Ok(result); return Ok(result);
} }
catch (Exception ex) catch (Exception ex)

2
src/X1.WebUI/src/constants/auth.ts

@ -47,5 +47,5 @@ export const AUTH_CONSTANTS = {
export const DEFAULT_CREDENTIALS = { export const DEFAULT_CREDENTIALS = {
username: 'hyh', username: 'hyh',
password: 'Hyh@123456' password: 'H295172551@qq.com'
}; };

10
src/X1.WebUI/src/pages/roles/RoleTable.tsx

@ -69,7 +69,7 @@ export default function RoleTable({
{visibleColumns.map(col => ( {visibleColumns.map(col => (
<TableHead <TableHead
key={col.key} key={col.key}
className={`text-foreground ${col.key === 'actions' ? 'text-right' : ''} ${cellPadding}`} className={`text-foreground ${col.key === 'actions' ? 'text-right' : 'text-center'} ${cellPadding}`}
> >
{col.title} {col.title}
</TableHead> </TableHead>
@ -93,10 +93,10 @@ export default function RoleTable({
roles.map((role) => ( roles.map((role) => (
<TableRow key={role.id} className={rowClass}> <TableRow key={role.id} className={rowClass}>
{visibleColumns.map(col => { {visibleColumns.map(col => {
if (col.key === 'name') return <TableCell key={col.key} className={`text-foreground ${cellPadding}`}>{role.name}</TableCell>; if (col.key === 'name') return <TableCell key={col.key} className={`text-foreground text-center ${cellPadding}`}>{role.name}</TableCell>;
if (col.key === 'description') return <TableCell key={col.key} className={`text-foreground ${cellPadding}`}>{role.description}</TableCell>; if (col.key === 'description') return <TableCell key={col.key} className={`text-foreground text-center ${cellPadding}`}>{role.description}</TableCell>;
if (col.key === 'createdAt') return <TableCell key={col.key} className={`text-foreground ${cellPadding}`}>{formatToBeijingTime(role.createdAt)}</TableCell>; if (col.key === 'createdAt') return <TableCell key={col.key} className={`text-foreground text-center ${cellPadding}`}>{formatToBeijingTime(role.createdAt)}</TableCell>;
if (col.key === 'updatedAt') return <TableCell key={col.key} className={`text-foreground ${cellPadding}`}>{formatToBeijingTime(role.updatedAt)}</TableCell>; if (col.key === 'updatedAt') return <TableCell key={col.key} className={`text-foreground text-center ${cellPadding}`}>{formatToBeijingTime(role.updatedAt)}</TableCell>;
if (col.key === 'actions') return ( if (col.key === 'actions') return (
<TableCell key={col.key} className={`text-right ${cellPadding}`}> <TableCell key={col.key} className={`text-right ${cellPadding}`}>
<div className="flex justify-end gap-4"> <div className="flex justify-end gap-4">

Loading…
Cancel
Save