Skip to content

Commit 3582f54

Browse files
added indexes to RavenUserAuthRepository and ncrunch to gitignore.
1 parent 5540f36 commit 3582f54

5 files changed

Lines changed: 178 additions & 85 deletions

File tree

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ _ReSharper*/
3434
App_Data/
3535
*.resharper.user
3636
*.suo
37-
*.user
37+
*.user
38+
39+
# NCrunch
40+
41+
*.ncrunch*
42+
43+
.*crunch*.local.xml

src/ServiceStack.Authentication.Raven/RavenUserAuthRepository.cs

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,26 @@ public class RavenUserAuthRepository : IUserAuthRepository
1515
//http://stackoverflow.com/questions/3588623/c-sharp-regex-for-a-username-with-a-few-restrictions
1616
public Regex ValidUserNameRegEx = new Regex(@"^(?=.{3,15}$)([A-Za-z0-9][._-]?)*$", RegexOptions.Compiled);
1717

18-
private IDocumentStore _documentStore;
18+
private readonly IDocumentStore _documentStore;
19+
private static bool _isInitialized = false;
20+
21+
public static void CreateOrUpdateUserAuthIndex(IDocumentStore store)
22+
{
23+
// put this index into the ravendb database
24+
new ServiceStack_UserAuth_ByUserNameOrEmail().Execute(store);
25+
new ServiceStack_UserAuth_ByOAuthProvider().Execute(store);
26+
_isInitialized = true;
27+
}
1928

2029
public RavenUserAuthRepository(IDocumentStore documentStore)
2130
{
2231
_documentStore = documentStore;
32+
33+
// if the user didn't call this method in their AppHostBase
34+
// Let's call if for them. No worries if this is called a few
35+
// times, we just don't want it running all the time
36+
if (!_isInitialized)
37+
CreateOrUpdateUserAuthIndex(documentStore);
2338
}
2439

2540
private void ValidateNewUser(UserAuth newUser, string password)
@@ -118,16 +133,16 @@ public UserAuth UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string p
118133
}
119134

120135
public UserAuth GetUserAuthByUserName(string userNameOrEmail)
121-
{
122-
using (var session = _documentStore.OpenSession())
123-
{
124-
var isEmail = userNameOrEmail.Contains("@");
125-
var userAuth = isEmail
126-
? session.Query<UserAuth>().FirstOrDefault(q => q.Email == userNameOrEmail)
127-
: session.Query<UserAuth>().FirstOrDefault(q => q.UserName == userNameOrEmail);
128-
129-
return userAuth;
130-
}
136+
{
137+
using (var session = _documentStore.OpenSession())
138+
{
139+
var userAuth = session.Query<ServiceStack_UserAuth_ByUserNameOrEmail.Result, ServiceStack_UserAuth_ByUserNameOrEmail>()
140+
.Search(x => x.Search, userNameOrEmail)
141+
.OfType<UserAuth>()
142+
.FirstOrDefault();
143+
144+
return userAuth;
145+
}
131146
}
132147

133148
public bool TryAuthenticate(string userName, string password, out UserAuth userAuth)
@@ -231,7 +246,11 @@ public List<UserOAuthProvider> GetUserOAuthProviders(string userAuthId)
231246
using (var session = _documentStore.OpenSession())
232247
{
233248
var id = int.Parse(userAuthId);
234-
return session.Query<UserOAuthProvider>().Where(q => q.UserAuthId == id).OrderBy(x => x.ModifiedDate).ToList();
249+
return session.Query<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
250+
.Where(q => q.UserAuthId == id)
251+
.OrderBy(x => x.ModifiedDate)
252+
.OfType<UserOAuthProvider>()
253+
.ToList();
235254
}
236255
}
237256

@@ -251,11 +270,13 @@ public UserAuth GetUserAuth(IAuthSession authSession, IOAuthTokens tokens)
251270
if (tokens == null || tokens.Provider.IsNullOrEmpty() || tokens.UserId.IsNullOrEmpty())
252271
return null;
253272

254-
using (var session = _documentStore.OpenSession())
255-
{
256-
var oAuthProvider = session
257-
.Query<UserOAuthProvider>()
258-
.FirstOrDefault<UserOAuthProvider>(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId);
273+
using (var session = _documentStore.OpenSession())
274+
{
275+
var oAuthProvider = session
276+
.Query<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
277+
.Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId)
278+
.OfType<UserOAuthProvider>()
279+
.FirstOrDefault();
259280

260281
if (oAuthProvider != null)
261282
{
@@ -271,8 +292,12 @@ public string CreateOrMergeAuthSession(IAuthSession authSession, IOAuthTokens to
271292
var userAuth = GetUserAuth(authSession, tokens) ?? new UserAuth();
272293

273294
using (var session = _documentStore.OpenSession())
274-
{
275-
var oAuthProvider = session.Query<UserOAuthProvider>().FirstOrDefault(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId);
295+
{
296+
var oAuthProvider = session
297+
.Query<ServiceStack_UserAuth_ByOAuthProvider.Result, ServiceStack_UserAuth_ByOAuthProvider>()
298+
.Where(q => q.Provider == tokens.Provider && q.UserId == tokens.UserId)
299+
.OfType<UserOAuthProvider>()
300+
.FirstOrDefault();
276301

277302
if (oAuthProvider == null)
278303
{
Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,74 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4-
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{A30BD5B5-00C9-4B9D-9C9D-514B4A676FF3}</ProjectGuid>
8-
<OutputType>Library</OutputType>
9-
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>ServiceStack.Authentication.Raven</RootNamespace>
11-
<AssemblyName>ServiceStack.Authentication.Raven</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13-
<FileAlignment>512</FileAlignment>
14-
</PropertyGroup>
15-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16-
<DebugSymbols>true</DebugSymbols>
17-
<DebugType>full</DebugType>
18-
<Optimize>false</Optimize>
19-
<OutputPath>bin\Debug\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21-
<ErrorReport>prompt</ErrorReport>
22-
<WarningLevel>4</WarningLevel>
23-
</PropertyGroup>
24-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25-
<DebugType>pdbonly</DebugType>
26-
<Optimize>true</Optimize>
27-
<OutputPath>bin\Release\</OutputPath>
28-
<DefineConstants>TRACE</DefineConstants>
29-
<ErrorReport>prompt</ErrorReport>
30-
<WarningLevel>4</WarningLevel>
31-
</PropertyGroup>
32-
<ItemGroup>
33-
<Reference Include="Raven.Abstractions">
34-
<HintPath>..\packages\RavenDB.Client.2.0.2230\lib\net40\Raven.Abstractions.dll</HintPath>
35-
</Reference>
36-
<Reference Include="Raven.Client.Lightweight">
37-
<HintPath>..\packages\RavenDB.Client.2.0.2230\lib\net40\Raven.Client.Lightweight.dll</HintPath>
38-
</Reference>
39-
<Reference Include="ServiceStack.Common">
40-
<HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
41-
</Reference>
42-
<Reference Include="ServiceStack.ServiceInterface">
43-
<HintPath>..\..\lib\ServiceStack.ServiceInterface.dll</HintPath>
44-
</Reference>
45-
<Reference Include="ServiceStack.Text">
46-
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
47-
</Reference>
48-
<Reference Include="System" />
49-
<Reference Include="System.ComponentModel.Composition" />
50-
<Reference Include="System.Core" />
51-
<Reference Include="System.Xml.Linq" />
52-
<Reference Include="System.Data.DataSetExtensions" />
53-
<Reference Include="Microsoft.CSharp" />
54-
<Reference Include="System.Data" />
55-
<Reference Include="System.Xml" />
56-
</ItemGroup>
57-
<ItemGroup>
58-
<Compile Include="RavenUserAuthRepository.cs" />
59-
<Compile Include="Properties\AssemblyInfo.cs" />
60-
</ItemGroup>
61-
<ItemGroup>
62-
<None Include="packages.config" />
63-
</ItemGroup>
64-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A30BD5B5-00C9-4B9D-9C9D-514B4A676FF3}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ServiceStack.Authentication.Raven</RootNamespace>
11+
<AssemblyName>ServiceStack.Authentication.Raven</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="Raven.Abstractions">
34+
<HintPath>..\packages\RavenDB.Client.2.0.2230\lib\net40\Raven.Abstractions.dll</HintPath>
35+
</Reference>
36+
<Reference Include="Raven.Client.Lightweight">
37+
<HintPath>..\packages\RavenDB.Client.2.0.2230\lib\net40\Raven.Client.Lightweight.dll</HintPath>
38+
</Reference>
39+
<Reference Include="ServiceStack.Common">
40+
<HintPath>..\..\lib\ServiceStack.Common.dll</HintPath>
41+
</Reference>
42+
<Reference Include="ServiceStack.ServiceInterface">
43+
<HintPath>..\..\lib\ServiceStack.ServiceInterface.dll</HintPath>
44+
</Reference>
45+
<Reference Include="ServiceStack.Text">
46+
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
47+
</Reference>
48+
<Reference Include="System" />
49+
<Reference Include="System.ComponentModel.Composition" />
50+
<Reference Include="System.Core" />
51+
<Reference Include="System.Xml.Linq" />
52+
<Reference Include="System.Data.DataSetExtensions" />
53+
<Reference Include="Microsoft.CSharp" />
54+
<Reference Include="System.Data" />
55+
<Reference Include="System.Xml" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<Compile Include="ServiceStack_UserAuth_ByOAuthProvider.cs" />
59+
<Compile Include="ServiceStack_UserAuth_ByUserNameOrEmail.cs" />
60+
<Compile Include="RavenUserAuthRepository.cs" />
61+
<Compile Include="Properties\AssemblyInfo.cs" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="packages.config" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6567
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6668
Other similar extension points exist, see Microsoft.Common.targets.
6769
<Target Name="BeforeBuild">
6870
</Target>
6971
<Target Name="AfterBuild">
7072
</Target>
71-
-->
73+
-->
7274
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Linq;
3+
using Raven.Client.Indexes;
4+
using ServiceStack.ServiceInterface.Auth;
5+
6+
namespace ServiceStack.Authentication.Raven
7+
{
8+
public class ServiceStack_UserAuth_ByOAuthProvider : AbstractIndexCreationTask<UserOAuthProvider, ServiceStack_UserAuth_ByOAuthProvider.Result>
9+
{
10+
public class Result
11+
{
12+
public string Provider { get; set; }
13+
public string UserId { get; set; }
14+
public int UserAuthId { get; set; }
15+
public DateTime ModifiedDate { get; set; }
16+
}
17+
18+
public ServiceStack_UserAuth_ByOAuthProvider()
19+
{
20+
Map = oauthProviders => from oauthProvider in oauthProviders
21+
select new Result
22+
{
23+
Provider = oauthProvider.Provider,
24+
UserId = oauthProvider.UserId,
25+
ModifiedDate = oauthProvider.ModifiedDate,
26+
UserAuthId = oauthProvider.UserAuthId
27+
};
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Linq;
2+
using Raven.Abstractions.Indexing;
3+
using Raven.Client.Indexes;
4+
using ServiceStack.ServiceInterface.Auth;
5+
6+
namespace ServiceStack.Authentication.Raven
7+
{
8+
public class ServiceStack_UserAuth_ByUserNameOrEmail : AbstractIndexCreationTask<UserAuth, ServiceStack_UserAuth_ByUserNameOrEmail.Result>
9+
{
10+
public class Result
11+
{
12+
public string UserName { get; set; }
13+
public string Email { get; set; }
14+
public string[] Search { get; set; }
15+
}
16+
17+
public ServiceStack_UserAuth_ByUserNameOrEmail()
18+
{
19+
Map = users => from user in users
20+
select new Result
21+
{
22+
UserName = user.UserName,
23+
Email = user.Email,
24+
Search = new[] { user.UserName, user.Email }
25+
};
26+
27+
Index(x => x.Search, FieldIndexing.Analyzed);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)