From d088a03986713a71e274fc16144ca42b7f020e3f Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Tue, 23 Jul 2019 16:47:53 +0800 Subject: WIP: Develop UserService. Remove unused components. --- Timeline/Services/UserService.cs | 204 ++++++++++----------------------------- 1 file changed, 50 insertions(+), 154 deletions(-) (limited to 'Timeline/Services/UserService.cs') diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index 49c9747d..ec8e5091 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -50,70 +50,6 @@ namespace Timeline.Services System.Runtime.Serialization.StreamingContext context) : base(info, context) { } } - public enum PutUserResult - { - /// - /// A new user is created. - /// - Created, - /// - /// A existing user is modified. - /// - Modified - } - - public enum PatchUserResult - { - /// - /// Succeed to modify user. - /// - Success, - /// - /// A user of given username does not exist. - /// - NotExists - } - - public enum DeleteUserResult - { - /// - /// A existing user is deleted. - /// - Deleted, - /// - /// A user of given username does not exist. - /// - NotExists - } - - public enum ChangePasswordResult - { - /// - /// Success to change password. - /// - Success, - /// - /// The user does not exists. - /// - NotExists, - /// - /// Old password is wrong. - /// - BadOldPassword - } - - public enum PutAvatarResult - { - /// - /// Success to upload avatar. - /// - Success, - /// - /// The user does not exists. - /// - UserNotExists - } - public interface IUserService { /// @@ -160,31 +96,29 @@ namespace Timeline.Services /// /// Username of user. /// Password of user. - /// Array of roles of user. - /// Return if a new user is created. - /// Return if a existing user is modified. - Task PutUser(string username, string password, bool isAdmin); + /// Whether the user is administrator. + /// Return if a new user is created. + /// Return if a existing user is modified. + /// Thrown when or is null. + Task PutUser(string username, string password, bool isAdmin); /// - /// Partially modify a use of given username. + /// Partially modify a user of given username. /// - /// Username of the user to modify. - /// New password. If not modify, then null. - /// New roles. If not modify, then null. - /// Return if modification succeeds. - /// Return if the user of given username doesn't exist. - Task PatchUser(string username, string password, bool? isAdmin); + /// Username of the user to modify. Can't be null. + /// New password. Null if not modify. + /// Whether the user is administrator. Null if not modify. + /// Thrown if is null. + /// Thrown if the user with given username does not exist. + Task PatchUser(string username, string password, bool? isAdmin); /// /// Delete a user of given username. - /// Return if the user is deleted. - /// Return if the user of given username - /// does not exist. /// - /// Username of thet user to delete. - /// if the user is deleted. - /// if the user doesn't exist. - Task DeleteUser(string username); + /// Username of thet user to delete. Can't be null. + /// Thrown if is null. + /// Thrown if the user with given username does not exist. + Task DeleteUser(string username); /// /// Try to change a user's password with old password. @@ -192,27 +126,10 @@ namespace Timeline.Services /// The name of user to change password of. /// The user's old password. /// The user's new password. - /// if success. - /// if user does not exist. - /// if old password is wrong. - Task ChangePassword(string username, string oldPassword, string newPassword); - - /// - /// Get the true avatar url of a user. - /// - /// The name of user. - /// The url if user exists. Null if user does not exist. - Task GetAvatarUrl(string username); - - /// - /// Put a avatar of a user. - /// - /// The name of user. - /// The data of avatar image. - /// The mime type of the image. - /// Return if success. - /// Return if user does not exist. - Task PutAvatar(string username, byte[] data, string mimeType); + /// Thrown if or or is null. + /// Thrown if the user with given username does not exist. + /// Thrown if the old password is wrong. + Task ChangePassword(string username, string oldPassword, string newPassword); } internal class UserCache @@ -348,8 +265,13 @@ namespace Timeline.Services .ToArrayAsync(); } - public async Task PutUser(string username, string password, bool isAdmin) + public async Task PutUser(string username, string password, bool isAdmin) { + if (username == null) + throw new ArgumentNullException(nameof(username)); + if (password == null) + throw new ArgumentNullException(nameof(password)); + var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); if (user == null) @@ -362,7 +284,7 @@ namespace Timeline.Services Version = 0 }); await _databaseContext.SaveChangesAsync(); - return PutUserResult.Created; + return PutResult.Created; } user.EncryptedPassword = _passwordService.HashPassword(password); @@ -373,15 +295,17 @@ namespace Timeline.Services //clear cache RemoveCache(user.Id); - return PutUserResult.Modified; + return PutResult.Modified; } - public async Task PatchUser(string username, string password, bool? isAdmin) + public async Task PatchUser(string username, string password, bool? isAdmin) { - var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); + if (username == null) + throw new ArgumentNullException(nameof(username)); + var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); if (user == null) - return PatchUserResult.NotExists; + throw new UserNotExistException(); bool modified = false; @@ -399,78 +323,50 @@ namespace Timeline.Services if (modified) { + user.Version += 1; await _databaseContext.SaveChangesAsync(); //clear cache RemoveCache(user.Id); } - - return PatchUserResult.Success; } - public async Task DeleteUser(string username) + public async Task DeleteUser(string username) { - var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); + if (username == null) + throw new ArgumentNullException(nameof(username)); + var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); if (user == null) - { - return DeleteUserResult.NotExists; - } + throw new UserNotExistException(); _databaseContext.Users.Remove(user); await _databaseContext.SaveChangesAsync(); //clear cache RemoveCache(user.Id); - - return DeleteUserResult.Deleted; } - public async Task ChangePassword(string username, string oldPassword, string newPassword) + public async Task ChangePassword(string username, string oldPassword, string newPassword) { + if (username == null) + throw new ArgumentNullException(nameof(username)); + if (oldPassword == null) + throw new ArgumentNullException(nameof(oldPassword)); + if (newPassword == null) + throw new ArgumentNullException(nameof(newPassword)); + var user = await _databaseContext.Users.Where(u => u.Name == username).SingleOrDefaultAsync(); if (user == null) - return ChangePasswordResult.NotExists; + throw new UserNotExistException(); var verifyResult = _passwordService.VerifyPassword(user.EncryptedPassword, oldPassword); if (!verifyResult) - return ChangePasswordResult.BadOldPassword; + throw new BadPasswordException(); user.EncryptedPassword = _passwordService.HashPassword(newPassword); + user.Version += 1; await _databaseContext.SaveChangesAsync(); //clear cache RemoveCache(user.Id); - - return ChangePasswordResult.Success; - } - - public async Task GetAvatarUrl(string username) - { - if (username == null) - throw new ArgumentNullException(nameof(username)); - - if ((await GetUser(username)) == null) - return null; - - var exists = await _cosService.IsObjectExists("avatar", username); - if (exists) - return _cosService.GenerateObjectGetUrl("avatar", username); - else - return _cosService.GenerateObjectGetUrl("avatar", "__default"); - } - - public async Task PutAvatar(string username, byte[] data, string mimeType) - { - if (username == null) - throw new ArgumentNullException(nameof(username)); - if (data == null) - throw new ArgumentNullException(nameof(data)); - if (mimeType == null) - throw new ArgumentNullException(nameof(mimeType)); - - if ((await GetUser(username)) == null) - return PutAvatarResult.UserNotExists; - - await _cosService.PutObject("avatar", username, data, mimeType); - return PutAvatarResult.Success; } } } -- cgit v1.2.3