Skip to content

Commit

Permalink
added migration;
Browse files Browse the repository at this point in the history
version 8.3.2
  • Loading branch information
vova3211 committed Oct 25, 2021
1 parent a76c070 commit c685656
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<Authors>Matteo Fabbri</Authors>
<Company />
<Product>AspNetCore.Identity.Mongo</Product>
<Version>8.3.1</Version>
<AssemblyVersion>8.3.1.0</AssemblyVersion>
<FileVersion>8.3.1.0</FileVersion>
<Version>8.3.2</Version>
<AssemblyVersion>8.3.2.0</AssemblyVersion>
<FileVersion>8.3.2.0</FileVersion>
<PackageProjectUrl>https://github.com/matteofabbri/AspNetCore.Identity.Mongo</PackageProjectUrl>
<NeutralLanguage />
<PackageReleaseNotes>Fixed recovery codes not being removed after use.</PackageReleaseNotes>
<PackageReleaseNotes>Removed obsolete properties.</PackageReleaseNotes>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
Expand Down
37 changes: 24 additions & 13 deletions src/AspNetCore.Identity.Mongo/Migrations/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace AspNetCore.Identity.Mongo.Migrations
internal static class Migrator
{
//Starting from 4 in case we want to implement migrations for previous versions
public static int CurrentVersion = 5;
public static int CurrentVersion = 6;

public static void Apply<TUser, TRole, TKey>(IMongoCollection<MigrationHistory> migrationCollection, IMongoCollection<TUser> usersCollection, IMongoCollection<TRole> rolesCollection)
where TKey : IEquatable<TKey>
Expand All @@ -30,20 +30,31 @@ internal static class Migrator
}

// 4 -> 5
var users = usersCollection.Find(x => !string.IsNullOrEmpty(x.AuthenticatorKey)).ToList();
foreach (var user in users)
if (lastHistory.DatabaseVersion == 4)
{
var tokens = user.Tokens;
tokens.Add(new Microsoft.AspNetCore.Identity.IdentityUserToken<string>()
var users = usersCollection.Find(x => !string.IsNullOrEmpty(x.AuthenticatorKey)).ToList();
foreach (var user in users)
{
UserId = user.Id.ToString(),
Value = user.AuthenticatorKey,
LoginProvider = "[AspNetUserStore]",
Name = "AuthenticatorKey"
});
usersCollection.UpdateOne(x => x.Id.Equals(user.Id),
Builders<TUser>.Update.Set(x => x.Tokens, tokens)
.Set(x => x.AuthenticatorKey, null));
var tokens = user.Tokens;
tokens.Add(new Microsoft.AspNetCore.Identity.IdentityUserToken<string>()
{
UserId = user.Id.ToString(),
Value = user.AuthenticatorKey,
LoginProvider = "[AspNetUserStore]",
Name = "AuthenticatorKey"
});
usersCollection.UpdateOne(x => x.Id.Equals(user.Id),
Builders<TUser>.Update.Set(x => x.Tokens, tokens)
.Set(x => x.AuthenticatorKey, null));
}
}

// 5 -> 6
if (lastHistory.DatabaseVersion == 5)
{
usersCollection.UpdateMany(x => true,
Builders<TUser>.Update.Unset(x => x.AuthenticatorKey)
.Unset(x => x.RecoveryCodes));
}
}

Expand Down

0 comments on commit c685656

Please sign in to comment.