Adding user activities.

These activities are useful when customizing user signup forms.
This commit is contained in:
Sipke Schoorstra
2014-11-23 00:58:27 -08:00
parent c5dbb80a72
commit 1557f64ccc
6 changed files with 315 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Users.Events;
using Orchard.Users.Models;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
namespace Orchard.Users.Activities {
[OrchardFeature("Orchard.Users.Workflows")]
public class ApproveUserActivity : Task {
private readonly IUserEventHandler _userEventHandlers;
public ApproveUserActivity(IUserEventHandler userEventHandlers) {
_userEventHandlers = userEventHandlers;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public override string Name {
get { return "ApproveUser"; }
}
public override LocalizedString Category {
get { return T("User"); }
}
public override LocalizedString Description {
get { return T("If the content item is a user, that user will be approved."); }
}
public override bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) {
return workflowContext.Content != null && workflowContext.Content.Is<UserPart>();
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
return new[] { T("Done") };
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {
var user = workflowContext.Content.As<UserPart>();
user.RegistrationStatus = UserStatus.Approved;
user.EmailStatus = UserStatus.Approved;
_userEventHandlers.Approved(user);
yield return T("Done");
}
}
}

View File

@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Users.Services;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
namespace Orchard.Users.Activities {
[OrchardFeature("Orchard.Users.Workflows")]
public class CreateUserActivity : Task {
private readonly IUserService _userService;
private readonly IMembershipService _membershipService;
public CreateUserActivity(IUserService userService, IMembershipService membershipService) {
_userService = userService;
_membershipService = membershipService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public override string Name {
get { return "CreateUser"; }
}
public override LocalizedString Category {
get { return T("User"); }
}
public override LocalizedString Description {
get { return T("Creates a new User based on the specified values."); }
}
public override string Form {
get { return "CreateUser"; }
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
return new[] {
T("InvalidUserNameOrEmail"),
T("InvalidPassword"),
T("UserNameOrEmailNotUnique"),
T("Done")
};
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {
var userName = activityContext.GetState<string>("UserName");
var email = activityContext.GetState<string>("Email");
var password = activityContext.GetState<string>("Password");
var approved = activityContext.GetState<bool>("Approved");
if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(email)) {
yield return T("InvalidUserNameOrEmail");
yield break;
}
if (String.IsNullOrWhiteSpace(password)) {
yield return T("InvalidPassword");
yield break;
}
if (!_userService.VerifyUserUnicity(userName, email)) {
yield return T("UserNameOrEmailNotUnique");
yield break;
}
var user = _membershipService.CreateUser(
new CreateUserParams(
userName,
password,
email,
isApproved: approved,
passwordQuestion: null,
passwordAnswer: null));
workflowContext.Content = user;
yield return T("Done");
}
}
}

View File

@@ -0,0 +1,55 @@
using System.Collections.Generic;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Users.Services;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
namespace Orchard.Users.Activities {
[OrchardFeature("Orchard.Users.Workflows")]
public class VerifyUserUnicityActivity : Task {
private readonly IUserService _userService;
public VerifyUserUnicityActivity(IUserService userService) {
_userService = userService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public override string Name {
get { return "VerifyUserUnicity"; }
}
public override LocalizedString Category {
get { return T("User"); }
}
public override LocalizedString Description {
get { return T("Verifies if the specified user name and email address are unique."); }
}
public override string Form {
get { return "VerifyUserUnicity"; }
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
return new[] {
T("Unique"),
T("NotUnique")
};
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {
var userName = activityContext.GetState<string>("UserName");
var email = activityContext.GetState<string>("Email");
if (_userService.VerifyUserUnicity(userName, email)) {
yield return T("Unique");
}
else {
yield return T("NotUnique");
}
}
}
}

View File

@@ -0,0 +1,66 @@
using System;
using Orchard.Environment.Extensions;
using Orchard.Forms.Services;
namespace Orchard.Users.Forms {
[OrchardFeature("Orchard.Users.Workflows")]
public class CreateUserForm : Component, IFormProvider, IFormEventHandler {
void IFormProvider.Describe(DescribeContext context) {
context.Form("CreateUser", factory => {
var shape = (dynamic) factory;
var form = shape.Form(
Id: "createUser",
_UserName: shape.Textbox(
Id: "userName",
Name: "UserName",
Title: T("User Name"),
Description: T("The user name of the user to be created."),
Classes: new[]{"text", "large", "tokenized"}),
_Email: shape.Textbox(
Id: "email",
Name: "Email",
Title: T("Email"),
Description: T("The email address of the user to be created."),
Classes: new[] { "text", "large", "tokenized" }),
_Password: shape.Textbox(
Id: "password",
Name: "Password",
Title: T("Password"),
Description: T("The password of the user to be created."),
Classes: new[] { "text", "large", "tokenized" }),
_Approved: shape.Checkbox(
Id: "approved",
Name: "Approved",
Title: T("Approved"),
Description: T("Check to approve the created user."),
Value: true));
return form;
});
}
void IFormEventHandler.Validating(ValidatingContext context) {
if (context.FormName != "CreateUser") return;
var userName = context.ValueProvider.GetValue("UserName").AttemptedValue;
var email = context.ValueProvider.GetValue("Email").AttemptedValue;
var password = context.ValueProvider.GetValue("Password").AttemptedValue;
if (String.IsNullOrWhiteSpace(userName)) {
context.ModelState.AddModelError("UserName", T("You must specify a username or a token that evaluates to a username.").Text);
}
if (String.IsNullOrWhiteSpace(email)) {
context.ModelState.AddModelError("Email", T("You must specify an email address or a token that evaluates to an email address.").Text);
}
if (String.IsNullOrWhiteSpace(password)) {
context.ModelState.AddModelError("Password", T("You must specify a password or a token that evaluates to a password.").Text);
}
}
void IFormEventHandler.Building(BuildingContext context) {}
void IFormEventHandler.Built(BuildingContext context) {}
void IFormEventHandler.Validated(ValidatingContext context) {}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using Orchard.Environment.Extensions;
using Orchard.Forms.Services;
namespace Orchard.Users.Forms {
[OrchardFeature("Orchard.Users.Workflows")]
public class VerifyUserUnicityForm : Component, IFormProvider, IFormEventHandler {
void IFormProvider.Describe(DescribeContext context) {
context.Form("VerifyUserUnicity", factory => {
var shape = (dynamic) factory;
var form = shape.Form(
Id: "verifyUserUnicity",
_UserName: shape.Textbox(
Id: "userName",
Name: "UserName",
Title: T("User Name"),
Description: T("The user name to be validated."),
Classes: new[]{"text", "large", "tokenized"}),
_Email: shape.Textbox(
Id: "email",
Name: "Email",
Title: T("Email"),
Description: T("The email address to be validated."),
Classes: new[] { "text", "large", "tokenized" }));
return form;
});
}
void IFormEventHandler.Validating(ValidatingContext context) {
if (context.FormName != "VerifyUserUnicity") return;
var userName = context.ValueProvider.GetValue("UserName").AttemptedValue;
var email = context.ValueProvider.GetValue("Email").AttemptedValue;
if (String.IsNullOrWhiteSpace(userName)) {
context.ModelState.AddModelError("UserName", T("You must specify a username or a token that evaluates to a username.").Text);
}
if (String.IsNullOrWhiteSpace(email)) {
context.ModelState.AddModelError("Email", T("You must specify an email address or a token that evaluates to an email address.").Text);
}
}
void IFormEventHandler.Building(BuildingContext context) {}
void IFormEventHandler.Built(BuildingContext context) {}
void IFormEventHandler.Validated(ValidatingContext context) {}
}
}

View File

@@ -67,12 +67,17 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\VerifyUserUnicityActivity.cs" />
<Compile Include="Activities\ApproveUserActivity.cs" />
<Compile Include="Activities\CreateUserActivity.cs" />
<Compile Include="Activities\UserActivity.cs" />
<Compile Include="Activities\UserIsApprovedActivity.cs" />
<Compile Include="Commands\UserCommands.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Drivers\UserPartDriver.cs" />
<Compile Include="Forms\VerifyUserUnicityForm.cs" />
<Compile Include="Forms\CreateUserForm.cs" />
<Compile Include="Handlers\WorkflowUserEventHandler.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Events\UserContext.cs" />
@@ -124,6 +129,10 @@
<Project>{9916839C-39FC-4CEB-A5AF-89CA7E87119F}</Project>
<Name>Orchard.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Forms\Orchard.Forms.csproj">
<Project>{642a49d7-8752-4177-80d6-bfbbcfad3de0}</Project>
<Name>Orchard.Forms</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Workflows\Orchard.Workflows.csproj">
<Project>{7059493c-8251-4764-9c1e-2368b8b485bc}</Project>
<Name>Orchard.Workflows</Name>