mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-18 17:47:54 +08:00
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:
@@ -15,6 +15,6 @@
|
||||
<Target Name="TeamCity" DependsOnTargets="BuildSolution">
|
||||
<!-- 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 -->
|
||||
<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>
|
||||
</Project>
|
||||
|
@@ -86,6 +86,8 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="DatabaseEnabledTestsBase.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\Services\MembershipServiceTests.cs" />
|
||||
</ItemGroup>
|
||||
@@ -94,6 +96,10 @@
|
||||
<Project>{ABC826D4-2FA1-4F2F-87DE-E6095F653810}</Project>
|
||||
<Name>Orchard.Tests</Name>
|
||||
</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">
|
||||
<Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project>
|
||||
<Name>Orchard.Users</Name>
|
||||
@@ -103,9 +109,6 @@
|
||||
<Name>Orchard</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Roles\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- 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.
|
||||
|
@@ -0,0 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Orchard.Tests.Packages.Roles.Controllers {
|
||||
class AdminControllerTests {
|
||||
}
|
||||
}
|
@@ -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"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -27,7 +27,7 @@ namespace Orchard.Tests.Packages.Users.Controllers {
|
||||
builder.Register<AdminController>();
|
||||
builder.Register<DefaultModelManager>().As<IModelManager>();
|
||||
builder.Register<UserDriver>().As<IModelDriver>();
|
||||
builder.Register(new Moq.Mock<INotifier>().Object);
|
||||
builder.Register(new Mock<INotifier>().Object);
|
||||
}
|
||||
|
||||
protected override IEnumerable<Type> DatabaseTypes {
|
||||
|
@@ -9,7 +9,7 @@ using Orchard.Users.Models;
|
||||
using Orchard.Users.ViewModels;
|
||||
|
||||
namespace Orchard.Users.Controllers {
|
||||
public class AdminController : Controller, ICurrentUser {
|
||||
public class AdminController : Controller {
|
||||
private readonly IModelManager _modelManager;
|
||||
private readonly IRepository<UserRecord> _userRepository;
|
||||
private readonly INotifier _notifier;
|
||||
@@ -23,6 +23,8 @@ namespace Orchard.Users.Controllers {
|
||||
_notifier = notifier;
|
||||
}
|
||||
|
||||
public IUser CurrentUser { get; set; }
|
||||
|
||||
public ActionResult Index() {
|
||||
var model = new UsersIndexViewModel();
|
||||
model.Rows = _userRepository.Fetch(x => x.UserName != null)
|
||||
@@ -65,7 +67,6 @@ namespace Orchard.Users.Controllers {
|
||||
return RedirectToAction("Edit", new { id });
|
||||
}
|
||||
|
||||
public IUser CurrentUser { get; set; }
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -155,7 +155,6 @@
|
||||
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
|
||||
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
|
||||
<Compile Include="Security\IAuthenticationService.cs" />
|
||||
<Compile Include="Security\ICurrentUser.cs" />
|
||||
<Compile Include="Security\Providers\FormsAuthenticationService.cs" />
|
||||
<Compile Include="Security\SecurityFilter.cs" />
|
||||
<Compile Include="Security\SecurityModule.cs" />
|
||||
|
@@ -1,5 +0,0 @@
|
||||
namespace Orchard.Security {
|
||||
public interface ICurrentUser {
|
||||
IUser CurrentUser { get; set; }
|
||||
}
|
||||
}
|
@@ -1,23 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using Autofac.Builder;
|
||||
using Module=Autofac.Builder.Module;
|
||||
|
||||
namespace Orchard.Security {
|
||||
public class SecurityModule : Module {
|
||||
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) {
|
||||
var userContainer = (ICurrentUser)e.Instance;
|
||||
var authenticationService = e.Context.Resolve<IAuthenticationService>();
|
||||
userContainer.CurrentUser = authenticationService.Authenticated();
|
||||
private static PropertyInfo FindUserProperty(Type type) {
|
||||
return type.GetProperty("CurrentUser", typeof (IUser));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user