[Rt-commit] rt branch 4.4/validate-value-for-select-cf created. rt-4.4.5-19-g83ca17cf0c

BPS Git Server git at git.bestpractical.com
Wed Jan 5 22:15:49 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 4.4/validate-value-for-select-cf has been created
        at  83ca17cf0c27d9eb3312f5e3d9c5755cc054525c (commit)

- Log -----------------------------------------------------------------
commit 83ca17cf0c27d9eb3312f5e3d9c5755cc054525c
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Jan 5 23:56:05 2022 +0800

    Add CF values on user create
    
    We already add cf values on user update when user logs in via
    externalauth, this commit makes it on user create too.
    
    This is initially for select user cfs, which don't allow arbitrary
    values any more, so we need to make new values valid in advance.

diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index 5c794cb5cb..b4491c7f27 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -204,6 +204,11 @@ sub Create {
         return ( 0, $self->loc('Could not create user') );
     }
 
+    # Add corresponding CustomFieldValue records for custom fields,
+    # the same as in RT::Authen::ExternalAuth::UpdateUserInfo.
+    require RT::Authen::ExternalAuth;
+    RT::Authen::ExternalAuth::AddCustomFieldValue(%args);
+
     # Handle any user CFs
     $self->UpdateObjectCustomFieldValues( %args );
 

commit c85754b3ac01595ca00feba0f8029cec6cc464ae
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 12:10:46 2021 -0500

    Set values for select CFs used in tests

diff --git a/t/api/action-createtickets.t b/t/api/action-createtickets.t
index ceed4231cc..2114426bad 100644
--- a/t/api/action-createtickets.t
+++ b/t/api/action-createtickets.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 use RT;
-use RT::Test tests => 54;
+use RT::Test tests => undef;
 
 
 {
@@ -25,6 +25,8 @@ my ($id, $msg)=  $global_cf->Create( Name => 'GlobalCF',
                                  Type=> 'SelectSingle');
 ok($id, 'Global custom field correctly created');
 
+($id,$msg) = $global_cf->AddValue(Name => 'A Value');
+ok($id,$msg);
 
 my $approvalsq = RT::Queue->new(RT->SystemUser);
 $approvalsq->Create(Name => 'Approvals');
@@ -40,7 +42,8 @@ my $queue_cf = RT::CustomField->new($RT::SystemUser);
 );
 ok($id, 'Queue-specific custom field correctly created');
 
-
+($id,$msg) = $queue_cf->AddValue(Name => 'Another Value');
+ok($id,$msg);
 
 my $approvals = 
 '===Create-Ticket: approval
@@ -266,3 +269,4 @@ foreach my $id ( sort keys %expected ) {
 
 }
 
+done_testing();
diff --git a/t/api/customfield.t b/t/api/customfield.t
index 7dd3c9e0d8..0beaf9ca4f 100644
--- a/t/api/customfield.t
+++ b/t/api/customfield.t
@@ -26,6 +26,11 @@ is($cf->Type, 'Select', "Is a select CF");
 ok($cf->SingleValue, "Also a single-value CF");
 is($cf->MaxValues, 1, "...meaning only one value, max");
 
+for my $value (qw/first second third forth fifth/) {
+    ( $ok, $msg ) = $cf->AddValue( Name => "$value value" );
+    ok( $ok, "Add $value to values: $msg" );
+}
+
 ($ok, $msg) = $cf->SetMaxValues('0');
 ok($ok, "Set to infinite values: $msg");
 is($cf->Type, 'Select', "Still a select CF");

commit 897228c21cdacf5ff3cc0aec02eeb9e2058630eb
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Jan 5 22:55:35 2022 +0800

    Validate cf values in advance before really adding them
    
    ValidateValue is called early in RT::Record::_AddCustomFieldValue,
    before we messing with existing values. Thus if the new value is
    invalid, we can short-circuit as soon as possible.
    
    This is initially to prevent users from setting arbitrary values to
    Select custom fields.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index 5b5221a608..79d90ffc71 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -703,6 +703,18 @@ sub DeleteValue {
     return ($ok, $self->loc("Custom field value deleted"));
 }
 
+=head2 ValidateValue Value
+
+Make sure that the supplied value is valid
+
+=cut
+
+sub ValidateValue {
+    my $self  = shift;
+    my $value = shift;
+    my ($ret) = $self->_CanonicalizeValue( { Content => $value } );
+    return $ret;
+}
 
 =head2 ValidateQueue Queue
 

commit 57c21aababb57063920d96289c6fa9a84621cb28
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Jan 5 22:35:43 2022 +0800

    Support to canonicalize select values
    
    Select values are treated case insensitively, even though previously
    cases were not auto-corrected. E.g. if you set a ticket select cf value
    to "foo" via email or command line(the official version is "Foo"), then
    it shows as "foo" on display page, but "Foo" is actually selected on
    edit pages, which is a bit confusing.
    
    This commit corrects value cases to make it consistent with the
    corresponding CustomFieldValue, to avoid the confusion.

diff --git a/lib/RT/CustomField.pm b/lib/RT/CustomField.pm
index e449c86bbc..5b5221a608 100644
--- a/lib/RT/CustomField.pm
+++ b/lib/RT/CustomField.pm
@@ -1940,6 +1940,28 @@ sub _CanonicalizeValueIPAddressRange {
     return 1;
 }
 
+sub _CanonicalizeValueSelect {
+    my $self = shift;
+    my $args = shift;
+
+    if ( defined $args->{Content} && length $args->{Content} ) {
+        my $system_object = RT::CustomField->new( RT->SystemUser );
+        $system_object->Load( $self->Id );
+        if ( !$system_object->IsExternalValues() ) {
+            my $cfvs = $system_object->Values;
+            $cfvs->Limit( FIELD => 'Name', VALUE => $args->{Content}, CASESENSITIVE => 0 );
+            if ( my $cfv = $cfvs->Next ) {
+                # If user passes "foo" and cfv actually has "Foo", canonicalize it to "Foo".
+                $args->{Content} = $cfv->Name;
+            }
+            else {
+                return ( 0, $self->loc("Content is not a valid value") );
+            }
+        }
+    }
+    return 1;
+}
+
 =head2 MatchPattern STRING
 
 Tests the incoming string against the Pattern of this custom field object

commit 58d306bd8621faa1fe57ab84fee4c29bad999c1b
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Feb 15 10:54:38 2021 -0500

    Add test setting select CF to a value not in values list

diff --git a/t/customfields/single_values.t b/t/customfields/single_values.t
index 42ba479b44..3ea02a15d8 100644
--- a/t/customfields/single_values.t
+++ b/t/customfields/single_values.t
@@ -2,7 +2,7 @@ use warnings;
 use strict;
 
 use RT;
-use RT::Test nodata => 1, tests => 8;
+use RT::Test tests => undef;
 
 
 
@@ -28,9 +28,24 @@ my $t = RT::Ticket->new(RT->SystemUser);
 
 ok($id,$msg);
 is($t->CustomFieldValues($cf->id)->Count, 0, "No values yet");
-$t->AddCustomFieldValue(Field => $cf->id, Value => 'First');
+
+my $ok;
+my $value = 'First';
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => $value);
+ok( $ok, $msg );
 is($t->CustomFieldValues($cf->id)->Count, 1, "One now");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is $value");
+
+$value = 'Second';
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => $value);
+ok( $ok, $msg );
+is($t->CustomFieldValues($cf->id)->Count, 1, "Still one value");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is $value");
 
-$t->AddCustomFieldValue(Field => $cf->id, Value => 'Second');
+($ok, $msg) = $t->AddCustomFieldValue(Field => $cf->id, Value => 'Bogus');
+ok( !$ok, 'Returned false with value not in values list' );
+like( $msg, qr/Invalid value/, 'Message reports invalid value');
 is($t->CustomFieldValues($cf->id)->Count, 1, "Still one");
+is($t->FirstCustomFieldValue($cf->id), $value, "Value is still $value");
 
+done_testing();

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list