1package OpenXPKI::Server::API2::Plugin::Workflow::fail_workflow;
2use OpenXPKI::Server::API2::EasyPlugin;
3
4=head1 NAME
5
6OpenXPKI::Server::API2::Plugin::Workflow::fail_workflow
7
8=cut
9
10# Project modules
11use OpenXPKI::Server::Context qw( CTX );
12use OpenXPKI::Server::API2::Types;
13use OpenXPKI::Server::API2::Plugin::Workflow::Util;
14
15
16
17=head1 COMMANDS
18
19=head2 fail_workflow
20
21B<Parameters>
22
23=over
24
25=item * C<id> I<Int> - workflow ID
26
27=item * C<workflow> I<Str> - workflow type. Default: queried via ID
28
29=item * C<error> I<Str> - error
30
31=item * C<reason> I<Str> - reason
32
33=back
34
35Returns a I<HashRef> with workflow informations like API command L<get_workflow_info|OpenXPKI::Server::API2::Plugin::Workflow::get_workflow_info/get_workflow_info>.
36
37=cut
38command "fail_workflow" => {
39    id       => { isa => 'Int', required => 1, },
40    error    => { isa => 'Str', },
41    reason   => { isa => 'Str', },
42    workflow => { isa => 'AlphaPunct', },
43} => sub {
44    my ($self, $params) = @_;
45
46    my $wf_id   = $params->id;
47    my $reason  = $params->reason;
48    my $error   = $params->error;
49
50    my $util = OpenXPKI::Server::API2::Plugin::Workflow::Util->new;
51
52    CTX('log')->system()->warn('Passing the attribute *workflow* to fail_workflow is deprecated.') if ($params->has_workflow);
53
54    # in case the workflow is in a state where the factory can not load
55    # it, e.g. as the workflow graph has changed we update the database
56    # here and try to reload. As this is in a DBI transaction it won't
57    # be persisted if it does not work
58    CTX('dbi')->update(
59        table => 'workflow',
60        set => { workflow_state => 'FAILURE' },
61        where => { workflow_id => $wf_id },
62    );
63
64    ##! 2: "load workflow"
65    my $workflow = $util->fetch_workflow($wf_id);
66
67    $util->factory->can_access_handle($workflow->type(), 'fail')
68    or OpenXPKI::Exception->throw (
69        message => "I18N_OPENXPKI_UI_WORKFLOW_PROPERTY_ACCESS_NOT_ALLOWED_FOR_ROLE",
70        params => { type => $workflow->type(),  handle => 'fail' }
71    );
72
73    if (!$error) { $error = 'Failed by user'; }
74    if (!$reason) { $reason = 'userfail'; }
75
76    $workflow->set_failed( $error, $reason );
77
78    CTX('log')->workflow()->info(sprintf('Failed workflow %s (type %s) with error %s', $wf_id, $workflow->type(), $error));
79
80    return $util->get_wf_info(workflow => $workflow, with_ui_info => 1);
81};
82
83__PACKAGE__->meta->make_immutable;
84