[#7396] Implements UnassignRoleActivity (#7414)

Fixes #7396
This commit is contained in:
Hermes Sbicego
2016-12-08 22:05:18 +01:00
committed by Sébastien Ros
parent bc66cdb36c
commit 3bda4acee1
6 changed files with 135 additions and 9 deletions

View File

@@ -41,7 +41,7 @@ namespace Orchard.Roles.Activities {
}
public override LocalizedString Description {
get { return T("Assign specific roles to the current content item if it's a user."); }
get { return T("Assign specific roles to the current content item if it's a user."); }
}
public override string Form {
@@ -49,7 +49,7 @@ namespace Orchard.Roles.Activities {
}
public override IEnumerable<LocalizedString> GetPossibleOutcomes(WorkflowContext workflowContext, ActivityContext activityContext) {
return new[] {T("Done")};
return new[] { T("Done") };
}
public override IEnumerable<LocalizedString> Execute(WorkflowContext workflowContext, ActivityContext activityContext) {
@@ -59,7 +59,7 @@ namespace Orchard.Roles.Activities {
if (user == null) {
user = _workContextAccessor.GetContext().CurrentUser.As<IUserRoles>();
}
var roles = GetRoles(activityContext);
if (user != null) {
@@ -67,7 +67,7 @@ namespace Orchard.Roles.Activities {
if (!user.Roles.Contains(role)) {
var roleRecord = _roleService.GetRoleByName(role);
if (roleRecord != null) {
_repository.Create(new UserRolesPartRecord {UserId = user.Id, Role = roleRecord});
_repository.Create(new UserRolesPartRecord { UserId = user.Id, Role = roleRecord });
}
else {
Logger.Debug("Role not found: {0}", role);
@@ -78,7 +78,7 @@ namespace Orchard.Roles.Activities {
yield return T("Done");
}
private IEnumerable<string> GetRoles(ActivityContext context) {
var roles = context.GetState<string>("Roles");
@@ -86,7 +86,8 @@ namespace Orchard.Roles.Activities {
return Enumerable.Empty<string>();
}
return roles.Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
return roles.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToList();
}
}
}
}

View File

@@ -0,0 +1,95 @@
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<UserRolesPartRecord> _repository;
private readonly IRoleService _roleService;
public UnssignRoleActivity(
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 "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<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) {
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<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();
}
}
}

View File

@@ -90,6 +90,7 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\UnassignRoleActivity.cs" />
<Compile Include="Activities\AssignRoleActivity.cs" />
<Compile Include="Activities\IsInRoleActivity.cs" />
<Compile Include="Activities\UserTaskActivity.cs" />
@@ -137,6 +138,7 @@
<ItemGroup>
<Content Include="Module.txt" />
<Content Include="Styles\admin-usertask.css" />
<Content Include="Styles\workflows-activity-unassign-role.css" />
<Content Include="Styles\workflows-activity-assign-role.css" />
<Content Include="Styles\workflows-activity-isinrole.css" />
<Content Include="Styles\workflows-activity-usertask.css" />
@@ -195,6 +197,9 @@
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Activity-UnassignRole.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,17 @@
activity-unassign-role {
width: 32px;
height: 32px;
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AsSDQYi3LJqFgAAAaRJREFUWMPtl7tKA1EQhr9dNIhXVETEylthYyEknYWCYKeFzyNEwRQ+ggipBAkEawsL9REsbARBEnRjTGGhlbGZyHo4N+OJF/CHIZvzzz+ZzM6Zswv/0KMbKAA3YgVZC8U7UQCaiu0G5J2oagJUA/LviA0JRJq1jINPo8f3n5oSKGrWDhx80eBr0zibMAEawJ6hyRIxtckyomkY+N+JTeBO0zwTDl61CrAumkkH/wH3hoAuXme3KZ2VjxRH245ofrKikU/c+Kfv+48n0NXB2M0/UYHQCSRAHsgB42I5YAeo+ZTMtg1dW+8Q6LPE7weOUv4zuuxtCSSWHy9ZDqhIuS6L5tx3Err4GjDoMQ9aGAbqol32uRVjDn7LcyClkRftvk8CJWkmE7/QRgKLor0K0YwDnk2ajjMk10/xdw0cBa8tbYgEplOl1lmsfAeYk89KiAQ22tC0ngfOQvTAV7bhSqiJ2M4gOg09ksvAiKUSo8Cx+NaB2U6cCXUZMlmgVywLbAOP4vMALIU+VeeBC48ZcAJMdfJ4X5MXkEvgGXgBruVFZVUneAM9RhZRUdfKzwAAAABJRU5ErkJggg==');
background-repeat: no-repeat;
background-position: center;
}
.toolbox-task.toolbox-unassign-role {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4AsSDQYi3LJqFgAAAaRJREFUWMPtl7tKA1EQhr9dNIhXVETEylthYyEknYWCYKeFzyNEwRQ+ggipBAkEawsL9REsbARBEnRjTGGhlbGZyHo4N+OJF/CHIZvzzz+ZzM6Zswv/0KMbKAA3YgVZC8U7UQCaiu0G5J2oagJUA/LviA0JRJq1jINPo8f3n5oSKGrWDhx80eBr0zibMAEawJ6hyRIxtckyomkY+N+JTeBO0zwTDl61CrAumkkH/wH3hoAuXme3KZ2VjxRH245ofrKikU/c+Kfv+48n0NXB2M0/UYHQCSRAHsgB42I5YAeo+ZTMtg1dW+8Q6LPE7weOUv4zuuxtCSSWHy9ZDqhIuS6L5tx3Err4GjDoMQ9aGAbqol32uRVjDn7LcyClkRftvk8CJWkmE7/QRgKLor0K0YwDnk2ajjMk10/xdw0cBa8tbYgEplOl1lmsfAeYk89KiAQ22tC0ngfOQvTAV7bhSqiJ2M4gOg09ksvAiKUSo8Cx+NaB2U6cCXUZMlmgVywLbAOP4vMALIU+VeeBC48ZcAJMdfJ4X5MXkEvgGXgBruVFZVUneAM9RhZRUdfKzwAAAABJRU5ErkJggg==');
background-repeat: no-repeat;
background-position: 6px 10px;
}
.toolbox-task.toolbox-unassign-role div {
margin-left: 36px;
}

View File

@@ -0,0 +1,8 @@
@{
var rolesData = (string)Model.State.Roles;
var roles = !String.IsNullOrWhiteSpace(rolesData) ? rolesData.Split(',') : Enumerable.Empty<string>();
var description = roles.Any() ? T("Unassign roles: {0}", String.Join(", ", roles)) : T("No roles specified");
}
<div class="activity-unassign-role" title="@description">
</div>