
* Updated a typo in manage tasks of Reoccuring -> Recurring * Fixed a bug in MinimumNumberFromRange where a regex wasn't properly constructed which could skew results. * Fixed a bug where Volume numbers that were a float wouldn't render correctly in the manga reader menu. * Added the ability to double click on the image to bookmark it. Optimized the bookmark and unbookmark flows to remove 2 DB calls and reworked some flow of calls to speed it up. Fixed some logic where when using double (manga) flow, both of the images wouldn't show the bookmark effect, despite both of them being saved. Likewise, fixed a bug where both images weren't updating UI state, so switching from double (manga) to single, the second image wouldn't show as bookmarked without a refresh. * Double click works perfectly for bookmarking * Collection cover image chooser will now prompt with all series covers by default. Reset button is now moved up to the first slot if applicable. * When a Completed series is fully read by a user, a nightly task will now remove that series from their Want to Read list. * Added ability to trigger Want to Read cleanup from Tasks page. * Moved the brightness readout to the label line and fixed a bootstrap migration bug where small buttons weren't actually small. * Implemented ability to filter against release year (min or max or both). * Fixed a log message that wasn't properly formatted when scan finished an no files changes. * Cleaned up some code and merged some methods * Implemented sort by Release year metadata filter. * Fixed the code that finds ComicInfo.xml inside archives to only check the root and check explicitly for casing, so it must be ComicInfo.xml. * Dependency updates * Refactored some strings into consts and used TriggerJob rather than just enqueuing * Fixed the prefetcher which wasn't properly loading in the correct order as it was designed. * Cleaned up all traces of CircularArray from MangaReader * Removed a debug code * Fixed a bug with webtoon reader in fullscreen mode where continuous reader wouldn't trigger * When cleaning up series from users' want to read lists, include both completed and cancelled. * Fixed a bug where small images wouldn't have the pagination area extend to the bottom on manga reader * Added a new method for hashing during prod builds and ensure we always use aot * Fixed a bug where the save button wouldn't enable when color change occured. * Cleaned up some issues in one of contributor's PR.
121 lines
4.1 KiB
C#
121 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Constants;
|
|
using API.Data;
|
|
using API.Entities;
|
|
using API.Errors;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace API.Services;
|
|
|
|
public interface IAccountService
|
|
{
|
|
Task<IEnumerable<ApiException>> ChangeUserPassword(AppUser user, string newPassword);
|
|
Task<IEnumerable<ApiException>> ValidatePassword(AppUser user, string password);
|
|
Task<IEnumerable<ApiException>> ValidateUsername(string username);
|
|
Task<IEnumerable<ApiException>> ValidateEmail(string email);
|
|
Task<bool> HasBookmarkPermission(AppUser user);
|
|
Task<bool> HasDownloadPermission(AppUser user);
|
|
}
|
|
|
|
public class AccountService : IAccountService
|
|
{
|
|
private readonly UserManager<AppUser> _userManager;
|
|
private readonly ILogger<AccountService> _logger;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
public const string DefaultPassword = "[k.2@RZ!mxCQkJzE";
|
|
|
|
public AccountService(UserManager<AppUser> userManager, ILogger<AccountService> logger, IUnitOfWork unitOfWork)
|
|
{
|
|
_userManager = userManager;
|
|
_logger = logger;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task<IEnumerable<ApiException>> ChangeUserPassword(AppUser user, string newPassword)
|
|
{
|
|
var passwordValidationIssues = (await ValidatePassword(user, newPassword)).ToList();
|
|
if (passwordValidationIssues.Any()) return passwordValidationIssues;
|
|
|
|
var result = await _userManager.RemovePasswordAsync(user);
|
|
if (!result.Succeeded)
|
|
{
|
|
_logger.LogError("Could not update password");
|
|
return result.Errors.Select(e => new ApiException(400, e.Code, e.Description));
|
|
}
|
|
|
|
|
|
result = await _userManager.AddPasswordAsync(user, newPassword);
|
|
if (!result.Succeeded)
|
|
{
|
|
_logger.LogError("Could not update password");
|
|
return result.Errors.Select(e => new ApiException(400, e.Code, e.Description));
|
|
}
|
|
|
|
return new List<ApiException>();
|
|
}
|
|
|
|
public async Task<IEnumerable<ApiException>> ValidatePassword(AppUser user, string password)
|
|
{
|
|
foreach (var validator in _userManager.PasswordValidators)
|
|
{
|
|
var validationResult = await validator.ValidateAsync(_userManager, user, password);
|
|
if (!validationResult.Succeeded)
|
|
{
|
|
return validationResult.Errors.Select(e => new ApiException(400, e.Code, e.Description));
|
|
}
|
|
}
|
|
|
|
return Array.Empty<ApiException>();
|
|
}
|
|
public async Task<IEnumerable<ApiException>> ValidateUsername(string username)
|
|
{
|
|
if (await _userManager.Users.AnyAsync(x => x.NormalizedUserName == username.ToUpper()))
|
|
{
|
|
return new List<ApiException>()
|
|
{
|
|
new ApiException(400, "Username is already taken")
|
|
};
|
|
}
|
|
|
|
return Array.Empty<ApiException>();
|
|
}
|
|
|
|
public async Task<IEnumerable<ApiException>> ValidateEmail(string email)
|
|
{
|
|
var user = await _unitOfWork.UserRepository.GetUserByEmailAsync(email);
|
|
if (user == null) return Array.Empty<ApiException>();
|
|
|
|
return new List<ApiException>()
|
|
{
|
|
new ApiException(400, "Email is already registered")
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does the user have the Bookmark permission or admin rights
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> HasBookmarkPermission(AppUser user)
|
|
{
|
|
var roles = await _userManager.GetRolesAsync(user);
|
|
return roles.Contains(PolicyConstants.BookmarkRole) || roles.Contains(PolicyConstants.AdminRole);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Does the user have the Download permission or admin rights
|
|
/// </summary>
|
|
/// <param name="user"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> HasDownloadPermission(AppUser user)
|
|
{
|
|
var roles = await _userManager.GetRolesAsync(user);
|
|
return roles.Contains(PolicyConstants.DownloadRole) || roles.Contains(PolicyConstants.AdminRole);
|
|
}
|
|
|
|
}
|