Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Fixed a critical bug where registration was broken for first time flow. Refactored how backup before migrations occured such that it now puts the db in temp. The db will be deleted automatically that night. (#900)
Joseph Milazzo6006faa574ec
diff --git a/API/Controllers/AccountController.cs b/API/Controllers/AccountController.csindex 01eb52412..62ad0ebb8 100644--- a/API/Controllers/AccountController.cs+++ b/API/Controllers/AccountController.cs@@ -78,7 +78,6 @@ namespace API.Controllers/// </summary>/// <param name="registerDto"></param>/// <returns></returns>- [Authorize(Policy = "RequireAdminRole")][HttpPost("register")]public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto){@@ -89,6 +88,17 @@ namespace API.Controllersreturn BadRequest("Username is taken.");}+ // If we are registering an admin account, ensure there are no existing admins or user registering is an admin+ if (registerDto.IsAdmin)+ {+ var firstTimeFlow = !(await _userManager.GetUsersInRoleAsync("Admin")).Any();+ if (!firstTimeFlow && !await _unitOfWork.UserRepository.IsUserAdminAsync(+ await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername())))+ {+ return BadRequest("You are not permitted to create an admin account");+ }+ }+var user = _mapper.Map<AppUser>(registerDto);user.UserPreferences ??= new AppUserPreferences();user.ApiKey = HashUtil.ApiKey();@@ -104,6 +114,7 @@ namespace API.Controllersif (!result.Succeeded) return BadRequest(result.Errors);+var role = registerDto.IsAdmin ? PolicyConstants.AdminRole : PolicyConstants.PlebRole;var roleResult = await _userManager.AddToRoleAsync(user, role);@@ -156,7 +167,7 @@ namespace API.Controllersif (user == null) return Unauthorized("Invalid username");- var isAdmin = await _unitOfWork.UserRepository.IsUserAdmin(user);+ var isAdmin = await _unitOfWork.UserRepository.IsUserAdminAsync(user);var settings = await _unitOfWork.SettingsRepository.GetSettingsDtoAsync();if (!settings.EnableAuthentication && !isAdmin){diff --git a/API/Controllers/CollectionController.cs b/API/Controllers/CollectionController.csindex 2b69499de..9f297273f 100644--- a/API/Controllers/CollectionController.cs+++ b/API/Controllers/CollectionController.cs@@ -32,7 +32,7 @@ namespace API.Controllerspublic async Task<IEnumerable<CollectionTagDto>> GetAllTags(){var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());- var isAdmin = await _unitOfWork.UserRepository.IsUserAdmin(user);+ var isAdmin = await _unitOfWork.UserRepository.IsUserAdminAsync(user);if (isAdmin){return await _unitOfWork.CollectionTagRepository.GetAllTagDtosAsync();diff --git a/API/Controllers/OPDSController.cs b/API/Controllers/OPDSController.csindex 68b9c14a2..7de25dcf3 100644--- a/API/Controllers/OPDSController.cs+++ b/API/Controllers/OPDSController.cs@@ -184,7 +184,7 @@ public class OpdsController : BaseApiControllerreturn BadRequest("OPDS is not enabled on this server");var userId = await GetUser(apiKey);var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);- var isAdmin = await _unitOfWork.UserRepository.IsUserAdmin(user);+ var isAdmin = await _unitOfWork.UserRepository.IsUserAdminAsync(user);IList<CollectionTagDto> tags = isAdmin ? (await _unitOfWork.CollectionTagRepository.GetAllTagDtosAsync()).ToList(): (await _unitOfWork.CollectionTagRepository.GetAllPromotedTagDtosAsync()).ToList();@@ -220,7 +220,7 @@ public class OpdsController : BaseApiControllerreturn BadRequest("OPDS is not enabled on this server");var userId = await GetUser(apiKey);var user = await _unitOfWork.UserRepository.GetUserByIdAsync(userId);- var isAdmin = await _unitOfWork.UserRepository.IsUserAdmin(user);+ var isAdmin = await _unitOfWork.UserRepository.IsUserAdminAsync(user);IEnumerable <CollectionTagDto> tags;if (isAdmin)diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.csindex bf7de16e7..ec63e3b3e 100644--- a/API/Controllers/SeriesController.cs+++ b/API/Controllers/SeriesController.cs@@ -154,7 +154,7 @@ namespace API.Controllerspublic async Task<ActionResult> UpdateSeriesRating(UpdateSeriesRatingDto updateSeriesRatingDto){var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername(), AppUserIncludes.Ratings);- var userRating = await _unitOfWork.UserRepository.GetUserRating(updateSeriesRatingDto.SeriesId, user.Id) ??+ var userRating = await _unitOfWork.UserRepository.GetUserRatingAsync(updateSeriesRatingDto.SeriesId, user.Id) ??new AppUserRating();userRating.Rating = updateSeriesRatingDto.UserRating;diff --git a/API/Data/Repositories/UserRepository.cs b/API/Data/Repositories/UserRepository.csindex 2b3bfd478..138ef15b8 100644--- a/API/Data/Repositories/UserRepository.cs+++ b/API/Data/Repositories/UserRepository.cs@@ -32,8 +32,8 @@ public interface IUserRepositoryTask<IEnumerable<MemberDto>> GetMembersAsync();Task<IEnumerable<AppUser>> GetAdminUsersAsync();Task<IEnumerable<AppUser>> GetNonAdminUsersAsync();- Task<bool> IsUserAdmin(AppUser user);- Task<AppUserRating> GetUserRating(int seriesId, int userId);+ Task<bool> IsUserAdminAsync(AppUser user);+ Task<AppUserRating> GetUserRatingAsync(int seriesId, int userId);Task<AppUserPreferences> GetPreferencesAsync(string username);Task<IEnumerable<BookmarkDto>> GetBookmarkDtosForSeries(int userId, int seriesId);Task<IEnumerable<BookmarkDto>> GetBookmarkDtosForVolume(int userId, int volumeId);@@ -208,12 +208,12 @@ public class UserRepository : IUserRepositoryreturn await _userManager.GetUsersInRoleAsync(PolicyConstants.PlebRole);}- public async Task<bool> IsUserAdmin(AppUser user)+ public async Task<bool> IsUserAdminAsync(AppUser user){return await _userManager.IsInRoleAsync(user, PolicyConstants.AdminRole);}- public async Task<AppUserRating> GetUserRating(int seriesId, int userId)+ public async Task<AppUserRating> GetUserRatingAsync(int seriesId, int userId){return await _context.AppUserRating.Where(r => r.SeriesId == seriesId && r.AppUserId == userId).SingleOrDefaultAsync();diff --git a/API/Program.cs b/API/Program.csindex 713e2c404..73b9b5b7f 100644--- a/API/Program.cs+++ b/API/Program.cs@@ -56,51 +56,29 @@ namespace APItry{+ var logger = services.GetRequiredService<ILogger<Program>>();var context = services.GetRequiredService<DataContext>();+ var pendingMigrations = await context.Database.GetPendingMigrationsAsync();+ if (pendingMigrations.Any())+ {+ logger.LogInformation("Performing backup as migrations are needed. Backup will be kavita.db in temp folder");+ directoryService.CopyFileToDirectory(directoryService.FileSystem.Path.Join(directoryService.ConfigDirectory, "kavita.db"), directoryService.TempDirectory);+ }++ await context.Database.MigrateAsync();+ var roleManager = services.GetRequiredService<RoleManager<AppRole>>();++ await Seed.SeedRoles(roleManager);+ await Seed.SeedSettings(context, directoryService);+ await Seed.SeedUserApiKeys(context);if (isDocker && new FileInfo("data/appsettings.json").Exists){- var logger = services.GetRequiredService<ILogger<Startup>>();+ //var logger = services.GetRequiredService<ILogger<Startup>>();logger.LogCritical("WARNING! Mount point is incorrect, nothing here will persist. Please change your container mount from /kavita/data to /kavita/config");return;}-- var requiresCoverImageMigration = !Directory.Exists(directoryService.CoverImageDirectory);- try- {- // If this is a new install, tables wont exist yet- if (requiresCoverImageMigration)- {- MigrateCoverImages.ExtractToImages(context, directoryService, services.GetRequiredService<ImageService>());- }- }- catch (Exception)- {- requiresCoverImageMigration = false;- }-- if (requiresCoverImageMigration)- {- await MigrateCoverImages.UpdateDatabaseWithImages(context, directoryService);- }-- // // Apply all migrations on startup- // // If we have pending migrations, make a backup first- // var pendingMigrations = await context.Database.GetPendingMigrationsAsync();- // if (pendingMigrations.Any())- // {- // var logger = services.GetRequiredService<ILogger<Program>>();- // logger.LogInformation("Performing backup as migrations are needed");- // // var backupService = services.GetRequiredService<BackupService>();- // // await backupService.BackupDatabase();- // }- //- // await context.Database.MigrateAsync();- //- // await Seed.SeedRoles(roleManager);- // await Seed.SeedSettings(context, directoryService);- // await Seed.SeedUserApiKeys(context);}catch (Exception ex){diff --git a/API/Startup.cs b/API/Startup.csindex b6bd5630e..f7eb31aee 100644--- a/API/Startup.cs+++ b/API/Startup.cs@@ -134,7 +134,7 @@ namespace API// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env,IHostApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, ICacheService cacheService,- IDirectoryService directoryService, IUnitOfWork unitOfWork, IBackupService backupService)+ IDirectoryService directoryService, IUnitOfWork unitOfWork, IBackupService backupService, IImageService imageService){// Apply Migrations@@ -144,26 +144,44 @@ namespace API{// Apply all migrations on startup// If we have pending migrations, make a backup first+ var isDocker = new OsInfo(Array.Empty<IOsVersionAdapter>()).IsDocker;var logger = serviceProvider.GetRequiredService<ILogger<Program>>();var context = serviceProvider.GetRequiredService<DataContext>();- var pendingMigrations = await context.Database.GetPendingMigrationsAsync();- if (pendingMigrations.Any())- {- logger.LogInformation("Performing backup as migrations are needed");- await backupService.BackupDatabase();- }-- await context.Database.MigrateAsync();+ // var pendingMigrations = await context.Database.GetPendingMigrationsAsync();+ // if (pendingMigrations.Any())+ // {+ // logger.LogInformation("Performing backup as migrations are needed");+ // await backupService.BackupDatabase();+ // }+ //+ // await context.Database.MigrateAsync();+ // var roleManager = serviceProvider.GetRequiredService<RoleManager<AppRole>>();+ //+ // await Seed.SeedRoles(roleManager);+ // await Seed.SeedSettings(context, directoryService);+ // await Seed.SeedUserApiKeys(context);await MigrateBookmarks.Migrate(directoryService, unitOfWork,logger, cacheService);- var roleManager = serviceProvider.GetRequiredService<RoleManager<AppRole>>();-- await Seed.SeedRoles(roleManager);- await Seed.SeedSettings(context, directoryService);- await Seed.SeedUserApiKeys(context);+ var requiresCoverImageMigration = !Directory.Exists(directoryService.CoverImageDirectory);+ try+ {+ // If this is a new install, tables wont exist yet+ if (requiresCoverImageMigration)+ {+ MigrateCoverImages.ExtractToImages(context, directoryService, imageService);+ }+ }+ catch (Exception)+ {+ requiresCoverImageMigration = false;+ }+ if (requiresCoverImageMigration)+ {+ await MigrateCoverImages.UpdateDatabaseWithImages(context, directoryService);+ }}).GetAwaiter().GetResult();}