Skip to content

Commit

Permalink
Merge pull request #101 from matteofabbri/MongoStoreExtension
Browse files Browse the repository at this point in the history
AddMongoDbStores extension to register only stores.
  • Loading branch information
vova3211 authored Jun 22, 2021
2 parents 73e2921 + 79e9ad8 commit 2e839fb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/AspNetCore.Identity.Mongo/Mongo/TypeConverterResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace AspNetCore.Identity.Mongo.Mongo
{
internal static class TypeConverterResolver
{
internal static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
{
Attribute[] attr = new Attribute[1];
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
attr[0] = vConv;
TypeDescriptor.AddAttributes(typeof(T), attr);
}
}
}
10 changes: 1 addition & 9 deletions src/AspNetCore.Identity.Mongo/MongoIdentityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static IdentityBuilder AddIdentityMongoDbProvider<TUser, TRole, TKey>(thi
// register custom ObjectId TypeConverter
if (typeof(TKey) == typeof(ObjectId))
{
RegisterTypeConverter<ObjectId, ObjectIdConverter>();
TypeConverterResolver.RegisterTypeConverter<ObjectId, ObjectIdConverter>();
}

// Identity Services
Expand All @@ -109,13 +109,5 @@ public static IdentityBuilder AddIdentityMongoDbProvider<TUser, TRole, TKey>(thi

return builder;
}

private static void RegisterTypeConverter<T, TC>() where TC : TypeConverter
{
Attribute[] attr = new Attribute[1];
TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
attr[0] = vConv;
TypeDescriptor.AddAttributes(typeof(T), attr);
}
}
}
55 changes: 55 additions & 0 deletions src/AspNetCore.Identity.Mongo/MongoStoreExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using AspNetCore.Identity.Mongo.Migrations;
using AspNetCore.Identity.Mongo.Model;
using AspNetCore.Identity.Mongo.Mongo;
using AspNetCore.Identity.Mongo.Stores;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading.Tasks;

namespace AspNetCore.Identity.Mongo
{
public static class MongoStoreExtensions
{
public static IdentityBuilder AddMongoDbStores<TUser>(this IdentityBuilder builder, Action<MongoIdentityOptions> setupDatabaseAction)
where TUser : MongoUser
{
return AddMongoDbStores<TUser, MongoRole, ObjectId>(builder, setupDatabaseAction);
}

public static IdentityBuilder AddMongoDbStores<TUser, TRole, TKey>(this IdentityBuilder builder, Action<MongoIdentityOptions> setupDatabaseAction)
where TKey : IEquatable<TKey>
where TUser : MongoUser<TKey>
where TRole : MongoRole<TKey>
{
var dbOptions = new MongoIdentityOptions();
setupDatabaseAction(dbOptions);

var migrationCollection = MongoUtil.FromConnectionString<MigrationHistory>(dbOptions, dbOptions.MigrationCollection);

Task.WaitAny(Migrator.Apply(migrationCollection));

var userCollection = MongoUtil.FromConnectionString<TUser>(dbOptions, dbOptions.UsersCollection);
var roleCollection = MongoUtil.FromConnectionString<TRole>(dbOptions, dbOptions.RolesCollection);

builder.Services.AddSingleton(x => userCollection);
builder.Services.AddSingleton(x => roleCollection);

// register custom ObjectId TypeConverter
if (typeof(TKey) == typeof(ObjectId))
{
TypeConverterResolver.RegisterTypeConverter<ObjectId, ObjectIdConverter>();
}

// Identity Services
builder.Services.AddTransient<IRoleStore<TRole>>(x => new RoleStore<TRole, TKey>(roleCollection));
builder.Services.AddTransient<IUserStore<TUser>>(x => new UserStore<TUser, TRole, TKey>(userCollection, new RoleStore<TRole, TKey>(roleCollection), x.GetService<ILookupNormalizer>()));

return builder;
}
}
}

0 comments on commit 2e839fb

Please sign in to comment.