[Rt-commit] rt branch, 3.999-trunk, updated. b1374d6e97fade790bf5fff54ba5760bc64089df

? sunnavy sunnavy at bestpractical.com
Thu Apr 8 11:14:59 EDT 2010


The branch, 3.999-trunk has been updated
       via  b1374d6e97fade790bf5fff54ba5760bc64089df (commit)
       via  b86edb03d3f4ba4206019bb8f1a1df2ac13a79b3 (commit)
      from  82de95e1909dcfd4eb0ca51004597bbae2343a98 (commit)

Summary of changes:
 lib/RT/Action/CreateTicket.pm       |  113 ++++++++++++++++++++--------------
 lib/RT/Dispatcher.pm                |    8 +++
 lib/RT/View/Ticket/Create.pm        |    1 +
 t/web/crypt-gnupg.t                 |    1 -
 t/web/gnupg-select-keys-on-create.t |  117 +++++-----------------------------
 5 files changed, 94 insertions(+), 146 deletions(-)

- Log -----------------------------------------------------------------
commit b86edb03d3f4ba4206019bb8f1a1df2ac13a79b3
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Apr 7 01:41:46 2010 +0800

    remove debug code

diff --git a/t/web/crypt-gnupg.t b/t/web/crypt-gnupg.t
index 3039da6..c6260a6 100644
--- a/t/web/crypt-gnupg.t
+++ b/t/web/crypt-gnupg.t
@@ -171,7 +171,6 @@ $m->get_ok($baseurl); # ensure that the mail has been processed
 
 @mail = RT::Test->fetch_caught_mails;
 ok(@mail, "got some mail");
-write_file('/tmp/x', @mail);
 for my $mail (@mail) {
     like $mail, qr/Some other content/, "outgoing mail was not encrypted";
     like $mail, qr/-----BEGIN PGP SIGNATURE-----[\s\S]+-----END PGP SIGNATURE-----/, "data has some kind of signature";

commit b1374d6e97fade790bf5fff54ba5760bc64089df
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Apr 8 22:34:17 2010 +0800

    let user select encrypt keys if having issues

diff --git a/lib/RT/Action/CreateTicket.pm b/lib/RT/Action/CreateTicket.pm
index 41b3cbc..d29040b 100644
--- a/lib/RT/Action/CreateTicket.pm
+++ b/lib/RT/Action/CreateTicket.pm
@@ -2,6 +2,7 @@ package RT::Action::CreateTicket;
 use strict;
 use warnings;
 use base 'RT::Action::TicketAction', 'Jifty::Action::Record::Create';
+__PACKAGE__->mk_accessors('watchers');
 
 use RT::Crypt::GnuPG;
 
@@ -144,6 +145,39 @@ sub setup_gnupg {
         render_as     => 'checkbox',
         default_value => $queue->encrypt,
     ));
+
+    my $encrypt_values = $self->encrypt_values;
+    if ( @$encrypt_values > 1 ) {
+
+     # not call $user->preferred_key is because it will set preferred_key if not
+     # defined before
+        my $preferred_key =
+          $self->current_user->user_object->first_attribute('preferred_key');
+        my $default =
+            $preferred_key
+          ? $self->current_user->email . ':' . $preferred_key->content
+          : '';
+        $self->fill_parameter(
+            encrypt_using => (
+                render_as        => 'select',
+                available_values => $encrypt_values,
+                $default && ( grep { $_->{value} eq $default }
+                  @$encrypt_values )
+                ? ( default_value => $default )
+                : (),
+            )
+        );
+    }
+    else {
+        $self->fill_parameter(
+            encrypt_using => (
+                render_as     => 'hidden',
+                default_value => $encrypt_values->[0]
+                ? $encrypt_values->[0]->{value}
+                : '',
+            )
+        );
+    }
 }
 
 sub canonicalize_sign_using {
@@ -173,26 +207,43 @@ sub validate_sign_using {
     return $self->validation_ok('sign');
 }
 
+sub encrypt_values {
+    my $self = shift;
+    my $watchers = $self->watchers;
+    my @keys;
+    for my $watcher (@$watchers) {
+        my %res = RT::Crypt::GnuPG::get_keys_for_encryption($watcher);
+        next unless $res{'info'};
+        push @keys, map {
+            {
+                value   => $watcher . ':' . $_->{'fingerprint'},
+                display => $watcher . ':' . $_->{'fingerprint'}
+                  . _( "(trust: %1)", $_->{'trust_terse'} )
+            }
+          }
+          sort { $b->{'trust_level'} <=> $a->{'trust_level'} }
+          @{ $res{'info'} };
+    }
+    return \@keys;
+}
+
 sub validate_encrypt {
     my $self  = shift;
-    my $crypt = shift;
-
-    return if !$crypt;
+    my $value = shift;
+    return $self->validation_ok('encrypt') unless $value;
 
-    # XXX: this is ugly and broken for multiple recipients
-    my @recipients = grep { length } map { $self->argument_value($_) }
-                     $self->role_group_parameters;
+    my $encrypt_using = $self->argument_value('encrypt_using');
+    if ( $encrypt_using && $encrypt_using =~ /(.+):(.+)/ ) {
+        my ( $email, $key ) = ( $1, $2 );
+        RT::Crypt::GnuPG::use_key_for_encryption( $email => $key );
+    }
 
+    my @watchers =
+      grep { length }
+      map  { $self->argument_value($_) } $self->role_group_parameters;
     my %seen;
-    @recipients = grep !$seen{ lc $_ }++, @recipients;
-
-    RT::Crypt::GnuPG::use_key_for_encryption(
-        map { (/^UseKey-(.*)$/)[0] => $self->argument_value($_) }
-        grep $self->argument_value($_) && /^UseKey-/,
-        keys %{ $self->arguments },
-    );
-
-    my ($ok, @issues) = RT::Crypt::GnuPG::check_recipients( @recipients );
+    @watchers = grep !$seen{ lc $_ }++, @watchers;
+    my ( $ok, @issues ) = RT::Crypt::GnuPG::check_recipients(@watchers);
     push @{ $self->{'GnuPGRecipientsKeyIssues'} ||= [] }, @issues;
     if ($ok) {
         return $self->validation_ok('encrypt');
@@ -200,41 +251,11 @@ sub validate_encrypt {
     else {
         return $self->validation_error(
             encrypt => join ',',
-            map { $_->{message} } @issues
+            map { _( $_->{message} ) } @issues
         );
     }
 }
 
-sub select_key_for_encryption {
-    my $self    = shift;
-    my $email   = shift;
-    my $default = shift;
-
-    my %res = RT::Crypt::GnuPG::get_keys_for_encryption($email);
-
-    # move the preferred key to the top of the list
-    my $d;
-    my @keys = map {
-                   $_->{'fingerprint'} eq ( $default || '' )
-                       ?  do { $d = $_; () }
-                       : $_
-               }
-               @{ $res{'info'} };
-
-    @keys = sort { $b->{'trust_level'} <=> $a->{'trust_level'} } @keys;
-
-    unshift @keys, $d if defined $d;
-
-    return map {
-        my $display = _("%1 (trust: %2)", $_->{fingerprint}, $_->{trust_terse});
-
-        {
-            value   => $_->{fingerprint},
-            display => $display,
-        }
-    } @keys;
-}
-
 sub set_initial_priority {
     my $self  = shift;
     my $queue = shift;
diff --git a/lib/RT/Dispatcher.pm b/lib/RT/Dispatcher.pm
index b149892..2f2e221 100644
--- a/lib/RT/Dispatcher.pm
+++ b/lib/RT/Dispatcher.pm
@@ -867,6 +867,14 @@ on '/ticket/create' => run {
     }
     else {
         set(queue => $queue);
+        if ($action) {
+            # this is for gpg encryption
+            my @watchers;
+            for my $w (qw/requestors cc admin_cc/) {
+                push @watchers, $action->argument($w) || ();
+            }
+            set( watchers => \@watchers );
+        }
         show '/ticket/create';
     }
 };
diff --git a/lib/RT/View/Ticket/Create.pm b/lib/RT/View/Ticket/Create.pm
index 708fe6b..e4c0c0c 100644
--- a/lib/RT/View/Ticket/Create.pm
+++ b/lib/RT/View/Ticket/Create.pm
@@ -60,6 +60,7 @@ template 'create' => page { title => _('Create a new ticket') } content {
         class   => 'CreateTicket',
         moniker => 'create_ticket',
     );
+    $create->watchers(get('watchers'));
     $create->set_queue($queue);
 
     with ( name => 'ticket_create' ), form {
diff --git a/t/web/gnupg-select-keys-on-create.t b/t/web/gnupg-select-keys-on-create.t
index 6e60de7..3e7931d 100644
--- a/t/web/gnupg-select-keys-on-create.t
+++ b/t/web/gnupg-select-keys-on-create.t
@@ -10,7 +10,7 @@ plan skip_all => 'GnuPG required.'
 plan skip_all => 'gpg executable is required.'
     unless RT::Test->find_executable('gpg');
 
-plan tests => 61;
+plan tests => 49;
 
 use RT::ScripAction::SendEmail;
 use File::Temp qw(tempdir);
@@ -91,21 +91,10 @@ diag "check that things don't work if there is no key" if $ENV{TEST_VERBOSE};
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There is no key suitable for encryption/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok !$form->find_input( 'UseKey-rt-test at example.com' ), 'no key selector';
-    }
-    else {
-        fail("there should have been a validation error");
-    }
-
     my @mail = RT::Test->fetch_caught_mails;
     ok !@mail, 'there are no outgoing emails';
 }
@@ -131,33 +120,10 @@ diag "check that things still doesn't work if key is not trusted" if $ENV{TEST_V
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There is one suitable key, but trust level is not set/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok my $input = $form->find_input( 'UseKey-rt-test at example.com' ), 'found key selector';
-        is scalar $input->possible_values, 1, 'one option';
-    }
-    else {
-        fail("could not find create_ticket form");
-    }
-
-    $m->select( 'UseKey-rt-test at example.com' => $fpr1 );
-    $m->submit;
-    $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
-        qr/Selected key either is not trusted/i,
-        'problems with keys'
-    );
-
     my @mail = RT::Test->fetch_caught_mails;
     ok !@mail, 'there are no outgoing emails';
 }
@@ -183,33 +149,10 @@ diag "check that things still doesn't work if two keys are not trusted" if $ENV{
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There are several keys suitable for encryption/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok my $input = $form->find_input( 'UseKey-rt-test at example.com' ), 'found key selector';
-        is scalar $input->possible_values, 2, 'two options';
-    }
-    else {
-        fail("there should have been a validation error");
-    }
-
-    $m->select( 'UseKey-rt-test at example.com' => $fpr1 );
-    $m->submit;
-    $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
-        qr/Selected key either is not trusted/i,
-        'problems with keys'
-    );
-
     my @mail = RT::Test->fetch_caught_mails;
     ok !@mail, 'there are no outgoing emails';
 }
@@ -234,22 +177,10 @@ diag "check that we see key selector even if only one key is trusted but there a
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There are several keys suitable for encryption/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok my $input = $form->find_input( 'UseKey-rt-test at example.com' ), 'found key selector';
-        is scalar $input->possible_values, 2, 'two options';
-    }
-    else {
-        fail("there should have been a validation error");
-    }
-
     my @mail = RT::Test->fetch_caught_mails;
     ok !@mail, 'there are no outgoing emails';
 }
@@ -267,25 +198,21 @@ diag "check that key selector works and we can select trusted key"
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There are several keys suitable for encryption/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok my $input = $form->find_input( 'UseKey-rt-test at example.com' ), 'found key selector';
-        is scalar $input->possible_values, 2, 'two options';
-    }
-    else {
-        fail("there should have been a validation error");
-    }
-
-    $m->select( 'UseKey-rt-test at example.com' => $fpr1 );
+    $m->fill_in_action_ok(
+        create_ticket => (
+            encrypt       => 1,
+            encrypt_using => "rt-test\@example.com:$fpr1",
+            requestors    => 'rt-test at example.com',
+            content       => 'Some content',
+        )
+    );
     $m->submit;
-    $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
+
+    $m->content_like( qr/Created ticket #\d+ in queue/i, 'ticket created' );
 
     my @mail = RT::Test->fetch_caught_mails;
     ok @mail, 'there are some emails';
@@ -301,29 +228,21 @@ diag "check encrypting of attachments" if $ENV{TEST_VERBOSE};
         encrypt => 1,
         requestors => 'rt-test at example.com',
         content => 'Some content',
-        attach => $0,
     ));
     $m->submit;
     $m->content_like(
-        qr/You are going to encrypt outgoing email messages/i,
-        'problems with keys'
-    );
-    $m->content_like(
         qr/There are several keys suitable for encryption/i,
         'problems with keys'
     );
 
-    if (my $form = $m->form_name('create_ticket')) {
-        ok my $input = $form->find_input( 'UseKey-rt-test at example.com' ), 'found key selector';
-        is scalar $input->possible_values, 2, 'two options';
-    }
-    else {
-        fail("there should have been a validation error");
-    }
-
-    $m->select( 'UseKey-rt-test at example.com' => $fpr1 );
+    $m->fill_in_action_ok(create_ticket => (
+        encrypt => 1,
+        encrypt_using => "rt-test\@example.com:$fpr1",
+        requestors => 'rt-test at example.com',
+        content => 'Some content',
+    ));
     $m->submit;
-    $m->content_like( qr/Ticket \d+ created in queue/i, 'ticket created' );
+    $m->content_like( qr/Created ticket #\d+ in queue/i, 'ticket created' );
 
     my @mail = RT::Test->fetch_caught_mails;
     ok @mail, 'there are some emails';

-----------------------------------------------------------------------


More information about the Rt-commit mailing list