--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-09-02 21:29:10 -07:00
74 changed files with 1755 additions and 293 deletions

View File

@@ -99,6 +99,14 @@
XPath="/configuration/system.web/compilation/@debug"
Value="false" />
<XmlUpdate XmlFileName="$(CloudRootFolder)\web.config"
XPath="/configuration/system.web/machineKey/@validationKey"
Value="AutoGenerate" />
<XmlUpdate XmlFileName="$(CloudRootFolder)\web.config"
XPath="/configuration/system.web/machineKey/@decryptionKey"
Value="AutoGenerate" />
<XmlUpdate XmlFileName="$(CloudRootFolder)\Config\Diagnostics.config"
XPath="/system.diagnostics/trace/@autoflush"
Value="false" />

View File

@@ -170,6 +170,14 @@
XPath="/configuration/system.web/compilation/@debug"
Value="false" />
<XmlUpdate XmlFileName="$(StageFolder)\web.config"
XPath="/configuration/system.web/machineKey/@validationKey"
Value="AutoGenerate" />
<XmlUpdate XmlFileName="$(StageFolder)\web.config"
XPath="/configuration/system.web/machineKey/@decryptionKey"
Value="AutoGenerate" />
<XmlUpdate XmlFileName="$(StageFolder)\Config\Diagnostics.config"
XPath="/system.diagnostics/trace/@autoflush"
Value="false" />

View File

@@ -2,7 +2,8 @@
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<machineKey xdt:Transform="Insert" validationKey="013B82F217ABB7EAB1F699E4E5B4D290030644D435994692354DAE82B06568B058BFE3C57BF199A41FFDBC84F3BC74D9C5BD96D1265F36A22D58347B591AC8DD" decryptionKey="04797035C490263D73ED991C84C5DFCD0D0206AD4F12BC3638A38FBEABEBB8C7" validation="SHA1" decryption="AES" />
<machineKey xdt:Transform="Remove" />
<machineKey xdt:Transform="Insert" validationKey="AutoGenerate" decryptionKey="AutoGenerate" validation="SHA1" decryption="AES" />
</system.web>
<system.web.extensions xdt:Transform="Remove" />
</configuration>

View File

@@ -2,7 +2,8 @@
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<machineKey xdt:Transform="Insert" validationKey="013B82F217ABB7EAB1F699E4E5B4D290030644D435994692354DAE82B06568B058BFE3C57BF199A41FFDBC84F3BC74D9C5BD96D1265F36A22D58347B591AC8DD" decryptionKey="04797035C490263D73ED991C84C5DFCD0D0206AD4F12BC3638A38FBEABEBB8C7" validation="SHA1" decryption="AES" />
<machineKey xdt:Transform="Remove" />
<machineKey xdt:Transform="Insert" validationKey="AutoGenerate" decryptionKey="AutoGenerate" validation="SHA1" decryption="AES" />
</system.web>
<system.web.extensions xdt:Transform="Remove" />
</configuration>

View File

@@ -19,6 +19,7 @@
<defaultSettings timeout="00:30:00"/>
</system.transactions>
<system.web>
<machineKey validationKey="013B82F217ABB7EAB1F699E4E5B4D290030644D435994692354DAE82B06568B058BFE3C57BF199A41FFDBC84F3BC74D9C5BD96D1265F36A22D58347B591AC8DD" decryptionKey="04797035C490263D73ED991C84C5DFCD0D0206AD4F12BC3638A38FBEABEBB8C7" validation="SHA1" decryption="AES" />
<httpRuntime requestValidationMode="2.0" />
<!--
Set compilation debug="true" to insert debugging

View File

@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Autofac;
using JetBrains.Annotations;
using Moq;
using NUnit.Framework;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.Records;
using Orchard.Core.Common.Handlers;
using Orchard.Core.Common.Models;
using Orchard.Data;
using Orchard.Environment;
using Orchard.Security;
using Orchard.Tests.Modules;
using Orchard.UI.Notify;
namespace Orchard.Core.Tests.Body {
[TestFixture]
public class BodyPartTests : DatabaseEnabledTestsBase {
public override void Register(ContainerBuilder builder) {
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterInstance(new Mock<IContentDefinitionManager>().Object);
builder.RegisterInstance(new Mock<ITransactionManager>().Object);
builder.RegisterInstance(new Mock<IAuthorizer>().Object);
builder.RegisterInstance(new Mock<INotifier>().Object);
builder.RegisterType<OrchardServices>().As<IOrchardServices>();
builder.RegisterType<ThingHandler>().As<IContentHandler>();
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
builder.RegisterType<BodyPartHandler>().As<IContentHandler>();
}
[Test]
public void BodyCanHandleLongText() {
var contentManager = _container.Resolve<IContentManager>();
contentManager.Create<Thing>(ThingDriver.ContentType.Name, t => {
t.As<BodyPart>().Record = new BodyPartRecord();
t.Text = new String('x', 10000);
});
var bodies = contentManager.Query<BodyPart>().List();
Assert.That(bodies, Is.Not.Null);
Assert.That(bodies.Any(), Is.True);
Assert.That(bodies.First().Text, Is.EqualTo(new String('x', 10000)));
}
protected override IEnumerable<Type> DatabaseTypes {
get {
return new[] {
typeof(BodyPartRecord),
typeof(ContentTypeRecord),
typeof(ContentItemRecord),
typeof(ContentItemVersionRecord),
typeof(CommonPartRecord),
typeof(CommonPartVersionRecord),
};
}
}
[UsedImplicitly]
public class ThingHandler : ContentHandler {
public ThingHandler() {
Filters.Add(new ActivatingFilter<Thing>(ThingDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<ContentPart<CommonPartVersionRecord>>(ThingDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<CommonPart>(ThingDriver.ContentType.Name));
Filters.Add(new ActivatingFilter<BodyPart>(ThingDriver.ContentType.Name));
}
}
public class Thing : ContentPart {
public int Id { get { return ContentItem.Id; } }
public string Text {
get { return this.As<BodyPart>().Text; }
set { this.As<BodyPart>().Text = value; }
}
}
public class ThingDriver : ContentPartDriver<Thing> {
public readonly static ContentType ContentType = new ContentType {
Name = "thing",
DisplayName = "Thing"
};
}
}
}

View File

@@ -101,6 +101,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Providers\CommonPartProviderTests.cs" />
<Compile Include="Body\BodyPartTests.cs" />
<Compile Include="Routable\Services\RoutableServiceTests.cs" />
<Compile Include="Feeds\Controllers\FeedControllerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -10,6 +10,7 @@ using NUnit.Framework;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.MetaData.Services;
using Orchard.Core.Messaging.Services;
using Orchard.Core.Settings.Metadata;
using Orchard.Data;
using Orchard.Environment;
@@ -17,6 +18,8 @@ using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.Records;
using Orchard.Localization;
using Orchard.Messaging.Events;
using Orchard.Messaging.Services;
using Orchard.Security;
using Orchard.Security.Permissions;
using Orchard.UI.Notify;
@@ -41,6 +44,8 @@ namespace Orchard.Tests.Modules.Users.Controllers {
builder.RegisterType<ContentDefinitionManager>().As<IContentDefinitionManager>();
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>().InstancePerDependency();
builder.RegisterType<DefaultMessageManager>().As<IMessageManager>();
builder.RegisterInstance(new Mock<IMessageEventHandler>().Object);
builder.RegisterType<MembershipService>().As<IMembershipService>();
builder.RegisterType<UserService>().As<IUserService>();
builder.RegisterType<UserPartHandler>().As<IContentHandler>();

View File

@@ -2,16 +2,20 @@
using System.Web.Security;
using System.Xml.Linq;
using Autofac;
using Moq;
using NHibernate;
using NUnit.Framework;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.MetaData.Services;
using Orchard.Core.Messaging.Services;
using Orchard.Core.Settings.Metadata;
using Orchard.Data;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.Records;
using Orchard.Messaging.Events;
using Orchard.Messaging.Services;
using Orchard.Security;
using Orchard.Users.Handlers;
using Orchard.Users.Models;
@@ -59,6 +63,7 @@ namespace Orchard.Tests.Modules.Users.Services {
var builder = new ContainerBuilder();
//builder.RegisterModule(new ImplicitCollectionSupportModule());
builder.RegisterType<MembershipService>().As<IMembershipService>();
builder.RegisterType<DefaultContentQuery>().As<IContentQuery>();
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
builder.RegisterType(typeof(SettingsFormatter))
.As(typeof(IMapper<XElement, SettingsDictionary>))
@@ -67,6 +72,8 @@ namespace Orchard.Tests.Modules.Users.Services {
builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
builder.RegisterType<UserPartHandler>().As<IContentHandler>();
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
builder.RegisterInstance(new Mock<IMessageEventHandler>().Object);
builder.RegisterType<DefaultMessageManager>().As<IMessageManager>();
_session = _sessionFactory.OpenSession();
builder.RegisterInstance(new TestSessionLocator(_session)).As<ISessionLocator>();
_container = builder.Build();

View File

@@ -14,43 +14,6 @@ using Orchard.Tests.Records;
namespace Orchard.Tests.Data.Builders {
[TestFixture]
public class SessionFactoryBuilderTests {
private string _tempDataFolder;
[SetUp]
public void Init() {
var tempFilePath = Path.GetTempFileName();
File.Delete(tempFilePath);
Directory.CreateDirectory(tempFilePath);
_tempDataFolder = tempFilePath;
}
[TearDown]
public void Term() {
try { Directory.Delete(_tempDataFolder, true); }
catch (IOException) { }
}
private static void CreateSqlServerDatabase(string databasePath) {
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
using (var connection = new SqlConnection(
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;")) {
connection.Open();
using (var command = connection.CreateCommand()) {
command.CommandText =
"CREATE DATABASE " + databaseName +
" ON PRIMARY (NAME=" + databaseName +
", FILENAME='" + databasePath.Replace("'", "''") + "')";
command.ExecuteNonQuery();
command.CommandText =
"EXEC sp_detach_db '" + databaseName + "', 'true'";
command.ExecuteNonQuery();
}
}
}
[Test]
public void SqlCeSchemaShouldBeGeneratedAndUsable() {
@@ -58,69 +21,8 @@ namespace Orchard.Tests.Data.Builders {
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
};
var parameters = new SessionFactoryParameters {
Provider = "SqlCe",
DataFolder = _tempDataFolder,
RecordDescriptors = recordDescriptors
};
var manager = (IDataServicesProviderFactory) new DataServicesProviderFactory(new[] {
new Meta<CreateDataServicesProvider>(
(dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString),
new Dictionary<string, object> {{"ProviderName", "SqlCe"}})
});
var configuration = manager
.CreateProvider(parameters)
.BuildConfiguration(parameters);
configuration.SetProperty("connection.release_mode", "on_close");
new SchemaExport(configuration).Execute(false, true, false);
var sessionFactory = configuration.BuildSessionFactory();
var session = sessionFactory.OpenSession();
var foo = new FooRecord {Name = "hi there", Id = 1};
session.Save(foo);
session.Flush();
session.Close();
Assert.That(foo, Is.Not.EqualTo(0));
sessionFactory.Close();
}
[Test]
public void SqlServerSchemaShouldBeGeneratedAndUsable() {
var databasePath = Path.Combine(_tempDataFolder, "Orchard.mdf");
CreateSqlServerDatabase(databasePath);
var recordDescriptors = new[] {
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
};
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
new Meta<CreateDataServicesProvider>(
(dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString),
new Dictionary<string, object> {{"ProviderName", "SqlServer"}})
});
var parameters = new SessionFactoryParameters {
Provider = "SqlServer",
DataFolder = _tempDataFolder,
ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFileName=" + databasePath + ";Integrated Security=True;User Instance=True;",
RecordDescriptors = recordDescriptors,
};
var configuration = manager
.CreateProvider(parameters)
.BuildConfiguration(parameters);
new SchemaExport(configuration).Execute(false, true, false);
var sessionFactory = configuration.BuildSessionFactory();
ProviderUtilities.RunWithSqlCe(recordDescriptors,
sessionFactory => {
var session = sessionFactory.OpenSession();
var foo = new FooRecord { Name = "hi there" };
session.Save(foo);
@@ -129,7 +31,27 @@ namespace Orchard.Tests.Data.Builders {
Assert.That(foo, Is.Not.EqualTo(0));
sessionFactory.Close();
});
}
[Test]
public void SqlServerSchemaShouldBeGeneratedAndUsable() {
var recordDescriptors = new[] {
new RecordBlueprint {TableName = "Hello", Type = typeof (FooRecord)}
};
ProviderUtilities.RunWithSqlServer(recordDescriptors,
sessionFactory => {
var session = sessionFactory.OpenSession();
var foo = new FooRecord { Name = "hi there" };
session.Save(foo);
session.Flush();
session.Close();
Assert.That(foo, Is.Not.EqualTo(0));
});
}
}
}

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using Autofac.Features.Metadata;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using Orchard.Data.Providers;
using Orchard.Environment.ShellBuilders.Models;
namespace Orchard.Tests.Data {
public class ProviderUtilities {
public static void RunWithSqlServer(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
var temporaryPath = Path.GetTempFileName();
if(File.Exists(temporaryPath))
File.Delete(temporaryPath);
Directory.CreateDirectory(temporaryPath);
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
try {
// create database
using ( var connection = new SqlConnection(
"Data Source=.\\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=true;User Instance=True;") ) {
connection.Open();
using ( var command = connection.CreateCommand() ) {
command.CommandText =
"CREATE DATABASE " + databaseName +
" ON PRIMARY (NAME=" + databaseName +
", FILENAME='" + databasePath.Replace("'", "''") + "')";
command.ExecuteNonQuery();
command.CommandText =
"EXEC sp_detach_db '" + databaseName + "', 'true'";
command.ExecuteNonQuery();
}
}
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
new Meta<CreateDataServicesProvider>(
(dataFolder, connectionString) => new SqlServerDataServicesProvider(dataFolder, connectionString),
new Dictionary<string, object> {{"ProviderName", "SqlServer"}})
});
var parameters = new SessionFactoryParameters {
Provider = "SqlServer",
DataFolder = temporaryPath,
ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFileName=" + databasePath + ";Integrated Security=True;User Instance=True;",
RecordDescriptors = recordDescriptors,
};
var configuration = manager
.CreateProvider(parameters)
.BuildConfiguration(parameters);
new SchemaExport(configuration).Execute(false, true, false);
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
action(sessionFactory);
}
}
finally {
try {
Directory.Delete(temporaryPath, true);
}
catch (IOException) {}
}
}
public static void RunWithSqlCe(IEnumerable<RecordBlueprint> recordDescriptors, Action<ISessionFactory> action) {
var temporaryPath = Path.GetTempFileName();
if ( File.Exists(temporaryPath) )
File.Delete(temporaryPath);
Directory.CreateDirectory(temporaryPath);
var databasePath = Path.Combine(temporaryPath, "Orchard.mdf");
var databaseName = Path.GetFileNameWithoutExtension(databasePath);
var parameters = new SessionFactoryParameters {
Provider = "SqlCe",
DataFolder = temporaryPath,
RecordDescriptors = recordDescriptors
};
try {
var manager = (IDataServicesProviderFactory)new DataServicesProviderFactory(new[] {
new Meta<CreateDataServicesProvider>(
(dataFolder, connectionString) => new SqlCeDataServicesProvider(dataFolder, connectionString),
new Dictionary<string, object> {{"ProviderName", "SqlCe"}})
});
var configuration = manager
.CreateProvider(parameters)
.BuildConfiguration(parameters);
configuration.SetProperty("connection.release_mode", "on_close");
new SchemaExport(configuration).Execute(false, true, false);
using ( var sessionFactory = configuration.BuildSessionFactory() ) {
action(sessionFactory);
}
}
finally {
try {
Directory.Delete(temporaryPath, true);
}
catch (IOException) {}
}
}
}
}

View File

@@ -0,0 +1,60 @@
using System;
using NUnit.Framework;
using Orchard.Environment.ShellBuilders.Models;
using Orchard.Tests.Records;
namespace Orchard.Tests.Data {
[TestFixture]
public class ProvidersTests {
[Test]
public void SqlCeShouldHandleBigFields() {
var recordDescriptors = new[] {
new RecordBlueprint {TableName = "Big", Type = typeof (BigRecord)}
};
ProviderUtilities.RunWithSqlCe(recordDescriptors,
sessionFactory => {
var session = sessionFactory.OpenSession();
var foo = new BigRecord { Body = new String('x', 10000) };
session.Save(foo);
session.Flush();
session.Close();
session = sessionFactory.OpenSession();
foo = session.Get<BigRecord>(foo.Id);
session.Close();
Assert.That(foo, Is.Not.Null);
Assert.That(foo.Body, Is.EqualTo(new String('x', 10000)));
});
}
[Test]
public void SqlServerShouldHandleBigFields() {
var recordDescriptors = new[] {
new RecordBlueprint {TableName = "Big", Type = typeof (BigRecord)}
};
ProviderUtilities.RunWithSqlServer(recordDescriptors,
sessionFactory => {
var session = sessionFactory.OpenSession();
var foo = new BigRecord { Body = new String('x', 10000) };
session.Save(foo);
session.Flush();
session.Close();
session = sessionFactory.OpenSession();
foo = session.Get<BigRecord>(foo.Id);
session.Close();
Assert.That(foo, Is.Not.Null);
Assert.That(foo.Body, Is.EqualTo(new String('x', 10000)));
});
}
}
}

View File

@@ -7,7 +7,6 @@ using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using Autofac;
using Autofac.Features.Metadata;
using NHibernate;
@@ -16,10 +15,8 @@ using Orchard.Caching;
using Orchard.ContentManagement.Records;
using Orchard.Data;
using Orchard.Data.Conventions;
using Orchard.Data.Migration;
using Orchard.Data.Migration.Generator;
using Orchard.Data.Migration.Interpreters;
using Orchard.Data.Migration.Records;
using Orchard.Data.Migration.Schema;
using Orchard.DevTools.Services;
using Orchard.Environment.Configuration;

View File

@@ -190,7 +190,9 @@
<Compile Include="DataMigration\Utilities\NullInterpreter.cs" />
<Compile Include="DataUtility.cs" />
<Compile Include="Data\Builders\SessionFactoryBuilderTests.cs" />
<Compile Include="Data\ProviderUtilities.cs" />
<Compile Include="Data\RepositoryTests.cs" />
<Compile Include="Data\ProvidersTests.cs" />
<Compile Include="Data\StubLocator.cs" />
<Compile Include="DisplayManagement\ArgsUtility.cs" />
<Compile Include="DisplayManagement\DefaultDisplayManagerTests.cs" />
@@ -222,6 +224,7 @@
<Compile Include="Mvc\Html\HtmlHelperExtensionsTests.cs" />
<Compile Include="Mvc\Routes\ShellRouteTests.cs" />
<Compile Include="Mvc\Routes\UrlPrefixTests.cs" />
<Compile Include="Records\BigRecord.cs" />
<Compile Include="Stubs\StubReportsCoordinator.cs" />
<Compile Include="Stubs\StubVirtualPathProvider.cs" />
<Compile Include="Stubs\StubFileSystem.cs" />

View File

@@ -0,0 +1,9 @@
using Orchard.Data.Conventions;
namespace Orchard.Tests.Records {
public class BigRecord {
public virtual int Id { get; set; }
[StringLengthMax]
public virtual string Body { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using Orchard.Data.Migration;
namespace Orchard.Core.Messaging.DataMigrations {
public class MessagingDataMigration : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable("MessageSettingsPartRecord", table => table
.ContentPartRecord()
.Column<string>("DefaultChannelService")
);
return 1;
}
}
}

View File

@@ -0,0 +1,48 @@
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Core.ContentsLocation.Models;
using Orchard.Core.Messaging.Models;
using Orchard.Core.Messaging.Services;
using Orchard.Core.Messaging.ViewModels;
using Orchard.Localization;
using Orchard.Messaging.Services;
namespace Orchard.Core.Messaging.Drivers {
[UsedImplicitly]
public class ContentSubscriptionPartDriver : ContentPartDriver<MessageSettingsPart> {
private readonly IMessageManager _messageQueueManager;
public IOrchardServices Services { get; set; }
public ContentSubscriptionPartDriver(IOrchardServices services, IMessageManager messageQueueManager) {
_messageQueueManager = messageQueueManager;
Services = services;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override string Prefix { get { return "MessageSettings"; } }
protected override DriverResult Editor(MessageSettingsPart part) {
var model = new ContentSubscriptionPartViewModel {
ChannelServices = _messageQueueManager.GetAvailableChannelServices(),
MessageSettings = part
};
return ContentPartTemplate(model, "Parts/Messaging.MessageSettings");
}
protected override DriverResult Editor(MessageSettingsPart part, IUpdateModel updater) {
var model = new ContentSubscriptionPartViewModel {
MessageSettings = part
};
if (updater.TryUpdateModel(model, Prefix, null, null)) {
}
return ContentPartTemplate(model, "Parts/Messaging.MessageSettings");
}
}
}

View File

@@ -0,0 +1,14 @@
using JetBrains.Annotations;
using Orchard.Core.Messaging.Models;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
namespace Orchard.Core.Messaging.Handlers {
[UsedImplicitly]
public class SmtpSettingsPartHandler : ContentHandler {
public SmtpSettingsPartHandler(IRepository<MessageSettingsPartRecord> repository) {
Filters.Add(new ActivatingFilter<MessageSettingsPart>("Site"));
Filters.Add(StorageFilter.For(repository));
}
}
}

View File

@@ -0,0 +1,11 @@
using Orchard.ContentManagement;
namespace Orchard.Core.Messaging.Models {
public class MessageSettingsPart : ContentPart<MessageSettingsPartRecord> {
public string DefaultChannelService {
get { return Record.DefaultChannelService; }
set { Record.DefaultChannelService = value; }
}
}
}

View File

@@ -0,0 +1,11 @@
using Orchard.ContentManagement.Records;
namespace Orchard.Core.Messaging.Models {
public class MessageSettingsPartRecord : ContentPartRecord {
/// <summary>
/// Default service used for messages
/// </summary>
public virtual string DefaultChannelService { get; set; }
}
}

View File

@@ -0,0 +1,12 @@
Name: Messaging
antiforgery: enabled
author: The Orchard Team
website: http://orchardproject.net
version: 0.1.0
orchardversion: 0.6.0
description: The Messaging module adds messaging functionalities.
features:
Messaging:
Description: Messaging services.
Category: Messaging
Dependencies: Settings

View File

@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Core.Messaging.Models;
using Orchard.Logging;
using Orchard.Messaging.Events;
using Orchard.Messaging.Models;
using Orchard.Messaging.Services;
using Orchard.Settings;
using Orchard.ContentManagement.Records;
namespace Orchard.Core.Messaging.Services {
public class DefaultMessageManager : IMessageManager {
private readonly IMessageEventHandler _messageEventHandler;
private readonly IEnumerable<IMessagingChannel> _channels;
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ILogger Logger { get; set; }
public DefaultMessageManager(
IMessageEventHandler messageEventHandler,
IEnumerable<IMessagingChannel> channels) {
_messageEventHandler = messageEventHandler;
_channels = channels;
}
public void Send(ContentItemRecord recipient, string type, string service = null, Dictionary<string, string> properties = null) {
if ( !HasChannels() )
return;
var messageSettings = CurrentSite.As<MessageSettingsPart>().Record;
if ( messageSettings == null || String.IsNullOrWhiteSpace(messageSettings.DefaultChannelService) ) {
return;
}
Logger.Information("Sending message {0}", type);
try {
// if the service is not explicit, use the default one, as per settings configuration
if (String.IsNullOrWhiteSpace(service)) {
service = messageSettings.DefaultChannelService;
}
var context = new MessageContext {
Recipient = recipient,
Type = type,
Service = service
};
if ( properties != null ) {
foreach (var key in properties.Keys)
context.Properties.Add(key, properties[key]);
}
_messageEventHandler.Sending(context);
foreach ( var channel in _channels ) {
channel.SendMessage(context);
}
_messageEventHandler.Sent(context);
Logger.Information("Message {0} sent", type);
}
catch ( Exception e ) {
Logger.Error(e, "An error occured while sending the message {0}", type);
}
}
public bool HasChannels() {
return _channels.Any();
}
public IEnumerable<string> GetAvailableChannelServices() {
return _channels.SelectMany(c => c.GetAvailableServices());
}
}
}

View File

@@ -0,0 +1,9 @@
using Orchard.Core.Messaging.Models;
using System.Collections.Generic;
namespace Orchard.Core.Messaging.ViewModels {
public class ContentSubscriptionPartViewModel {
public MessageSettingsPart MessageSettings { get; set; }
public IEnumerable<string> ChannelServices { get; set; }
}
}

View File

@@ -0,0 +1,20 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<ContentSubscriptionPartViewModel>" %>
<%@ Import Namespace="Orchard.Core.Messaging.Models"%>
<%@ Import Namespace="Orchard.Core.Messaging.ViewModels"%>
<fieldset>
<legend><%: T("Messaging")%></legend>
<div>
<label for="<%: Html.FieldIdFor(m => m.MessageSettings.DefaultChannelService)%>"><%: T("Default channel service for messages")%></label>
<% if ( Model.ChannelServices.Any() ) { %>
<select id="<%:Html.FieldIdFor(m => m.MessageSettings.DefaultChannelService) %>" name="<%:Html.FieldNameFor(m => m.MessageSettings.DefaultChannelService) %>">
<% foreach ( var service in Model.ChannelServices ) {%>
<option <%: Model.MessageSettings.DefaultChannelService == service ? "selected=\"selected\"" : "" %> value="<%: service %>"><%: service%></option>
<% }
}
else {%>
<span class="hint"><%: T("You must enable a messaging channel (e.g., Orchard.Email) before being able to send messages.") %></span>
<% }%>
</select>
</div>
</fieldset>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>

View File

@@ -90,6 +90,13 @@
<Compile Include="Contents\Settings\ContentPartSettings.cs" />
<Compile Include="Contents\ViewModels\PublishContentViewModel.cs" />
<Compile Include="Localization\ViewModels\EditLocalizationViewModel.cs" />
<Compile Include="Messaging\DataMigrations\MessagingDataMigration.cs" />
<Compile Include="Messaging\Drivers\MessageSettingsPartDriver.cs" />
<Compile Include="Messaging\Handlers\MessageSettingsPartHandler.cs" />
<Compile Include="Messaging\Models\MessageSettingsPart.cs" />
<Compile Include="Messaging\Models\MessageSettingsPartRecord.cs" />
<Compile Include="Messaging\Services\DefaultMessageManager.cs" />
<Compile Include="Messaging\ViewModels\MessageSettingsPartViewModel.cs" />
<Compile Include="PublishLater\DataMigrations\PublishLaterDataMigration.cs" />
<Compile Include="PublishLater\Drivers\PublishLaterPartDriver.cs" />
<Compile Include="PublishLater\Models\PublishLaterPart.cs" />
@@ -246,6 +253,8 @@
<Content Include="Contents\Views\DisplayTemplates\Parts\Contents.Publish.SummaryAdmin.ascx" />
<Content Include="Contents\Views\DisplayTemplates\Parts\Contents.Publish.ascx" />
<Content Include="Localization\Views\EditorTemplates\Parts\Localization.Translation.ascx" />
<Content Include="Messaging\Module.txt" />
<Content Include="Messaging\Views\EditorTemplates\Parts\Messaging.MessageSettings.ascx" />
<Content Include="PublishLater\Content\Admin\images\draft.gif" />
<Content Include="PublishLater\Content\Admin\images\offline.gif" />
<Content Include="PublishLater\Content\Admin\images\online.gif" />
@@ -364,6 +373,7 @@
<Content Include="Reports\Views\Web.config" />
<Content Include="PublishLater\Views\Web.config" />
<Content Include="ContentsLocation\Views\Web.config" />
<Content Include="Messaging\Views\Web.config" />
<None Include="Contents\Views\Content.cshtml" />
<None Include="Contents\Views\Item\Display.cshtml" />
</ItemGroup>

View File

@@ -0,0 +1,22 @@
using Orchard.Data.Migration;
namespace Orchard.Email.DataMigrations {
public class EmailDataMigration : DataMigrationImpl {
public int Create() {
SchemaBuilder.CreateTable("SmtpSettingsPartRecord", table => table
.ContentPartRecord()
.Column<string>("Address")
.Column<string>("Host")
.Column<int>("Port")
.Column<bool>("EnableSsl")
.Column<bool>("RequireCredentials")
.Column<string>("UserName")
.Column<string>("Password")
);
return 1;
}
}
}

View File

@@ -0,0 +1,29 @@
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Email.Models;
using Orchard.Localization;
namespace Orchard.Email.Drivers {
// We define a specific driver instead of using a TemplateFilterForRecord, because we need the ;odel to be the part and not the record.
// Thus the encryption/decryption will be done when accessing the part's property
public class SmtpSettingsPartDriver : ContentPartDriver<SmtpSettingsPart> {
public SmtpSettingsPartDriver() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override string Prefix { get { return "SmtpSettings"; } }
protected override DriverResult Editor(SmtpSettingsPart termPart) {
return ContentPartTemplate(termPart, "Parts/Smtp.SiteSettings");
}
protected override DriverResult Editor(SmtpSettingsPart termPart, IUpdateModel updater) {
updater.TryUpdateModel(termPart, Prefix, null, null);
return Editor(termPart);
}
}
}

View File

@@ -0,0 +1,14 @@
using JetBrains.Annotations;
using Orchard.Email.Models;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
namespace Orchard.Email.Handlers {
[UsedImplicitly]
public class SmtpSettingsPartHandler : ContentHandler {
public SmtpSettingsPartHandler(IRepository<SmtpSettingsPartRecord> repository) {
Filters.Add(new ActivatingFilter<SmtpSettingsPart>("Site"));
Filters.Add(StorageFilter.For(repository));
}
}
}

View File

@@ -0,0 +1,49 @@
using System.Text;
using System.Web.Security;
using Orchard.ContentManagement;
using System;
namespace Orchard.Email.Models {
public class SmtpSettingsPart : ContentPart<SmtpSettingsPartRecord> {
public bool IsValid() {
return !String.IsNullOrWhiteSpace(Record.Host)
&& Record.Port > 0
&& !String.IsNullOrWhiteSpace(Record.Address);
}
public string Address {
get { return Record.Address; }
set { Record.Address = value; }
}
public string Host {
get { return Record.Host; }
set { Record.Host = value; }
}
public int Port {
get { return Record.Port; }
set { Record.Port = value; }
}
public bool EnableSsl {
get { return Record.EnableSsl; }
set { Record.EnableSsl = value; }
}
public bool RequireCredentials {
get { return Record.RequireCredentials; }
set { Record.RequireCredentials = value; }
}
public string UserName {
get { return Record.UserName; }
set { Record.UserName = value; }
}
public string Password {
get { return String.IsNullOrWhiteSpace(Record.Password) ? String.Empty : Encoding.UTF8.GetString(MachineKey.Decode(Record.Password, MachineKeyProtection.All)); ; }
set { Record.Password = String.IsNullOrWhiteSpace(value) ? String.Empty : MachineKey.Encode(Encoding.UTF8.GetBytes(value), MachineKeyProtection.All); }
}
}
}

View File

@@ -0,0 +1,48 @@
using System.Net.Mail;
using Orchard.ContentManagement.Records;
using System.ComponentModel.DataAnnotations;
namespace Orchard.Email.Models {
public class SmtpSettingsPartRecord : ContentPartRecord {
/// <summary>
/// From address in the mail message
/// </summary>
public virtual string Address { get; set; }
/// <summary>
/// Server name hosting the SMTP service
/// </summary>
public virtual string Host { get; set; }
/// <summary>
/// Port number on which SMTP service runs
/// </summary>
public virtual int Port { get; set; }
/// <summary>
/// Whether to enable SSL communications with the server
/// </summary>
public virtual bool EnableSsl { get; set; }
/// <summary>
/// Whether specific credentials should be used
/// </summary>
public virtual bool RequireCredentials { get; set; }
/// <summary>
/// The username to connect to the SMTP server if DefaultCredentials is False
/// </summary>
public virtual string UserName { get; set; }
/// <summary>
/// The password to connect to the SMTP server if DefaultCredentials is False
/// </summary>
public virtual string Password { get; set; }
public SmtpSettingsPartRecord() {
Port = 25;
RequireCredentials = false;
EnableSsl = false;
}
}
}

View File

@@ -0,0 +1,12 @@
Name: Email Messaging
antiforgery: enabled
author: The Orchard Team
website: http://orchardproject.net
version: 0.1.0
orchardversion: 0.6.0
description: The Email Messaging module adds Email sending functionalities.
features:
Orchard.Email:
Description: Email Messaging services.
Category: Messaging
Dependencies: Messaging

View File

@@ -0,0 +1,142 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{05660F47-D649-48BD-9DED-DF4E01E7CFF9}</ProjectGuid>
<ProjectTypeGuids>{F85E285D-A4E0-4152-9332-AB1D724D3325};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Orchard.Email</RootNamespace>
<AssemblyName>Orchard.Email</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation />
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.ComponentModel.DataAnnotations">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions" />
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Services" />
<Reference Include="System.EnterpriseServices" />
<Reference Include="System.Web.Mobile" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataMigrations\EmailDataMigration.cs" />
<Compile Include="Drivers\SmtpSettingsPartDriver.cs" />
<Compile Include="Handlers\SmtpSettingsPartHandler.cs" />
<Compile Include="Models\SmtpSettingsPart.cs" />
<Compile Include="Models\SmtpSettingsPartRecord.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\EmailMessageEventHandler.cs" />
<Compile Include="Services\EmailMessagingChannel.cs" />
<Compile Include="Services\MissingSettingsBanner.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Module.txt" />
<Content Include="Views\EditorTemplates\Parts\Smtp.SiteSettings.ascx" />
<Content Include="Web.config" />
<Content Include="Views\Web.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<ProjectReference Include="..\..\..\Orchard\Orchard.Framework.csproj">
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
<Name>Orchard.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\..\Core\Orchard.Core.csproj">
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Users\Orchard.Users.csproj">
<Project>{79AED36E-ABD0-4747-93D3-8722B042454B}</Project>
<Name>Orchard.Users</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.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.
<Target Name="BeforeBuild">
</Target> -->
<!-- To enable MVC area subproject support, uncomment the following two lines:
<UsingTask TaskName="Microsoft.Web.Mvc.Build.CreateAreaManifest" AssemblyName="Microsoft.Web.Mvc.Build, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<UsingTask TaskName="Microsoft.Web.Mvc.Build.CopyAreaManifests" AssemblyName="Microsoft.Web.Mvc.Build, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
-->
<Target Name="AfterBuild" DependsOnTargets="AfterBuildCompiler">
<PropertyGroup>
<AreasManifestDir>$(ProjectDir)\..\Manifests</AreasManifestDir>
</PropertyGroup>
<!-- If this is an area child project, uncomment the following line:
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Child" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
-->
<!-- If this is an area parent project, uncomment the following lines:
<CreateAreaManifest AreaName="$(AssemblyName)" AreaType="Parent" AreaPath="$(ProjectDir)" ManifestPath="$(AreasManifestDir)" ContentFiles="@(Content)" />
<CopyAreaManifests ManifestPath="$(AreasManifestDir)" CrossCopy="false" RenameViews="true" />
-->
</Target>
<Target Name="AfterBuildCompiler" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>45979</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>True</UseCustomServer>
<CustomServerUrl>http://orchard.codeplex.com</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Orchard.Messaging.Email")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Orchard")]
[assembly: AssemblyCopyright("Copyright © CodePlex Foundation 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("9c778ece-c759-47fb-95b6-e73c03d9e969")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.5.0")]
[assembly: AssemblyFileVersion("0.5.0")]

View File

@@ -0,0 +1,29 @@
using Orchard.Messaging.Events;
using Orchard.ContentManagement;
using Orchard.Users.Models;
using Orchard.Messaging.Models;
namespace Orchard.Email.Services {
public class EmailMessageEventHandler : IMessageEventHandler {
private readonly IContentManager _contentManager;
public EmailMessageEventHandler(IContentManager contentManager) {
_contentManager = contentManager;
}
public void Sending(MessageContext context) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if ( contentItem == null )
return;
var recipient = contentItem.As<UserPart>();
if ( recipient == null )
return;
context.MailMessage.To.Add(recipient.Email);
}
public void Sent(MessageContext context) {
}
}
}

View File

@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Email.Models;
using Orchard.Settings;
using Orchard.Messaging.Services;
using Orchard.Messaging.Models;
namespace Orchard.Email.Services {
public class EmailMessagingChannel : IMessagingChannel {
public const string EmailService = "Email";
public EmailMessagingChannel() {
Logger = NullLogger.Instance;
}
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ILogger Logger { get; set; }
public Localizer T { get; set; }
public void SendMessage(MessageContext context) {
if ( !context.Service.Equals(EmailService, StringComparison.InvariantCultureIgnoreCase) )
return;
var smtpSettings = CurrentSite.As<SmtpSettingsPart>();
// can't process emails if the Smtp settings have not yet been set
if ( smtpSettings == null || !smtpSettings.IsValid() ) {
return;
}
var smtpClient = new SmtpClient { UseDefaultCredentials = !smtpSettings.RequireCredentials };
if ( !smtpClient.UseDefaultCredentials && !String.IsNullOrWhiteSpace(smtpSettings.UserName) ) {
smtpClient.Credentials = new NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
}
if(context.MailMessage.To.Count == 0) {
Logger.Error("Recipient is missing an email address");
return;
}
if ( smtpSettings.Host != null )
smtpClient.Host = smtpSettings.Host;
smtpClient.Port = smtpSettings.Port;
smtpClient.EnableSsl = smtpSettings.EnableSsl;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
context.MailMessage.From = new MailAddress(smtpSettings.Address);
context.MailMessage.IsBodyHtml = context.MailMessage.Body != null && context.MailMessage.Body.Contains("<") && context.MailMessage.Body.Contains(">");
try {
smtpClient.Send(context.MailMessage);
Logger.Debug("Message sent to {0}: {1}", context.MailMessage.To[0].Address, context.Type);
}
catch(Exception e) {
Logger.Error(e, "An unexpected error while sending a message to {0}: {1}", context.MailMessage.To[0].Address, context.Type);
}
}
public IEnumerable<string> GetAvailableServices() {
return new[] {EmailService};
}
}
}

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Core.Messaging.Models;
using Orchard.Localization;
using Orchard.Email.Models;
using Orchard.Settings;
using Orchard.UI.Admin.Notification;
using Orchard.UI.Notify;
namespace Orchard.Email.Services {
public class MissingSettingsBanner: INotificationProvider {
public MissingSettingsBanner() {
T = NullLocalizer.Instance;
}
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public Localizer T { get; set; }
public IEnumerable<NotifyEntry> GetNotifications() {
var smtpSettings = CurrentSite.As<SmtpSettingsPart>();
if ( smtpSettings == null || !smtpSettings.IsValid() ) {
yield return new NotifyEntry { Message = T("The SMTP settings needs to be configured." ), Type = NotifyType.Warning};
}
var messageSettings = CurrentSite.As<MessageSettingsPart>().Record;
if ( messageSettings == null || String.IsNullOrWhiteSpace(messageSettings.DefaultChannelService) ) {
yield return new NotifyEntry { Message = T("The default channel service needs to be configured."), Type = NotifyType.Warning };
}
}
}
}

View File

@@ -0,0 +1,43 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<SmtpSettingsPart>" %>
<%@ Import Namespace="Orchard.Email.Models"%>
<%@ Import Namespace="System.Net.Mail" %>
<fieldset>
<legend><%: T("SMTP")%></legend>
<div>
<label for="<%: Html.FieldIdFor(m => m.Address)%>"><%: T("Sender email address")%></label>
<%: Html.EditorFor(m => m.Address)%>
<%: Html.ValidationMessage("Address", "*")%>
</div>
<div>
<label for="<%: Html.FieldIdFor(m => m.Host)%>"><%: T("Host name")%></label>
<%: Html.EditorFor(m => m.Host)%>
<%: Html.ValidationMessage("Host", "*")%>
</div>
<div>
<label for="<%: Html.FieldIdFor(m => m.Port)%>"><%: T("Port number")%></label>
<%: Html.EditorFor(m => m.Port)%>
<%: Html.ValidationMessage("Port", "*")%>
</div>
<div>
<%: Html.EditorFor(m => m.EnableSsl)%>
<label for="<%: Html.FieldIdFor(m => m.EnableSsl)%>" class="forcheckbox"><%: T("Enable SSL communications")%></label>
<%: Html.ValidationMessage("EnableSsl", "*")%>
</div>
<div>
<%: Html.EditorFor(m => m.RequireCredentials)%>
<label for="<%: Html.FieldIdFor(m => m.RequireCredentials)%>" class="forcheckbox"><%: T("Require credentials")%></label>
<%: Html.ValidationMessage("RequireCredentials", "*")%>
</div>
<div data-controllerid="<%: Html.FieldIdFor(m => m.RequireCredentials)%>">
<div>
<label for="<%: Html.FieldIdFor(m => m.UserName)%>"><%: T("User name")%></label>
<%: Html.EditorFor(m => m.UserName)%>
<%: Html.ValidationMessage("UserName", "*")%>
</div>
<div>
<label for="<%: Html.FieldIdFor(m => m.Password)%>"><%: T("Password")%></label>
<%: Html.PasswordFor(m => m.Password)%>
<%: Html.ValidationMessage("Password", "*")%>
</div>
</div>
</fieldset>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add path="*" verb="*"
type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
</handlers>
</system.webServer>
</configuration>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
<add namespace="Orchard.Mvc.Html"/>
</namespaces>
</pages>
</system.web>
<system.web.extensions/>
</configuration>

View File

@@ -1,5 +1,9 @@
using System;
using System.Configuration;
using System.Security.Cryptography;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Linq;
using Orchard.FileSystems.AppData;
using Orchard.Setup.Services;
using Orchard.Setup.ViewModels;
@@ -34,7 +38,34 @@ namespace Orchard.Setup.Controllers {
return View(model);
}
private bool ValidateMachineKey() {
// Get the machineKey section.
var section = ConfigurationManager.GetSection("system.web/machineKey") as MachineKeySection;
if (section == null
|| section.DecryptionKey.Contains("AutoGenerate")
|| section.ValidationKey.Contains("AutoGenerate")) {
var rng = new RNGCryptoServiceProvider();
var decryptionData = new byte[32];
var validationData = new byte[64];
rng.GetBytes(decryptionData);
rng.GetBytes(validationData);
string decryptionKey = BitConverter.ToString(decryptionData).Replace("-", "");
string validationKey = BitConverter.ToString(validationData).Replace("-", "");
ModelState.AddModelError("MachineKey", T("You need to define a MachineKey value in your web.config file. Here is one for you:\n <machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\" decryption=\"AES\" />", validationKey, decryptionKey).ToString());
return false;
}
return true;
}
public ActionResult Index() {
ValidateMachineKey();
var initialSettings = _setupService.Prime();
return IndexViewResult(new SetupViewModel { AdminUsername = "admin", DatabaseIsPreconfigured = !string.IsNullOrEmpty(initialSettings.DataProvider)});
}
@@ -49,6 +80,8 @@ namespace Orchard.Setup.Controllers {
ModelState.AddModelError("ConfirmPassword", T("Password confirmation must match").ToString());
}
ValidateMachineKey();
if (!ModelState.IsValid) {
return IndexViewResult(model);
}

View File

@@ -77,6 +77,7 @@ namespace Orchard.Setup.Services {
"Routable",
"Settings",
//"XmlRpc",
"Messaging",
"Orchard.Users",
"Orchard.Roles",
//"TinyMce",

Binary file not shown.

After

Width:  |  Height:  |  Size: 603 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@@ -9,6 +9,11 @@ using Orchard.Mvc.Extensions;
using Orchard.Security;
using Orchard.Users.Services;
using Orchard.Users.ViewModels;
using Orchard.Settings;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Users.Models;
using Orchard.Mvc.Results;
namespace Orchard.Users.Controllers {
[HandleError]
@@ -30,6 +35,7 @@ namespace Orchard.Users.Controllers {
public ILogger Logger { get; set; }
public Localizer T { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ActionResult AccessDenied() {
var returnUrl = Request.QueryString["ReturnUrl"];
@@ -86,6 +92,12 @@ namespace Orchard.Users.Controllers {
}
public ActionResult Register() {
// ensure users can register
var registrationSettings = CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.UsersCanRegister ) {
return new NotFoundResult();
}
ViewData["PasswordLength"] = MinPasswordLength;
return View();
@@ -93,14 +105,26 @@ namespace Orchard.Users.Controllers {
[HttpPost]
public ActionResult Register(string userName, string email, string password, string confirmPassword) {
// ensure users can register
var registrationSettings = CurrentSite.As<RegistrationSettingsPart>();
if ( !registrationSettings.UsersCanRegister ) {
return new NotFoundResult();
}
ViewData["PasswordLength"] = MinPasswordLength;
if (ValidateRegistration(userName, email, password, confirmPassword)) {
// Attempt to register the user
var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, true));
var user = _membershipService.CreateUser(new CreateUserParams(userName, password, email, null, null, false));
if (user != null) {
if ( user.As<UserPart>().EmailStatus == UserStatus.Pending ) {
string challengeToken = _membershipService.GetEncryptedChallengeToken(user.As<UserPart>());
_membershipService.SendChallengeEmail(user.As<UserPart>(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new { Area = "Orchard.Users", token = challengeToken })));
return RedirectToAction("ChallengeEmailSent");
}
_authenticationService.SignIn(user, false /* createPersistentCookie */);
return Redirect("~/");
}
@@ -154,6 +178,29 @@ namespace Orchard.Users.Controllers {
return View();
}
public ActionResult ChallengeEmailSent() {
return View();
}
public ActionResult ChallengeEmailSuccess() {
return View();
}
public ActionResult ChallengeEmailFail() {
return View();
}
public ActionResult ChallengeEmail(string token) {
var user = _membershipService.ValidateChallengeToken(token);
if ( user != null ) {
_authenticationService.SignIn(user, false /* createPersistentCookie */);
return RedirectToAction("ChallengeEmailSuccess");
}
return RedirectToAction("ChallengeEmailFail");
}
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
if (filterContext.HttpContext.User.Identity is WindowsIdentity) {
throw new InvalidOperationException("Windows authentication is not supported.");

View File

@@ -1,12 +1,15 @@
using System.Linq;
using System.Web.Mvc;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Settings;
using Orchard.UI.Notify;
using Orchard.Users.Models;
using Orchard.Users.Services;
using Orchard.Users.ViewModels;
using Orchard.Mvc.Extensions;
namespace Orchard.Users.Controllers {
[ValidateInput(false)]
@@ -26,6 +29,7 @@ namespace Orchard.Users.Controllers {
public IOrchardServices Services { get; set; }
public Localizer T { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public ActionResult Index() {
if (!Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to list users")))
@@ -144,6 +148,55 @@ namespace Orchard.Users.Controllers {
return RedirectToAction("Index");
}
public ActionResult SendChallengeEmail(int id) {
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
return new HttpUnauthorizedResult();
var user = Services.ContentManager.Get(id);
if ( user != null ) {
string challengeToken = _membershipService.GetEncryptedChallengeToken(user.As<UserPart>());
_membershipService.SendChallengeEmail(user.As<UserPart>(), Url.AbsoluteAction(() => Url.Action("ChallengeEmail", "Account", new {Area = "Orchard.Users", token = challengeToken})));
}
Services.Notifier.Information(T("Challenge email sent"));
return RedirectToAction("Index");
}
public ActionResult Approve(int id) {
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
return new HttpUnauthorizedResult();
var user = Services.ContentManager.Get(id);
if ( user != null ) {
user.As<UserPart>().RegistrationStatus = UserStatus.Approved;
Services.Notifier.Information(T("User approved"));
}
return RedirectToAction("Index");
}
public ActionResult Moderate(int id) {
if ( !Services.Authorizer.Authorize(Permissions.ManageUsers, T("Not authorized to manage users")) )
return new HttpUnauthorizedResult();
var user = Services.ContentManager.Get(id);
if ( user != null ) {
if ( CurrentSite.SuperUser.Equals(user.As<UserPart>().UserName) ) {
Services.Notifier.Error(T("Super user can't be moderated"));
}
else {
user.As<UserPart>().RegistrationStatus = UserStatus.Pending;
Services.Notifier.Information(T("User moderated"));
}
}
return RedirectToAction("Index");
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

View File

@@ -18,5 +18,25 @@ namespace Orchard.Users.DataMigrations {
return 1;
}
public int UpdateFrom1() {
// Adds registration fields to previous versions
SchemaBuilder
.AlterTable("UserPartRecord", table => table.AddColumn<string>("RegistrationStatus", c => c.WithDefault("'Approved'")))
.AlterTable("UserPartRecord", table => table.AddColumn<string>("EmailStatus", c => c.WithDefault("'Approved'")))
.AlterTable("UserPartRecord", table => table.AddColumn<string>("EmailChallengeToken"));
// Site Settings record
SchemaBuilder.CreateTable("RegistrationSettingsPartRecord", table => table
.ContentPartRecord()
.Column<bool>("UsersCanRegister", c => c.WithDefault("'0'"))
.Column<bool>("UsersMustValidateEmail", c => c.WithDefault("'0'"))
.Column<bool>("UsersAreModerated", c => c.WithDefault("'0'"))
.Column<bool>("NotifyModeration", c => c.WithDefault("'0'"))
);
return 2;
}
}
}

View File

@@ -0,0 +1,16 @@
using Orchard.Events;
namespace Orchard.Users.Events {
public interface IUserEventHandler : IEventHandler {
/// <summary>
/// Called before a User is created
/// </summary>
void Creating(UserContext context);
/// <summary>
/// Called once a user has been created
/// </summary>
void Created(UserContext context);
}
}

View File

@@ -0,0 +1,8 @@
using Orchard.Users.Models;
namespace Orchard.Users.Events {
public class UserContext {
public UserPart User { get; set; }
public bool Cancel { get; set; }
}
}

View File

@@ -0,0 +1,38 @@
using Orchard.Messaging.Events;
using Orchard.Messaging.Models;
using Orchard.ContentManagement;
using Orchard.Users.Models;
namespace Orchard.Users.Handlers {
public class ModerationMessageAlteration : IMessageEventHandler {
private readonly IContentManager _contentManager;
public ModerationMessageAlteration(IContentManager contentManager) {
_contentManager = contentManager;
}
public void Sending(MessageContext context) {
var contentItem = _contentManager.Get(context.Recipient.Id);
if ( contentItem == null )
return;
var recipient = contentItem.As<UserPart>();
if ( recipient == null )
return;
if ( context.Type == MessageTypes.Moderation ) {
context.MailMessage.Subject = "User needs moderation";
context.MailMessage.Body = string.Format("The following user account needs to be moderated: {0}", recipient.UserName);
}
if ( context.Type == MessageTypes.Validation ) {
context.MailMessage.Subject = "User account validation";
context.MailMessage.Body = string.Format("Dear {0}, please <a href=\"{1}\">click here</a> to validate you email address.", recipient.UserName, context.Properties["ChallengeUrl"]);
}
}
public void Sent(MessageContext context) {
}
}
}

View File

@@ -0,0 +1,15 @@
using JetBrains.Annotations;
using Orchard.Data;
using Orchard.ContentManagement.Handlers;
using Orchard.Users.Models;
namespace Orchard.Users.Handlers {
[UsedImplicitly]
public class RegistrationSettingsPartHandler : ContentHandler {
public RegistrationSettingsPartHandler(IRepository<RegistrationSettingsPartRecord> repository) {
Filters.Add(new ActivatingFilter<RegistrationSettingsPart>("Site"));
Filters.Add(StorageFilter.For(repository));
Filters.Add(new TemplateFilterForRecord<RegistrationSettingsPartRecord>("RegistrationSettings", "Parts/Users.RegistrationSettings"));
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Orchard.Users.Models {
public static class MessageTypes {
public const string Moderation = "ORCHARD_USERS_MODERATION";
public const string Validation = "ORCHARD_USERS_VALIDATION";
}
}

View File

@@ -0,0 +1,27 @@
using Orchard.ContentManagement;
using System;
namespace Orchard.Users.Models {
public class RegistrationSettingsPart : ContentPart<RegistrationSettingsPartRecord> {
public bool UsersCanRegister {
get { return Record.UsersCanRegister; }
set { Record.UsersCanRegister = value; }
}
public bool UsersMustValidateEmail {
get { return Record.UsersMustValidateEmail; }
set { Record.UsersMustValidateEmail = value; }
}
public bool UsersAreModerated {
get { return Record.UsersAreModerated; }
set { Record.UsersAreModerated = value; }
}
public bool NotifyModeration {
get { return Record.NotifyModeration; }
set { Record.NotifyModeration = value; }
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Net.Mail;
using Orchard.ContentManagement.Records;
using System.ComponentModel.DataAnnotations;
namespace Orchard.Users.Models {
public class RegistrationSettingsPartRecord : ContentPartRecord {
public virtual bool UsersCanRegister { get; set; }
public virtual bool UsersMustValidateEmail { get; set; }
public virtual bool UsersAreModerated { get; set; }
public virtual bool NotifyModeration { get; set; }
}
}

View File

@@ -21,5 +21,15 @@ namespace Orchard.Users.Models {
get { return Record.NormalizedUserName; }
set { Record.NormalizedUserName = value; }
}
public UserStatus RegistrationStatus {
get { return Record.RegistrationStatus; }
set { Record.RegistrationStatus = value; }
}
public UserStatus EmailStatus {
get { return Record.EmailStatus; }
set { Record.EmailStatus = value; }
}
}
}

View File

@@ -11,5 +11,9 @@ namespace Orchard.Users.Models {
public virtual MembershipPasswordFormat PasswordFormat { get; set; }
public virtual string HashAlgorithm { get; set; }
public virtual string PasswordSalt { get; set; }
public virtual UserStatus RegistrationStatus { get; set; }
public virtual UserStatus EmailStatus { get; set; }
public virtual string EmailChallengeToken { get; set; }
}
}

View File

@@ -0,0 +1,6 @@
namespace Orchard.Users.Models {
public enum UserStatus {
Pending,
Approved
}
}

View File

@@ -45,6 +45,7 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Security" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
@@ -68,9 +69,17 @@
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="DataMigrations\UsersDataMigration.cs" />
<Compile Include="Events\UserContext.cs" />
<Compile Include="Handlers\ModerationMessageAlteration.cs" />
<Compile Include="Handlers\RegistrationSettingsPartHandler.cs" />
<Compile Include="Events\IUserEventHandler.cs" />
<Compile Include="Models\MessageTypes.cs" />
<Compile Include="Models\RegistrationSettingsPart.cs" />
<Compile Include="Models\RegistrationSettingsPartRecord.cs" />
<Compile Include="Models\UserPart.cs" />
<Compile Include="Handlers\UserPartHandler.cs" />
<Compile Include="Models\UserPartRecord.cs" />
<Compile Include="Models\UserStatus.cs" />
<Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\IUserService.cs" />
@@ -83,10 +92,15 @@
<Compile Include="ViewModels\UsersIndexViewModel.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Content\Admin\images\offline.gif" />
<Content Include="Content\Admin\images\online.gif" />
<Content Include="Module.txt" />
<Content Include="Views\Account\ChangePassword.ascx" />
<Content Include="Views\Account\ChangePasswordSuccess.ascx" />
<Content Include="Views\Account\AccessDenied.ascx" />
<Content Include="Views\Account\ChallengeEmailSuccess.ascx" />
<Content Include="Views\Account\ChallengeEmailSent.ascx" />
<Content Include="Views\Account\ChallengeEmailFail.ascx" />
<Content Include="Views\Account\LogOn.ascx" />
<Content Include="Views\Account\Register.ascx" />
<Content Include="Views\Admin\Edit.aspx" />
@@ -94,6 +108,7 @@
<Content Include="Views\Admin\EditorTemplates\inputPasswordLarge.ascx" />
<Content Include="Views\Admin\EditorTemplates\inputTextLarge.ascx" />
<Content Include="Views\Admin\Index.aspx" />
<Content Include="Views\EditorTemplates\Parts\Users.RegistrationSettings.ascx" />
<Content Include="Web.config" />
<Content Include="Views\Web.config" />
</ItemGroup>

View File

@@ -1,28 +1,37 @@
using System;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;
using System.Xml.Linq;
using JetBrains.Annotations;
using Orchard.Data;
using Orchard.Logging;
using Orchard.ContentManagement;
using Orchard.Security;
using Orchard.Users.Events;
using Orchard.Users.Models;
using Orchard.Settings;
using Orchard.Messaging.Services;
using System.Collections.Generic;
namespace Orchard.Users.Services {
[UsedImplicitly]
public class MembershipService : IMembershipService {
private static readonly TimeSpan DelayToValidate = new TimeSpan(7, 0, 0, 0); // one week to validate email
private readonly IContentManager _contentManager;
private readonly IRepository<UserPartRecord> _userRepository;
private readonly IMessageManager _messageManager;
private readonly IEnumerable<IUserEventHandler> _userEventHandlers;
public MembershipService(IContentManager contentManager, IRepository<UserPartRecord> userRepository) {
public MembershipService(IContentManager contentManager, IMessageManager messageManager, IEnumerable<IUserEventHandler> userEventHandlers) {
_contentManager = contentManager;
_userRepository = userRepository;
_messageManager = messageManager;
_userEventHandlers = userEventHandlers;
Logger = NullLogger.Instance;
}
public ILogger Logger { get; set; }
protected virtual ISite CurrentSite { get; [UsedImplicitly] private set; }
public MembershipSettings GetSettings() {
var settings = new MembershipSettings();
@@ -33,40 +42,124 @@ namespace Orchard.Users.Services {
public IUser CreateUser(CreateUserParams createUserParams) {
Logger.Information("CreateUser {0} {1}", createUserParams.Username, createUserParams.Email);
return _contentManager.Create<UserPart>("User", init =>
{
init.Record.UserName = createUserParams.Username;
init.Record.Email = createUserParams.Email;
init.Record.NormalizedUserName = createUserParams.Username.ToLower();
init.Record.HashAlgorithm = "SHA1";
SetPassword(init.Record, createUserParams.Password);
});
var registrationSettings = CurrentSite.As<RegistrationSettingsPart>();
var user = _contentManager.New<UserPart>("User");
user.Record.UserName = createUserParams.Username;
user.Record.Email = createUserParams.Email;
user.Record.NormalizedUserName = createUserParams.Username.ToLower();
user.Record.HashAlgorithm = "SHA1";
SetPassword(user.Record, createUserParams.Password);
if ( registrationSettings != null ) {
user.Record.RegistrationStatus = registrationSettings.UsersAreModerated ? UserStatus.Pending : UserStatus.Approved;
user.Record.EmailStatus = registrationSettings.UsersMustValidateEmail ? UserStatus.Pending : UserStatus.Approved;
}
if(createUserParams.IsApproved) {
user.Record.RegistrationStatus = UserStatus.Approved;
user.Record.EmailStatus = UserStatus.Approved;
}
var userContext = new UserContext {User = user, Cancel = false};
foreach(var userEventHandler in _userEventHandlers) {
userEventHandler.Creating(userContext);
}
if(userContext.Cancel) {
return null;
}
_contentManager.Create(user);
foreach ( var userEventHandler in _userEventHandlers ) {
userEventHandler.Created(userContext);
}
if ( registrationSettings != null && registrationSettings.UsersAreModerated && registrationSettings.NotifyModeration && !createUserParams.IsApproved ) {
var superUser = GetUser(CurrentSite.SuperUser);
if(superUser != null)
_messageManager.Send(superUser.ContentItem.Record, MessageTypes.Moderation);
}
return user;
}
public void SendChallengeEmail(IUser user, string url) {
_messageManager.Send(user.ContentItem.Record, MessageTypes.Validation, "Email", new Dictionary<string, string> { { "ChallengeUrl", url } });
}
public IUser ValidateChallengeToken(string challengeToken) {
string username;
DateTime validateByUtc;
if(!DecryptChallengeToken(challengeToken, out username, out validateByUtc)) {
return null;
}
if ( validateByUtc < DateTime.UtcNow )
return null;
var user = GetUser(username);
if ( user == null )
return null;
user.As<UserPart>().EmailStatus = UserStatus.Approved;
return user;
}
public string GetEncryptedChallengeToken(IUser user) {
var challengeToken = new XElement("Token", new XAttribute("username", user.UserName), new XAttribute("validate-by-utc", DateTime.UtcNow.Add(DelayToValidate).ToString(CultureInfo.InvariantCulture))).ToString();
var data = Encoding.UTF8.GetBytes(challengeToken);
return MachineKey.Encode(data, MachineKeyProtection.All);
}
private static bool DecryptChallengeToken(string challengeToken, out string username, out DateTime validateByUtc) {
username = null;
validateByUtc = DateTime.UtcNow;
try {
var data = MachineKey.Decode(challengeToken, MachineKeyProtection.All);
var xml = Encoding.UTF8.GetString(data);
var element = XElement.Parse(xml);
username = element.Attribute("username").Value;
validateByUtc = DateTime.Parse(element.Attribute("validate-by-utc").Value, CultureInfo.InvariantCulture);
return true;
}
catch {
return false;
}
}
public IUser GetUser(string username) {
var lowerName = username == null ? "" : username.ToLower();
var userRecord = _userRepository.Get(x => x.NormalizedUserName == lowerName);
if (userRecord == null) {
return null;
}
return _contentManager.Get<IUser>(userRecord.Id);
return _contentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName).List().FirstOrDefault();
}
public IUser ValidateUser(string userNameOrEmail, string password) {
var lowerName = userNameOrEmail == null ? "" : userNameOrEmail.ToLower();
var userRecord = _userRepository.Get(x => x.NormalizedUserName == lowerName);
if(userRecord == null)
userRecord = _userRepository.Get(x => x.Email == lowerName);
if (userRecord == null || ValidatePassword(userRecord, password) == false)
var user = _contentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName).List().FirstOrDefault();
if(user == null)
user = _contentManager.Query<UserPart, UserPartRecord>().Where(u => u.Email == lowerName).List().FirstOrDefault();
if ( user == null || ValidatePassword(user.As<UserPart>().Record, password) == false )
return null;
return _contentManager.Get<IUser>(userRecord.Id);
if ( user.EmailStatus != UserStatus.Approved )
return null;
if ( user.RegistrationStatus != UserStatus.Approved )
return null;
return user;
}
public void SetPassword(IUser user, string password) {
if (!user.Is<UserPart>())
throw new InvalidCastException();
@@ -92,7 +185,7 @@ namespace Orchard.Users.Services {
}
}
private bool ValidatePassword(UserPartRecord partRecord, string password) {
private static bool ValidatePassword(UserPartRecord partRecord, string password) {
// Note - the password format stored with the record is used
// otherwise changing the password format on the site would invalidate
// all logins
@@ -157,5 +250,6 @@ namespace Orchard.Users.Services {
private static bool ValidatePasswordEncrypted(UserPartRecord partRecord, string password) {
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,3 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<object>" %>
<h1><%: Html.TitleForPage(T("Challenge Email").ToString()) %></h1>
<p><%: T("Your email address could not be validated.") %></p>

View File

@@ -0,0 +1,3 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<object>" %>
<h1><%: Html.TitleForPage(T("Challenge Email Sent").ToString()) %></h1>
<p><%: T("An email has been sent to you. Please click on the link it contains in order to have access on this site.") %></p>

View File

@@ -0,0 +1,3 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<object>" %>
<h1><%: Html.TitleForPage(T("Challenge Email").ToString()) %></h1>
<p><%: T("Your email address has been validated.") %></p>

View File

@@ -1,4 +1,5 @@
<%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage<UsersIndexViewModel>" %>
<%@ Import Namespace="Orchard.Users.Models" %>
<%@ Import Namespace="Orchard.Users.ViewModels"%>
<h1><%: Html.TitleForPage(T("Manage Users").ToString()) %></h1>
<% using (Html.BeginFormAntiForgeryPost()) { %>
@@ -22,6 +23,12 @@
{ %>
<tr>
<td>
<% if(row.UserPart.RegistrationStatus == UserStatus.Approved) { %>
<img class="icon" src="<%=ResolveUrl("~/Modules/Orchard.Users/Content/Admin/images/online.gif") %>" alt="<%:T("Approved") %>" title="<%:T("User is approved") %>" />
<% }
else { %>
<img class="icon" src="<%=ResolveUrl("~/Modules/Orchard.Users/Content/Admin/images/offline.gif") %>" alt="<%:T("Moderated") %>" title="<%:T("User is moderated") %>" />
<% } %>
<%: row.UserPart.UserName %>
</td>
<td>
@@ -29,7 +36,11 @@
</td>
<td>
<%: Html.ActionLink(T("Edit").ToString(), "Edit", new { row.UserPart.Id })%> |
<%: Html.ActionLink(T("Remove").ToString(), "Delete", new { row.UserPart.Id })%>
<%: Html.ActionLink(T("Remove").ToString(), "Delete", new { row.UserPart.Id })%> |
<%: row.UserPart.RegistrationStatus == UserStatus.Pending ? Html.ActionLink(T("Approve").ToString(), "Approve", new { row.UserPart.Id }) : Html.ActionLink(T("Disable").ToString(), "Moderate", new { row.UserPart.Id })%>
<% if ( row.UserPart.EmailStatus == UserStatus.Pending ) { %> |
<%: Html.ActionLink(T("Challenge Email").ToString(), "SendChallengeEmail", new { row.UserPart.Id })%>
<% } %>
</td>
</tr>
<%}%>

View File

@@ -0,0 +1,25 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<RegistrationSettingsPartRecord>" %>
<%@ Import Namespace="Orchard.Users.Models"%>
<fieldset>
<legend><%: T("Users registration")%></legend>
<div>
<%: Html.EditorFor(m => m.UsersCanRegister) %>
<label class="forcheckbox" for="<%: Html.FieldIdFor( m => m.UsersCanRegister) %>"><%: T("Users can create new accounts on the site")%></label>
<%: Html.ValidationMessage("UsersCanRegister", "*")%>
</div>
<div>
<%: Html.EditorFor(m => m.UsersMustValidateEmail)%>
<label class="forcheckbox" for="<%: Html.FieldIdFor( m => m.UsersMustValidateEmail) %>"><%: T("Users must justify their email address")%></label>
<%: Html.ValidationMessage("UsersMustValidateEmail", "*")%>
</div>
<div>
<%: Html.EditorFor(m => m.UsersAreModerated)%>
<label class="forcheckbox" for="<%: Html.FieldIdFor( m => m.UsersAreModerated) %>"><%: T("Users must be approved before they can log in")%></label>
<%: Html.ValidationMessage("UsersAreModerated", "*")%>
</div>
<div data-controllerid="<%:Html.FieldIdFor(m => m.UsersAreModerated) %>">
<%: Html.EditorFor(m => m.NotifyModeration)%>
<label class="forcheckbox" for="<%: Html.FieldIdFor( m => m.NotifyModeration) %>"><%: T("Send a notification when a user needs moderation")%></label>
<%: Html.ValidationMessage("NotifyModeration", "*")%>
</div>
</fieldset>

View File

@@ -42,6 +42,8 @@
<defaultSettings timeout="00:30:00"/>
</system.transactions>
<system.web>
<machineKey validationKey="013B82F217ABB7EAB1F699E4E5B4D290030644D435994692354DAE82B06568B058BFE3C57BF199A41FFDBC84F3BC74D9C5BD96D1265F36A22D58347B591AC8DD" decryptionKey="04797035C490263D73ED991C84C5DFCD0D0206AD4F12BC3638A38FBEABEBB8C7" validation="SHA1" decryption="AES" />
<httpRuntime requestValidationMode="2.0" />
<!--
Set compilation debug="true" to insert debugging
@@ -68,33 +70,6 @@
<authentication mode="Forms">
<forms loginUrl="~/Users/Account/AccessDenied" timeout="2880"/>
</authentication>
<membership defaultProvider="OrchardMembershipProvider">
<providers>
<clear/>
<!--<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
applicationName="/"
/>-->
<add name="OrchardMembershipProvider" type="Orchard.Security.Providers.OrchardMembershipProvider, Orchard.Framework" applicationName="/"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>

View File

@@ -18,6 +18,11 @@ namespace Orchard.Data.Migration.Schema {
TableCommands.Add(command);
}
public void AddColumn<T>(string columnName, Action<AddColumnCommand> column = null) {
var dbType = SchemaUtils.ToDbType(typeof(T));
AddColumn(columnName, dbType, column);
}
public void DropColumn(string columnName) {
var command = new DropColumnCommand(Name, columnName);
TableCommands.Add(command);

View File

@@ -0,0 +1,9 @@
using Orchard.Events;
using Orchard.Messaging.Models;
namespace Orchard.Messaging.Events {
public interface IMessageEventHandler : IEventHandler {
void Sending(MessageContext context);
void Sent(MessageContext context);
}
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Net.Mail;
using Orchard.ContentManagement.Records;
namespace Orchard.Messaging.Models {
public class MessageContext {
public MailMessage MailMessage { get; private set; }
public string Type { get; set; }
public string Service { get; set; }
public ContentItemRecord Recipient { get; set; }
public Dictionary<string, string> Properties { get; private set; }
public MessageContext() {
Properties = new Dictionary<string, string>();
MailMessage = new MailMessage();
}
}
}

View File

@@ -0,0 +1,21 @@
using System.Collections.Generic;
using Orchard.ContentManagement.Records;
namespace Orchard.Messaging.Services {
public interface IMessageManager : IDependency {
/// <summary>
/// Sends a message to a channel
/// </summary>
void Send(ContentItemRecord recipient, string type, string service = null, Dictionary<string, string> properties = null);
/// <summary>
/// Wether at least one channel is active on the current site
/// </summary>
bool HasChannels();
/// <summary>
/// Provides a list of all the current available channel services
/// </summary>
IEnumerable<string> GetAvailableChannelServices();
}
}

View File

@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.Messaging.Models;
namespace Orchard.Messaging.Services {
public interface IMessagingChannel : IDependency {
/// <summary>
/// Actually sends the message though this channel
/// </summary>
void SendMessage(MessageContext message);
/// <summary>
/// Provides all the handled services, the user can choose from when receving messages
/// </summary>
IEnumerable<string> GetAvailableServices();
}
}

View File

@@ -397,6 +397,10 @@
<Compile Include="Environment\IHostLocalRestart.cs" />
<Compile Include="Environment\IShellContainerRegistrations.cs" />
<Compile Include="FileSystems\Dependencies\DynamicModuleVirtualPathProvider.cs" />
<Compile Include="Messaging\Events\IMessageEventHandler.cs" />
<Compile Include="Messaging\Models\MessageContext.cs" />
<Compile Include="Messaging\Services\IMessageManager.cs" />
<Compile Include="Messaging\Services\IMessagingChannel.cs" />
<Compile Include="IWorkContextAccessor.cs" />
<Compile Include="WorkContextExtensions.cs" />
<Compile Include="Mvc\Html\Shapes.cs" />
@@ -773,7 +777,6 @@
<Compile Include="Security\IUser.cs" />
<Compile Include="Security\Permissions\IPermissionProvider.cs" />
<Compile Include="Security\Permissions\Permission.cs" />
<Compile Include="Security\Providers\OrchardMembershipProvider.cs" />
<Compile Include="Security\Providers\OrchardRoleProvider.cs" />
<Compile Include="Services\Clock.cs" />
<Compile Include="FileSystems\Media\FileSystemStorageProvider.cs" />

View File

@@ -7,5 +7,9 @@
IUser ValidateUser(string userNameOrEmail, string password);
void SetPassword(IUser user, string password);
IUser ValidateChallengeToken(string challengeToken);
void SendChallengeEmail(IUser user, string url);
string GetEncryptedChallengeToken(IUser user);
}
}

View File

@@ -1,127 +0,0 @@
using System;
using System.Web.Security;
using Orchard.Environment;
namespace Orchard.Security.Providers {
public class OrchardMembershipProvider : MembershipProvider {
static IMembershipService GetService() {
throw new NotImplementedException("The OrchardMemberShipProvider is not supported anymore. Use the IMembershipService interface instead.");
}
static MembershipSettings GetSettings() {
return GetService().GetSettings();
}
private MembershipUser BuildMembershipUser(IUser user) {
return new MembershipUser(Name,
user.UserName,
user.Id,
user.Email,
null,
null,
true,
false,
DateTime.UtcNow,
DateTime.UtcNow,
DateTime.UtcNow,
DateTime.UtcNow,
DateTime.UtcNow);
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
var user = GetService().CreateUser(new CreateUserParams(username, password, email, passwordQuestion, passwordAnswer, isApproved));
if (user == null) {
status = MembershipCreateStatus.ProviderError;
return null;
}
status = MembershipCreateStatus.Success;
return BuildMembershipUser(user);
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
throw new NotImplementedException();
}
public override string GetPassword(string username, string answer) {
throw new NotImplementedException();
}
public override bool ChangePassword(string username, string oldPassword, string newPassword) {
var service = GetService();
var user = service.ValidateUser(username, oldPassword);
if (user == null)
return false;
service.SetPassword(user, newPassword);
return true;
}
public override string ResetPassword(string username, string answer) {
throw new NotImplementedException();
}
public override void UpdateUser(MembershipUser user) {
throw new NotImplementedException();
}
public override bool ValidateUser(string username, string password) {
return (GetService().ValidateUser(username, password) != null);
}
public override bool UnlockUser(string userName) {
throw new NotImplementedException();
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
throw new NotImplementedException();
}
public override MembershipUser GetUser(string username, bool userIsOnline) {
throw new NotImplementedException();
}
public override string GetUserNameByEmail(string email) {
throw new NotImplementedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData) {
throw new NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
throw new NotImplementedException();
}
public override int GetNumberOfUsersOnline() {
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
throw new NotImplementedException();
}
public override string ApplicationName {
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override bool EnablePasswordRetrieval { get { return GetSettings().EnablePasswordRetrieval; } }
public override bool EnablePasswordReset { get { return GetSettings().EnablePasswordReset; } }
public override bool RequiresQuestionAndAnswer { get { return GetSettings().RequiresQuestionAndAnswer; } }
public override int MaxInvalidPasswordAttempts { get { return GetSettings().MaxInvalidPasswordAttempts; } }
public override int PasswordAttemptWindow { get { return GetSettings().PasswordAttemptWindow; } }
public override bool RequiresUniqueEmail { get { return GetSettings().RequiresUniqueEmail; } }
public override MembershipPasswordFormat PasswordFormat { get { return GetSettings().PasswordFormat; } }
public override int MinRequiredPasswordLength { get { return GetSettings().MinRequiredPasswordLength; } }
public override int MinRequiredNonAlphanumericCharacters { get { return GetSettings().MinRequiredNonAlphanumericCharacters; } }
public override string PasswordStrengthRegularExpression { get { return GetSettings().PasswordStrengthRegularExpression; } }
}
}