Skip to content

Commit a004de8

Browse files
committed
Rename UserRole to AccountRole and update property names
1 parent d4de743 commit a004de8

15 files changed

+61
-60
lines changed

OpenBioCardServer/Controllers/AdminController.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public async Task<ActionResult<UserListResponse>> GetUsers()
6767
}
6868

6969
var users = await _context.Accounts
70-
.Where(a => a.Role != UserRole.Root)
70+
.Where(a => a.Role != AccountRole.Root)
7171
.Select(a => new UserInfoDto
7272
{
73-
Username = a.UserName,
73+
Username = a.AccountName,
7474
Type = a.Role.ToString().ToLower()
7575
})
7676
.ToListAsync();
@@ -96,7 +96,7 @@ public async Task<ActionResult<TokenResponse>> CreateUser([FromBody] CreateUserR
9696
return Unauthorized(new { Error = "Invalid token or insufficient permissions" });
9797
}
9898

99-
if (!Enum.TryParse<UserRole>(request.Type, true, out var userType) || userType == UserRole.Root)
99+
if (!Enum.TryParse<AccountRole>(request.Type, true, out var userType) || userType == AccountRole.Root)
100100
{
101101
return BadRequest(new { Error = "Invalid user type" });
102102
}
@@ -114,7 +114,7 @@ public async Task<ActionResult<TokenResponse>> CreateUser([FromBody] CreateUserR
114114
await transaction.CommitAsync();
115115

116116
_logger.LogInformation("Admin {AdminUser} created new user: {NewUser} (Type: {Type})",
117-
account.UserName, request.NewUsername, userType);
117+
account.AccountName, request.NewUsername, userType);
118118

119119
return Ok(new TokenResponse { Token = newToken });
120120
}
@@ -140,7 +140,7 @@ public async Task<IActionResult> DeleteUser(string username)
140140
return Unauthorized(new { Error = "Invalid token or insufficient permissions" });
141141
}
142142

143-
if (adminAccount.UserName == username)
143+
if (adminAccount.AccountName == username)
144144
{
145145
return BadRequest(new { Error = "Cannot delete your own account" });
146146
}
@@ -151,7 +151,7 @@ public async Task<IActionResult> DeleteUser(string username)
151151
return NotFound(new { Error = "User not found" });
152152
}
153153

154-
if (targetAccount.Role == UserRole.Root)
154+
if (targetAccount.Role == AccountRole.Root)
155155
{
156156
return Forbid("Cannot delete root account");
157157
}
@@ -160,7 +160,7 @@ public async Task<IActionResult> DeleteUser(string username)
160160
await _authService.DeleteAccountAsync(targetAccount);
161161

162162
_logger.LogInformation("Admin {AdminUser} deleted user: {TargetUser}",
163-
adminAccount.UserName, username);
163+
adminAccount.AccountName, username);
164164

165165
return Ok(new { Message = "User deleted successfully" });
166166
}

OpenBioCardServer/Controllers/AuthController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ public async Task<ActionResult<TokenResponse>> SignUp([FromBody] SignUpRequest r
4747
return BadRequest(new { Error = "Missing required fields" });
4848
}
4949

50-
if (!Enum.TryParse<UserRole>(request.UserType, true, out var userType))
50+
if (!Enum.TryParse<AccountRole>(request.UserType, true, out var userType))
5151
{
5252
return BadRequest(new { Error = "Invalid user type" });
5353
}
5454

55-
if (userType == UserRole.Root)
55+
if (userType == AccountRole.Root)
5656
{
5757
return Forbid("Cannot create root users");
5858
}
@@ -118,12 +118,12 @@ public async Task<IActionResult> DeleteAccount([FromBody] DeleteRequest request)
118118
}
119119

120120
var (isValid, account) = await _authService.ValidateTokenAsync(token);
121-
if (!isValid || account == null || account.UserName != request.Username)
121+
if (!isValid || account == null || account.AccountName != request.Username)
122122
{
123123
return Unauthorized(new { Error = "Invalid token or token does not match username" });
124124
}
125125

126-
if (account.Role == UserRole.Root)
126+
if (account.Role == AccountRole.Root)
127127
{
128128
return Forbid("Cannot delete root account");
129129
}

OpenBioCardServer/Controllers/Classic/ClassicAdminController.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<IActionResult> CheckPermission([FromBody] ClassicAdminRequest
4949
return Unauthorized(new ClassicErrorResponse("Invalid token"));
5050
}
5151

52-
if (account.UserName != request.Username)
52+
if (account.AccountName != request.Username)
5353
{
5454
return Unauthorized(new ClassicErrorResponse("Invalid token"));
5555
}
@@ -106,7 +106,7 @@ private async Task<IActionResult> GetUsersInternal(string token, string username
106106
return Unauthorized(new ClassicErrorResponse("Invalid token"));
107107
}
108108

109-
if (account.UserName != username)
109+
if (account.AccountName != username)
110110
{
111111
return Unauthorized(new ClassicErrorResponse("Invalid token"));
112112
}
@@ -118,10 +118,10 @@ private async Task<IActionResult> GetUsersInternal(string token, string username
118118

119119
// Get all non-root users
120120
var users = await _context.Accounts
121-
.Where(a => a.Role != UserRole.Root)
121+
.Where(a => a.Role != AccountRole.Root)
122122
.Select(a => new ClassicUserInfo
123123
{
124-
Username = a.UserName,
124+
Username = a.AccountName,
125125
Type = a.Role.ToString().ToLower()
126126
})
127127
.ToListAsync();
@@ -152,7 +152,7 @@ public async Task<IActionResult> CreateUser([FromBody] ClassicCreateUserRequest
152152
return Unauthorized(new ClassicErrorResponse("Invalid token"));
153153
}
154154

155-
if (account.UserName != request.Username)
155+
if (account.AccountName != request.Username)
156156
{
157157
return Unauthorized(new ClassicErrorResponse("Invalid token"));
158158
}
@@ -163,19 +163,19 @@ public async Task<IActionResult> CreateUser([FromBody] ClassicCreateUserRequest
163163
}
164164

165165
// Validate new user type
166-
if (!Enum.TryParse<UserRole>(request.Type, true, out var userType))
166+
if (!Enum.TryParse<AccountRole>(request.Type, true, out var userType))
167167
{
168168
return BadRequest(new ClassicErrorResponse("Invalid user type"));
169169
}
170170

171171
// Cannot create root users
172-
if (userType == UserRole.Root)
172+
if (userType == AccountRole.Root)
173173
{
174174
return StatusCode(403, new ClassicErrorResponse("Cannot create root users"));
175175
}
176176

177177
// Check if username already exists
178-
if (await _context.Accounts.AnyAsync(a => a.UserName == request.NewUsername))
178+
if (await _context.Accounts.AnyAsync(a => a.AccountName == request.NewUsername))
179179
{
180180
return Conflict(new ClassicErrorResponse("Username already exists"));
181181
}
@@ -184,7 +184,7 @@ public async Task<IActionResult> CreateUser([FromBody] ClassicCreateUserRequest
184184
var (hash, salt) = PasswordHasher.HashPassword(request.Password);
185185
var newAccount = new Account
186186
{
187-
UserName = request.NewUsername,
187+
AccountName = request.NewUsername,
188188
PasswordHash = hash,
189189
PasswordSalt = salt,
190190
Role = userType
@@ -242,7 +242,7 @@ public async Task<IActionResult> DeleteUser(string targetUsername, [FromBody] Cl
242242
return Unauthorized(new ClassicErrorResponse("Invalid token"));
243243
}
244244

245-
if (account.UserName != request.Username)
245+
if (account.AccountName != request.Username)
246246
{
247247
return Unauthorized(new ClassicErrorResponse("Invalid token"));
248248
}
@@ -253,21 +253,21 @@ public async Task<IActionResult> DeleteUser(string targetUsername, [FromBody] Cl
253253
}
254254

255255
// Cannot delete self
256-
if (account.UserName == targetUsername)
256+
if (account.AccountName == targetUsername)
257257
{
258258
return StatusCode(403, new ClassicErrorResponse("Cannot delete your own account"));
259259
}
260260

261261
var targetAccount = await _context.Accounts
262-
.FirstOrDefaultAsync(a => a.UserName == targetUsername);
262+
.FirstOrDefaultAsync(a => a.AccountName == targetUsername);
263263

264264
if (targetAccount == null)
265265
{
266266
return NotFound(new ClassicErrorResponse("User not found"));
267267
}
268268

269269
// Cannot delete root account
270-
if (targetAccount.Role == UserRole.Root)
270+
if (targetAccount.Role == AccountRole.Root)
271271
{
272272
return StatusCode(403, new ClassicErrorResponse("Cannot delete root account"));
273273
}

OpenBioCardServer/Controllers/Classic/ClassicAuthController.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ public async Task<IActionResult> SignUp([FromBody] ClassicSignUpRequest request)
5252
}
5353

5454
// Check if username already exists
55-
if (await _context.Accounts.AnyAsync(a => a.UserName == request.Username))
55+
if (await _context.Accounts.AnyAsync(a => a.AccountName == request.Username))
5656
{
5757
return Conflict(new ClassicErrorResponse("Username already exists"));
5858
}
5959

6060
// Validate and parse user type
61-
if (!Enum.TryParse<UserRole>(request.Type, true, out var userType))
61+
if (!Enum.TryParse<AccountRole>(request.Type, true, out var userType))
6262
{
6363
return BadRequest(new ClassicErrorResponse("Invalid user type"));
6464
}
6565

6666
// Cannot create root users via signup
67-
if (userType == UserRole.Root)
67+
if (userType == AccountRole.Root)
6868
{
6969
return StatusCode(403, new ClassicErrorResponse("Cannot create root users"));
7070
}
@@ -73,7 +73,7 @@ public async Task<IActionResult> SignUp([FromBody] ClassicSignUpRequest request)
7373
var (hash, salt) = PasswordHasher.HashPassword(request.Password);
7474
var account = new Account
7575
{
76-
UserName = request.Username,
76+
AccountName = request.Username,
7777
PasswordHash = hash,
7878
PasswordSalt = salt,
7979
Role = userType
@@ -144,7 +144,7 @@ private async Task<IActionResult> HandleSignInAsync(ClassicSignInRequest request
144144

145145
// 查询账户
146146
var account = await _context.Accounts
147-
.FirstOrDefaultAsync(a => a.UserName == request.Username);
147+
.FirstOrDefaultAsync(a => a.AccountName == request.Username);
148148

149149
// 账户不存在
150150
if (account == null)
@@ -167,7 +167,7 @@ private async Task<IActionResult> HandleSignInAsync(ClassicSignInRequest request
167167
await _context.SaveChangesAsync();
168168

169169
// 记录日志(区分Root和普通用户)
170-
if (account.Role == UserRole.Root)
170+
if (account.Role == AccountRole.Root)
171171
{
172172
_logger.LogInformation("Root user logged in");
173173
}
@@ -201,13 +201,13 @@ public async Task<IActionResult> DeleteAccount([FromBody] ClassicDeleteRequest r
201201
return Unauthorized(new ClassicErrorResponse("Invalid token"));
202202
}
203203

204-
if (account.UserName != request.Username)
204+
if (account.AccountName != request.Username)
205205
{
206206
return Unauthorized(new ClassicErrorResponse("Invalid token"));
207207
}
208208

209209
// Root account cannot be deleted
210-
if (account.Role == UserRole.Root)
210+
if (account.Role == AccountRole.Root)
211211
{
212212
return StatusCode(403, new ClassicErrorResponse("Cannot delete root account"));
213213
}

OpenBioCardServer/Controllers/Classic/ClassicSettingsController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task<IActionResult> GetAdminSettings([FromBody] ClassicAdminRequest
8888
return Unauthorized(new ClassicErrorResponse("Invalid token"));
8989
}
9090

91-
if (account.UserName != request.Username)
91+
if (account.AccountName != request.Username)
9292
{
9393
return Unauthorized(new ClassicErrorResponse("Invalid token"));
9494
}
@@ -133,7 +133,7 @@ public async Task<IActionResult> UpdateSettings([FromBody] ClassicUpdateSettings
133133
return Unauthorized(new ClassicErrorResponse("Invalid token"));
134134
}
135135

136-
if (account.UserName != request.Username)
136+
if (account.AccountName != request.Username)
137137
{
138138
return Unauthorized(new ClassicErrorResponse("Invalid token"));
139139
}

OpenBioCardServer/Controllers/Classic/ClassicUserController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public async Task<IActionResult> PatchProfile(string username, [FromBody] Classi
6464

6565
var (isValid, account) = await _authService.ValidateTokenAsync(token);
6666

67-
if (!isValid || account == null || account.UserName != username)
67+
if (!isValid || account == null || account.AccountName != username)
6868
return Unauthorized(new ClassicErrorResponse("Invalid token"));
6969

7070
try
@@ -96,7 +96,7 @@ public async Task<IActionResult> FullUpdateProfile(string username, [FromBody] C
9696

9797
var (isValid, account) = await _authService.ValidateTokenAsync(token);
9898

99-
if (!isValid || account == null || account.UserName != username)
99+
if (!isValid || account == null || account.AccountName != username)
100100
return Unauthorized(new ClassicErrorResponse("Invalid token"));
101101

102102
try
@@ -128,7 +128,7 @@ public async Task<IActionResult> ExportData(string username)
128128

129129
var (isValid, account) = await _authService.ValidateTokenAsync(token);
130130

131-
if (!isValid || account == null || account.UserName != username)
131+
if (!isValid || account == null || account.AccountName != username)
132132
return Unauthorized(new ClassicErrorResponse("Invalid token"));
133133

134134
try
@@ -160,7 +160,7 @@ public async Task<IActionResult> ImportData(string username, [FromBody] ClassicU
160160

161161
var (isValid, account) = await _authService.ValidateTokenAsync(token);
162162

163-
if (!isValid || account == null || account.UserName != username)
163+
if (!isValid || account == null || account.AccountName != username)
164164
return Unauthorized(new ClassicErrorResponse("Invalid token"));
165165

166166
try

OpenBioCardServer/Controllers/ProfileController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
115115
}
116116

117117
var (isValid, account) = await _authService.ValidateTokenAsync(token);
118-
return (isValid && account != null && account.UserName == username, account);
118+
return (isValid && account != null && account.AccountName == username, account);
119119
}
120120

121121
private string? GetTokenFromHeader() =>

OpenBioCardServer/Data/AppDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3535
modelBuilder.Entity<Account>(entity =>
3636
{
3737
// Unique index on Username for fast lookup
38-
entity.HasIndex(e => e.UserName).IsUnique();
38+
entity.HasIndex(e => e.AccountName).IsUnique();
3939

4040
// One-to-One with Profile
4141
entity.HasOne(e => e.Profile)

OpenBioCardServer/Models/Entities/Account.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Account
1010

1111
[Required]
1212
[MaxLength(64)]
13-
public string UserName { get; set; } = string.Empty;
13+
public string AccountName { get; set; } = string.Empty; // Username
1414

1515
[MaxLength(128)]
1616
public string EMail { get; set; } = string.Empty;
@@ -21,7 +21,7 @@ public class Account
2121
[MaxLength(256)]
2222
public string PasswordSalt { get; set; } = string.Empty;
2323

24-
public UserRole Role { get; set; } = UserRole.User;
24+
public AccountRole Role { get; set; } = AccountRole.User;
2525
public AccountType Type { get; set; } = AccountType.Personal;
2626

2727
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

OpenBioCardServer/Models/Enums/UserRole.cs renamed to OpenBioCardServer/Models/Enums/AccountRole.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace OpenBioCardServer.Models.Enums;
22

3-
public enum UserRole
3+
public enum AccountRole
44
{
55
User,
66
Admin,

0 commit comments

Comments
 (0)