Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions src/ServiceStack.Authentication.RavenDb/RavenUserAuthRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -129,20 +131,20 @@ 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;
}

public UserAuth GetUserAuthByUserName(string userNameOrEmail)
{
using (var session = _documentStore.OpenSession())
using (_session)
{
var userAuth = session.Query<ServiceStack_UserAuth_ByUserNameOrEmail.Result, ServiceStack_UserAuth_ByUserNameOrEmail>()
var userAuth = _session.Query<ServiceStack_UserAuth_ByUserNameOrEmail.Result, ServiceStack_UserAuth_ByUserNameOrEmail>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.Search(x => x.Search, userNameOrEmail)
.OfType<UserAuth>()
Expand Down Expand Up @@ -209,19 +211,22 @@ public UserAuth GetUserAuth(string userAuthId)
{
using (var session = _documentStore.OpenSession())
{
return session.Load<UserAuth>(userAuthId);
int intAuthId;
return int.TryParse(userAuthId, out intAuthId)
? session.Load<UserAuth>(intAuthId)
: session.Load<UserAuth>(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<UserAuth>(idInt)
: authSession.TranslateTo<UserAuth>();
? _session.Load<UserAuth>(idInt)
: authSession.TranslateTo<UserAuth>();

if (userAuth.Id == default(int) && !authSession.UserAuthId.IsNullOrEmpty())
userAuth.Id = idInt;
Expand All @@ -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<UserOAuthProvider> GetUserOAuthProviders(string userAuthId)
{
using (var session = _documentStore.OpenSession())
using (_session)
{
var id = int.Parse(userAuthId);
return session.Query<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
return _session.Query<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.Where(q => q.UserAuthId == id)
.OrderBy(x => x.ModifiedDate)
Expand All @@ -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<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId)
Expand All @@ -289,7 +294,7 @@ public UserAuth GetUserAuth(IAuthSession authSession, IOAuthTokens tokens)

if (oAuthProvider != null)
{
var userAuth = session.Load<UserAuth>(oAuthProvider.UserAuthId);
var userAuth = _session.Load<UserAuth>(oAuthProvider.UserAuthId);
return userAuth;
}
return null;
Expand All @@ -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<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
.Customize(x => x.WaitForNonStaleResultsAsOfNow())
.Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId)
Expand All @@ -325,17 +330,17 @@ 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;

if (oAuthProvider.CreatedDate == default(DateTime))
oAuthProvider.CreatedDate = userAuth.ModifiedDate;
oAuthProvider.ModifiedDate = userAuth.ModifiedDate;

session.Store(oAuthProvider);
session.SaveChanges();
_session.Store(oAuthProvider);
_session.SaveChanges();

return oAuthProvider.UserAuthId.ToString(CultureInfo.InvariantCulture);
}
Expand Down