Add some if guards around some CStoreEditor results in Actor.pm.
authorJason Stephenson <jstephenson@mvlc.org>
Wed, 21 Sep 2011 16:46:03 +0000 (12:46 -0400)
committerJason Stephenson <jstephenson@mvlc.org>
Wed, 21 Sep 2011 16:46:03 +0000 (12:46 -0400)
Signed-off-by: Jason Stephenson <jstephenson@mvlc.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Actor.pm

index 4710d81..367f6cf 100644 (file)
@@ -88,7 +88,12 @@ sub update_user_setting {
 
     for my $name (keys %$settings) {
         my $val = $$settings{$name};
-        my $set = $e->search_actor_user_setting({usr => $user_id, name => $name})->[0];
+        my $set = $e->search_actor_user_setting({usr => $user_id, name => $name});
+        if ($set && ref($set) eq 'ARRAY') {
+            $set = $set->[0];
+        } else {
+            $set = undef;
+        }
 
         if(defined $val) {
             $val = OpenSRF::Utils::JSON->perl2JSON($val);
@@ -144,7 +149,12 @@ sub set_ou_settings {
             $name,
             {flesh => 1, flesh_fields => {'coust' => ['update_perm']}}
         ]) or return $e->die_event;
-        my $set = $e->search_actor_org_unit_setting({org_unit => $org_id, name => $name})->[0];
+        my $set = $e->search_actor_org_unit_setting({org_unit => $org_id, name => $name});
+        if ($set && ref($set) eq 'ARRAY') {
+            $set = $set->[0];
+        } else {
+            $set = undef;
+        }
 
         # If there is no relevant permission, the default assumption will
         # be, "no, the caller cannot change that value."
@@ -191,7 +201,12 @@ sub user_settings {
 
     sub get_setting {
         my($e, $user_id, $setting) = @_;
-        my $val = $e->search_actor_user_setting({usr => $user_id, name => $setting})->[0];
+        my $val = $e->search_actor_user_setting({usr => $user_id, name => $setting});
+        if ($val && ref($val) eq 'ARRAY') {
+            $val = $val->[0];
+        } else {
+            $val = undef;
+        }
         return undef unless $val; # XXX this should really return undef, but needs testing
         return OpenSRF::Utils::JSON->JSON2perl($val->value);
     }
@@ -1034,8 +1049,12 @@ sub user_retrieve_by_barcode {
     my $e = new_editor(authtoken => $auth);
     return $e->event unless $e->checkauth;
 
-    my $card = $e->search_actor_card({barcode => $barcode})->[0]
-        or return $e->event;
+    my $card = $e->search_actor_card({barcode => $barcode});
+    if (ref($card) eq 'ARRAY' && $card->[0]) {
+        $card = $card->[0];
+    } else {
+        return $e->event;
+    }
 
        my $user = flesh_user($card->usr, $e, $flesh_home_ou);
     return $e->event unless $e->allowed(
@@ -1304,8 +1323,8 @@ sub update_passwd {
         if( $api =~ /username/o ) {
 
             # make sure no one else has this username
-            my $exist = $e->search_actor_user({usrname=>$new_val},{idlist=>1}); 
-            if (@$exist) {
+            my $exist = $e->search_actor_user({usrname=>$new_val},{idlist=>1});
+            if (ref($exist) eq 'ARRAY' && @$exist) {
                 $e->rollback;
                 return new OpenILS::Event('USERNAME_EXISTS');
             }
@@ -1544,7 +1563,13 @@ sub user_fines_summary {
                        $e->allowed('VIEW_USER_FINES_SUMMARY', $user->home_ou);
        }
 
-    return $e->search_money_open_user_summary({usr => $user_id})->[0];
+    my $mous = $e->search_money_open_user_summary({usr => $user_id});
+    if (ref($mous) eq 'ARRAY') {
+        $mous = $mous->[0];
+    } else {
+        $mous = undef;
+    }
+    return $mous;
 }
 
 
@@ -1969,11 +1994,13 @@ sub checked_in_with_fines {
 
 
        my( @lost, @cr, @lo );
-       for my $c (@$open) {
-               push( @lost, $c->id ) if $c->stop_fines eq 'LOST';
-               push( @cr, $c->id ) if $c->stop_fines eq 'CLAIMSRETURNED';
-               push( @lo, $c->id ) if $c->stop_fines eq 'LONGOVERDUE';
-       }
+    if (ref($open) eq 'ARRAY') {
+        for my $c (@$open) {
+            push( @lost, $c->id ) if $c->stop_fines eq 'LOST';
+            push( @cr, $c->id ) if $c->stop_fines eq 'CLAIMSRETURNED';
+            push( @lo, $c->id ) if $c->stop_fines eq 'LONGOVERDUE';
+        }
+    }
 
        return {
                lost            => \@lost,
@@ -2085,6 +2112,7 @@ sub user_transaction_history {
         ]
     );
 
+    return [] if (ref($mbts) ne 'ARRAY');
     return [map {$_->id} @$mbts] if $api =~ /\.ids/;
     return $mbts unless $api =~ /fleshed/;
 
@@ -2175,19 +2203,24 @@ __PACKAGE__->register_method(
     api_name => "open-ils.actor.groups.tree.retrieve",
     notes    => "Returns a list of user groups"
 );
-       
+
 sub retrieve_groups_tree {
        my( $self, $client ) = @_;
-       return new_editor()->search_permission_grp_tree(
+       my $tree = new_editor()->search_permission_grp_tree(
                [
                        { parent => undef},
-                       {       
+                       {
                                flesh                           => -1,
-                               flesh_fields    => { pgt => ["children"] }, 
+                               flesh_fields    => { pgt => ["children"] },
                                order_by                        => { pgt => 'name'}
                        }
                ]
-       )->[0];
+       );
+    if (ref($tree) eq 'ARRAY') {
+        return $tree->[0];
+    } else {
+        return undef;
+    }
 }
 
 
@@ -2303,7 +2336,10 @@ sub register_workstation {
        my $e = new_editor(authtoken=>$authtoken, xact=>1);
        return $e->die_event unless $e->checkauth;
        return $e->die_event unless $e->allowed('REGISTER_WORKSTATION', $owner);
-       my $existing = $e->search_actor_workstation({name => $name})->[0];
+       my $existing = $e->search_actor_workstation({name => $name});
+    if (ref($existing) eq 'ARRAY') {
+        $existing = $existing->[0];
+    }
 
        if( $existing ) {
 
@@ -2706,9 +2742,14 @@ sub slim_tree {
                                select                  => { aou => ["id","shortname", "name"]},
                        }
                ]
-       )->[0];
+       );
+    if (ref($tree) eq 'ARRAY') {
+        $tree = trim_tree($tree->[0]);
+    } else {
+        $tree = undef;
+    }
 
-       return trim_tree($tree);
+       return $tree;
 }
 
 
@@ -2832,7 +2873,9 @@ sub ranged_penalty_thresholds {
         {org_unit => $U->get_org_ancestors($context_org)},
         {order_by => {pgpt => 'id'}}
     ]);
-    $conn->respond($_) for @$list;
+    if (ref($list) eq 'ARRAY') {
+        $conn->respond($_) for @$list;
+    }
     return undef;
 }
 
@@ -3004,7 +3047,7 @@ sub user_opt_in_at_org {
     my $vals = $e->search_actor_usr_org_unit_opt_in(
         {org_unit=>$opt_orgs, usr=>$user_id},{idlist=>1});
 
-    return 1 if @$vals;
+    return 1 if (ref($vals) eq 'ARRAY' && @$vals);
     return 0;
 }
 
@@ -3022,7 +3065,7 @@ sub create_user_opt_in_at_org {
 
        my $e = new_editor(authtoken => $auth, xact=>1);
        return $e->die_event unless $e->checkauth;
-   
+
     # if a specific org unit wasn't passed in, get one based on the defaults;
     if(!$org_id){
         my $wsou = $e->requestor->ws_ou;
@@ -3030,9 +3073,12 @@ sub create_user_opt_in_at_org {
         my $opt_depth = $U->ou_ancestor_setting_value($wsou,'org.patron_opt_default'); 
         # get the org unit at that depth
         my $org = $e->json_query({ 
-            from => [ 'actor.org_unit_ancestor_at_depth', $wsou, $opt_depth ]})->[0];
-
-       $org_id = $org->{id};
+            from => [ 'actor.org_unit_ancestor_at_depth', $wsou, $opt_depth ]});
+        if ($ref($org) eq 'ARRAY') {
+            $org = $org->[0];
+            $org_id = $org->{id};
+        }
+        return $e->event unless($org_id);
     }
 
     my $user = $e->retrieve_actor_user($user_id) or return $e->die_event;
@@ -3093,7 +3139,12 @@ sub verify_user_password {
     if($barcode) {
         my $card = $e->search_actor_card([
             {barcode => $barcode},
-            {flesh => 1, flesh_fields => {ac => ['usr']}}])->[0] or return 0;
+            {flesh => 1, flesh_fields => {ac => ['usr']}}]);
+        if (ref($card) eq 'ARRAY' && @$card) {
+            $card = $card->[0];
+        } else {
+            return 0;
+        }
         $user_by_barcode = $card->usr;
         $user = $user_by_barcode;
     }
@@ -3129,7 +3180,8 @@ sub retrieve_usr_id_via_barcode_or_usrname {
     if($barcode) {
         my $card = $e->search_actor_card([
             {barcode => $barcode},
-            {flesh => 1, flesh_fields => {ac => ['usr']}}])->[0];
+            {flesh => 1, flesh_fields => {ac => ['usr']}}]);
+        $card = $card->[0] if (ref($card) eq 'ARRAY');
         if ($id_as_barcode =~ /^t/i) {
             if (!$card) {
                 $user = $e->retrieve_actor_user($barcode);
@@ -3146,7 +3198,12 @@ sub retrieve_usr_id_via_barcode_or_usrname {
     }
 
     if ($username) {
-        $user_by_username = $e->search_actor_user({usrname => $username})->[0] or return OpenILS::Event->new( 'ACTOR_USR_NOT_FOUND' );
+        $user_by_username = $e->search_actor_user({usrname => $username});
+        if (ref($user_by_username) eq 'ARRAY' && @$user_by_username) {
+            $user_by_username = $user_by_username->[0];
+        } else {
+            return OpenILS::Event->new( 'ACTOR_USR_NOT_FOUND' );
+        }
 
         $user = $user_by_username;
     }
@@ -3176,7 +3233,7 @@ sub merge_users {
 
     # disallow the merge if any subordinate accounts are in collections
     my $colls = $e->search_money_collections_tracker({usr => $user_ids}, {idlist => 1});
-    return OpenILS::Event->new('MERGED_USER_IN_COLLECTIONS', payload => $user_ids) if @$colls;
+    return OpenILS::Event->new('MERGED_USER_IN_COLLECTIONS', payload => $user_ids) if (ref($colls) eq 'ARRAY' && @$colls);
 
     my $master_user = $e->retrieve_actor_user($master_id) or return $e->die_event;
     my $del_addrs = ($U->ou_ancestor_setting_value(
@@ -3699,7 +3756,8 @@ sub request_password_reset {
 
     # Get the user, if any, depending on the input value
     if ($user_id_type eq 'username') {
-        $user = $e->search_actor_user({usrname => $user_id})->[0];
+        $user = $e->search_actor_user({usrname => $user_id});
+        $user = (ref($user) eq 'ARRAY') ? $user->[0] : undef;
         if (!$user) {
             $e->die_event;
             return OpenILS::Event->new( 'ACTOR_USER_NOT_FOUND' );
@@ -3707,20 +3765,21 @@ sub request_password_reset {
     } elsif ($user_id_type eq 'barcode') {
         my $card = $e->search_actor_card([
             {barcode => $user_id},
-            {flesh => 1, flesh_fields => {ac => ['usr']}}])->[0];
-        if (!$card) { 
+            {flesh => 1, flesh_fields => {ac => ['usr']}}]);
+        $card = (ref($card) eq 'ARRAY') ? $card->[0] : undef;
+        if (!$card) {
             $e->die_event;
             return OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
         }
         $user = $card->usr;
     }
-    
+
     # If the user doesn't have an email address, we can't help them
     if (!$user->email) {
         $e->die_event;
         return OpenILS::Event->new('PATRON_NO_EMAIL_ADDRESS');
     }
-    
+
     my $email_must_match = $U->ou_ancestor_setting_value($user->home_ou, 'circ.password_reset_request_requires_matching_email');
     if ($email_must_match) {
         if ($user->email ne $email) {
@@ -3863,7 +3922,7 @@ sub commit_password_reset {
         has_been_reset => 0
     });
 
-    if (!$aupr->[0]) {
+    if (ref($aupr) ne 'ARRAY' || !$aupr->[0]) {
         $e->die_event;
         return OpenILS::Event->new('PATRON_NOT_AN_ACTIVE_PASSWORD_RESET_REQUEST');
     }
@@ -3977,7 +4036,7 @@ sub event_def_opt_in_settings {
         }
     });
 
-    if(@$types) {
+    if(ref($types) eq 'ARRAY' && @$types) {
         $conn->respond($_) for 
             @{$e->search_config_usr_setting_type({name => [map {$_->{name}} @$types]})};
     }
@@ -4127,7 +4186,7 @@ sub user_visible_circs {
         substream => 1
     });
 
-    return undef unless @$data;
+    return undef unless (ref($data) eq 'ARRAY' && @$data);
 
     if ($for_print) {
 
@@ -4324,8 +4383,9 @@ sub get_barcodes {
         }
     );
     if($context =~ /actor/) {
-        my $filter_result = ();
+        my $filter_result = [];
         my $patron;
+        return $filter_result if (ref($db_result) ne 'ARRAY');
         foreach my $result (@$db_result) {
             if($result->{type} eq 'actor') {
                 if($e->requestor->id != $result->{id}) {