mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-04-05 21:01:35 +08:00
Creating AssignRole activity
--HG-- branch : 1.x
This commit is contained in:
parent
4c694ab97d
commit
4971ec51fb
@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Data;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Roles.Models;
|
||||
using Orchard.Roles.Services;
|
||||
using Orchard.Workflows.Models;
|
||||
using Orchard.Workflows.Services;
|
||||
|
||||
namespace Orchard.Workflows.Activities {
|
||||
public class AssignRoleActivity : Task {
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IRepository<UserRolesPartRecord> _repository;
|
||||
private readonly IRoleService _roleService;
|
||||
|
||||
public AssignRoleActivity(
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
IRepository<UserRolesPartRecord> repository,
|
||||
IRoleService roleService) {
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_repository = repository;
|
||||
_roleService = roleService;
|
||||
T = NullLocalizer.Instance;
|
||||
Logger = NullLogger.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
public override string Name {
|
||||
get { return "AssignRole"; }
|
||||
}
|
||||
|
||||
public override LocalizedString Category {
|
||||
get { return T("User"); }
|
||||
}
|
||||
|
||||
public override LocalizedString Description {
|
||||
get { return T("Assign specific roles to the current content item if it's a user."); }
|
||||
}
|
||||
|
||||
public override string Form {
|
||||
get { return "SelectRoles"; }
|
||||
}
|
||||
|
||||
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<IUserRoles>();
|
||||
|
||||
// if the current workflow subject is not a user, use current user
|
||||
if (user == null) {
|
||||
user = _workContextAccessor.GetContext().CurrentUser.As<IUserRoles>();
|
||||
}
|
||||
|
||||
var roles = GetRoles(activityContext);
|
||||
|
||||
if (user != null) {
|
||||
foreach (var role in roles) {
|
||||
if (!user.Roles.Contains(role)) {
|
||||
var roleRecord = _roleService.GetRoleByName(role);
|
||||
if (roleRecord != null) {
|
||||
_repository.Create(new UserRolesPartRecord {UserId = user.Id, Role = roleRecord});
|
||||
}
|
||||
else {
|
||||
Logger.Debug("Role not found: {0}", role);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
yield return T("Done");
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetRoles(ActivityContext context) {
|
||||
var roles = context.GetState<string>("Roles");
|
||||
|
||||
if (String.IsNullOrEmpty(roles)) {
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
return roles.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ using System.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Roles.Models;
|
||||
using Orchard.Roles.Services;
|
||||
using Orchard.Security;
|
||||
using Orchard.Workflows.Models;
|
||||
using Orchard.Workflows.Services;
|
||||
@ -12,13 +11,9 @@ using Orchard.Workflows.Services;
|
||||
namespace Orchard.Workflows.Activities {
|
||||
public class UserTaskActivity : Event {
|
||||
private readonly IWorkContextAccessor _workContextAccessor;
|
||||
private readonly IRoleService _roleService;
|
||||
|
||||
public UserTaskActivity(
|
||||
IWorkContextAccessor workContextAccessor,
|
||||
IRoleService roleService) {
|
||||
public UserTaskActivity(IWorkContextAccessor workContextAccessor) {
|
||||
_workContextAccessor = workContextAccessor;
|
||||
_roleService = roleService;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
@ -41,9 +36,7 @@ namespace Orchard.Workflows.Activities {
|
||||
}
|
||||
|
||||
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
|
||||
foreach (var action in GetActions(activityContext)) {
|
||||
yield return T(action);
|
||||
}
|
||||
return GetActions(activityContext).Select(action => T(action));
|
||||
}
|
||||
|
||||
public override bool CanExecute(WorkflowContext workflowContext, ActivityContext activityContext) {
|
||||
@ -103,24 +96,24 @@ namespace Orchard.Workflows.Activities {
|
||||
|
||||
private IEnumerable<string> GetRoles(ActivityContext context) {
|
||||
|
||||
string roles = context.GetState<string>("Roles");
|
||||
var roles = context.GetState<string>("Roles");
|
||||
|
||||
if (String.IsNullOrEmpty(roles)) {
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
return roles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
|
||||
return roles.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetActions(ActivityContext context) {
|
||||
|
||||
string actions = context.GetState<string>("Actions");
|
||||
var actions = context.GetState<string>("Actions");
|
||||
|
||||
if (String.IsNullOrEmpty(actions)) {
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
|
||||
return actions.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
|
||||
return actions.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -119,6 +119,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Activities\ContentActivity.cs" />
|
||||
<Compile Include="Activities\BranchActivity.cs" />
|
||||
<Compile Include="Activities\AssignRoleActivity.cs" />
|
||||
<Compile Include="Activities\MergeBranchActivity.cs" />
|
||||
<Compile Include="Activities\DeleteActivity.cs" />
|
||||
<Compile Include="Activities\ExclusiveBranchActivity.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user