mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-04 03:27:05 -05:00 
			
		
		
		
	* Added ReadingList age rating from all series and started on some unit tests for the new flows. * Wrote more unit tests for Reading Lists * Added ability to restrict user accounts to a given age rating via admin edit user modal and invite user. This commit contains all basic code, but no query modifications. * When updating a reading list's title via UI, explicitly check if there is an existing RL with the same title. * Refactored Reading List calculation to work properly in the flows it's invoked from. * Cleaned up an unused method * Promoted Collections no longer show tags where a Series exists within them that is above the user's age rating. * Collection search now respects age restrictions * Series Detail page now checks if the user has explicit access (as a user might bypass with direct url access) * Hooked up age restriction for dashboard activity streams. * Refactored some methods from Series Controller and Library Controller to a new Search Controller to keep things organized * Updated Search to respect age restrictions * Refactored all the Age Restriction queries to extensions * Related Series no longer show up if they are out of the age restriction * Fixed a bad mapping for the update age restriction api * Fixed a UI state change after updating age restriction * Fixed unit test * Added a migration for reading lists * Code cleanup
		
			
				
	
	
		
			133 lines
		
	
	
		
			4.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			133 lines
		
	
	
		
			4.5 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);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Does the user have Change Restriction permission or admin rights
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="user"></param>
 | 
						|
    /// <returns></returns>
 | 
						|
    public async Task<bool> HasChangeRestrictionRole(AppUser user)
 | 
						|
    {
 | 
						|
        var roles = await _userManager.GetRolesAsync(user);
 | 
						|
        return roles.Contains(PolicyConstants.ChangePasswordRole) || roles.Contains(PolicyConstants.AdminRole);
 | 
						|
    }
 | 
						|
 | 
						|
}
 |