diff options
author | crupest <crupest@outlook.com> | 2021-04-27 19:29:20 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-27 19:29:20 +0800 |
commit | a665f5d894539cae5f4188e4a72ea9634b8c4ed0 (patch) | |
tree | 03aa9d578e13213018c178ba47755167e2ccc2d4 /BackEnd/Timeline/Services/User/UserService.cs | |
parent | 89e3c2103fa478ebc659a1b914e75d5138c06c08 (diff) | |
download | timeline-a665f5d894539cae5f4188e4a72ea9634b8c4ed0.tar.gz timeline-a665f5d894539cae5f4188e4a72ea9634b8c4ed0.tar.bz2 timeline-a665f5d894539cae5f4188e4a72ea9634b8c4ed0.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Services/User/UserService.cs')
-rw-r--r-- | BackEnd/Timeline/Services/User/UserService.cs | 148 |
1 files changed, 73 insertions, 75 deletions
diff --git a/BackEnd/Timeline/Services/User/UserService.cs b/BackEnd/Timeline/Services/User/UserService.cs index bbbe15b0..6496b55b 100644 --- a/BackEnd/Timeline/Services/User/UserService.cs +++ b/BackEnd/Timeline/Services/User/UserService.cs @@ -10,57 +10,6 @@ using Timeline.Models.Validation; namespace Timeline.Services.User
{
- /// <summary>
- /// Null means not change.
- /// </summary>
- public class ModifyUserParams
- {
- public string? Username { get; set; }
- public string? Password { get; set; }
- public string? Nickname { get; set; }
- }
-
- public interface IUserService : IBasicUserService
- {
- /// <summary>
- /// Try to get a user by id.
- /// </summary>
- /// <param name="id">The id of the user.</param>
- /// <returns>The user info.</returns>
- /// <exception cref="UserNotExistException">Thrown when the user with given id does not exist.</exception>
- Task<UserEntity> GetUser(long id);
-
- /// <summary>
- /// List all users.
- /// </summary>
- /// <returns>The user info of users.</returns>
- Task<List<UserEntity>> GetUsers();
-
- /// <summary>
- /// Create a user with given info.
- /// </summary>
- /// <param name="username">The username of new user.</param>
- /// <param name="password">The password of new user.</param>
- /// <returns>The the new user.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> or <paramref name="password"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="username"/> or <paramref name="password"/> is of bad format.</exception>
- /// <exception cref="EntityAlreadyExistException">Thrown when a user with given username already exists.</exception>
- Task<UserEntity> CreateUser(string username, string password);
-
- /// <summary>
- /// Modify a user.
- /// </summary>
- /// <param name="id">The id of the user.</param>
- /// <param name="param">The new information.</param>
- /// <returns>The new user info.</returns>
- /// <exception cref="ArgumentException">Thrown when some fields in <paramref name="param"/> is bad.</exception>
- /// <exception cref="UserNotExistException">Thrown when user with given id does not exist.</exception>
- /// <remarks>
- /// Version will increase if password is changed.
- /// </remarks>
- Task<UserEntity> ModifyUser(long id, ModifyUserParams? param);
- }
-
public class UserService : BasicUserService, IUserService
{
private readonly ILogger<UserService> _logger;
@@ -110,58 +59,63 @@ namespace Timeline.Services.User throw new UserAlreadyExistException(user);
}
- public async Task<UserEntity> GetUser(long id)
+ public async Task<UserEntity> GetUserAsync(long id)
{
var user = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync();
- if (user == null)
+ if (user is null)
throw new UserNotExistException(id);
return user;
}
- public async Task<List<UserEntity>> GetUsers()
+ public async Task<List<UserEntity>> GetUsersAsync()
{
return await _databaseContext.Users.ToListAsync();
}
- public async Task<UserEntity> CreateUser(string username, string password)
+ public async Task<UserEntity> CreateUserAsync(CreateUserParams param)
{
- if (username == null)
- throw new ArgumentNullException(nameof(username));
- if (password == null)
- throw new ArgumentNullException(nameof(password));
-
- CheckUsernameFormat(username, nameof(username));
- CheckPasswordFormat(password, nameof(password));
-
- var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == username);
+ if (param is null)
+ throw new ArgumentNullException(nameof(param));
+ if (param.Username is null)
+ throw new ArgumentException(Resource.ExceptionUsernameNull, nameof(param));
+ if (param.Password is null)
+ throw new ArgumentException(Resource.ExceptionPasswordNull, nameof(param));
+ CheckUsernameFormat(param.Username, nameof(param));
+ CheckPasswordFormat(param.Password, nameof(param));
+ if (param.Nickname is not null)
+ CheckNicknameFormat(param.Nickname, nameof(param));
+
+ var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == param.Username);
if (conflict)
ThrowUsernameConflict(null);
var newEntity = new UserEntity
{
- Username = username,
- Password = _passwordService.HashPassword(password),
+ Username = param.Username,
+ Password = _passwordService.HashPassword(param.Password),
+ Nickname = param.Nickname,
Version = 1
};
_databaseContext.Users.Add(newEntity);
await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogUserCreated, param.Username, newEntity.Id);
return newEntity;
}
- public async Task<UserEntity> ModifyUser(long id, ModifyUserParams? param)
+ public async Task<UserEntity> ModifyUserAsync(long id, ModifyUserParams? param)
{
- if (param != null)
+ if (param is not null)
{
- if (param.Username != null)
+ if (param.Username is not null)
CheckUsernameFormat(param.Username, nameof(param));
- if (param.Password != null)
+ if (param.Password is not null)
CheckPasswordFormat(param.Password, nameof(param));
- if (param.Nickname != null)
+ if (param.Nickname is not null)
CheckNicknameFormat(param.Nickname, nameof(param));
}
@@ -169,13 +123,13 @@ namespace Timeline.Services.User if (entity == null)
throw new UserNotExistException(id);
- if (param != null)
+ if (param is not null)
{
var now = _clock.GetCurrentTime();
bool updateLastModified = false;
var username = param.Username;
- if (username != null && username != entity.Username)
+ if (username is not null && username != entity.Username)
{
var conflict = await _databaseContext.Users.AnyAsync(u => u.Username == username);
if (conflict)
@@ -187,14 +141,14 @@ namespace Timeline.Services.User }
var password = param.Password;
- if (password != null)
+ if (password is not null)
{
entity.Password = _passwordService.HashPassword(password);
entity.Version += 1;
}
var nickname = param.Nickname;
- if (nickname != null && nickname != entity.Nickname)
+ if (nickname is not null && nickname != entity.Nickname)
{
entity.Nickname = nickname;
updateLastModified = true;
@@ -206,9 +160,53 @@ namespace Timeline.Services.User }
await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogUserModified, entity.Username, id);
}
return entity;
}
+
+ public async Task<long> VerifyCredential(string username, string password)
+ {
+ if (username is null)
+ throw new ArgumentNullException(nameof(username));
+ if (password is null)
+ throw new ArgumentNullException(nameof(password));
+ CheckUsernameFormat(username, nameof(username));
+ CheckPasswordFormat(password, nameof(password));
+
+ var entity = await _databaseContext.Users.Where(u => u.Username == username).Select(u => new { u.Id, u.Password }).SingleOrDefaultAsync();
+
+ if (entity is null)
+ throw new UserNotExistException(username);
+
+ if (!_passwordService.VerifyPassword(entity.Password, password))
+ throw new BadPasswordException(password);
+
+ return entity.Id;
+ }
+
+ public async Task ChangePassword(long id, string oldPassword, string newPassword)
+ {
+ if (oldPassword == null)
+ throw new ArgumentNullException(nameof(oldPassword));
+ if (newPassword == null)
+ throw new ArgumentNullException(nameof(newPassword));
+ CheckPasswordFormat(oldPassword, nameof(oldPassword));
+ CheckPasswordFormat(newPassword, nameof(newPassword));
+
+ var entity = await _databaseContext.Users.Where(u => u.Id == id).SingleOrDefaultAsync();
+
+ if (entity is null)
+ throw new UserNotExistException(id);
+
+ if (!_passwordService.VerifyPassword(entity.Password, oldPassword))
+ throw new BadPasswordException(oldPassword);
+
+ entity.Password = _passwordService.HashPassword(newPassword);
+ entity.Version += 1;
+ await _databaseContext.SaveChangesAsync();
+ _logger.LogInformation(Resource.LogChangePassowrd, entity.Username, id);
+ }
}
}
|