Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work queue api #1567

Merged
merged 6 commits into from May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/Origam.Server/Common/SessionManager.cs
Expand Up @@ -124,7 +124,7 @@ public SessionStore GetSession(Guid sessionFormIdentifier, bool rootSession)
.GetOrAdd(
sessionFormIdentifier,
guid => throw new SessionExpiredException()
);
);

if (ss == null)
{
Expand Down
88 changes: 88 additions & 0 deletions backend/Origam.Server/Controller/WorkQueueController.cs
@@ -0,0 +1,88 @@
#region license

/*
Copyright 2005 - 2023 Advantage Solutions, s. r. o.

This file is part of ORIGAM (http://www.origam.org).

ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/

#endregion

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Origam.Service.Core;
using Origam.Workbench.Services;

namespace Origam.Server.Controller;

[ApiController]
public class WorkQueueController : ControllerBase
{
private static readonly log4net.ILog log
= log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

[HttpPost]
[Route("workQueue/{workQueueCode}/{commandText}")]
public IActionResult CreateSessionAsync(string workQueueCode,
string commandText, [FromQuery] [Required] Guid workQueueEntryId)
{
if (log.IsDebugEnabled)
{
log.Debug("Processing: " + HttpContext.Request.GetDisplayUrl());
}
HttpContext.Response.ContentType = "application/json";
try
{
IWorkQueueService workQueueService =
ServiceManager.Services.GetService<IWorkQueueService>();
workQueueService.HandleAction(workQueueCode, commandText,
workQueueEntryId);
return Ok();
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error(ex.Message, ex);
}
string output;
if (ex is RuleException ruleException)
{
output = String.Format(
"{{\"Message\" : {0}, \"RuleResult\" : {1}}}",
JsonConvert.SerializeObject(ruleException.Message),
JsonConvert.SerializeObject(ruleException.RuleResult));
}
else if (ex is ArgumentOutOfRangeException argumentException)
{
output = String.Format(
"{{\"Message\" : {0}, \"ParamName\" : {1}, \"ActualValue\" : {2}}}",
JsonConvert.SerializeObject(argumentException.Message),
JsonConvert.SerializeObject(argumentException.ParamName),
JsonConvert.SerializeObject(argumentException.ActualValue));
}
else
{
output = JsonConvert.SerializeObject(ex);
}
return BadRequest(output);
}
}
}
14 changes: 14 additions & 0 deletions backend/Origam.Server/IApplicationBuilderExtensions.cs
Expand Up @@ -107,6 +107,20 @@ public static void UseCustomSpa(this IApplicationBuilder app, string pathToClien
apiBranch.UseMiddleware<UserApiMiddleware>();
});
}
public static void UseWorkQueueApi(this IApplicationBuilder app)
{
app.MapWhen(
context => context.Request.Path.ToString().StartsWith("/workQueue"),
apiBranch =>
{
apiBranch.UseMiddleware<UserApiTokenAuthenticationMiddleware>();
apiBranch.UseMvc(routes =>
{
routes.MapRoute("default", "{controller}/{action=Index}/{id?}");
});
}
);
}

private static bool IsRestrictedUserApiRoute(
StartUpConfiguration startUpConfiguration, HttpContext context)
Expand Down
1 change: 1 addition & 0 deletions backend/Origam.Server/Startup.cs
Expand Up @@ -319,6 +319,7 @@ var authenticationPostProcessor
app.UseIdentityServer();
app.UseMiddleware<FatalErrorMiddleware>();
app.UseUserApi(startUpConfiguration, identityServerConfig);
app.UseWorkQueueApi();
app.UseAuthentication();
app.UseHttpsRedirection();
if (startUpConfiguration.EnableSoapInterface)
Expand Down
2 changes: 1 addition & 1 deletion backend/Origam.Services/IWorkQueueService.cs
Expand Up @@ -54,7 +54,7 @@ public interface IWorkQueueService : IWorkbenchService
void HandleAction(Guid queueId, string queueClass, DataTable selectedRows,
Guid commandType, string command, string param1, string param2,
object errorQueueId);
void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi, string transactionId);
void HandleAction(string workQueueCode, string commandText, Guid queueEntryId);
IDataDocument GenerateNotificationMessage(
Guid notificationTemplateId
, IXmlContainer notificationSource
Expand Down
15 changes: 7 additions & 8 deletions backend/Origam.Workflow/WorkQueue/WorkQueueService.cs
Expand Up @@ -901,10 +901,9 @@ private static void CheckSelectedRowsCountPositive(int count)
}
}

public void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi, string transactionId)
public void HandleAction(string workQueueCode, string commandText, Guid queueEntryId)
{
// get info about queue (from command)
Guid queueId = workQueueUtils.GetQueueId(commandId);
Guid queueId = workQueueUtils.GetQueueId(workQueueCode);
// get all queue data from database (no entries)
WorkQueueData queue = GetQueue(queueId);
// extract WorkQueueClass name and construct WorkQueueClass from name
Expand All @@ -913,7 +912,7 @@ public void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi,

// authorize access from API
IOrigamAuthorizationProvider auth = SecurityManager.GetAuthorizationProvider();
if (calledFromApi && (queueRow.IsApiAccessRolesNull() || !auth.Authorize(SecurityManager.CurrentPrincipal, queueRow.ApiAccessRoles)))
if (queueRow.IsApiAccessRolesNull() || !auth.Authorize(SecurityManager.CurrentPrincipal, queueRow.ApiAccessRoles))
{
throw new RuleException(
String.Format(ResourceUtils.GetString("ErrorWorkQueueApiNotAuthorized"),
Expand All @@ -925,7 +924,7 @@ public void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi,
WorkQueueData.WorkQueueCommandRow commandRow = null;
foreach (WorkQueueData.WorkQueueCommandRow cmd in queue.WorkQueueCommand.Rows)
{
if (cmd.Id == commandId)
if (cmd.Text == commandText)
{
commandRow = cmd;
}
Expand All @@ -935,11 +934,11 @@ public void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi,
{
throw new RuleException(
String.Format(ResourceUtils.GetString("ErrorWorkQueueCommandNotAuthorized"),
commandId, queueId),
commandText, queueId),
RuleExceptionSeverity.High, "commandId", "");
}
// fetch a single queue entry
DataSet queueEntryDS = FetchSingleQueueEntry(wqc, queueEntryId, transactionId);
DataSet queueEntryDS = FetchSingleQueueEntry(wqc, queueEntryId, null);

// call handle action
HandleAction(queueRow, queueRow.WorkQueueClass, queueEntryDS.Tables[0],
Expand All @@ -949,7 +948,7 @@ public void HandleAction(Guid queueEntryId, Guid commandId, bool calledFromApi,
commandRow.IsParam2Null() ? null : commandRow.Param2,
true,
commandRow.IsrefErrorWorkQueueIdNull() ? (object)null : (object)commandRow.refErrorWorkQueueId,
transactionId);
null);
}

public DataRow GetNextItem(string workQueueName, string transactionId,
Expand Down
37 changes: 37 additions & 0 deletions model-tests/ApiCallExamples/workQueueAPI.http
@@ -0,0 +1,37 @@
# Input file for Visual Studio Code REST client:
# https://marketplace.visualstudio.com/items?itemName=humao.rest-client
#
# - install the client
# - set @port variable
# - insert the following into the database
#
# INSERT [dbo].[WorkQueue] ([refRemovalOrigamStateMachineEventTypeId], [RemovalNewValue], [RemovalOldValue], [ReferenceCode], [CreationNewValue], [CreationFieldName], [CreationOldValue], [WorkQueueClass], [Roles], [refCreationOrigamStateMachineEventTypeId], [Name], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [IsActive], [refWorkQueueExternalSourceTypeId], [ExternalSourceUserName], [ExternalSourcePassword], [ExternalSourceConnection], [ExternalSourceLastMessage], [ExternalSourceLastTime], [ExternalSourceState], [IsErrorQueue], [RemovalFieldName], [ReverseLookupFieldValues], [CreationCondition], [RemovalCondition], [IsMessageCountDisplayed], [ApiAccessRoles], [QueueProcessor], [refWorkQueueRetryTypeId], [RetryIntervalSeconds], [MaxRetries], [ExponentialRetryBase], [ThrottlingIntervalSeconds], [ThrottlingItemsPerInterval] ,[EnableThrottling])
# VALUES (NULL, NULL, NULL, N'TestQueue1', NULL, NULL, NULL, N'TestQueue1', N'*', NULL, N'TestQueue1', N'827f974b-ca7c-4c75-9420-c58ab400ac8f', NULL, N'751bd582-2604-4259-b560-6afb8a772fca', getdate(), NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, 1, NULL, NULL, '69460BCF-81D4-4A97-94F7-5A391D16F771', 30, 3, 2, 60, 10, 0)
# INSERT [dbo].[WorkQueueCommand] ([Param1], [refWorkQueueId], [refWorkQueueCommandTypeId], [Text], [Param2], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [Command], [AutoProcessingConditionXPath], [refErrorWorkQueueId], [IsAutoProcessed], [SortOrder], [IsDefault], [IsAutoProcessedWithErrors], [Roles])
# VALUES (NULL, N'751bd582-2604-4259-b560-6afb8a772fca', N'8d4117e0-590b-4495-9fd3-de46d9e768af', N'Delete', NULL, N'827f974b-ca7c-4c75-9420-c58ab400ac8f', NULL, N'6c0212dc-6a53-4b0f-a812-b9ff8987d7f1', getdate(), NULL, NULL, NULL, NULL, 1, 1, 0, 0, N'*')
#
# INSERT [dbo].[WorkQueueEntry] ([b5], [d8], [b1], [g6], [i5], [c2], [s9], [b8], [g5], [g14], [g15], [d2], [c3], [g20], [blob1], [d9], [g11], [g17], [g8], [g18], [s3], [refRel1Id], [b10], [d6], [d1], [c6], [refLockedByBusinessPartnerId], [refRel2Id], [refRel4Id], [f8], [f5], [d5], [f9], [refRel7Id], [refRel5Id], [i2], [refRel3Id], [ErrorText], [m4], [g10], [s2], [b3], [b2], [c5], [m3], [i4], [b6], [g3], [d10], [g7], [f3], [i6], [g2], [m5], [m2], [refId], [s5], [d3], [f7], [b4], [g12], [i8], [c7], [g9], [f10], [b9], [f6], [s4], [d7], [c4], [s8], [i7], [c1], [f2], [i3], [c10], [s7], [s1], [i9], [i1], [g4], [f4], [refWorkQueueId], [m1], [g19], [g13], [c9], [refRel6Id], [f1], [s6], [c8], [g1], [s10], [b7], [i10], [IsLocked], [g16], [d4], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [AttemptCount], [InRetry]) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CAST(N'2023-02-06T17:46:52.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'33015714-c7d3-4ab5-8c2a-a1cccc2b7f8f', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'751BD582-2604-4259-B560-6AFB8A772FCA', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'2fb0244e-edf8-4702-87c1-1120e2ce2040', NULL, NULL, NULL, 0, NULL, NULL, N'56052d2e-0460-4325-a123-1725c1b9c940', NULL, N'2568523f-5c02-4e4c-8261-c8913a72eb1f', CAST(N'2023-02-06T17:46:52.643' AS DateTime), NULL, 0, 0)
# INSERT [dbo].[WorkQueueEntry] ([b5], [d8], [b1], [g6], [i5], [c2], [s9], [b8], [g5], [g14], [g15], [d2], [c3], [g20], [blob1], [d9], [g11], [g17], [g8], [g18], [s3], [refRel1Id], [b10], [d6], [d1], [c6], [refLockedByBusinessPartnerId], [refRel2Id], [refRel4Id], [f8], [f5], [d5], [f9], [refRel7Id], [refRel5Id], [i2], [refRel3Id], [ErrorText], [m4], [g10], [s2], [b3], [b2], [c5], [m3], [i4], [b6], [g3], [d10], [g7], [f3], [i6], [g2], [m5], [m2], [refId], [s5], [d3], [f7], [b4], [g12], [i8], [c7], [g9], [f10], [b9], [f6], [s4], [d7], [c4], [s8], [i7], [c1], [f2], [i3], [c10], [s7], [s1], [i9], [i1], [g4], [f4], [refWorkQueueId], [m1], [g19], [g13], [c9], [refRel6Id], [f1], [s6], [c8], [g1], [s10], [b7], [i10], [IsLocked], [g16], [d4], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [AttemptCount], [InRetry]) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CAST(N'2023-02-06T17:46:52.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'68fa2a98-418a-4c25-b1f7-d7a6a49d554e', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'751BD582-2604-4259-B560-6AFB8A772FCA', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'bd9cd879-7540-4601-9a9c-85169537f2b5', NULL, NULL, NULL, 0, NULL, NULL, N'56052d2e-0460-4325-a123-1725c1b9c940', NULL, N'e0092d42-bc61-40f0-a270-239f5716286d', CAST(N'2023-02-06T17:46:52.643' AS DateTime), NULL, 0, 0)
# INSERT [dbo].[WorkQueueEntry] ([b5], [d8], [b1], [g6], [i5], [c2], [s9], [b8], [g5], [g14], [g15], [d2], [c3], [g20], [blob1], [d9], [g11], [g17], [g8], [g18], [s3], [refRel1Id], [b10], [d6], [d1], [c6], [refLockedByBusinessPartnerId], [refRel2Id], [refRel4Id], [f8], [f5], [d5], [f9], [refRel7Id], [refRel5Id], [i2], [refRel3Id], [ErrorText], [m4], [g10], [s2], [b3], [b2], [c5], [m3], [i4], [b6], [g3], [d10], [g7], [f3], [i6], [g2], [m5], [m2], [refId], [s5], [d3], [f7], [b4], [g12], [i8], [c7], [g9], [f10], [b9], [f6], [s4], [d7], [c4], [s8], [i7], [c1], [f2], [i3], [c10], [s7], [s1], [i9], [i1], [g4], [f4], [refWorkQueueId], [m1], [g19], [g13], [c9], [refRel6Id], [f1], [s6], [c8], [g1], [s10], [b7], [i10], [IsLocked], [g16], [d4], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [AttemptCount], [InRetry]) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CAST(N'2023-02-06T17:46:52.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'60b75b58-bea3-4316-b4d4-726efda675ed', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'751BD582-2604-4259-B560-6AFB8A772FCA', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'f58d222b-7378-49a3-ba60-22ca4db17ff0', NULL, NULL, NULL, 0, NULL, NULL, N'56052d2e-0460-4325-a123-1725c1b9c940', NULL, N'341493be-7a44-4d38-9c9b-77db1e9be283', CAST(N'2023-02-06T17:46:52.643' AS DateTime), NULL, 0, 0)
# INSERT [dbo].[WorkQueueEntry] ([b5], [d8], [b1], [g6], [i5], [c2], [s9], [b8], [g5], [g14], [g15], [d2], [c3], [g20], [blob1], [d9], [g11], [g17], [g8], [g18], [s3], [refRel1Id], [b10], [d6], [d1], [c6], [refLockedByBusinessPartnerId], [refRel2Id], [refRel4Id], [f8], [f5], [d5], [f9], [refRel7Id], [refRel5Id], [i2], [refRel3Id], [ErrorText], [m4], [g10], [s2], [b3], [b2], [c5], [m3], [i4], [b6], [g3], [d10], [g7], [f3], [i6], [g2], [m5], [m2], [refId], [s5], [d3], [f7], [b4], [g12], [i8], [c7], [g9], [f10], [b9], [f6], [s4], [d7], [c4], [s8], [i7], [c1], [f2], [i3], [c10], [s7], [s1], [i9], [i1], [g4], [f4], [refWorkQueueId], [m1], [g19], [g13], [c9], [refRel6Id], [f1], [s6], [c8], [g1], [s10], [b7], [i10], [IsLocked], [g16], [d4], [RecordCreatedBy], [RecordUpdatedBy], [Id], [RecordCreated], [RecordUpdated], [AttemptCount], [InRetry]) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CAST(N'2023-02-06T17:46:52.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'0f12e815-f643-4143-9b13-3c179354b53b', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'751BD582-2604-4259-B560-6AFB8A772FCA', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, N'0f0342e2-cce3-4b3b-81a2-ccbf37ba2c87', NULL, NULL, NULL, 0, NULL, NULL, N'56052d2e-0460-4325-a123-1725c1b9c940', NULL, N'31be390c-b9ec-4a43-8b9d-15ecb0c248c0', CAST(N'2023-02-06T17:46:52.643' AS DateTime), NULL, 0, 0)
#
# - click "Send Request" over the request you want to send

@port = 44357


# Should delete the work queue entry,
# Change "API Access Roles" in Settings -> General -> Work Queues to "*" if you get authorization error
POST https://localhost:{{port}}/workQueue/TestQueue1/Delete/?workQueueEntryId=e0092d42-bc61-40f0-a270-239f5716286d

###
POST https://localhost:{{port}}/workQueue/TestQueue1/Delete/?workQueueEntryId=2568523f-5c02-4e4c-8261-c8913a72eb1f

###
POST https://localhost:{{port}}/workQueue/TestQueue1/Delete/?workQueueEntryId=341493be-7a44-4d38-9c9b-77db1e9be283

###
POST https://localhost:{{port}}/workQueue/TestQueue1/Delete/?workQueueEntryId=31be390c-b9ec-4a43-8b9d-15ecb0c248c0
Authorization: Bearer 81D6D08664B8A5037DDCC92AC75DEDD829BBB4EDBD61AAAF3F961A7C686C1077