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);
58 __PACKAGE__->register_method(
59 method => "set_ou_settings",
60 api_name => "open-ils.actor.org_unit.settings.update",
63 my( $self, $client, $user_session, $ouid, $settings ) = @_;
65 throw OpenSRF::EX::InvalidArg ("OrgUnit ID and Settings hash required for setting OrgUnit settings") unless ($ouid && $settings);
67 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
69 if($apputils->check_user_perms($user_obj->id, $ouid, "UPDATE_ORG_UNIT")) {
70 return OpenILS::Perm->new("UPDATE_ORG_UNIT");
75 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.merge');
77 return $set_ou_settings->run(map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
83 my $fetch_user_settings;
84 my $fetch_ou_settings;
86 __PACKAGE__->register_method(
87 method => "user_settings",
88 api_name => "open-ils.actor.patron.settings.retrieve",
91 my( $self, $client, $user_session, $uid ) = @_;
93 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
95 $uid ||= $user_obj->id;
97 if( $user_obj->id != $uid ) {
98 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
99 return OpenILS::Perm->new("VIEW_USER");
103 $fetch_user_settings ||=
104 $self->method_lookup('open-ils.storage.direct.actor.user_setting.search.usr.atomic');
106 my ($s) = $fetch_user_settings->run($uid);
108 return { map { ($_->name,$_->value) } @$s };
113 __PACKAGE__->register_method(
114 method => "ou_settings",
115 api_name => "open-ils.actor.org_unit.settings.retrieve",
118 my( $self, $client, $ouid ) = @_;
120 throw OpenSRF::EX::InvalidArg ("OrgUnit ID required for lookup of OrgUnit settings") unless ($ouid);
122 $fetch_ou_settings ||=
123 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic');
125 my ($s) = $fetch_ou_settings->run($ouid);
127 return { map { ($_->name,$_->value) } @$s };
132 __PACKAGE__->register_method(
133 method => "update_patron",
134 api_name => "open-ils.actor.patron.update",);
137 my( $self, $client, $user_session, $patron ) = @_;
139 my $session = $apputils->start_db_session();
142 warn $user_session . " " . $patron . "\n";
146 OpenILS::Application::AppUtils->check_user_session(
147 $user_session ); #throws EX on error
149 # XXX does this user have permission to add/create users. Granularity?
150 # $new_patron is the patron in progress. $patron is the original patron
151 # passed in with the method. new_patron will change as the components
152 # of patron are added/updated.
156 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
157 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
158 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
160 # create/update the patron first so we can use his id
161 if($patron->isnew()) {
163 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
165 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
166 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
167 $client->respond_complete($new_patron->ex);
171 } else { $new_patron = $patron; }
173 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
175 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
176 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
177 $client->respond_complete($new_patron->ex);
181 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
183 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
184 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
185 $client->respond_complete($new_patron->ex);
189 $new_patron = _add_survey_responses($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);
197 # re-update the patron if anything has happened to him during this process
198 if($new_patron->ischanged()) {
199 $new_patron = _update_patron($session, $new_patron, $user_obj);
201 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
202 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
203 $client->respond_complete($new_patron->ex);
208 $apputils->commit_db_session($session);
210 $session = OpenSRF::AppSession->create("open-ils.storage");
211 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
212 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
213 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
214 $client->respond_complete($new_patron->ex);
219 warn "Patron Update/Create complete\n";
220 return flesh_user($new_patron->id());
226 __PACKAGE__->register_method(
227 method => "user_retrieve_fleshed_by_id",
228 api_name => "open-ils.actor.user.fleshed.retrieve",);
230 sub user_retrieve_fleshed_by_id {
231 my( $self, $client, $user_session, $user_id ) = @_;
232 my $user_obj = $apputils->check_user_session( $user_session );
234 if(!defined($user_id)) { $user_id = $user_obj->id; }
236 if( $user_obj->id ne $user_id ) {
237 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
238 return OpenILS::Perm->new("VIEW_USER");
242 return flesh_user($user_id);
253 $session = OpenSRF::AppSession->create("open-ils.storage");
257 # grab the user with the given id
258 my $ureq = $session->request(
259 "open-ils.storage.direct.actor.user.retrieve", $id);
260 my $user = $ureq->gather(1);
262 if(!$user) { return undef; }
265 my $cards_req = $session->request(
266 "open-ils.storage.direct.actor.card.search.usr.atomic",
268 $user->cards( $cards_req->gather(1) );
270 for my $c(@{$user->cards}) {
271 if($c->id == $user->card || $c->id eq $user->card ) {
272 warn "Setting my card to " . $c->id . "\n";
277 my $add_req = $session->request(
278 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
280 $user->addresses( $add_req->gather(1) );
282 for my $c(@{$user->addresses}) {
283 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
284 warn "Setting my address to " . $c->id . "\n";
285 $user->billing_address($c);
289 for my $c(@{$user->addresses}) {
290 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
291 warn "Setting my address to " . $c->id . "\n";
292 $user->mailing_address($c);
296 my $stat_req = $session->request(
297 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
299 $user->stat_cat_entries($stat_req->gather(1));
301 if($kill) { $session->disconnect(); }
302 $user->clear_passwd();
310 # clone and clear stuff that would break the database
314 my $new_patron = $patron->clone;
316 # Using the Fieldmapper clone method
317 #my $new_patron = Fieldmapper::actor::user->new();
319 #my $fmap = $Fieldmapper::fieldmap;
320 #no strict; # shallow clone, may be useful in the fieldmapper
322 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
323 # $new_patron->$field( $patron->$field() );
328 $new_patron->clear_billing_address();
329 $new_patron->clear_mailing_address();
330 $new_patron->clear_addresses();
331 $new_patron->clear_card();
332 $new_patron->clear_cards();
333 $new_patron->clear_id();
334 $new_patron->clear_isnew();
335 $new_patron->clear_ischanged();
336 $new_patron->clear_isdeleted();
337 $new_patron->clear_stat_cat_entries();
346 my $user_obj = shift;
349 if($apputils->check_user_perms(
350 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
351 return OpenILS::Perm->new("CREATE_USER");
354 warn "Creating new patron\n";
357 my $req = $session->request(
358 "open-ils.storage.direct.actor.user.create",$patron);
359 my $id = $req->gather(1);
361 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
364 # retrieve the patron from the db to collect defaults
365 my $ureq = $session->request(
366 "open-ils.storage.direct.actor.user.retrieve",
369 warn "Created new patron with id $id\n";
371 return $ureq->gather(1);
376 my( $session, $patron, $user_obj) = @_;
379 if($apputils->check_user_perms(
380 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
381 return OpenILS::Perm->new("UPDATE_USER");
384 warn "updating patron " . Dumper($patron) . "\n";
386 my $req = $session->request(
387 "open-ils.storage.direct.actor.user.update",$patron );
388 my $status = $req->gather(1);
389 if(!defined($status)) {
390 throw OpenSRF::EX::ERROR
391 ("Unknown error updating patron");
397 sub _add_update_addresses {
400 my $new_patron = shift;
402 my $current_id; # id of the address before creation
404 for my $address (@{$patron->addresses()}) {
406 $address->usr($new_patron->id());
408 if(ref($address) and $address->isnew()) {
409 warn "Adding new address at street " . $address->street1() . "\n";
411 $current_id = $address->id();
412 $address = _add_address($session,$address);
414 if( $patron->billing_address() and
415 $patron->billing_address() == $current_id ) {
416 $new_patron->billing_address($address->id());
417 $new_patron->ischanged(1);
420 if( $patron->mailing_address() and
421 $patron->mailing_address() == $current_id ) {
422 $new_patron->mailing_address($address->id());
423 $new_patron->ischanged(1);
426 } elsif( ref($address) and $address->ischanged() ) {
427 warn "Updating address at street " . $address->street1();
428 $address->usr($new_patron->id());
429 _update_address($session,$address);
431 } elsif( ref($address) and $address->isdeleted() ) {
432 warn "Deleting address at street " . $address->street1();
434 if( $address->id() == $new_patron->mailing_address() ) {
435 $new_patron->clear_mailing_address();
436 _update_patron($session, $new_patron);
439 if( $address->id() == $new_patron->billing_address() ) {
440 $new_patron->clear_billing_address();
441 _update_patron($session, $new_patron);
444 _delete_address($session,$address);
452 # adds an address to the db and returns the address with new id
454 my($session, $address) = @_;
455 $address->clear_id();
458 warn "Adding Address:\n";
459 warn Dumper($address);
461 # put the address into the database
462 my $req = $session->request(
463 "open-ils.storage.direct.actor.user_address.create",
467 my $id = $req->gather(1);
469 throw OpenSRF::EX::ERROR
470 ("Unable to create new user address");
473 warn "Created address with id $id\n";
475 # update all the necessary id's
481 sub _update_address {
482 my( $session, $address ) = @_;
483 my $req = $session->request(
484 "open-ils.storage.direct.actor.user_address.update",
486 my $status = $req->gather(1);
487 if(!defined($status)) {
488 throw OpenSRF::EX::ERROR
489 ("Unknown error updating address");
496 sub _add_update_cards {
500 my $new_patron = shift;
502 my $virtual_id; #id of the card before creation
503 for my $card (@{$patron->cards()}) {
505 $card->usr($new_patron->id());
507 if(ref($card) and $card->isnew()) {
509 $virtual_id = $card->id();
510 $card = _add_card($session,$card);
511 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
515 #if(ref($patron->card)) { $patron->card($patron->card->id); }
516 if($patron->card() == $virtual_id) {
517 $new_patron->card($card->id());
518 $new_patron->ischanged(1);
521 } elsif( ref($card) and $card->ischanged() ) {
522 $card->usr($new_patron->id());
523 _update_card($session, $card);
530 # adds an card to the db and returns the card with new id
532 my( $session, $card ) = @_;
535 warn "Adding card with barcode " . $card->barcode() . "\n";
536 my $req = $session->request(
537 "open-ils.storage.direct.actor.card.create",
540 my $id = $req->gather(1);
542 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
546 warn "Created patron card with id $id\n";
552 my( $session, $card ) = @_;
555 my $req = $session->request(
556 "open-ils.storage.direct.actor.card.update",
558 my $status = $req->gather(1);
559 if(!defined($status)) {
560 throw OpenSRF::EX::ERROR
561 ("Unknown error updating card");
569 sub _delete_address {
570 my( $session, $address ) = @_;
572 warn "Deleting address " . $address->street1() . "\n";
574 my $req = $session->request(
575 "open-ils.storage.direct.actor.user_address.delete",
577 my $status = $req->gather(1);
578 if(!defined($status)) {
579 throw OpenSRF::EX::ERROR
580 ("Unknown error updating address");
582 warn "Delete address status is $status\n";
587 sub _add_survey_responses {
588 my ($session, $patron, $new_patron) = @_;
590 warn "updating responses for user " . $new_patron->id . "\n";
592 my $responses = $patron->survey_responses;
596 for my $resp( @$responses ) {
597 $resp->usr($new_patron->id);
600 my $status = $apputils->simple_scalar_request(
602 "open-ils.circ.survey.submit.user_id",
611 sub _create_stat_maps {
613 my($session, $user_session, $patron, $new_patron) = @_;
615 my $maps = $patron->stat_cat_entries();
617 for my $map (@$maps) {
619 next unless($map->isnew() || $map->ischanged());
621 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
623 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
627 $map->target_usr($new_patron->id);
629 warn "Updating stat entry with method $method and session $user_session and map $map\n";
631 my $req = $session->request($method, $map);
632 my $status = $req->gather(1);
637 throw OpenSRF::EX::ERROR
638 ("Error updating stat map with method $method");
648 __PACKAGE__->register_method(
649 method => "search_username",
650 api_name => "open-ils.actor.user.search.username",
653 sub search_username {
654 my($self, $client, $username) = @_;
655 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
657 "open-ils.storage.direct.actor.user.search.usrname.atomic",
665 __PACKAGE__->register_method(
666 method => "user_retrieve_by_barcode",
667 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
669 sub user_retrieve_by_barcode {
670 my($self, $client, $user_session, $barcode) = @_;
671 warn "Searching for user with barcode $barcode\n";
672 my $user_obj = $apputils->check_user_session( $user_session );
674 my $session = OpenSRF::AppSession->create("open-ils.storage");
676 # find the card with the given barcode
677 my $creq = $session->request(
678 "open-ils.storage.direct.actor.card.search.barcode.atomic",
680 my $card = $creq->gather(1);
682 if(!$card || !$card->[0]) {
683 $session->disconnect();
688 my $user = flesh_user($card->usr(), $session);
689 $session->disconnect();
696 __PACKAGE__->register_method(
697 method => "get_user_by_id",
698 api_name => "open-ils.actor.user.retrieve",);
701 my ($self, $client, $user_session, $id) = @_;
703 my $user_obj = $apputils->check_user_session( $user_session );
705 return $apputils->simple_scalar_request(
707 "open-ils.storage.direct.actor.user.retrieve",
713 __PACKAGE__->register_method(
714 method => "get_org_types",
715 api_name => "open-ils.actor.org_types.retrieve",);
719 my($self, $client) = @_;
721 return $org_types if $org_types;
723 $apputils->simple_scalar_request(
725 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
730 __PACKAGE__->register_method(
731 method => "get_user_profiles",
732 api_name => "open-ils.actor.user.profiles.retrieve",
736 sub get_user_profiles {
737 return $user_profiles if $user_profiles;
739 return $user_profiles =
740 $apputils->simple_scalar_request(
742 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
747 __PACKAGE__->register_method(
748 method => "get_user_ident_types",
749 api_name => "open-ils.actor.user.ident_types.retrieve",
752 sub get_user_ident_types {
753 return $ident_types if $ident_types;
754 return $ident_types =
755 $apputils->simple_scalar_request(
757 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
763 __PACKAGE__->register_method(
764 method => "get_org_unit",
765 api_name => "open-ils.actor.org_unit.retrieve",
770 my( $self, $client, $user_session, $org_id ) = @_;
772 if(defined($user_session) && !defined($org_id)) {
774 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
775 if(!defined($org_id)) {
776 $org_id = $user_obj->home_ou;
781 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
783 "open-ils.storage.direct.actor.org_unit.retrieve",
792 __PACKAGE__->register_method(
793 method => "get_org_tree",
794 api_name => "open-ils.actor.org_tree.retrieve",
796 note => "Returns the entire org tree structure",
800 my( $self, $client) = @_;
803 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
805 # see if it's in the cache
806 warn "Getting ORG Tree\n";
807 my $tree = $cache_client->get_cache('orgtree');
809 warn "Found orgtree in cache. returning...\n";
813 my $orglist = $apputils->simple_scalar_request(
815 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
818 warn "found org list\n";
821 $tree = $self->build_org_tree($orglist);
822 $cache_client->put_cache('orgtree', $tree);
828 # turns an org list into an org tree
831 my( $self, $orglist) = @_;
833 return $orglist unless (
834 ref($orglist) and @$orglist > 1 );
837 $a->ou_type <=> $b->ou_type ||
838 $a->name cmp $b->name } @$orglist;
840 for my $org (@list) {
842 next unless ($org and defined($org->parent_ou));
843 my ($parent) = grep { $_->id == $org->parent_ou } @list;
846 $parent->children([]) unless defined($parent->children);
847 push( @{$parent->children}, $org );
855 __PACKAGE__->register_method(
856 method => "get_org_descendants",
857 api_name => "open-ils.actor.org_tree.descendants.retrieve"
860 # depth is optional. org_unit is the id
861 sub get_org_descendants {
862 my( $self, $client, $org_unit, $depth ) = @_;
863 my $orglist = $apputils->simple_scalar_request(
865 "open-ils.storage.actor.org_unit.descendants.atomic",
867 return $self->build_org_tree($orglist);
871 __PACKAGE__->register_method(
872 method => "get_org_ancestors",
873 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
876 # depth is optional. org_unit is the id
877 sub get_org_ancestors {
878 my( $self, $client, $org_unit, $depth ) = @_;
879 my $orglist = $apputils->simple_scalar_request(
881 "open-ils.storage.actor.org_unit.ancestors.atomic",
883 return $self->build_org_tree($orglist);
887 __PACKAGE__->register_method(
888 method => "get_standings",
889 api_name => "open-ils.actor.standings.retrieve"
894 return $user_standings if $user_standings;
895 return $user_standings =
896 $apputils->simple_scalar_request(
898 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
903 __PACKAGE__->register_method(
904 method => "get_my_org_path",
905 api_name => "open-ils.actor.org_unit.full_path.retrieve"
908 sub get_my_org_path {
909 my( $self, $client, $user_session, $org_id ) = @_;
910 my $user_obj = $apputils->check_user_session($user_session);
911 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
913 return $apputils->simple_scalar_request(
915 "open-ils.storage.actor.org_unit.full_path.atomic",
920 __PACKAGE__->register_method(
921 method => "patron_adv_search",
922 api_name => "open-ils.actor.patron.search.advanced" );
924 sub patron_adv_search {
925 my( $self, $client, $staff_login, $search_hash ) = @_;
928 warn "patron adv with $staff_login and search " .
929 Dumper($search_hash) . "\n";
931 my $session = OpenSRF::AppSession->create("open-ils.storage");
932 my $req = $session->request(
933 "open-ils.storage.actor.user.crazy_search", $search_hash);
935 my $ans = $req->gather(1);
937 my %hash = map { ($_ =>1) } @$ans;
938 $ans = [ keys %hash ];
940 warn "Returning @$ans\n";
942 $session->disconnect();
949 sub _verify_password {
950 my($user_session, $password) = @_;
951 my $user_obj = $apputils->check_user_session($user_session);
953 #grab the user with password
954 $user_obj = $apputils->simple_scalar_request(
956 "open-ils.storage.direct.actor.user.retrieve",
959 if($user_obj->passwd eq $password) {
967 __PACKAGE__->register_method(
968 method => "update_password",
969 api_name => "open-ils.actor.user.password.update");
971 __PACKAGE__->register_method(
972 method => "update_password",
973 api_name => "open-ils.actor.user.username.update");
975 __PACKAGE__->register_method(
976 method => "update_password",
977 api_name => "open-ils.actor.user.email.update");
979 sub update_password {
980 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
982 warn "Updating user with method " .$self->api_name . "\n";
983 my $user_obj = $apputils->check_user_session($user_session);
985 if($self->api_name =~ /password/) {
987 #make sure they know the current password
988 if(!_verify_password($user_session, md5_hex($current_password))) {
989 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
992 $user_obj->passwd($new_value);
994 elsif($self->api_name =~ /username/) {
995 $user_obj->usrname($new_value);
998 elsif($self->api_name =~ /email/) {
999 warn "Updating email to $new_value\n";
1000 $user_obj->email($new_value);
1003 my $session = $apputils->start_db_session();
1004 $user_obj = _update_patron($session, $user_obj);
1005 $apputils->commit_db_session($session);
1007 if($user_obj) { return 1; }
1012 __PACKAGE__->register_method(
1013 method => "check_user_perms",
1014 api_name => "open-ils.actor.user.perm.check",
1015 notes => <<" NOTES");
1016 Takes a login session, user id, an org id, and an array of perm type strings. For each
1017 perm type, if the user does *not* have the given permission it is added
1018 to a list which is returned from the method. If all permissions
1019 are allowed, an empty list is returned
1020 if the logged in user does not match 'user_id', then the logged in user must
1021 have VIEW_PERMISSION priveleges.
1024 sub check_user_perms {
1025 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1026 my $user_obj = $apputils->check_user_session($login_session);
1028 if($user_obj->id ne $user_id) {
1029 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
1030 return OpenILS::Perm->new("VIEW_PERMISSION");
1035 for my $perm (@$perm_types) {
1036 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1037 push @not_allowed, $perm;
1041 return \@not_allowed
1046 __PACKAGE__->register_method(
1047 method => "user_fines_summary",
1048 api_name => "open-ils.actor.user.fines.summary",
1049 notes => <<" NOTES");
1050 Returns a short summary of the users total open fines, excluding voided fines
1051 Params are login_session, user_id
1052 Returns a 'mus' object.
1055 sub user_fines_summary {
1056 my( $self, $client, $login_session, $user_id ) = @_;
1058 my $user_obj = $apputils->check_user_session($login_session);
1059 if($user_obj->id ne $user_id) {
1060 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1061 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1065 return $apputils->simple_scalar_request(
1067 "open-ils.storage.direct.money.user_summary.search.usr",
1075 __PACKAGE__->register_method(
1076 method => "user_transactions",
1077 api_name => "open-ils.actor.user.transactions",
1078 notes => <<" NOTES");
1079 Returns a list of open user transactions (mbts objects);
1080 Params are login_session, user_id
1081 Optional third parameter is the transactions type. defaults to all
1084 __PACKAGE__->register_method(
1085 method => "user_transactions",
1086 api_name => "open-ils.actor.user.transactions.have_charge",
1087 notes => <<" NOTES");
1088 Returns a list of all open user transactions (mbts objects) that have an initial charge
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_balance",
1096 notes => <<" NOTES");
1097 Returns a list of all open user transactions (mbts objects) that have a balance
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.fleshed",
1105 notes => <<" NOTES");
1106 Returns an object/hash of transaction, circ, title where transaction = an open
1107 user transactions (mbts objects), circ is the attached circluation, and title
1108 is the title the circ points to
1109 Params are login_session, user_id
1110 Optional third parameter is the transactions type. defaults to all
1113 __PACKAGE__->register_method(
1114 method => "user_transactions",
1115 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1116 notes => <<" NOTES");
1117 Returns an object/hash of transaction, circ, title where transaction = an open
1118 user transactions that has an initial charge (mbts objects), circ is the
1119 attached circluation, and title is the title the circ points to
1120 Params are login_session, user_id
1121 Optional third parameter is the transactions type. defaults to all
1124 __PACKAGE__->register_method(
1125 method => "user_transactions",
1126 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1127 notes => <<" NOTES");
1128 Returns an object/hash of transaction, circ, title where transaction = an open
1129 user transaction that has a balance (mbts objects), circ is the attached
1130 circluation, and title is the title the circ points to
1131 Params are login_session, user_id
1132 Optional third parameter is the transaction type. defaults to all
1137 sub user_transactions {
1138 my( $self, $client, $login_session, $user_id, $type ) = @_;
1140 my $user_obj = $apputils->check_user_session($login_session);
1141 if($user_obj->id ne $user_id) {
1142 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1143 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1147 my $api = $self->api_name();
1150 if(defined($type)) { @xact = (xact_type => $type);
1151 } else { @xact = (); }
1153 if($api =~ /have_charge/) {
1155 $trans = $apputils->simple_scalar_request(
1157 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1158 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1160 } elsif($api =~ /have_balance/) {
1162 $trans = $apputils->simple_scalar_request(
1164 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1165 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1169 $trans = $apputils->simple_scalar_request(
1171 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1172 { usr => $user_id, @xact });
1175 if($api !~ /fleshed/) { return $trans; }
1180 for my $t (@$trans) {
1184 my $circ = $apputils->simple_scalar_request(
1186 "open-ils.storage.direct.action.circulation.retrieve",
1191 my $title = $apputils->simple_scalar_request(
1193 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1194 $circ->target_copy );
1198 my $u = OpenILS::Utils::ModsParser->new();
1199 $u->start_mods_batch($title->marc());
1200 my $mods = $u->finish_mods_batch();
1202 push @resp, {transaction => $t, circ => $circ, record => $mods };
1212 __PACKAGE__->register_method(
1213 method => "retrieve_groups",
1214 api_name => "open-ils.actor.groups.retrieve",
1215 notes => <<" NOTES");
1216 Returns a list of user groups
1218 sub retrieve_groups {
1219 my( $self, $client ) = @_;
1220 return $apputils->simple_scalar_request(
1222 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1276 some old methods that may be good to keep around for now
1279 my( $session, $card ) = @_;
1281 warn "Deleting card with barcode " . $card->barcode() . "\n";
1282 my $req = $session->request(
1283 "open-ils.storage.direct.actor.card.delete",
1285 my $status = $req->gather(1);
1286 if(!defined($status)) {
1287 throw OpenSRF::EX::ERROR
1288 ("Unknown error updating card");
1294 # deletes the patron and any attached addresses and cards
1295 __PACKAGE__->register_method(
1296 method => "delete_patron",
1297 api_name => "open-ils.actor.patron.delete",
1302 my( $self, $client, $patron ) = @_;
1303 my $session = $apputils->start_db_session();
1308 $patron->clear_mailing_address();
1309 $patron->clear_billing_address();
1310 $patron->ischanged(1);
1312 _update_patron($session, $patron);
1313 _delete_address($session,$_) for (@{$patron->addresses()});
1314 _delete_card($session,$_) for (@{$patron->cards()});
1315 _delete_patron($session,$patron);
1316 $apputils->commit_db_session($session);
1318 } catch Error with {
1320 $err = "-*- Failure deleting user: $e";
1321 $apputils->rollback_db_session($session);
1325 if($err) { throw OpenSRF::EX::ERROR ($err); }
1326 warn "Patron Delete complete\n";
1330 sub _delete_patron {
1331 my( $session, $patron ) = @_;
1333 warn "Deleting patron " . $patron->usrname() . "\n";
1335 my $req = $session->request(
1336 "open-ils.storage.direct.actor.user.delete",
1338 my $status = $req->gather(1);
1339 if(!defined($status)) {
1340 throw OpenSRF::EX::ERROR
1341 ("Unknown error updating patron");