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;
20 use OpenILS::Application::Actor::Container;
22 OpenILS::Application::Actor::Container->initialize();
25 my $apputils = "OpenILS::Application::AppUtils";
27 sub _d { warn "Patron:\n" . Dumper(shift()); }
32 my $set_user_settings;
35 __PACKAGE__->register_method(
36 method => "set_user_settings",
37 api_name => "open-ils.actor.patron.settings.update",
39 sub set_user_settings {
40 my( $self, $client, $user_session, $uid, $settings ) = @_;
42 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
44 if (ref($uid) eq 'HASH') {
48 $uid ||= $user_obj->id;
50 if( $user_obj->id != $uid ) {
51 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
52 return OpenILS::Perm->new("UPDATE_USER");
56 # $set_user_settings ||=
57 # $self->method_lookup('open-ils.storage.direct.actor.user_setting.batch.merge');
59 # return $set_user_settings->run(map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
61 return $apputils->simple_scalar_request(
63 'open-ils.storage.direct.actor.user_setting.batch.merge',
64 map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings );
71 __PACKAGE__->register_method(
72 method => "set_ou_settings",
73 api_name => "open-ils.actor.org_unit.settings.update",
76 my( $self, $client, $user_session, $ouid, $settings ) = @_;
78 throw OpenSRF::EX::InvalidArg ("OrgUnit ID and Settings hash required for setting OrgUnit settings") unless ($ouid && $settings);
80 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
82 if($apputils->check_user_perms($user_obj->id, $ouid, "UPDATE_ORG_UNIT")) {
83 return OpenILS::Perm->new("UPDATE_ORG_UNIT");
88 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.merge');
90 return $set_ou_settings->run(map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
96 my $fetch_user_settings;
97 my $fetch_ou_settings;
99 __PACKAGE__->register_method(
100 method => "user_settings",
101 api_name => "open-ils.actor.patron.settings.retrieve",
104 my( $self, $client, $user_session, $uid ) = @_;
106 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
108 $uid ||= $user_obj->id;
110 if( $user_obj->id != $uid ) {
111 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
112 return OpenILS::Perm->new("VIEW_USER");
116 warn "fetching user settings for $uid...\n";
117 my $s = $apputils->simple_scalar_request(
119 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
121 return { map { ($_->name,$_->value) } @$s };
126 __PACKAGE__->register_method(
127 method => "ou_settings",
128 api_name => "open-ils.actor.org_unit.settings.retrieve",
131 my( $self, $client, $ouid ) = @_;
133 throw OpenSRF::EX::InvalidArg ("OrgUnit ID required for lookup of OrgUnit settings") unless ($ouid);
135 $fetch_ou_settings ||=
136 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic');
138 my ($s) = $fetch_ou_settings->run($ouid);
140 return { map { ($_->name,$_->value) } @$s };
145 __PACKAGE__->register_method(
146 method => "update_patron",
147 api_name => "open-ils.actor.patron.update",);
150 my( $self, $client, $user_session, $patron ) = @_;
152 my $session = $apputils->start_db_session();
155 warn $user_session . " " . $patron . "\n";
159 OpenILS::Application::AppUtils->check_user_session(
160 $user_session ); #throws EX on error
162 # XXX does this user have permission to add/create users. Granularity?
163 # $new_patron is the patron in progress. $patron is the original patron
164 # passed in with the method. new_patron will change as the components
165 # of patron are added/updated.
169 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
170 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
171 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
173 # create/update the patron first so we can use his id
174 if($patron->isnew()) {
176 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
178 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
179 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
180 $client->respond_complete($new_patron->ex);
184 } else { $new_patron = $patron; }
186 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
188 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
189 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
190 $client->respond_complete($new_patron->ex);
194 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
196 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
197 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
198 $client->respond_complete($new_patron->ex);
202 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
203 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
204 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
205 $client->respond_complete($new_patron->ex);
210 # re-update the patron if anything has happened to him during this process
211 if($new_patron->ischanged()) {
212 $new_patron = _update_patron($session, $new_patron, $user_obj);
214 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
215 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
216 $client->respond_complete($new_patron->ex);
221 $apputils->commit_db_session($session);
223 $session = OpenSRF::AppSession->create("open-ils.storage");
224 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
225 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
226 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
227 $client->respond_complete($new_patron->ex);
232 warn "Patron Update/Create complete\n";
233 return flesh_user($new_patron->id());
239 __PACKAGE__->register_method(
240 method => "user_retrieve_fleshed_by_id",
241 api_name => "open-ils.actor.user.fleshed.retrieve",);
243 sub user_retrieve_fleshed_by_id {
244 my( $self, $client, $user_session, $user_id ) = @_;
245 my $user_obj = $apputils->check_user_session( $user_session );
247 if(!defined($user_id)) { $user_id = $user_obj->id; }
249 if( $user_obj->id ne $user_id ) {
250 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
251 return OpenILS::Perm->new("VIEW_USER");
255 return flesh_user($user_id);
266 $session = OpenSRF::AppSession->create("open-ils.storage");
270 # grab the user with the given id
271 my $ureq = $session->request(
272 "open-ils.storage.direct.actor.user.retrieve", $id);
273 my $user = $ureq->gather(1);
275 if(!$user) { return undef; }
278 my $cards_req = $session->request(
279 "open-ils.storage.direct.actor.card.search.usr.atomic",
281 $user->cards( $cards_req->gather(1) );
283 for my $c(@{$user->cards}) {
284 if($c->id == $user->card || $c->id eq $user->card ) {
285 warn "Setting my card to " . $c->id . "\n";
290 my $add_req = $session->request(
291 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
293 $user->addresses( $add_req->gather(1) );
295 for my $c(@{$user->addresses}) {
296 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
297 warn "Setting my address to " . $c->id . "\n";
298 $user->billing_address($c);
302 for my $c(@{$user->addresses}) {
303 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
304 warn "Setting my address to " . $c->id . "\n";
305 $user->mailing_address($c);
309 my $stat_req = $session->request(
310 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
312 $user->stat_cat_entries($stat_req->gather(1));
314 if($kill) { $session->disconnect(); }
315 $user->clear_passwd();
323 # clone and clear stuff that would break the database
327 my $new_patron = $patron->clone;
329 # Using the Fieldmapper clone method
330 #my $new_patron = Fieldmapper::actor::user->new();
332 #my $fmap = $Fieldmapper::fieldmap;
333 #no strict; # shallow clone, may be useful in the fieldmapper
335 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
336 # $new_patron->$field( $patron->$field() );
341 $new_patron->clear_billing_address();
342 $new_patron->clear_mailing_address();
343 $new_patron->clear_addresses();
344 $new_patron->clear_card();
345 $new_patron->clear_cards();
346 $new_patron->clear_id();
347 $new_patron->clear_isnew();
348 $new_patron->clear_ischanged();
349 $new_patron->clear_isdeleted();
350 $new_patron->clear_stat_cat_entries();
359 my $user_obj = shift;
362 if($apputils->check_user_perms(
363 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
364 return OpenILS::Perm->new("CREATE_USER");
367 warn "Creating new patron\n";
370 my $req = $session->request(
371 "open-ils.storage.direct.actor.user.create",$patron);
372 my $id = $req->gather(1);
374 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
377 # retrieve the patron from the db to collect defaults
378 my $ureq = $session->request(
379 "open-ils.storage.direct.actor.user.retrieve",
382 warn "Created new patron with id $id\n";
384 return $ureq->gather(1);
389 my( $session, $patron, $user_obj) = @_;
392 if($patron->id ne $user_obj->id) {
393 if($apputils->check_user_perms(
394 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
395 return OpenILS::Perm->new("UPDATE_USER");
399 warn "updating patron " . Dumper($patron) . "\n";
401 my $req = $session->request(
402 "open-ils.storage.direct.actor.user.update",$patron );
403 my $status = $req->gather(1);
404 if(!defined($status)) {
405 throw OpenSRF::EX::ERROR
406 ("Unknown error updating patron");
412 sub _add_update_addresses {
415 my $new_patron = shift;
417 my $current_id; # id of the address before creation
419 for my $address (@{$patron->addresses()}) {
421 $address->usr($new_patron->id());
423 if(ref($address) and $address->isnew()) {
424 warn "Adding new address at street " . $address->street1() . "\n";
426 $current_id = $address->id();
427 $address = _add_address($session,$address);
429 if( $patron->billing_address() and
430 $patron->billing_address() == $current_id ) {
431 $new_patron->billing_address($address->id());
432 $new_patron->ischanged(1);
435 if( $patron->mailing_address() and
436 $patron->mailing_address() == $current_id ) {
437 $new_patron->mailing_address($address->id());
438 $new_patron->ischanged(1);
441 } elsif( ref($address) and $address->ischanged() ) {
442 warn "Updating address at street " . $address->street1();
443 $address->usr($new_patron->id());
444 _update_address($session,$address);
446 } elsif( ref($address) and $address->isdeleted() ) {
447 warn "Deleting address at street " . $address->street1();
449 if( $address->id() == $new_patron->mailing_address() ) {
450 $new_patron->clear_mailing_address();
451 _update_patron($session, $new_patron);
454 if( $address->id() == $new_patron->billing_address() ) {
455 $new_patron->clear_billing_address();
456 _update_patron($session, $new_patron);
459 _delete_address($session,$address);
467 # adds an address to the db and returns the address with new id
469 my($session, $address) = @_;
470 $address->clear_id();
473 warn "Adding Address:\n";
474 warn Dumper($address);
476 # put the address into the database
477 my $req = $session->request(
478 "open-ils.storage.direct.actor.user_address.create",
482 my $id = $req->gather(1);
484 throw OpenSRF::EX::ERROR
485 ("Unable to create new user address");
488 warn "Created address with id $id\n";
490 # update all the necessary id's
496 sub _update_address {
497 my( $session, $address ) = @_;
498 my $req = $session->request(
499 "open-ils.storage.direct.actor.user_address.update",
501 my $status = $req->gather(1);
502 if(!defined($status)) {
503 throw OpenSRF::EX::ERROR
504 ("Unknown error updating address");
511 sub _add_update_cards {
515 my $new_patron = shift;
517 my $virtual_id; #id of the card before creation
518 for my $card (@{$patron->cards()}) {
520 $card->usr($new_patron->id());
522 if(ref($card) and $card->isnew()) {
524 $virtual_id = $card->id();
525 $card = _add_card($session,$card);
526 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
530 #if(ref($patron->card)) { $patron->card($patron->card->id); }
531 if($patron->card() == $virtual_id) {
532 $new_patron->card($card->id());
533 $new_patron->ischanged(1);
536 } elsif( ref($card) and $card->ischanged() ) {
537 $card->usr($new_patron->id());
538 _update_card($session, $card);
545 # adds an card to the db and returns the card with new id
547 my( $session, $card ) = @_;
550 warn "Adding card with barcode " . $card->barcode() . "\n";
551 my $req = $session->request(
552 "open-ils.storage.direct.actor.card.create",
555 my $id = $req->gather(1);
557 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
561 warn "Created patron card with id $id\n";
567 my( $session, $card ) = @_;
570 my $req = $session->request(
571 "open-ils.storage.direct.actor.card.update",
573 my $status = $req->gather(1);
574 if(!defined($status)) {
575 throw OpenSRF::EX::ERROR
576 ("Unknown error updating card");
584 sub _delete_address {
585 my( $session, $address ) = @_;
587 warn "Deleting address " . $address->street1() . "\n";
589 my $req = $session->request(
590 "open-ils.storage.direct.actor.user_address.delete",
592 my $status = $req->gather(1);
593 if(!defined($status)) {
594 throw OpenSRF::EX::ERROR
595 ("Unknown error updating address");
597 warn "Delete address status is $status\n";
602 sub _add_survey_responses {
603 my ($session, $patron, $new_patron) = @_;
605 warn "updating responses for user " . $new_patron->id . "\n";
607 my $responses = $patron->survey_responses;
611 for my $resp( @$responses ) {
612 $resp->usr($new_patron->id);
615 my $status = $apputils->simple_scalar_request(
617 "open-ils.circ.survey.submit.user_id",
626 sub _create_stat_maps {
628 my($session, $user_session, $patron, $new_patron) = @_;
630 my $maps = $patron->stat_cat_entries();
632 for my $map (@$maps) {
634 next unless($map->isnew() || $map->ischanged());
636 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
638 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
642 $map->target_usr($new_patron->id);
644 warn "Updating stat entry with method $method and session $user_session and map $map\n";
646 my $req = $session->request($method, $map);
647 my $status = $req->gather(1);
652 throw OpenSRF::EX::ERROR
653 ("Error updating stat map with method $method");
663 __PACKAGE__->register_method(
664 method => "search_username",
665 api_name => "open-ils.actor.user.search.username",
668 sub search_username {
669 my($self, $client, $username) = @_;
670 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
672 "open-ils.storage.direct.actor.user.search.usrname.atomic",
680 __PACKAGE__->register_method(
681 method => "user_retrieve_by_barcode",
682 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
684 sub user_retrieve_by_barcode {
685 my($self, $client, $user_session, $barcode) = @_;
686 warn "Searching for user with barcode $barcode\n";
687 my $user_obj = $apputils->check_user_session( $user_session );
689 my $session = OpenSRF::AppSession->create("open-ils.storage");
691 # find the card with the given barcode
692 my $creq = $session->request(
693 "open-ils.storage.direct.actor.card.search.barcode.atomic",
695 my $card = $creq->gather(1);
697 if(!$card || !$card->[0]) {
698 $session->disconnect();
703 my $user = flesh_user($card->usr(), $session);
704 $session->disconnect();
711 __PACKAGE__->register_method(
712 method => "get_user_by_id",
713 api_name => "open-ils.actor.user.retrieve",);
716 my ($self, $client, $user_session, $id) = @_;
718 my $user_obj = $apputils->check_user_session( $user_session );
720 return $apputils->simple_scalar_request(
722 "open-ils.storage.direct.actor.user.retrieve",
728 __PACKAGE__->register_method(
729 method => "get_org_types",
730 api_name => "open-ils.actor.org_types.retrieve",);
734 my($self, $client) = @_;
736 return $org_types if $org_types;
738 $apputils->simple_scalar_request(
740 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
745 __PACKAGE__->register_method(
746 method => "get_user_profiles",
747 api_name => "open-ils.actor.user.profiles.retrieve",
751 sub get_user_profiles {
752 return $user_profiles if $user_profiles;
754 return $user_profiles =
755 $apputils->simple_scalar_request(
757 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
762 __PACKAGE__->register_method(
763 method => "get_user_ident_types",
764 api_name => "open-ils.actor.user.ident_types.retrieve",
767 sub get_user_ident_types {
768 return $ident_types if $ident_types;
769 return $ident_types =
770 $apputils->simple_scalar_request(
772 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
778 __PACKAGE__->register_method(
779 method => "get_org_unit",
780 api_name => "open-ils.actor.org_unit.retrieve",
785 my( $self, $client, $user_session, $org_id ) = @_;
787 if(defined($user_session) && !defined($org_id)) {
789 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
790 if(!defined($org_id)) {
791 $org_id = $user_obj->home_ou;
796 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
798 "open-ils.storage.direct.actor.org_unit.retrieve",
807 __PACKAGE__->register_method(
808 method => "get_org_tree",
809 api_name => "open-ils.actor.org_tree.retrieve",
811 note => "Returns the entire org tree structure",
815 my( $self, $client) = @_;
818 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
820 # see if it's in the cache
821 warn "Getting ORG Tree\n";
822 my $tree = $cache_client->get_cache('orgtree');
824 warn "Found orgtree in cache. returning...\n";
828 my $orglist = $apputils->simple_scalar_request(
830 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
833 warn "found org list\n";
836 $tree = $self->build_org_tree($orglist);
837 $cache_client->put_cache('orgtree', $tree);
843 # turns an org list into an org tree
846 my( $self, $orglist) = @_;
848 return $orglist unless (
849 ref($orglist) and @$orglist > 1 );
852 $a->ou_type <=> $b->ou_type ||
853 $a->name cmp $b->name } @$orglist;
855 for my $org (@list) {
857 next unless ($org and defined($org->parent_ou));
858 my ($parent) = grep { $_->id == $org->parent_ou } @list;
861 $parent->children([]) unless defined($parent->children);
862 push( @{$parent->children}, $org );
870 __PACKAGE__->register_method(
871 method => "get_org_descendants",
872 api_name => "open-ils.actor.org_tree.descendants.retrieve"
875 # depth is optional. org_unit is the id
876 sub get_org_descendants {
877 my( $self, $client, $org_unit, $depth ) = @_;
878 my $orglist = $apputils->simple_scalar_request(
880 "open-ils.storage.actor.org_unit.descendants.atomic",
882 return $self->build_org_tree($orglist);
886 __PACKAGE__->register_method(
887 method => "get_org_ancestors",
888 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
891 # depth is optional. org_unit is the id
892 sub get_org_ancestors {
893 my( $self, $client, $org_unit, $depth ) = @_;
894 my $orglist = $apputils->simple_scalar_request(
896 "open-ils.storage.actor.org_unit.ancestors.atomic",
898 return $self->build_org_tree($orglist);
902 __PACKAGE__->register_method(
903 method => "get_standings",
904 api_name => "open-ils.actor.standings.retrieve"
909 return $user_standings if $user_standings;
910 return $user_standings =
911 $apputils->simple_scalar_request(
913 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
918 __PACKAGE__->register_method(
919 method => "get_my_org_path",
920 api_name => "open-ils.actor.org_unit.full_path.retrieve"
923 sub get_my_org_path {
924 my( $self, $client, $user_session, $org_id ) = @_;
925 my $user_obj = $apputils->check_user_session($user_session);
926 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
928 return $apputils->simple_scalar_request(
930 "open-ils.storage.actor.org_unit.full_path.atomic",
935 __PACKAGE__->register_method(
936 method => "patron_adv_search",
937 api_name => "open-ils.actor.patron.search.advanced" );
939 sub patron_adv_search {
940 my( $self, $client, $staff_login, $search_hash ) = @_;
943 warn "patron adv with $staff_login and search " .
944 Dumper($search_hash) . "\n";
946 my $session = OpenSRF::AppSession->create("open-ils.storage");
947 my $req = $session->request(
948 "open-ils.storage.actor.user.crazy_search", $search_hash);
950 my $ans = $req->gather(1);
952 my %hash = map { ($_ =>1) } @$ans;
953 $ans = [ keys %hash ];
955 warn "Returning @$ans\n";
957 $session->disconnect();
964 sub _verify_password {
965 my($user_session, $password) = @_;
966 my $user_obj = $apputils->check_user_session($user_session);
968 #grab the user with password
969 $user_obj = $apputils->simple_scalar_request(
971 "open-ils.storage.direct.actor.user.retrieve",
974 if($user_obj->passwd eq $password) {
982 __PACKAGE__->register_method(
983 method => "update_password",
984 api_name => "open-ils.actor.user.password.update");
986 __PACKAGE__->register_method(
987 method => "update_password",
988 api_name => "open-ils.actor.user.username.update");
990 __PACKAGE__->register_method(
991 method => "update_password",
992 api_name => "open-ils.actor.user.email.update");
994 sub update_password {
995 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
997 warn "Updating user with method " .$self->api_name . "\n";
998 my $user_obj = $apputils->check_user_session($user_session);
1000 if($self->api_name =~ /password/) {
1002 #make sure they know the current password
1003 if(!_verify_password($user_session, md5_hex($current_password))) {
1004 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
1007 $user_obj->passwd($new_value);
1009 elsif($self->api_name =~ /username/) {
1010 $user_obj->usrname($new_value);
1013 elsif($self->api_name =~ /email/) {
1014 warn "Updating email to $new_value\n";
1015 $user_obj->email($new_value);
1018 my $session = $apputils->start_db_session();
1019 $user_obj = _update_patron($session, $user_obj, $user_obj);
1020 $apputils->commit_db_session($session);
1022 if($user_obj) { return 1; }
1027 __PACKAGE__->register_method(
1028 method => "check_user_perms",
1029 api_name => "open-ils.actor.user.perm.check",
1030 notes => <<" NOTES");
1031 Takes a login session, user id, an org id, and an array of perm type strings. For each
1032 perm type, if the user does *not* have the given permission it is added
1033 to a list which is returned from the method. If all permissions
1034 are allowed, an empty list is returned
1035 if the logged in user does not match 'user_id', then the logged in user must
1036 have VIEW_PERMISSION priveleges.
1039 sub check_user_perms {
1040 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1041 my $user_obj = $apputils->check_user_session($login_session);
1043 if($user_obj->id ne $user_id) {
1044 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
1045 return OpenILS::Perm->new("VIEW_PERMISSION");
1050 for my $perm (@$perm_types) {
1051 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1052 push @not_allowed, $perm;
1056 return \@not_allowed
1061 __PACKAGE__->register_method(
1062 method => "user_fines_summary",
1063 api_name => "open-ils.actor.user.fines.summary",
1064 notes => <<" NOTES");
1065 Returns a short summary of the users total open fines, excluding voided fines
1066 Params are login_session, user_id
1067 Returns a 'mous' object.
1070 sub user_fines_summary {
1071 my( $self, $client, $login_session, $user_id ) = @_;
1073 my $user_obj = $apputils->check_user_session($login_session);
1074 if($user_obj->id ne $user_id) {
1075 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1076 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1080 return $apputils->simple_scalar_request(
1082 "open-ils.storage.direct.money.open_user_summary.search.usr",
1090 __PACKAGE__->register_method(
1091 method => "user_transactions",
1092 api_name => "open-ils.actor.user.transactions",
1093 notes => <<" NOTES");
1094 Returns a list of open user transactions (mbts objects);
1095 Params are login_session, user_id
1096 Optional third parameter is the transactions type. defaults to all
1099 __PACKAGE__->register_method(
1100 method => "user_transactions",
1101 api_name => "open-ils.actor.user.transactions.have_charge",
1102 notes => <<" NOTES");
1103 Returns a list of all open user transactions (mbts objects) that have an initial charge
1104 Params are login_session, user_id
1105 Optional third parameter is the transactions type. defaults to all
1108 __PACKAGE__->register_method(
1109 method => "user_transactions",
1110 api_name => "open-ils.actor.user.transactions.have_balance",
1111 notes => <<" NOTES");
1112 Returns a list of all open user transactions (mbts objects) that have a balance
1113 Params are login_session, user_id
1114 Optional third parameter is the transactions type. defaults to all
1117 __PACKAGE__->register_method(
1118 method => "user_transactions",
1119 api_name => "open-ils.actor.user.transactions.fleshed",
1120 notes => <<" NOTES");
1121 Returns an object/hash of transaction, circ, title where transaction = an open
1122 user transactions (mbts objects), circ is the attached circluation, and title
1123 is the title the circ points to
1124 Params are login_session, user_id
1125 Optional third parameter is the transactions type. defaults to all
1128 __PACKAGE__->register_method(
1129 method => "user_transactions",
1130 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1131 notes => <<" NOTES");
1132 Returns an object/hash of transaction, circ, title where transaction = an open
1133 user transactions that has an initial charge (mbts objects), circ is the
1134 attached circluation, and title is the title the circ points to
1135 Params are login_session, user_id
1136 Optional third parameter is the transactions type. defaults to all
1139 __PACKAGE__->register_method(
1140 method => "user_transactions",
1141 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1142 notes => <<" NOTES");
1143 Returns an object/hash of transaction, circ, title where transaction = an open
1144 user transaction that has a balance (mbts objects), circ is the attached
1145 circluation, and title is the title the circ points to
1146 Params are login_session, user_id
1147 Optional third parameter is the transaction type. defaults to all
1152 sub user_transactions {
1153 my( $self, $client, $login_session, $user_id, $type ) = @_;
1155 my $user_obj = $apputils->check_user_session($login_session);
1156 if($user_obj->id ne $user_id) {
1157 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1158 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1162 my $api = $self->api_name();
1165 if(defined($type)) { @xact = (xact_type => $type);
1166 } else { @xact = (); }
1168 if($api =~ /have_charge/) {
1170 $trans = $apputils->simple_scalar_request(
1172 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1173 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1175 } elsif($api =~ /have_balance/) {
1177 $trans = $apputils->simple_scalar_request(
1179 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1180 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1184 $trans = $apputils->simple_scalar_request(
1186 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1187 { usr => $user_id, @xact });
1190 if($api !~ /fleshed/) { return $trans; }
1195 for my $t (@$trans) {
1199 my $circ = $apputils->simple_scalar_request(
1201 "open-ils.storage.direct.action.circulation.retrieve",
1206 my $title = $apputils->simple_scalar_request(
1208 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1209 $circ->target_copy );
1213 my $u = OpenILS::Utils::ModsParser->new();
1214 $u->start_mods_batch($title->marc());
1215 my $mods = $u->finish_mods_batch();
1217 push @resp, {transaction => $t, circ => $circ, record => $mods };
1227 __PACKAGE__->register_method(
1228 method => "retrieve_groups",
1229 api_name => "open-ils.actor.groups.retrieve",
1230 notes => <<" NOTES");
1231 Returns a list of user groups
1233 sub retrieve_groups {
1234 my( $self, $client ) = @_;
1235 return $apputils->simple_scalar_request(
1237 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1291 some old methods that may be good to keep around for now
1294 my( $session, $card ) = @_;
1296 warn "Deleting card with barcode " . $card->barcode() . "\n";
1297 my $req = $session->request(
1298 "open-ils.storage.direct.actor.card.delete",
1300 my $status = $req->gather(1);
1301 if(!defined($status)) {
1302 throw OpenSRF::EX::ERROR
1303 ("Unknown error updating card");
1309 # deletes the patron and any attached addresses and cards
1310 __PACKAGE__->register_method(
1311 method => "delete_patron",
1312 api_name => "open-ils.actor.patron.delete",
1317 my( $self, $client, $patron ) = @_;
1318 my $session = $apputils->start_db_session();
1323 $patron->clear_mailing_address();
1324 $patron->clear_billing_address();
1325 $patron->ischanged(1);
1327 _update_patron($session, $patron);
1328 _delete_address($session,$_) for (@{$patron->addresses()});
1329 _delete_card($session,$_) for (@{$patron->cards()});
1330 _delete_patron($session,$patron);
1331 $apputils->commit_db_session($session);
1333 } catch Error with {
1335 $err = "-*- Failure deleting user: $e";
1336 $apputils->rollback_db_session($session);
1340 if($err) { throw OpenSRF::EX::ERROR ($err); }
1341 warn "Patron Delete complete\n";
1345 sub _delete_patron {
1346 my( $session, $patron ) = @_;
1348 warn "Deleting patron " . $patron->usrname() . "\n";
1350 my $req = $session->request(
1351 "open-ils.storage.direct.actor.user.delete",
1353 my $status = $req->gather(1);
1354 if(!defined($status)) {
1355 throw OpenSRF::EX::ERROR
1356 ("Unknown error updating patron");