Adding SiginInUser workflow activity.

This commit is contained in:
Sipke Schoorstra
2014-11-24 14:52:31 -08:00
parent dc9490d895
commit ae9c8a9457
3 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Security;
using Orchard.Workflows.Models;
using Orchard.Workflows.Services;
namespace Orchard.Users.Activities {
[OrchardFeature("Orchard.Users.Workflows")]
public class SignInUserActivity : Task {
private readonly IMembershipService _membershipService;
private readonly IAuthenticationService _authenticationService;
public SignInUserActivity(IMembershipService membershipService, IAuthenticationService authenticationService) {
_membershipService = membershipService;
_authenticationService = authenticationService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public override string Name {
get { return "SignInUser"; }
}
public override LocalizedString Category {
get { return T("User"); }
}
public override LocalizedString Description {
get { return T("Signs in a user based on the specified credentials."); }
}
public override string Form {
get { return "SignInUser"; }
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
return new[] {
T("IncorrectUserNameOrPassword"),
T("Done")
};
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {
var userNameOrEmail = activityContext.GetState<string>("UserNameOrEmail");
var password = activityContext.GetState<string>("Password");
var createPersistentCookie = IsTrueish(activityContext.GetState<string>("CreatePersistentCookie"));
if (String.IsNullOrWhiteSpace(userNameOrEmail) || String.IsNullOrWhiteSpace(password)) {
yield return T("IncorrectUserNameOrPassword");
yield break;
}
var user = _membershipService.ValidateUser(userNameOrEmail, password);
if (user == null) {
yield return T("IncorrectUserNameOrPassword");
yield break;
}
_authenticationService.SignIn(user, createPersistentCookie);
yield return T("Done");
}
private bool IsTrueish(string value) {
if (String.IsNullOrWhiteSpace(value))
return false;
var falseValues = new[] {"false", "off", "no"};
return falseValues.All(x => !String.Equals(x, value, StringComparison.OrdinalIgnoreCase));
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using Orchard.Environment.Extensions;
using Orchard.Forms.Services;
namespace Orchard.Users.Forms {
[OrchardFeature("Orchard.Users.Workflows")]
public class SignInUserForm : Component, IFormProvider, IFormEventHandler {
void IFormProvider.Describe(DescribeContext context) {
context.Form("SignInUser", factory => {
var shape = (dynamic) factory;
var form = shape.Form(
Id: "signInUser",
_UserName: shape.Textbox(
Id: "userNameOrEmail",
Name: "UserNameOrEmail",
Title: T("User Name or Email"),
Description: T("The user name or email of the user to sign in."),
Classes: new[]{"text", "large", "tokenized"}),
_Password: shape.Textbox(
Id: "password",
Name: "Password",
Title: T("Password"),
Description: T("The password of the user to sign in."),
Classes: new[] { "text", "large", "tokenized" }),
_CreatePersistentCookie: shape.Textbox(
Id: "createPersistentCookie",
Name: "CreatePersistentCookie",
Title: T("Create Persistent Cookie"),
Description: T("A value evaluating to 'true' to create a persistent cookie."),
Classes: new[]{"text", "large", "tokenized"}));
return form;
});
}
void IFormEventHandler.Validating(ValidatingContext context) {
if (context.FormName != "SignInUser") return;
var userName = context.ValueProvider.GetValue("UserNameOrEmail").AttemptedValue;
var password = context.ValueProvider.GetValue("Password").AttemptedValue;
if (String.IsNullOrWhiteSpace(userName)) {
context.ModelState.AddModelError("UserNameOrEmail", T("You must specify a user name, email address or a token that evaluates to a username or 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

@@ -67,6 +67,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\SignInUserActivity.cs" />
<Compile Include="Activities\VerifyUserUnicityActivity.cs" />
<Compile Include="Activities\ApproveUserActivity.cs" />
<Compile Include="Activities\CreateUserActivity.cs" />
@@ -76,6 +77,7 @@
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Drivers\UserPartDriver.cs" />
<Compile Include="Forms\SignInUserForm.cs" />
<Compile Include="Forms\VerifyUserUnicityForm.cs" />
<Compile Include="Forms\CreateUserForm.cs" />
<Compile Include="Handlers\WorkflowUserEventHandler.cs" />