using System; using System.Collections.Generic; using System.Linq; using Orchard.ContentManagement; using Orchard.Data; using Orchard.Environment.Extensions; using Orchard.Localization; using Orchard.Logging; using Orchard.Roles.Models; using Orchard.Roles.Services; using Orchard.Workflows.Models; using Orchard.Workflows.Services; namespace Orchard.Roles.Activities { [OrchardFeature("Orchard.Roles.Workflows")] public class UnssignRoleActivity : Task { private readonly IWorkContextAccessor _workContextAccessor; private readonly IRepository _repository; private readonly IRoleService _roleService; public UnssignRoleActivity( IWorkContextAccessor workContextAccessor, IRepository 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 "UnassignRole"; } } public override LocalizedString Category { get { return T("User"); } } public override LocalizedString Description { get { return T("Unassign specific roles from the current content item if it's a user."); } } public override string Form { get { return "SelectRoles"; } } 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(); // if the current workflow subject is not a user, use current user if (user == null) { user = _workContextAccessor.GetContext().CurrentUser.As(); } 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) { var currentUserRoleRecord = _repository.Fetch(x => x.UserId == user.Id && x.Role.Id == roleRecord.Id).SingleOrDefault(); if (currentUserRoleRecord != null) { _repository.Delete(currentUserRoleRecord); } } else { Logger.Debug("Role not found: {0}", role); } } } } yield return T("Done"); } private IEnumerable GetRoles(ActivityContext context) { var roles = context.GetState("Roles"); if (String.IsNullOrEmpty(roles)) { return Enumerable.Empty(); } return roles.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList(); } } }