mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
31 lines
1018 B
C#
31 lines
1018 B
C#
using System.Threading.Tasks;
|
|
using API.Constants;
|
|
using API.Entities;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
namespace API.Data;
|
|
|
|
/// <summary>
|
|
/// New role introduced in v0.5.1. Adds the role to all users.
|
|
/// </summary>
|
|
public static class MigrateChangePasswordRoles
|
|
{
|
|
/// <summary>
|
|
/// Will not run if any users have the ChangePassword role already
|
|
/// </summary>
|
|
/// <param name="unitOfWork"></param>
|
|
/// <param name="userManager"></param>
|
|
public static async Task Migrate(IUnitOfWork unitOfWork, UserManager<AppUser> userManager)
|
|
{
|
|
var usersWithRole = await userManager.GetUsersInRoleAsync(PolicyConstants.ChangePasswordRole);
|
|
if (usersWithRole.Count != 0) return;
|
|
|
|
var allUsers = await unitOfWork.UserRepository.GetAllUsers();
|
|
foreach (var user in allUsers)
|
|
{
|
|
await userManager.RemoveFromRoleAsync(user, "ChangePassword");
|
|
await userManager.AddToRoleAsync(user, PolicyConstants.ChangePasswordRole);
|
|
}
|
|
}
|
|
}
|