diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-04 15:46:31 +0800 |
---|---|---|
committer | 杨宇千 <crupest@outlook.com> | 2019-08-04 15:46:31 +0800 |
commit | 36da221444807742a9e26f2ba636d9c6aef6155f (patch) | |
tree | deace7e60ff91362572bf178004f3928175a340e /Timeline | |
parent | a17aa8770b0b4861849c6e01812b2ff686497f02 (diff) | |
download | timeline-36da221444807742a9e26f2ba636d9c6aef6155f.tar.gz timeline-36da221444807742a9e26f2ba636d9c6aef6155f.tar.bz2 timeline-36da221444807742a9e26f2ba636d9c6aef6155f.zip |
Continue to add unit tests for token. Fix a bug thanks to unit test.
Diffstat (limited to 'Timeline')
-rw-r--r-- | Timeline/Controllers/TokenController.cs | 2 | ||||
-rw-r--r-- | Timeline/Services/UserService.cs | 23 |
2 files changed, 10 insertions, 15 deletions
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index 66cf3dad..21f87ded 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -119,7 +119,7 @@ namespace Timeline.Controllers } catch (BadTokenVersionException e) { - var code = ErrorCodes.Verify_BadToken; + var code = ErrorCodes.Verify_BadVersion; _logger.LogInformation(LoggingEventIds.VerifyFailed, e, "Attemp to verify a bad token because version is old. Code: {} Token: {}.", code, request.Token); return BadRequest(new CommonResponse(code, "The token is expired. Try recreate a token.")); } diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 328dbff0..65ac98d3 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -19,7 +19,7 @@ namespace Timeline.Services [Serializable] public class UserNotExistException : Exception { - public UserNotExistException(): base("The user does not exist.") { } + public UserNotExistException() : base("The user does not exist.") { } public UserNotExistException(string message) : base(message) { } public UserNotExistException(string message, Exception inner) : base(message, inner) { } protected UserNotExistException( @@ -30,7 +30,7 @@ namespace Timeline.Services [Serializable] public class BadPasswordException : Exception { - public BadPasswordException(): base("Password is wrong.") { } + public BadPasswordException() : base("Password is wrong.") { } public BadPasswordException(string message) : base(message) { } public BadPasswordException(string message, Exception inner) : base(message, inner) { } protected BadPasswordException( @@ -42,7 +42,7 @@ namespace Timeline.Services [Serializable] public class BadTokenVersionException : Exception { - public BadTokenVersionException(): base("Token version is expired.") { } + public BadTokenVersionException() : base("Token version is expired.") { } public BadTokenVersionException(string message) : base(message) { } public BadTokenVersionException(string message, Exception inner) : base(message, inner) { } protected BadTokenVersionException( @@ -105,6 +105,8 @@ namespace Timeline.Services /// <summary> /// Partially modify a user of given username. + /// + /// Note that whether actually modified or not, Version of the user will always increase. /// </summary> /// <param name="username">Username of the user to modify. Can't be null.</param> /// <param name="password">New password. Null if not modify.</param> @@ -309,27 +311,20 @@ namespace Timeline.Services if (user == null) throw new UserNotExistException(); - bool modified = false; - if (password != null) { - modified = true; user.EncryptedPassword = _passwordService.HashPassword(password); } if (administrator != null) { - modified = true; user.RoleString = IsAdminToRoleString(administrator.Value); } - if (modified) - { - user.Version += 1; - await _databaseContext.SaveChangesAsync(); - //clear cache - RemoveCache(user.Id); - } + user.Version += 1; + await _databaseContext.SaveChangesAsync(); + //clear cache + RemoveCache(user.Id); } public async Task DeleteUser(string username) |