Changing CurrentUser to be driven by a property only, removes the ICurrentUser interface. Also adding test infrastructure for roles package.

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4039957
This commit is contained in:
loudej
2009-11-12 19:56:13 +00:00
parent df24fa629a
commit 9bc4af3b80
9 changed files with 85 additions and 20 deletions

View File

@@ -15,6 +15,6 @@
<Target Name="TeamCity" DependsOnTargets="BuildSolution"> <Target Name="TeamCity" DependsOnTargets="BuildSolution">
<!-- TODO: This will change once we have a proper build task into the "dist" directory --> <!-- TODO: This will change once we have a proper build task into the "dist" directory -->
<!-- Note: We use x86 because we are using a x86 version of SQLite --> <!-- Note: We use x86 because we are using a x86 version of SQLite -->
<NUnit Platform="x86" NUnitVersion="NUnit-2.5.0" Assemblies="src\Orchard.Tests\bin\Debug\Orchard.Tests.dll;src\Orchard.Web.Tests\bin\Debug\Orchard.Web.Tests.dll;src\Orchard.Web\Packages\Orchard.CmsPages.Tests\bin\debug\Orchard.CmsPages.Tests.dll"/> <NUnit Platform="x86" NUnitVersion="NUnit-2.5.0" Assemblies="src\Orchard.Tests\bin\Debug\Orchard.Tests.dll;src\Orchard.Web.Tests\bin\Debug\Orchard.Web.Tests.dll;src\Orchard.Tests.Packages\bin\Debug\Orchard.Tests.Packages.dll;src\Orchard.Web\Packages\Orchard.CmsPages.Tests\bin\debug\Orchard.CmsPages.Tests.dll"/>
</Target> </Target>
</Project> </Project>

View File

@@ -86,6 +86,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="DatabaseEnabledTestsBase.cs" /> <Compile Include="DatabaseEnabledTestsBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Roles\Controllers\AdminControllerTests.cs" />
<Compile Include="Roles\Services\RoleServiceTests.cs" />
<Compile Include="Users\Controllers\AdminControllerTests.cs" /> <Compile Include="Users\Controllers\AdminControllerTests.cs" />
<Compile Include="Users\Services\MembershipServiceTests.cs" /> <Compile Include="Users\Services\MembershipServiceTests.cs" />
</ItemGroup> </ItemGroup>
@@ -94,6 +96,10 @@
<Project>{ABC826D4-2FA1-4F2F-87DE-E6095F653810}</Project> <Project>{ABC826D4-2FA1-4F2F-87DE-E6095F653810}</Project>
<Name>Orchard.Tests</Name> <Name>Orchard.Tests</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\Orchard.Web\Packages\Orchard.Roles\Orchard.Roles.csproj">
<Project>{D10AD48F-407D-4DB5-A328-173EC7CB010F}</Project>
<Name>Orchard.Roles</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Web\Packages\Orchard.Users\Orchard.Users.csproj"> <ProjectReference Include="..\Orchard.Web\Packages\Orchard.Users\Orchard.Users.csproj">
<Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project> <Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project>
<Name>Orchard.Users</Name> <Name>Orchard.Users</Name>
@@ -103,9 +109,6 @@
<Name>Orchard</Name> <Name>Orchard</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Roles\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.

View File

@@ -0,0 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Orchard.Tests.Packages.Roles.Controllers {
class AdminControllerTests {
}
}

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Autofac.Builder;
using NUnit.Framework;
using Orchard.Roles.Models;
using Orchard.Roles.Services;
namespace Orchard.Tests.Packages.Roles.Services {
[TestFixture]
public class RoleServiceTests : DatabaseEnabledTestsBase{
public override void Register(ContainerBuilder builder) {
builder.Register<RoleService>().As<IRoleService>();
}
protected override IEnumerable<Type> DatabaseTypes {
get {
return new[] { typeof(RoleRecord), typeof(PermissionRecord), typeof(RolesPermissions) };
}
}
[Test]
public void CreateRoleShouldAddToList() {
var service = _container.Resolve<IRoleService>();
service.CreateRole("one");
service.CreateRole("two");
service.CreateRole("three");
ClearSession();
var roles = service.GetRoles();
Assert.That(roles.Count(), Is.EqualTo(3));
Assert.That(roles, Has.Some.Property("Name").EqualTo("one"));
Assert.That(roles, Has.Some.Property("Name").EqualTo("two"));
Assert.That(roles, Has.Some.Property("Name").EqualTo("three"));
}
[Test, Ignore("Permissions should be created first it appears?")]
public void PermissionRecordsShouldBeCreatedOnDemand() {
var service = _container.Resolve<IRoleService>();
service.CreateRole("one");
service.CreatePermissionForRole("one", "foo");
service.CreatePermissionForRole("one", "bar");
ClearSession();
var one = service.GetRoles().Single(x => x.Name == "one");
Assert.That(one.RolesPermissions, Has.Count.EqualTo(2));
Assert.That(one.RolesPermissions.Select(x => x.Permission.Name), Has.Some.EqualTo("foo"));
Assert.That(one.RolesPermissions.Select(x => x.Permission.Name), Has.Some.EqualTo("bar"));
}
}
}

View File

@@ -27,7 +27,7 @@ namespace Orchard.Tests.Packages.Users.Controllers {
builder.Register<AdminController>(); builder.Register<AdminController>();
builder.Register<DefaultModelManager>().As<IModelManager>(); builder.Register<DefaultModelManager>().As<IModelManager>();
builder.Register<UserDriver>().As<IModelDriver>(); builder.Register<UserDriver>().As<IModelDriver>();
builder.Register(new Moq.Mock<INotifier>().Object); builder.Register(new Mock<INotifier>().Object);
} }
protected override IEnumerable<Type> DatabaseTypes { protected override IEnumerable<Type> DatabaseTypes {

View File

@@ -9,7 +9,7 @@ using Orchard.Users.Models;
using Orchard.Users.ViewModels; using Orchard.Users.ViewModels;
namespace Orchard.Users.Controllers { namespace Orchard.Users.Controllers {
public class AdminController : Controller, ICurrentUser { public class AdminController : Controller {
private readonly IModelManager _modelManager; private readonly IModelManager _modelManager;
private readonly IRepository<UserRecord> _userRepository; private readonly IRepository<UserRecord> _userRepository;
private readonly INotifier _notifier; private readonly INotifier _notifier;
@@ -23,6 +23,8 @@ namespace Orchard.Users.Controllers {
_notifier = notifier; _notifier = notifier;
} }
public IUser CurrentUser { get; set; }
public ActionResult Index() { public ActionResult Index() {
var model = new UsersIndexViewModel(); var model = new UsersIndexViewModel();
model.Rows = _userRepository.Fetch(x => x.UserName != null) model.Rows = _userRepository.Fetch(x => x.UserName != null)
@@ -65,7 +67,6 @@ namespace Orchard.Users.Controllers {
return RedirectToAction("Edit", new { id }); return RedirectToAction("Edit", new { id });
} }
public IUser CurrentUser { get; set; }
} }
} }

View File

@@ -155,7 +155,6 @@
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" /> <Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" /> <Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
<Compile Include="Security\IAuthenticationService.cs" /> <Compile Include="Security\IAuthenticationService.cs" />
<Compile Include="Security\ICurrentUser.cs" />
<Compile Include="Security\Providers\FormsAuthenticationService.cs" /> <Compile Include="Security\Providers\FormsAuthenticationService.cs" />
<Compile Include="Security\SecurityFilter.cs" /> <Compile Include="Security\SecurityFilter.cs" />
<Compile Include="Security\SecurityModule.cs" /> <Compile Include="Security\SecurityModule.cs" />

View File

@@ -1,5 +0,0 @@
namespace Orchard.Security {
public interface ICurrentUser {
IUser CurrentUser { get; set; }
}
}

View File

@@ -1,23 +1,29 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using Autofac.Builder; using Module=Autofac.Builder.Module;
namespace Orchard.Security { namespace Orchard.Security {
public class SecurityModule : Module { public class SecurityModule : Module {
protected override void AttachToComponentRegistration(Autofac.IContainer container, Autofac.IComponentRegistration registration) { protected override void AttachToComponentRegistration(Autofac.IContainer container, Autofac.IComponentRegistration registration) {
if (typeof(ICurrentUser).IsAssignableFrom(registration.Descriptor.BestKnownImplementationType)) {
registration.Activated += OnActivated; var userProperty = FindUserProperty(registration.Descriptor.BestKnownImplementationType);
if (userProperty != null) {
registration.Activated += (sender, e) => {
var authenticationService = e.Context.Resolve<IAuthenticationService>();
var currentUser = authenticationService.Authenticated();
userProperty.SetValue(e.Instance, currentUser, null);
};
} }
} }
static void OnActivated(object sender, Autofac.ActivatedEventArgs e) { private static PropertyInfo FindUserProperty(Type type) {
var userContainer = (ICurrentUser)e.Instance; return type.GetProperty("CurrentUser", typeof (IUser));
var authenticationService = e.Context.Resolve<IAuthenticationService>();
userContainer.CurrentUser = authenticationService.Authenticated();
} }
} }
} }