diff --git a/src/Orchard.Web/Modules/Orchard.Users/Activities/ApproveUserActivity.cs b/src/Orchard.Web/Modules/Orchard.Users/Activities/ApproveUserActivity.cs new file mode 100644 index 000000000..3abdd6ad9 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Users/Activities/ApproveUserActivity.cs @@ -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(); + } + + public override IEnumerable GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) { + return new[] { T("Done") }; + } + + public override IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext) { + var user = workflowContext.Content.As(); + + user.RegistrationStatus = UserStatus.Approved; + user.EmailStatus = UserStatus.Approved; + _userEventHandlers.Approved(user); + + yield return T("Done"); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Activities/CreateUserActivity.cs b/src/Orchard.Web/Modules/Orchard.Users/Activities/CreateUserActivity.cs new file mode 100644 index 000000000..8ef1512ef --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Users/Activities/CreateUserActivity.cs @@ -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 GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) { + return new[] { + T("InvalidUserNameOrEmail"), + T("InvalidPassword"), + T("UserNameOrEmailNotUnique"), + T("Done") + }; + } + + public override IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext) { + var userName = activityContext.GetState("UserName"); + var email = activityContext.GetState("Email"); + var password = activityContext.GetState("Password"); + var approved = activityContext.GetState("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"); + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Activities/VerifyUserUnicityActivity.cs b/src/Orchard.Web/Modules/Orchard.Users/Activities/VerifyUserUnicityActivity.cs new file mode 100644 index 000000000..2b5a90edc --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Users/Activities/VerifyUserUnicityActivity.cs @@ -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 GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) { + return new[] { + T("Unique"), + T("NotUnique") + }; + } + + public override IEnumerable Execute(WorkflowContext workflowContext, ActivityContext activityContext) { + var userName = activityContext.GetState("UserName"); + var email = activityContext.GetState("Email"); + + if (_userService.VerifyUserUnicity(userName, email)) { + yield return T("Unique"); + } + else { + yield return T("NotUnique"); + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Forms/CreateUserForm.cs b/src/Orchard.Web/Modules/Orchard.Users/Forms/CreateUserForm.cs new file mode 100644 index 000000000..989110a4a --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Users/Forms/CreateUserForm.cs @@ -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) {} + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Forms/VerifyUserUnicityForm.cs b/src/Orchard.Web/Modules/Orchard.Users/Forms/VerifyUserUnicityForm.cs new file mode 100644 index 000000000..590a9c980 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Users/Forms/VerifyUserUnicityForm.cs @@ -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) {} + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj b/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj index b63d52f38..bee157c2e 100644 --- a/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj +++ b/src/Orchard.Web/Modules/Orchard.Users/Orchard.Users.csproj @@ -67,12 +67,17 @@ + + + + + @@ -124,6 +129,10 @@ {9916839C-39FC-4CEB-A5AF-89CA7E87119F} Orchard.Core + + {642a49d7-8752-4177-80d6-bfbbcfad3de0} + Orchard.Forms + {7059493c-8251-4764-9c1e-2368b8b485bc} Orchard.Workflows