diff --git a/UserStore.cs b/UserStore.cs index bd5dded..442a472 100644 --- a/UserStore.cs +++ b/UserStore.cs @@ -23,11 +23,25 @@ public class UserStore : UserStore /// The context. public UserStore(IdentityDbContext context) : base(context.db) { } + /// /// Initializes a new instance of the class. /// /// The database. public UserStore(MongoDatabase db) : base(db) { } + + /// + /// Initializes a new instance of the class. + /// + /// The database. + /// The user collection name + public UserStore(MongoDatabase db, string collectionName) : base(db, collectionName) { } + + /// + /// Initializes a new instance of the class. + /// + /// The user collection + public UserStore(MongoCollection userCollection) : base(userCollection) { } } @@ -51,12 +65,10 @@ public class UserStore : { #region Private Methods & Variables - - /// - /// The database + /// The user collection /// - private readonly MongoDatabase db; + private readonly MongoCollection userCollection; /// /// The _disposed @@ -76,15 +88,25 @@ public class UserStore : /// /// Initializes a new instance of the class. /// - public UserStore(): this(new IdentityDbContext().db) {} + public UserStore() : this(new IdentityDbContext().db) { } /// /// Initializes a new instance of the class. /// /// The context. - public UserStore(MongoDatabase context) + /// The user collection name + public UserStore(MongoDatabase context, string collectionName = collectionName) + { + userCollection = context.GetCollection(collectionName); + } + + /// + /// Initializes a new instance of the class. + /// + /// The user collection + public UserStore(MongoCollection userCollection) { - db = context; + this.userCollection = userCollection; } @@ -164,7 +186,7 @@ public Task CreateAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName).Insert(user); + userCollection.Insert(user); return Task.FromResult(user); } @@ -181,7 +203,7 @@ public Task DeleteAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName).Remove((Query.EQ("_id", ObjectId.Parse(user.Id.ToString())))); + userCollection.Remove((Query.EQ("_id", ObjectId.Parse(user.Id.ToString())))); return Task.FromResult(true); } @@ -193,7 +215,7 @@ public Task DeleteAsync(TUser user) public Task FindByIdAsync(TKey userId) { ThrowIfDisposed(); - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("_id", ObjectId.Parse(userId.ToString())))); + TUser user = userCollection.FindOne((Query.EQ("_id", ObjectId.Parse(userId.ToString())))); return Task.FromResult(user); } @@ -205,7 +227,7 @@ public Task FindByIdAsync(TKey userId) public Task FindByIdAsync(string userId) { ThrowIfDisposed(); - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("_id", ObjectId.Parse(userId)))); + TUser user = userCollection.FindOne((Query.EQ("_id", ObjectId.Parse(userId)))); return Task.FromResult(user); } @@ -217,8 +239,8 @@ public Task FindByIdAsync(string userId) public Task FindByNameAsync(string userName) { ThrowIfDisposed(); - - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("UserName", userName))); + + TUser user = userCollection.FindOne((Query.EQ("UserName", userName))); return Task.FromResult(user); } @@ -228,10 +250,15 @@ public Task FindByNameAsync(string userName) /// The user. /// if set to true [confirmed]. /// Task. - /// + /// user public Task SetEmailConfirmedAsync(TUser user, bool confirmed) { - throw new NotImplementedException(); + this.ThrowIfDisposed(); + if (user == null) + throw new ArgumentNullException("user"); + + user.EmailConfirmed = confirmed; + return Task.FromResult(0); } /// @@ -243,7 +270,7 @@ public Task FindByEmailAsync(string email) { ThrowIfDisposed(); - TUser user = db.GetCollection(collectionName).FindOne((Query.EQ("Email", email))); + TUser user = userCollection.FindOne((Query.EQ("Email", email))); return Task.FromResult(user); } @@ -253,10 +280,15 @@ public Task FindByEmailAsync(string email) /// The user. /// The email. /// Task. - /// + /// user public Task SetEmailAsync(TUser user, string email) { - throw new NotImplementedException(); + ThrowIfDisposed(); + if (user == null) + throw new ArgumentNullException("user"); + + user.Email = email; + return Task.FromResult(0); } /// @@ -272,7 +304,7 @@ public Task GetEmailAsync(TUser user) { throw new ArgumentException("user"); } - return Task.FromResult(user.Email); + return Task.FromResult(user.Email); } /// @@ -280,10 +312,13 @@ public Task GetEmailAsync(TUser user) /// /// The user. /// Task{System.Boolean}. - /// + /// user public Task GetEmailConfirmedAsync(TUser user) { - throw new NotImplementedException(); + this.ThrowIfDisposed(); + if (user == null) + throw new ArgumentNullException("user"); + return Task.FromResult(user.EmailConfirmed); } /// @@ -298,7 +333,7 @@ public Task UpdateAsync(TUser user) if (user == null) throw new ArgumentNullException("user"); - db.GetCollection(collectionName).Update(Query.EQ("_id", ObjectId.Parse(user.Id.ToString())), Update.Replace(user), UpdateFlags.Upsert); + userCollection.Update(Query.EQ("_id", ObjectId.Parse(user.Id.ToString())), Update.Replace(user), UpdateFlags.Upsert); return Task.FromResult(user); } @@ -340,10 +375,7 @@ public Task AddLoginAsync(TUser user, UserLoginInfo login) public Task FindAsync(UserLoginInfo login) { TUser user = null; - user = - db.GetCollection(collectionName) - .FindOne(Query.And(Query.EQ("Logins.LoginProvider", login.LoginProvider), - Query.EQ("Logins.ProviderKey", login.ProviderKey))); + user = userCollection.FindOne(Query.And(Query.EQ("Logins.LoginProvider", login.LoginProvider), Query.EQ("Logins.ProviderKey", login.ProviderKey))); return Task.FromResult(user); } @@ -576,7 +608,7 @@ public Task GetPhoneNumberAsync(TUser user) { throw new ArgumentNullException("user"); } - + return Task.FromResult(user.PhoneNumber); } @@ -649,4 +681,3 @@ public Task GetTwoFactorEnabledAsync(TUser user) } } } - \ No newline at end of file