import rt 3.8.7
[freeside.git] / rt / share / html / Elements / GnuPG / SignEncryptWidget
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %# 
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 <table>
49 <td><% loc('Sign')%></td>
50 <td><& /Widgets/Form/Boolean:InputOnly, Name => 'Sign', CurrentValue => $self->{'Sign'} &>
51 using <& SelectKeyForSigning, User => $session{'CurrentUser'}->UserObj &>
52 </td>
53 <td><% loc('Encrypt')%></td>
54 <td><& /Widgets/Form/Boolean:InputOnly, Name => 'Encrypt', CurrentValue => $self->{'Encrypt'} &></td>
55 </table>
56 <%ARGS>
57 $self => undef,
58 </%ARGS>
59 <%INIT>
60 return unless $self;
61 </%INIT>
62
63 <%METHOD new>
64 <%ARGS>
65 $Arguments => {}
66 </%ARGS>
67 <%INIT>
68 return undef unless RT->Config->Get('GnuPG')->{'Enable'};
69
70 require RT::Crypt::GnuPG;
71 return { %$Arguments };
72 </%INIT>
73 </%METHOD>
74
75 <%METHOD ShowIssues>
76 <%ARGS>
77 $self => undef,
78 </%ARGS>
79 <%INIT>
80 return unless $self;
81
82 return $m->comp( '/Elements/GnuPG/KeyIssues',
83     Issues => $self->{'GnuPGRecipientsKeyIssues'} || [],
84     SignAddresses => $self->{'GnuPGCanNotSignAs'} || [],
85 );
86 </%INIT>
87 </%METHOD>
88
89
90 <%METHOD Process>
91 <%ARGS>
92 $self => undef
93 $QueueObj => undef
94 $TicketObj => undef
95 </%ARGS>
96 <%INIT>
97 return unless $self;
98
99 $QueueObj ||= $TicketObj->QueueObj if $TicketObj;
100
101 foreach ( qw(Sign Encrypt) ) {
102     $self->{ $_ } = $m->comp( '/Widgets/Form/Boolean:Process',
103         Name => $_,
104         DefaultValue => $QueueObj->$_,
105         Arguments => $self,
106     );
107 }
108 </%INIT>
109 </%METHOD>
110
111 <%METHOD Check>
112 <%ARGS>
113 $self      => undef
114 $Operation => 'Update'
115 $TicketObj => undef
116 $QueueObj  => undef
117 </%ARGS>
118 <%INIT>
119 return 1 unless $self;
120
121 my $checks_failure = 0;
122
123 if ( $self->{'Sign'} ) {
124     $QueueObj ||= $TicketObj->QueueObj
125         if $TicketObj;
126
127     my $address = $self->{'SignUsing'};
128     $address ||= ($self->{'UpdateType'} && $self->{'UpdateType'} eq "private")
129         ? ( $QueueObj->CommentAddress || RT->Config->Get('CommentAddress') )
130         : ( $QueueObj->CorrespondAddress || RT->Config->Get('CorrespondAddress') );
131
132     unless ( RT::Crypt::GnuPG::DrySign( $address ) ) {
133         push @{ $self->{'GnuPGCanNotSignAs'} ||= [] }, $address;
134         $checks_failure = 1;
135     } else {
136         RT::Crypt::GnuPG::UseKeyForSigning( $self->{'SignUsing'} )
137             if $self->{'SignUsing'};
138     }
139 }
140
141 if ( $self->{'Encrypt'} ) {
142
143     my @recipients;
144
145     if ( $Operation eq 'Update' ) {
146         # skip any email addresses that we won't be sending mail to
147         my %squelch = $m->comp(
148             '/Ticket/Elements/PreviewScrips:SquelchRecipients',
149             %$self,
150             TicketObj => $TicketObj
151         );
152
153         @recipients = $m->comp(
154             '/Ticket/Elements/PreviewScrips:GetRecipients',
155             %$self, TicketObj => $TicketObj
156         );
157     }
158     elsif ( $Operation eq 'Create' ) {
159         @recipients = $m->comp(
160             '/Ticket/Elements/PreviewScrips:GetRecipientsOnCreate',
161             %$self,
162         );
163     }
164     else {
165         $RT::Logger->crit('Incorrect operation: '. $Operation );
166     }
167
168     my %seen;
169     @recipients = grep !$seen{ lc $_ }++, @recipients;
170
171     RT::Crypt::GnuPG::UseKeyForEncryption(
172         map { (/^UseKey-(.*)$/)[0] => $self->{ $_ } }
173         grep $self->{ $_ } && /^UseKey-/,
174         keys %$self
175     );
176
177     my ($status, @issues) = RT::Crypt::GnuPG::CheckRecipients( @recipients );
178     push @{ $self->{'GnuPGRecipientsKeyIssues'} ||= [] }, @issues;
179     $checks_failure = 1 unless $status;
180 }
181
182 return $checks_failure ? 0 : 1;
183 </%INIT>
184 </%METHOD>