diff --git a/src/ServiceStack.Authentication.RavenDb/RavenUserAuthRepository.cs b/src/ServiceStack.Authentication.RavenDb/RavenUserAuthRepository.cs index 20e7b5d..62fc798 100644 --- a/src/ServiceStack.Authentication.RavenDb/RavenUserAuthRepository.cs +++ b/src/ServiceStack.Authentication.RavenDb/RavenUserAuthRepository.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; +using System.ComponentModel.Composition.Hosting; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using Raven.Client; +using Raven.Client.Indexes; using ServiceStack.Common; using ServiceStack.ServiceInterface.Auth; using ServiceStack.Text; @@ -16,25 +18,25 @@ public class RavenUserAuthRepository : IUserAuthRepository public Regex ValidUserNameRegEx = new Regex(@"^(?=.{3,15}$)([A-Za-z0-9][._-]?)*$", RegexOptions.Compiled); private readonly IDocumentStore _documentStore; + //Session object is created somewhere else and injected here + private readonly IDocumentSession _session; private static bool _isInitialized = false; - public static void CreateOrUpdateUserAuthIndex(IDocumentStore store) + //Change to allow for Multi- Tenancy cases + //User has to call this method every time he creates a new tenant DB + public static void CreateOrUpdateUserAuthIndex(IDocumentStore store,string databaseName) { // put this index into the ravendb database - new ServiceStack_UserAuth_ByUserNameOrEmail().Execute(store); - new ServiceStack_UserAuth_ByOAuthProvider().Execute(store); + var catalog = new CompositionContainer(new AssemblyCatalog(typeof(ServiceStack_UserAuth_ByUserNameOrEmail).Assembly)); + IndexCreation.CreateIndexes(catalog, store.DatabaseCommands.ForDatabase(databaseName), store.Conventions); _isInitialized = true; } - public RavenUserAuthRepository(IDocumentStore documentStore) + public RavenUserAuthRepository(IDocumentStore documentStore,IDocumentSession session) { _documentStore = documentStore; - - // if the user didn't call this method in their AppHostBase - // Let's call if for them. No worries if this is called a few - // times, we just don't want it running all the time - if (!_isInitialized) - CreateOrUpdateUserAuthIndex(documentStore); + _session = session; + } private void ValidateNewUser(UserAuth newUser, string password) @@ -75,10 +77,10 @@ public UserAuth CreateUserAuth(UserAuth newUser, string password) newUser.CreatedDate = DateTime.UtcNow; newUser.ModifiedDate = newUser.CreatedDate; - using (var session = _documentStore.OpenSession()) + using (_session) { - session.Store(newUser); - session.SaveChanges(); + _session.Store(newUser); + _session.SaveChanges(); } return newUser; @@ -129,10 +131,10 @@ public UserAuth UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string p newUser.CreatedDate = existingUser.CreatedDate; newUser.ModifiedDate = DateTime.UtcNow; - using (var session = _documentStore.OpenSession()) + using (_session) { - session.Store(newUser); - session.SaveChanges(); + _session.Store(newUser); + _session.SaveChanges(); } return newUser; @@ -140,9 +142,9 @@ public UserAuth UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string p public UserAuth GetUserAuthByUserName(string userNameOrEmail) { - using (var session = _documentStore.OpenSession()) + using (_session) { - var userAuth = session.Query() + var userAuth = _session.Query() .Customize(x => x.WaitForNonStaleResultsAsOfNow()) .Search(x => x.Search, userNameOrEmail) .OfType() @@ -209,19 +211,22 @@ public UserAuth GetUserAuth(string userAuthId) { using (var session = _documentStore.OpenSession()) { - return session.Load(userAuthId); + int intAuthId; + return int.TryParse(userAuthId, out intAuthId) + ? session.Load(intAuthId) + : session.Load(userAuthId); } } public void SaveUserAuth(IAuthSession authSession) { - using (var session = _documentStore.OpenSession()) + using (_session) { int idInt = int.Parse(authSession.UserAuthId); var userAuth = !authSession.UserAuthId.IsNullOrEmpty() - ? session.Load(idInt) - : authSession.TranslateTo(); + ? _session.Load(idInt) + : authSession.TranslateTo(); if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty()) userAuth.Id = idInt; @@ -230,30 +235,30 @@ public void SaveUserAuth(IAuthSession authSession) if (userAuth.CreatedDate == default(DateTime)) userAuth.CreatedDate = userAuth.ModifiedDate; - session.Store(userAuth); - session.SaveChanges(); + _session.Store(userAuth); + _session.SaveChanges(); } } public void SaveUserAuth(UserAuth userAuth) { - using (var session = _documentStore.OpenSession()) + using (_session) { userAuth.ModifiedDate = DateTime.UtcNow; if (userAuth.CreatedDate == default(DateTime)) userAuth.CreatedDate = userAuth.ModifiedDate; - session.Store(userAuth); - session.SaveChanges(); + _session.Store(userAuth); + _session.SaveChanges(); } } public List GetUserOAuthProviders(string userAuthId) { - using (var session = _documentStore.OpenSession()) + using (_session) { var id = int.Parse(userAuthId); - return session.Query() + return _session.Query() .Customize(x => x.WaitForNonStaleResultsAsOfNow()) .Where(q => q.UserAuthId == id) .OrderBy(x => x.ModifiedDate) @@ -278,9 +283,9 @@ public UserAuth GetUserAuth(IAuthSession authSession, IOAuthTokens tokens) if (tokens == null || tokens.Provider.IsNullOrEmpty() || tokens.UserId.IsNullOrEmpty()) return null; - using (var session = _documentStore.OpenSession()) + using (_session) { - var oAuthProvider = session + var oAuthProvider = _session .Query() .Customize(x => x.WaitForNonStaleResultsAsOfNow()) .Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId) @@ -289,7 +294,7 @@ public UserAuth GetUserAuth(IAuthSession authSession, IOAuthTokens tokens) if (oAuthProvider != null) { - var userAuth = session.Load(oAuthProvider.UserAuthId); + var userAuth = _session.Load(oAuthProvider.UserAuthId); return userAuth; } return null; @@ -300,9 +305,9 @@ public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens to { var userAuth = GetUserAuth(authSession, tokens) ?? new UserAuth(); - using (var session = _documentStore.OpenSession()) + using (_session) { - var oAuthProvider = session + var oAuthProvider = _session .Query() .Customize(x => x.WaitForNonStaleResultsAsOfNow()) .Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId) @@ -325,8 +330,8 @@ public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens to if (userAuth.CreatedDate == default(DateTime)) userAuth.CreatedDate = userAuth.ModifiedDate; - session.Store(userAuth); - session.SaveChanges(); + _session.Store(userAuth); + _session.SaveChanges(); oAuthProvider.UserAuthId = userAuth.Id; @@ -334,8 +339,8 @@ public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens to oAuthProvider.CreatedDate = userAuth.ModifiedDate; oAuthProvider.ModifiedDate = userAuth.ModifiedDate; - session.Store(oAuthProvider); - session.SaveChanges(); + _session.Store(oAuthProvider); + _session.SaveChanges(); return oAuthProvider.UserAuthId.ToString(CultureInfo.InvariantCulture); }