1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
6 use Digest::MD5 qw(md5_hex);
8 use OpenSRF::EX qw(:try);
12 use OpenILS::Application::AppUtils;
13 use OpenILS::Utils::Fieldmapper;
14 use OpenILS::Application::Search::Actor;
15 use OpenILS::Utils::ModsParser;
17 use OpenSRF::Utils::Cache;
19 my $apputils = "OpenILS::Application::AppUtils";
21 sub _d { warn "Patron:\n" . Dumper(shift()); }
26 my $set_user_settings;
29 __PACKAGE__->register_method(
30 method => "set_user_settings",
31 api_name => "open-ils.actor.patron.settings.update",
33 sub set_user_settings {
34 my( $self, $client, $user_session, $uid, $settings ) = @_;
36 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
38 if (ref($uid) eq 'HASH') {
42 $uid ||= $user_obj->id;
44 if( $user_obj->id != $uid ) {
45 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
46 return OpenILS::Perm->new("UPDATE_USER");
50 # $set_user_settings ||=
51 # $self->method_lookup('open-ils.storage.direct.actor.user_setting.batch.merge');
53 # return $set_user_settings->run(map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
55 return $apputils->simple_scalar_request(
57 'open-ils.storage.direct.actor.user_setting.batch.merge',
58 map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings );
65 __PACKAGE__->register_method(
66 method => "set_ou_settings",
67 api_name => "open-ils.actor.org_unit.settings.update",
70 my( $self, $client, $user_session, $ouid, $settings ) = @_;
72 throw OpenSRF::EX::InvalidArg ("OrgUnit ID and Settings hash required for setting OrgUnit settings") unless ($ouid && $settings);
74 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
76 if($apputils->check_user_perms($user_obj->id, $ouid, "UPDATE_ORG_UNIT")) {
77 return OpenILS::Perm->new("UPDATE_ORG_UNIT");
82 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.merge');
84 return $set_ou_settings->run(map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
90 my $fetch_user_settings;
91 my $fetch_ou_settings;
93 __PACKAGE__->register_method(
94 method => "user_settings",
95 api_name => "open-ils.actor.patron.settings.retrieve",
98 my( $self, $client, $user_session, $uid ) = @_;
100 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
102 $uid ||= $user_obj->id;
104 if( $user_obj->id != $uid ) {
105 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
106 return OpenILS::Perm->new("VIEW_USER");
110 warn "fetching user settings for $uid...\n";
111 my $s = $apputils->simple_scalar_request(
113 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
115 return { map { ($_->name,$_->value) } @$s };
120 __PACKAGE__->register_method(
121 method => "ou_settings",
122 api_name => "open-ils.actor.org_unit.settings.retrieve",
125 my( $self, $client, $ouid ) = @_;
127 throw OpenSRF::EX::InvalidArg ("OrgUnit ID required for lookup of OrgUnit settings") unless ($ouid);
129 $fetch_ou_settings ||=
130 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic');
132 my ($s) = $fetch_ou_settings->run($ouid);
134 return { map { ($_->name,$_->value) } @$s };
139 __PACKAGE__->register_method(
140 method => "update_patron",
141 api_name => "open-ils.actor.patron.update",);
144 my( $self, $client, $user_session, $patron ) = @_;
146 my $session = $apputils->start_db_session();
149 warn $user_session . " " . $patron . "\n";
153 OpenILS::Application::AppUtils->check_user_session(
154 $user_session ); #throws EX on error
156 # XXX does this user have permission to add/create users. Granularity?
157 # $new_patron is the patron in progress. $patron is the original patron
158 # passed in with the method. new_patron will change as the components
159 # of patron are added/updated.
163 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
164 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
165 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
167 # create/update the patron first so we can use his id
168 if($patron->isnew()) {
170 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
172 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
173 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
174 $client->respond_complete($new_patron->ex);
178 } else { $new_patron = $patron; }
180 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
182 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
183 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
184 $client->respond_complete($new_patron->ex);
188 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
190 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
191 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
192 $client->respond_complete($new_patron->ex);
196 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
197 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
198 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
199 $client->respond_complete($new_patron->ex);
204 # re-update the patron if anything has happened to him during this process
205 if($new_patron->ischanged()) {
206 $new_patron = _update_patron($session, $new_patron, $user_obj);
208 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
209 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
210 $client->respond_complete($new_patron->ex);
215 $apputils->commit_db_session($session);
217 $session = OpenSRF::AppSession->create("open-ils.storage");
218 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
219 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
220 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
221 $client->respond_complete($new_patron->ex);
226 warn "Patron Update/Create complete\n";
227 return flesh_user($new_patron->id());
233 __PACKAGE__->register_method(
234 method => "user_retrieve_fleshed_by_id",
235 api_name => "open-ils.actor.user.fleshed.retrieve",);
237 sub user_retrieve_fleshed_by_id {
238 my( $self, $client, $user_session, $user_id ) = @_;
239 my $user_obj = $apputils->check_user_session( $user_session );
241 if(!defined($user_id)) { $user_id = $user_obj->id; }
243 if( $user_obj->id ne $user_id ) {
244 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
245 return OpenILS::Perm->new("VIEW_USER");
249 return flesh_user($user_id);
260 $session = OpenSRF::AppSession->create("open-ils.storage");
264 # grab the user with the given id
265 my $ureq = $session->request(
266 "open-ils.storage.direct.actor.user.retrieve", $id);
267 my $user = $ureq->gather(1);
269 if(!$user) { return undef; }
272 my $cards_req = $session->request(
273 "open-ils.storage.direct.actor.card.search.usr.atomic",
275 $user->cards( $cards_req->gather(1) );
277 for my $c(@{$user->cards}) {
278 if($c->id == $user->card || $c->id eq $user->card ) {
279 warn "Setting my card to " . $c->id . "\n";
284 my $add_req = $session->request(
285 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
287 $user->addresses( $add_req->gather(1) );
289 for my $c(@{$user->addresses}) {
290 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
291 warn "Setting my address to " . $c->id . "\n";
292 $user->billing_address($c);
296 for my $c(@{$user->addresses}) {
297 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
298 warn "Setting my address to " . $c->id . "\n";
299 $user->mailing_address($c);
303 my $stat_req = $session->request(
304 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
306 $user->stat_cat_entries($stat_req->gather(1));
308 if($kill) { $session->disconnect(); }
309 $user->clear_passwd();
317 # clone and clear stuff that would break the database
321 my $new_patron = $patron->clone;
323 # Using the Fieldmapper clone method
324 #my $new_patron = Fieldmapper::actor::user->new();
326 #my $fmap = $Fieldmapper::fieldmap;
327 #no strict; # shallow clone, may be useful in the fieldmapper
329 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
330 # $new_patron->$field( $patron->$field() );
335 $new_patron->clear_billing_address();
336 $new_patron->clear_mailing_address();
337 $new_patron->clear_addresses();
338 $new_patron->clear_card();
339 $new_patron->clear_cards();
340 $new_patron->clear_id();
341 $new_patron->clear_isnew();
342 $new_patron->clear_ischanged();
343 $new_patron->clear_isdeleted();
344 $new_patron->clear_stat_cat_entries();
353 my $user_obj = shift;
356 if($apputils->check_user_perms(
357 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
358 return OpenILS::Perm->new("CREATE_USER");
361 warn "Creating new patron\n";
364 my $req = $session->request(
365 "open-ils.storage.direct.actor.user.create",$patron);
366 my $id = $req->gather(1);
368 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
371 # retrieve the patron from the db to collect defaults
372 my $ureq = $session->request(
373 "open-ils.storage.direct.actor.user.retrieve",
376 warn "Created new patron with id $id\n";
378 return $ureq->gather(1);
383 my( $session, $patron, $user_obj) = @_;
386 if($patron->id ne $user_obj->id) {
387 if($apputils->check_user_perms(
388 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
389 return OpenILS::Perm->new("UPDATE_USER");
393 warn "updating patron " . Dumper($patron) . "\n";
395 my $req = $session->request(
396 "open-ils.storage.direct.actor.user.update",$patron );
397 my $status = $req->gather(1);
398 if(!defined($status)) {
399 throw OpenSRF::EX::ERROR
400 ("Unknown error updating patron");
406 sub _add_update_addresses {
409 my $new_patron = shift;
411 my $current_id; # id of the address before creation
413 for my $address (@{$patron->addresses()}) {
415 $address->usr($new_patron->id());
417 if(ref($address) and $address->isnew()) {
418 warn "Adding new address at street " . $address->street1() . "\n";
420 $current_id = $address->id();
421 $address = _add_address($session,$address);
423 if( $patron->billing_address() and
424 $patron->billing_address() == $current_id ) {
425 $new_patron->billing_address($address->id());
426 $new_patron->ischanged(1);
429 if( $patron->mailing_address() and
430 $patron->mailing_address() == $current_id ) {
431 $new_patron->mailing_address($address->id());
432 $new_patron->ischanged(1);
435 } elsif( ref($address) and $address->ischanged() ) {
436 warn "Updating address at street " . $address->street1();
437 $address->usr($new_patron->id());
438 _update_address($session,$address);
440 } elsif( ref($address) and $address->isdeleted() ) {
441 warn "Deleting address at street " . $address->street1();
443 if( $address->id() == $new_patron->mailing_address() ) {
444 $new_patron->clear_mailing_address();
445 _update_patron($session, $new_patron);
448 if( $address->id() == $new_patron->billing_address() ) {
449 $new_patron->clear_billing_address();
450 _update_patron($session, $new_patron);
453 _delete_address($session,$address);
461 # adds an address to the db and returns the address with new id
463 my($session, $address) = @_;
464 $address->clear_id();
467 warn "Adding Address:\n";
468 warn Dumper($address);
470 # put the address into the database
471 my $req = $session->request(
472 "open-ils.storage.direct.actor.user_address.create",
476 my $id = $req->gather(1);
478 throw OpenSRF::EX::ERROR
479 ("Unable to create new user address");
482 warn "Created address with id $id\n";
484 # update all the necessary id's
490 sub _update_address {
491 my( $session, $address ) = @_;
492 my $req = $session->request(
493 "open-ils.storage.direct.actor.user_address.update",
495 my $status = $req->gather(1);
496 if(!defined($status)) {
497 throw OpenSRF::EX::ERROR
498 ("Unknown error updating address");
505 sub _add_update_cards {
509 my $new_patron = shift;
511 my $virtual_id; #id of the card before creation
512 for my $card (@{$patron->cards()}) {
514 $card->usr($new_patron->id());
516 if(ref($card) and $card->isnew()) {
518 $virtual_id = $card->id();
519 $card = _add_card($session,$card);
520 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
524 #if(ref($patron->card)) { $patron->card($patron->card->id); }
525 if($patron->card() == $virtual_id) {
526 $new_patron->card($card->id());
527 $new_patron->ischanged(1);
530 } elsif( ref($card) and $card->ischanged() ) {
531 $card->usr($new_patron->id());
532 _update_card($session, $card);
539 # adds an card to the db and returns the card with new id
541 my( $session, $card ) = @_;
544 warn "Adding card with barcode " . $card->barcode() . "\n";
545 my $req = $session->request(
546 "open-ils.storage.direct.actor.card.create",
549 my $id = $req->gather(1);
551 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
555 warn "Created patron card with id $id\n";
561 my( $session, $card ) = @_;
564 my $req = $session->request(
565 "open-ils.storage.direct.actor.card.update",
567 my $status = $req->gather(1);
568 if(!defined($status)) {
569 throw OpenSRF::EX::ERROR
570 ("Unknown error updating card");
578 sub _delete_address {
579 my( $session, $address ) = @_;
581 warn "Deleting address " . $address->street1() . "\n";
583 my $req = $session->request(
584 "open-ils.storage.direct.actor.user_address.delete",
586 my $status = $req->gather(1);
587 if(!defined($status)) {
588 throw OpenSRF::EX::ERROR
589 ("Unknown error updating address");
591 warn "Delete address status is $status\n";
596 sub _add_survey_responses {
597 my ($session, $patron, $new_patron) = @_;
599 warn "updating responses for user " . $new_patron->id . "\n";
601 my $responses = $patron->survey_responses;
605 for my $resp( @$responses ) {
606 $resp->usr($new_patron->id);
609 my $status = $apputils->simple_scalar_request(
611 "open-ils.circ.survey.submit.user_id",
620 sub _create_stat_maps {
622 my($session, $user_session, $patron, $new_patron) = @_;
624 my $maps = $patron->stat_cat_entries();
626 for my $map (@$maps) {
628 next unless($map->isnew() || $map->ischanged());
630 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
632 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
636 $map->target_usr($new_patron->id);
638 warn "Updating stat entry with method $method and session $user_session and map $map\n";
640 my $req = $session->request($method, $map);
641 my $status = $req->gather(1);
646 throw OpenSRF::EX::ERROR
647 ("Error updating stat map with method $method");
657 __PACKAGE__->register_method(
658 method => "search_username",
659 api_name => "open-ils.actor.user.search.username",
662 sub search_username {
663 my($self, $client, $username) = @_;
664 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
666 "open-ils.storage.direct.actor.user.search.usrname.atomic",
674 __PACKAGE__->register_method(
675 method => "user_retrieve_by_barcode",
676 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
678 sub user_retrieve_by_barcode {
679 my($self, $client, $user_session, $barcode) = @_;
680 warn "Searching for user with barcode $barcode\n";
681 my $user_obj = $apputils->check_user_session( $user_session );
683 my $session = OpenSRF::AppSession->create("open-ils.storage");
685 # find the card with the given barcode
686 my $creq = $session->request(
687 "open-ils.storage.direct.actor.card.search.barcode.atomic",
689 my $card = $creq->gather(1);
691 if(!$card || !$card->[0]) {
692 $session->disconnect();
697 my $user = flesh_user($card->usr(), $session);
698 $session->disconnect();
705 __PACKAGE__->register_method(
706 method => "get_user_by_id",
707 api_name => "open-ils.actor.user.retrieve",);
710 my ($self, $client, $user_session, $id) = @_;
712 my $user_obj = $apputils->check_user_session( $user_session );
714 return $apputils->simple_scalar_request(
716 "open-ils.storage.direct.actor.user.retrieve",
722 __PACKAGE__->register_method(
723 method => "get_org_types",
724 api_name => "open-ils.actor.org_types.retrieve",);
728 my($self, $client) = @_;
730 return $org_types if $org_types;
732 $apputils->simple_scalar_request(
734 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
739 __PACKAGE__->register_method(
740 method => "get_user_profiles",
741 api_name => "open-ils.actor.user.profiles.retrieve",
745 sub get_user_profiles {
746 return $user_profiles if $user_profiles;
748 return $user_profiles =
749 $apputils->simple_scalar_request(
751 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
756 __PACKAGE__->register_method(
757 method => "get_user_ident_types",
758 api_name => "open-ils.actor.user.ident_types.retrieve",
761 sub get_user_ident_types {
762 return $ident_types if $ident_types;
763 return $ident_types =
764 $apputils->simple_scalar_request(
766 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
772 __PACKAGE__->register_method(
773 method => "get_org_unit",
774 api_name => "open-ils.actor.org_unit.retrieve",
779 my( $self, $client, $user_session, $org_id ) = @_;
781 if(defined($user_session) && !defined($org_id)) {
783 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
784 if(!defined($org_id)) {
785 $org_id = $user_obj->home_ou;
790 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
792 "open-ils.storage.direct.actor.org_unit.retrieve",
801 __PACKAGE__->register_method(
802 method => "get_org_tree",
803 api_name => "open-ils.actor.org_tree.retrieve",
805 note => "Returns the entire org tree structure",
809 my( $self, $client) = @_;
812 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
814 # see if it's in the cache
815 warn "Getting ORG Tree\n";
816 my $tree = $cache_client->get_cache('orgtree');
818 warn "Found orgtree in cache. returning...\n";
822 my $orglist = $apputils->simple_scalar_request(
824 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
827 warn "found org list\n";
830 $tree = $self->build_org_tree($orglist);
831 $cache_client->put_cache('orgtree', $tree);
837 # turns an org list into an org tree
840 my( $self, $orglist) = @_;
842 return $orglist unless (
843 ref($orglist) and @$orglist > 1 );
846 $a->ou_type <=> $b->ou_type ||
847 $a->name cmp $b->name } @$orglist;
849 for my $org (@list) {
851 next unless ($org and defined($org->parent_ou));
852 my ($parent) = grep { $_->id == $org->parent_ou } @list;
855 $parent->children([]) unless defined($parent->children);
856 push( @{$parent->children}, $org );
864 __PACKAGE__->register_method(
865 method => "get_org_descendants",
866 api_name => "open-ils.actor.org_tree.descendants.retrieve"
869 # depth is optional. org_unit is the id
870 sub get_org_descendants {
871 my( $self, $client, $org_unit, $depth ) = @_;
872 my $orglist = $apputils->simple_scalar_request(
874 "open-ils.storage.actor.org_unit.descendants.atomic",
876 return $self->build_org_tree($orglist);
880 __PACKAGE__->register_method(
881 method => "get_org_ancestors",
882 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
885 # depth is optional. org_unit is the id
886 sub get_org_ancestors {
887 my( $self, $client, $org_unit, $depth ) = @_;
888 my $orglist = $apputils->simple_scalar_request(
890 "open-ils.storage.actor.org_unit.ancestors.atomic",
892 return $self->build_org_tree($orglist);
896 __PACKAGE__->register_method(
897 method => "get_standings",
898 api_name => "open-ils.actor.standings.retrieve"
903 return $user_standings if $user_standings;
904 return $user_standings =
905 $apputils->simple_scalar_request(
907 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
912 __PACKAGE__->register_method(
913 method => "get_my_org_path",
914 api_name => "open-ils.actor.org_unit.full_path.retrieve"
917 sub get_my_org_path {
918 my( $self, $client, $user_session, $org_id ) = @_;
919 my $user_obj = $apputils->check_user_session($user_session);
920 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
922 return $apputils->simple_scalar_request(
924 "open-ils.storage.actor.org_unit.full_path.atomic",
929 __PACKAGE__->register_method(
930 method => "patron_adv_search",
931 api_name => "open-ils.actor.patron.search.advanced" );
933 sub patron_adv_search {
934 my( $self, $client, $staff_login, $search_hash ) = @_;
937 warn "patron adv with $staff_login and search " .
938 Dumper($search_hash) . "\n";
940 my $session = OpenSRF::AppSession->create("open-ils.storage");
941 my $req = $session->request(
942 "open-ils.storage.actor.user.crazy_search", $search_hash);
944 my $ans = $req->gather(1);
946 my %hash = map { ($_ =>1) } @$ans;
947 $ans = [ keys %hash ];
949 warn "Returning @$ans\n";
951 $session->disconnect();
958 sub _verify_password {
959 my($user_session, $password) = @_;
960 my $user_obj = $apputils->check_user_session($user_session);
962 #grab the user with password
963 $user_obj = $apputils->simple_scalar_request(
965 "open-ils.storage.direct.actor.user.retrieve",
968 if($user_obj->passwd eq $password) {
976 __PACKAGE__->register_method(
977 method => "update_password",
978 api_name => "open-ils.actor.user.password.update");
980 __PACKAGE__->register_method(
981 method => "update_password",
982 api_name => "open-ils.actor.user.username.update");
984 __PACKAGE__->register_method(
985 method => "update_password",
986 api_name => "open-ils.actor.user.email.update");
988 sub update_password {
989 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
991 warn "Updating user with method " .$self->api_name . "\n";
992 my $user_obj = $apputils->check_user_session($user_session);
994 if($self->api_name =~ /password/) {
996 #make sure they know the current password
997 if(!_verify_password($user_session, md5_hex($current_password))) {
998 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
1001 $user_obj->passwd($new_value);
1003 elsif($self->api_name =~ /username/) {
1004 $user_obj->usrname($new_value);
1007 elsif($self->api_name =~ /email/) {
1008 warn "Updating email to $new_value\n";
1009 $user_obj->email($new_value);
1012 my $session = $apputils->start_db_session();
1013 $user_obj = _update_patron($session, $user_obj, $user_obj);
1014 $apputils->commit_db_session($session);
1016 if($user_obj) { return 1; }
1021 __PACKAGE__->register_method(
1022 method => "check_user_perms",
1023 api_name => "open-ils.actor.user.perm.check",
1024 notes => <<" NOTES");
1025 Takes a login session, user id, an org id, and an array of perm type strings. For each
1026 perm type, if the user does *not* have the given permission it is added
1027 to a list which is returned from the method. If all permissions
1028 are allowed, an empty list is returned
1029 if the logged in user does not match 'user_id', then the logged in user must
1030 have VIEW_PERMISSION priveleges.
1033 sub check_user_perms {
1034 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1035 my $user_obj = $apputils->check_user_session($login_session);
1037 if($user_obj->id ne $user_id) {
1038 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
1039 return OpenILS::Perm->new("VIEW_PERMISSION");
1044 for my $perm (@$perm_types) {
1045 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1046 push @not_allowed, $perm;
1050 return \@not_allowed
1055 __PACKAGE__->register_method(
1056 method => "user_fines_summary",
1057 api_name => "open-ils.actor.user.fines.summary",
1058 notes => <<" NOTES");
1059 Returns a short summary of the users total open fines, excluding voided fines
1060 Params are login_session, user_id
1061 Returns a 'mous' object.
1064 sub user_fines_summary {
1065 my( $self, $client, $login_session, $user_id ) = @_;
1067 my $user_obj = $apputils->check_user_session($login_session);
1068 if($user_obj->id ne $user_id) {
1069 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1070 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1074 return $apputils->simple_scalar_request(
1076 "open-ils.storage.direct.money.open_user_summary.search.usr",
1084 __PACKAGE__->register_method(
1085 method => "user_transactions",
1086 api_name => "open-ils.actor.user.transactions",
1087 notes => <<" NOTES");
1088 Returns a list of open user transactions (mbts objects);
1089 Params are login_session, user_id
1090 Optional third parameter is the transactions type. defaults to all
1093 __PACKAGE__->register_method(
1094 method => "user_transactions",
1095 api_name => "open-ils.actor.user.transactions.have_charge",
1096 notes => <<" NOTES");
1097 Returns a list of all open user transactions (mbts objects) that have an initial charge
1098 Params are login_session, user_id
1099 Optional third parameter is the transactions type. defaults to all
1102 __PACKAGE__->register_method(
1103 method => "user_transactions",
1104 api_name => "open-ils.actor.user.transactions.have_balance",
1105 notes => <<" NOTES");
1106 Returns a list of all open user transactions (mbts objects) that have a balance
1107 Params are login_session, user_id
1108 Optional third parameter is the transactions type. defaults to all
1111 __PACKAGE__->register_method(
1112 method => "user_transactions",
1113 api_name => "open-ils.actor.user.transactions.fleshed",
1114 notes => <<" NOTES");
1115 Returns an object/hash of transaction, circ, title where transaction = an open
1116 user transactions (mbts objects), circ is the attached circluation, and title
1117 is the title the circ points to
1118 Params are login_session, user_id
1119 Optional third parameter is the transactions type. defaults to all
1122 __PACKAGE__->register_method(
1123 method => "user_transactions",
1124 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1125 notes => <<" NOTES");
1126 Returns an object/hash of transaction, circ, title where transaction = an open
1127 user transactions that has an initial charge (mbts objects), circ is the
1128 attached circluation, and title is the title the circ points to
1129 Params are login_session, user_id
1130 Optional third parameter is the transactions type. defaults to all
1133 __PACKAGE__->register_method(
1134 method => "user_transactions",
1135 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1136 notes => <<" NOTES");
1137 Returns an object/hash of transaction, circ, title where transaction = an open
1138 user transaction that has a balance (mbts objects), circ is the attached
1139 circluation, and title is the title the circ points to
1140 Params are login_session, user_id
1141 Optional third parameter is the transaction type. defaults to all
1146 sub user_transactions {
1147 my( $self, $client, $login_session, $user_id, $type ) = @_;
1149 my $user_obj = $apputils->check_user_session($login_session);
1150 if($user_obj->id ne $user_id) {
1151 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1152 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1156 my $api = $self->api_name();
1159 if(defined($type)) { @xact = (xact_type => $type);
1160 } else { @xact = (); }
1162 if($api =~ /have_charge/) {
1164 $trans = $apputils->simple_scalar_request(
1166 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1167 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1169 } elsif($api =~ /have_balance/) {
1171 $trans = $apputils->simple_scalar_request(
1173 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1174 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1178 $trans = $apputils->simple_scalar_request(
1180 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1181 { usr => $user_id, @xact });
1184 if($api !~ /fleshed/) { return $trans; }
1189 for my $t (@$trans) {
1193 my $circ = $apputils->simple_scalar_request(
1195 "open-ils.storage.direct.action.circulation.retrieve",
1200 my $title = $apputils->simple_scalar_request(
1202 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1203 $circ->target_copy );
1207 my $u = OpenILS::Utils::ModsParser->new();
1208 $u->start_mods_batch($title->marc());
1209 my $mods = $u->finish_mods_batch();
1211 push @resp, {transaction => $t, circ => $circ, record => $mods };
1221 __PACKAGE__->register_method(
1222 method => "retrieve_groups",
1223 api_name => "open-ils.actor.groups.retrieve",
1224 notes => <<" NOTES");
1225 Returns a list of user groups
1227 sub retrieve_groups {
1228 my( $self, $client ) = @_;
1229 return $apputils->simple_scalar_request(
1231 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1285 some old methods that may be good to keep around for now
1288 my( $session, $card ) = @_;
1290 warn "Deleting card with barcode " . $card->barcode() . "\n";
1291 my $req = $session->request(
1292 "open-ils.storage.direct.actor.card.delete",
1294 my $status = $req->gather(1);
1295 if(!defined($status)) {
1296 throw OpenSRF::EX::ERROR
1297 ("Unknown error updating card");
1303 # deletes the patron and any attached addresses and cards
1304 __PACKAGE__->register_method(
1305 method => "delete_patron",
1306 api_name => "open-ils.actor.patron.delete",
1311 my( $self, $client, $patron ) = @_;
1312 my $session = $apputils->start_db_session();
1317 $patron->clear_mailing_address();
1318 $patron->clear_billing_address();
1319 $patron->ischanged(1);
1321 _update_patron($session, $patron);
1322 _delete_address($session,$_) for (@{$patron->addresses()});
1323 _delete_card($session,$_) for (@{$patron->cards()});
1324 _delete_patron($session,$patron);
1325 $apputils->commit_db_session($session);
1327 } catch Error with {
1329 $err = "-*- Failure deleting user: $e";
1330 $apputils->rollback_db_session($session);
1334 if($err) { throw OpenSRF::EX::ERROR ($err); }
1335 warn "Patron Delete complete\n";
1339 sub _delete_patron {
1340 my( $session, $patron ) = @_;
1342 warn "Deleting patron " . $patron->usrname() . "\n";
1344 my $req = $session->request(
1345 "open-ils.storage.direct.actor.user.delete",
1347 my $status = $req->gather(1);
1348 if(!defined($status)) {
1349 throw OpenSRF::EX::ERROR
1350 ("Unknown error updating patron");